Debian Codegraph @

fastly/acl_entries_batch_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package fastly
2
3 import (
4 "sort"
5 "testing"
6 )
7
8 func TestClient_BatchModifyAclEntries_Create(t *testing.T) {
9
10 fixtureBase := "acl_entries_batch/create/"
11 nameSuffix := "BatchModifyAclEntries_Create"
12
13 // Given: a test service with an ACL and a batch of create operations,
14 testService := createTestService(t, fixtureBase+"create_service", nameSuffix)
15 defer deleteTestService(t, fixtureBase+"delete_service", testService.ID)
16
17 testVersion := createTestVersion(t, fixtureBase+"create_version", testService.ID)
18
19 testACL := createTestACL(t, fixtureBase+"create_acl", testService.ID, testVersion.Number, nameSuffix)
20 defer deleteTestACL(t, testACL, fixtureBase+"delete_acl")
21
22 batchCreateOperations := &BatchModifyACLEntriesInput{
23 Service: testService.ID,
24 ACL: testACL.ID,
25 Entries: []*BatchACLEntry{
26 {
27 Operation: CreateBatchOperation,
28 IP: "127.0.0.1",
29 Subnet: "24",
30 Negated: false,
31 Comment: "ACL Entry 1",
32 },
33 {
34 Operation: CreateBatchOperation,
35 IP: "192.168.0.1",
36 Subnet: "24",
37 Negated: false,
38 Comment: "ACL Entry 2",
39 },
40 },
41 }
42
43 // When: I execute the batch create operations against the Fastly API,
44 var err error
45 record(t, fixtureBase+"create_acl_entries", func(c *Client) {
46
47 err = c.BatchModifyACLEntries(batchCreateOperations)
48 })
49 if err != nil {
50 t.Fatal(err)
51 }
52
53 // Then: I expect to be able to list all of the created ACL entries.
54 var actualACLEntries []*ACLEntry
55 record(t, fixtureBase+"list_after_create", func(c *Client) {
56 actualACLEntries, err = c.ListACLEntries(&ListACLEntriesInput{
57 Service: testService.ID,
58 ACL: testACL.ID,
59 })
60 })
61 if err != nil {
62 t.Fatal(err)
63 }
64
65 sort.Slice(actualACLEntries, func(i, j int) bool {
66 return actualACLEntries[i].IP < actualACLEntries[j].IP
67 })
68
69 actualNumberOfACLEntries := len(actualACLEntries)
70 expectedNumberOfACLEntries := len(batchCreateOperations.Entries)
71 if actualNumberOfACLEntries != expectedNumberOfACLEntries {
72 t.Errorf("Incorrect number of ACL entries returned, expected: %d, got %d", expectedNumberOfACLEntries, actualNumberOfACLEntries)
73 }
74
75 for i, entry := range actualACLEntries {
76
77 actualIp := entry.IP
78 expectedIp := batchCreateOperations.Entries[i].IP
79
80 if actualIp != expectedIp {
81 t.Errorf("IP did not match, expected %s, got %s", expectedIp, actualIp)
82 }
83
84 actualSubnet := entry.Subnet
85 expectedSubnet := batchCreateOperations.Entries[i].Subnet
86
87 if actualSubnet != expectedSubnet {
88 t.Errorf("Subnet did not match, expected %s, got %s", expectedSubnet, actualSubnet)
89 }
90
91 actualNegated := entry.Negated
92 expectedNegated := batchCreateOperations.Entries[i].Negated
93
94 if actualNegated != expectedNegated {
95 t.Errorf("Negated did not match, expected %t, got %t", expectedNegated, actualNegated)
96 }
97
98 actualComment := entry.Comment
99 expectedComment := batchCreateOperations.Entries[i].Comment
100
101 if actualComment != expectedComment {
102 t.Errorf("Comment did not match, expected %s, got %s", expectedComment, actualComment)
103 }
104 }
105
106 }
107
108 func TestClient_BatchModifyAclEntries_Delete(t *testing.T) {
109
110 fixtureBase := "acl_entries_batch/delete/"
111 nameSuffix := "BatchModifyAclEntries_Delete"
112
113 // Given: a test service with an ACL and a batch of create operations,
114 testService := createTestService(t, fixtureBase+"create_service", nameSuffix)
115 defer deleteTestService(t, fixtureBase+"delete_service", testService.ID)
116
117 testVersion := createTestVersion(t, fixtureBase+"create_version", testService.ID)
118
119 testACL := createTestACL(t, fixtureBase+"create_acl", testService.ID, testVersion.Number, nameSuffix)
120 defer deleteTestACL(t, testACL, fixtureBase+"delete_acl")
121
122 batchCreateOperations := &BatchModifyACLEntriesInput{
123 Service: testService.ID,
124 ACL: testACL.ID,
125 Entries: []*BatchACLEntry{
126 {
127 Operation: CreateBatchOperation,
128 IP: "127.0.0.1",
129 Subnet: "24",
130 Negated: false,
131 Comment: "ACL Entry 1",
132 },
133 {
134 Operation: CreateBatchOperation,
135 IP: "192.168.0.1",
136 Subnet: "24",
137 Negated: false,
138 Comment: "ACL Entry 2",
139 },
140 },
141 }
142
143 var err error
144 record(t, fixtureBase+"create_acl_entries", func(c *Client) {
145
146 err = c.BatchModifyACLEntries(batchCreateOperations)
147 })
148 if err != nil {
149 t.Fatal(err)
150 }
151
152 var createdACLEntries []*ACLEntry
153 record(t, fixtureBase+"list_before_delete", func(client *Client) {
154 createdACLEntries, err = client.ListACLEntries(&ListACLEntriesInput{
155 Service: testService.ID,
156 ACL: testACL.ID,
157 })
158 })
159 if err != nil {
160 t.Fatal(err)
161 }
162
163 sort.Slice(createdACLEntries, func(i, j int) bool {
164 return createdACLEntries[i].IP < createdACLEntries[j].IP
165 })
166
167 // When: I execute the batch delete operations against the Fastly API,
168 batchDeleteOperations := &BatchModifyACLEntriesInput{
169 Service: testService.ID,
170 ACL: testACL.ID,
171 Entries: []*BatchACLEntry{
172 {
173 Operation: DeleteBatchOperation,
174 ID: createdACLEntries[0].ID,
175 },
176 },
177 }
178
179 record(t, fixtureBase+"delete_acl_entries", func(c *Client) {
180
181 err = c.BatchModifyACLEntries(batchDeleteOperations)
182 })
183 if err != nil {
184 t.Fatal(err)
185 }
186
187 // Then: I expect to be able to list a single ACL entry.
188 var actualACLEntries []*ACLEntry
189 record(t, fixtureBase+"list_after_delete", func(client *Client) {
190 actualACLEntries, err = client.ListACLEntries(&ListACLEntriesInput{
191 Service: testService.ID,
192 ACL: testACL.ID,
193 })
194 })
195 if err != nil {
196 t.Fatal(err)
197 }
198
199 sort.Slice(actualACLEntries, func(i, j int) bool {
200 return actualACLEntries[i].IP < actualACLEntries[j].IP
201 })
202
203 actualNumberOfACLEntries := len(actualACLEntries)
204 expectedNumberOfACLEntries := len(batchDeleteOperations.Entries)
205 if actualNumberOfACLEntries != expectedNumberOfACLEntries {
206 t.Errorf("Incorrect number of ACL entries returned, expected: %d, got %d", expectedNumberOfACLEntries, actualNumberOfACLEntries)
207 }
208 }
209
210 func TestClient_BatchModifyAclEntries_Update(t *testing.T) {
211
212 fixtureBase := "acl_entries_batch/update/"
213 nameSuffix := "BatchModifyAclEntries_Update"
214
215 // Given: a test service with an ACL and ACL entries,
216 testService := createTestService(t, fixtureBase+"create_service", nameSuffix)
217 defer deleteTestService(t, fixtureBase+"delete_service", testService.ID)
218
219 testVersion := createTestVersion(t, fixtureBase+"create_version", testService.ID)
220
221 testACL := createTestACL(t, fixtureBase+"create_acl", testService.ID, testVersion.Number, nameSuffix)
222 defer deleteTestACL(t, testACL, fixtureBase+"delete_acl")
223
224 batchCreateOperations := &BatchModifyACLEntriesInput{
225 Service: testService.ID,
226 ACL: testACL.ID,
227 Entries: []*BatchACLEntry{
228 {
229 Operation: CreateBatchOperation,
230 IP: "127.0.0.1",
231 Subnet: "24",
232 Negated: false,
233 Comment: "ACL Entry 1",
234 },
235 {
236 Operation: CreateBatchOperation,
237 IP: "192.168.0.1",
238 Subnet: "24",
239 Negated: false,
240 Comment: "ACL Entry 2",
241 },
242 },
243 }
244
245 var err error
246 record(t, fixtureBase+"create_acl_entries", func(c *Client) {
247
248 err = c.BatchModifyACLEntries(batchCreateOperations)
249 })
250 if err != nil {
251 t.Fatal(err)
252 }
253
254 var createdACLEntries []*ACLEntry
255 record(t, fixtureBase+"list_before_update", func(client *Client) {
256 createdACLEntries, err = client.ListACLEntries(&ListACLEntriesInput{
257 Service: testService.ID,
258 ACL: testACL.ID,
259 })
260 })
261 if err != nil {
262 t.Fatal(err)
263 }
264
265 sort.Slice(createdACLEntries, func(i, j int) bool {
266 return createdACLEntries[i].IP < createdACLEntries[j].IP
267 })
268
269 // When: I execute the batch update operations against the Fastly API,
270 batchUpdateOperations := &BatchModifyACLEntriesInput{
271 Service: testService.ID,
272 ACL: testACL.ID,
273 Entries: []*BatchACLEntry{
274 {
275 Operation: UpdateBatchOperation,
276 ID: createdACLEntries[0].ID,
277 IP: "127.0.0.2",
278 Subnet: "16",
279 Negated: true,
280 Comment: "Updated ACL Entry 1",
281 },
282 },
283 }
284
285 record(t, fixtureBase+"update_acl_entries", func(c *Client) {
286
287 err = c.BatchModifyACLEntries(batchUpdateOperations)
288 })
289 if err != nil {
290 t.Fatal(err)
291 }
292
293 // Then: I expect to be able to list all of the ACL entries with modifications applied to a single item.
294 var actualACLEntries []*ACLEntry
295 record(t, fixtureBase+"list_after_update", func(client *Client) {
296 actualACLEntries, err = client.ListACLEntries(&ListACLEntriesInput{
297 Service: testService.ID,
298 ACL: testACL.ID,
299 })
300 })
301 if err != nil {
302 t.Fatal(err)
303 }
304
305 sort.Slice(actualACLEntries, func(i, j int) bool {
306 return actualACLEntries[i].IP < actualACLEntries[j].IP
307 })
308
309 actualNumberOfAclEntries := len(actualACLEntries)
310 expectedNumberOfAclEntries := len(batchCreateOperations.Entries)
311 if actualNumberOfAclEntries != expectedNumberOfAclEntries {
312 t.Errorf("Incorrect number of ACL entries returned, expected: %d, got %d", expectedNumberOfAclEntries, actualNumberOfAclEntries)
313 }
314
315 actualID := actualACLEntries[0].ID
316 expectedID := batchUpdateOperations.Entries[0].ID
317
318 if actualID != expectedID {
319 t.Errorf("First ID did not match, expected %s, got %s", expectedID, actualID)
320 }
321
322 actualIP := actualACLEntries[0].IP
323 expectedIP := batchUpdateOperations.Entries[0].IP
324
325 if actualIP != expectedIP {
326 t.Errorf("First IP did not match, expected %s, got %s", expectedIP, actualIP)
327 }
328
329 actualSubnet := actualACLEntries[0].Subnet
330 expectedSubnet := batchUpdateOperations.Entries[0].Subnet
331
332 if actualSubnet != expectedSubnet {
333 t.Errorf("First Subnet did not match, expected %s, got %s", expectedSubnet, actualSubnet)
334 }
335
336 actualNegated := actualACLEntries[0].Negated
337 expectedNegated := batchUpdateOperations.Entries[0].Negated
338
339 if actualNegated != expectedNegated {
340 t.Errorf("First Subnet did not match, expected %t, got %t", expectedNegated, actualNegated)
341 }
342
343 actualComment := actualACLEntries[0].Comment
344 expectedComment := batchUpdateOperations.Entries[0].Comment
345
346 if actualComment != expectedComment {
347 t.Errorf("First Comment did not match, expected %s, got %s", expectedComment, actualComment)
348 }
349
350 }