Debian Codegraph @

pkg/apis/dpf/v1/common_configs/cc_sec_transfer_acl_test.go

raw
1 package common_configs_test
2
3 import (
4 "context"
5 "net/http"
6
7 . "github.com/onsi/ginkgo"
8 . "github.com/onsi/gomega"
9
10 "github.com/jarcoal/httpmock"
11 "github.com/mimuret/golang-iij-dpf/pkg/apis/dpf/v1/common_configs"
12 "github.com/mimuret/golang-iij-dpf/pkg/testtool"
13 )
14
15 var _ = Describe("cc_sec_transfer_acls", func() {
16 var (
17 c common_configs.CcSecTransferAcl
18 cl *testtool.TestClient
19 err error
20 reqId string
21 s1, s2 common_configs.CcSecTransferAcl
22 slist common_configs.CcSecTransferAclList
23 )
24 BeforeEach(func() {
25 cl = testtool.NewTestClient("", "http://localhost", nil)
26 s1 = common_configs.CcSecTransferAcl{
27 AttributeMeta: common_configs.AttributeMeta{
28 CommonConfigID: 1,
29 },
30 ID: 1,
31 Network: testtool.MustParseIPNet("192.168.1.0/24"),
32 TsigID: 0,
33 }
34 s2 = common_configs.CcSecTransferAcl{
35 AttributeMeta: common_configs.AttributeMeta{
36 CommonConfigID: 1,
37 },
38 ID: 2,
39 Network: testtool.MustParseIPNet("2001:db8::/64"),
40 TsigID: 2,
41 }
42 slist = common_configs.CcSecTransferAclList{
43 AttributeMeta: common_configs.AttributeMeta{
44 CommonConfigID: 1,
45 },
46 Items: []common_configs.CcSecTransferAcl{s1, s2},
47 }
48 })
49 Describe("CcSecTransferAcl", func() {
50 Context("Read", func() {
51 BeforeEach(func() {
52 httpmock.RegisterResponder(http.MethodGet, "http://localhost/common_configs/1/cc_sec_transfer_acls/1", httpmock.NewBytesResponder(200, []byte(`{
53 "request_id": "C5E6406DCBFC4931A0EE480ABA9FA505",
54 "result": {
55 "id": 1,
56 "network": "192.168.1.0/24",
57 "tsig_id": null
58 }
59 }`)))
60 httpmock.RegisterResponder(http.MethodGet, "http://localhost/common_configs/1/cc_sec_transfer_acls/2", httpmock.NewBytesResponder(200, []byte(`{
61 "request_id": "3D82ADCA5479408094DEF876A679BFD3",
62 "result": {
63 "id": 2,
64 "network": "2001:db8::/64",
65 "tsig_id": 2
66 }
67 }`)))
68 })
69 AfterEach(func() {
70 httpmock.Reset()
71 })
72 When("returns IPv4 server", func() {
73 BeforeEach(func() {
74 c = common_configs.CcSecTransferAcl{
75 AttributeMeta: common_configs.AttributeMeta{
76 CommonConfigID: 1,
77 },
78 ID: 1,
79 }
80 reqId, err = cl.Read(context.Background(), &c)
81 })
82 It("returns normal", func() {
83 Expect(err).To(Succeed())
84 Expect(reqId).To(Equal("C5E6406DCBFC4931A0EE480ABA9FA505"))
85 Expect(c).To(Equal(s1))
86 })
87 })
88 When("returns IPv6 server", func() {
89 BeforeEach(func() {
90 c = common_configs.CcSecTransferAcl{
91 AttributeMeta: common_configs.AttributeMeta{
92 CommonConfigID: 1,
93 },
94 ID: 2,
95 }
96 reqId, err = cl.Read(context.Background(), &c)
97 })
98 It("returns normal", func() {
99 Expect(err).To(Succeed())
100 Expect(reqId).To(Equal("3D82ADCA5479408094DEF876A679BFD3"))
101 Expect(c).To(Equal(s2))
102 })
103 })
104 })
105 Context("Create", func() {
106 var (
107 id1, bs1 = testtool.CreateAsyncResponse()
108 id2, bs2 = testtool.CreateAsyncResponse()
109 )
110 BeforeEach(func() {
111 httpmock.RegisterResponder(http.MethodPost, "http://localhost/common_configs/2/cc_sec_transfer_acls", httpmock.NewBytesResponder(202, bs1))
112 httpmock.RegisterResponder(http.MethodPost, "http://localhost/common_configs/3/cc_sec_transfer_acls", httpmock.NewBytesResponder(202, bs2))
113 })
114 AfterEach(func() {
115 httpmock.Reset()
116 })
117 When("ipv4 host", func() {
118 BeforeEach(func() {
119 s := common_configs.CcSecTransferAcl{
120 AttributeMeta: common_configs.AttributeMeta{
121 CommonConfigID: 2,
122 },
123 Network: testtool.MustParseIPNet("10.0.0.0/8"),
124 TsigID: 1,
125 }
126 reqId, err = cl.Create(context.Background(), &s, nil)
127 })
128 It("returns job_id", func() {
129 Expect(err).To(Succeed())
130 Expect(reqId).To(Equal(id1))
131 })
132 It("post json", func() {
133 Expect(cl.RequestBody["/common_configs/2/cc_sec_transfer_acls"]).To(MatchJSON(`{
134 "network": "10.0.0.0/8",
135 "tsig_id": 1
136 }`))
137 })
138 })
139 When("ipv6 host", func() {
140 BeforeEach(func() {
141 s := common_configs.CcSecTransferAcl{
142 AttributeMeta: common_configs.AttributeMeta{
143 CommonConfigID: 3,
144 },
145 Network: testtool.MustParseIPNet("2001:db8::/48"),
146 }
147 reqId, err = cl.Create(context.Background(), &s, nil)
148 })
149 It("returns job_id", func() {
150 Expect(err).To(Succeed())
151 Expect(reqId).To(Equal(id2))
152 })
153 It("post json", func() {
154 Expect(cl.RequestBody["/common_configs/3/cc_sec_transfer_acls"]).To(MatchJSON(`{
155 "network": "2001:db8::/48",
156 "tsig_id": null
157 }`))
158 })
159 })
160 })
161 Context("Update", func() {
162 var (
163 id1, bs1 = testtool.CreateAsyncResponse()
164 id2, bs2 = testtool.CreateAsyncResponse()
165 )
166 BeforeEach(func() {
167 httpmock.RegisterResponder(http.MethodPatch, "http://localhost/common_configs/1/cc_sec_transfer_acls/1", httpmock.NewBytesResponder(202, bs1))
168 httpmock.RegisterResponder(http.MethodPatch, "http://localhost/common_configs/1/cc_sec_transfer_acls/2", httpmock.NewBytesResponder(202, bs2))
169 })
170 AfterEach(func() {
171 httpmock.Reset()
172 })
173 When("ipv4 host", func() {
174 BeforeEach(func() {
175 reqId, err = cl.Update(context.Background(), &s1, nil)
176 })
177 It("returns job_id", func() {
178 Expect(err).To(Succeed())
179 Expect(reqId).To(Equal(id1))
180 })
181 It("post json", func() {
182 Expect(cl.RequestBody["/common_configs/1/cc_sec_transfer_acls/1"]).To(MatchJSON(`{
183 "network": "192.168.1.0/24",
184 "tsig_id": null
185 }`))
186 })
187 })
188 When("ipv6 host", func() {
189 BeforeEach(func() {
190 reqId, err = cl.Update(context.Background(), &s2, nil)
191 })
192 It("returns job_id", func() {
193 Expect(err).To(Succeed())
194 Expect(reqId).To(Equal(id2))
195 })
196 It("post json", func() {
197 Expect(cl.RequestBody["/common_configs/1/cc_sec_transfer_acls/2"]).To(MatchJSON(`{
198 "network": "2001:db8::/64",
199 "tsig_id": 2
200 }`))
201 })
202 })
203 })
204 Context("Delete", func() {
205 var (
206 id1, bs1 = testtool.CreateAsyncResponse()
207 id2, bs2 = testtool.CreateAsyncResponse()
208 )
209 BeforeEach(func() {
210 httpmock.RegisterResponder(http.MethodDelete, "http://localhost/common_configs/1/cc_sec_transfer_acls/1", httpmock.NewBytesResponder(202, bs1))
211 httpmock.RegisterResponder(http.MethodDelete, "http://localhost/common_configs/1/cc_sec_transfer_acls/2", httpmock.NewBytesResponder(202, bs2))
212 })
213 AfterEach(func() {
214 httpmock.Reset()
215 })
216 When("returns IPv4 server", func() {
217 BeforeEach(func() {
218 reqId, err = cl.Delete(context.Background(), &s1)
219 })
220 It("returns normal", func() {
221 Expect(err).To(Succeed())
222 Expect(reqId).To(Equal(id1))
223 })
224 })
225 When("returns IPv6 server", func() {
226 BeforeEach(func() {
227 reqId, err = cl.Delete(context.Background(), &s2)
228 })
229 It("returns normal", func() {
230 Expect(err).To(Succeed())
231 Expect(reqId).To(Equal(id2))
232 })
233 })
234 })
235 Context("SetPathParams", func() {
236 When("no arguments, nothing to do", func() {
237 BeforeEach(func() {
238 err = s1.SetPathParams()
239 })
240 It("returns error", func() {
241 Expect(err).To(Succeed())
242 })
243 })
244 When("enough arguments", func() {
245 BeforeEach(func() {
246 err = s1.SetPathParams(100, 200)
247 })
248 It("not returns error", func() {
249 Expect(err).To(Succeed())
250 })
251 It("can set CommonConfigID", func() {
252 Expect(s1.GetCommonConfigID()).To(Equal(int64(100)))
253 })
254 It("can set Id", func() {
255 Expect(s1.GetID()).To(Equal(int64(200)))
256 })
257 })
258 When("not enough arguments", func() {
259 BeforeEach(func() {
260 err = s1.SetPathParams(100)
261 })
262 It("returns error", func() {
263 Expect(err).To(HaveOccurred())
264 })
265 })
266 When("arguments has extra value", func() {
267 BeforeEach(func() {
268 err = s1.SetPathParams(100, 1, 2)
269 })
270 It("returns error", func() {
271 Expect(err).To(HaveOccurred())
272 })
273 })
274 When("arguments type missmatch (CommonConfigID)", func() {
275 BeforeEach(func() {
276 err = s1.SetPathParams("2", 1)
277 })
278 It("returns error", func() {
279 Expect(err).To(HaveOccurred())
280 })
281 })
282 When("arguments type missmatch (Id)", func() {
283 BeforeEach(func() {
284 err = s1.SetPathParams(100, "1")
285 })
286 It("returns error", func() {
287 Expect(err).To(HaveOccurred())
288 })
289 })
290 })
291 Context("api.Spec common test", func() {
292 var nilSpec *common_configs.CcSecTransferAcl
293 testtool.TestDeepCopyObject(&s1, nilSpec)
294 testtool.TestGetName(&s1, "cc_sec_transfer_acls")
295 testtool.TestGetPathMethodForSpec(&s1, "/common_configs/1/cc_sec_transfer_acls", "/common_configs/1/cc_sec_transfer_acls/1")
296 })
297 Context("contracts.ChildSpec common test", func() {
298 Context("GetID", func() {
299 It("returns Id", func() {
300 Expect(s1.GetID()).To(Equal(s1.ID))
301 })
302 })
303 Context("SetID", func() {
304 BeforeEach(func() {
305 s1.SetID(2)
306 })
307 It("can set Id", func() {
308 Expect(s1.GetID()).To(Equal(int64(2)))
309 })
310 })
311 })
312 })
313 Describe("CcSecTransferAclList", func() {
314 var c common_configs.CcSecTransferAclList
315 Context("List", func() {
316 BeforeEach(func() {
317 httpmock.RegisterResponder(http.MethodGet, "http://localhost/common_configs/1/cc_sec_transfer_acls", httpmock.NewBytesResponder(200, []byte(`{
318 "request_id": "13032F0785DA47768B3E36A1A854A808",
319 "results": [
320 {
321 "id": 1,
322 "network": "192.168.1.0/24",
323 "tsig_id": null
324 },
325 {
326 "id": 2,
327 "network": "2001:db8::/64",
328 "tsig_id": 2
329 }
330 ]
331 }`)))
332 })
333 AfterEach(func() {
334 httpmock.Reset()
335 })
336 When("returns list ", func() {
337 BeforeEach(func() {
338 c = common_configs.CcSecTransferAclList{
339 AttributeMeta: common_configs.AttributeMeta{
340 CommonConfigID: 1,
341 },
342 }
343 reqId, err = cl.List(context.Background(), &c, nil)
344 })
345 It("returns normal", func() {
346 Expect(err).To(Succeed())
347 Expect(reqId).To(Equal("13032F0785DA47768B3E36A1A854A808"))
348 Expect(c).To(Equal(slist))
349 })
350 })
351 })
352 Context("SetPathParams", func() {
353 When("no arguments, nothing to do", func() {
354 BeforeEach(func() {
355 err = slist.SetPathParams()
356 })
357 It("returns error", func() {
358 Expect(err).To(Succeed())
359 })
360 })
361 When("enough arguments", func() {
362 BeforeEach(func() {
363 err = slist.SetPathParams(99)
364 })
365 It("not returns error", func() {
366 Expect(err).To(Succeed())
367 })
368 It("can set CommonConfigID", func() {
369 Expect(slist.GetCommonConfigID()).To(Equal(int64(99)))
370 })
371 })
372 When("arguments has extra value", func() {
373 BeforeEach(func() {
374 err = slist.SetPathParams(100, 2)
375 })
376 It("returns error", func() {
377 Expect(err).To(HaveOccurred())
378 })
379 })
380 When("arguments type missmatch (CommonConfigID)", func() {
381 BeforeEach(func() {
382 err = slist.SetPathParams("2")
383 })
384 It("returns error", func() {
385 Expect(err).To(HaveOccurred())
386 })
387 })
388 })
389 Context("api.Spec common test", func() {
390 var nilSpec *common_configs.CcSecTransferAclList
391 testtool.TestDeepCopyObject(&slist, nilSpec)
392 testtool.TestGetName(&slist, "cc_sec_transfer_acls")
393 testtool.TestGetPathMethodForList(&slist, "/common_configs/1/cc_sec_transfer_acls")
394 })
395 Context("api.ListSpec common test", func() {
396 testtool.TestGetItems(&slist, &slist.Items)
397 testtool.TestLen(&slist, 2)
398 Context("Index", func() {
399 When("index exist", func() {
400 It("returns item", func() {
401 Expect(slist.Index(0)).To(Equal(s1))
402 })
403 })
404 })
405 })
406 })
407 })