Debian Codegraph @

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

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