Debian Codegraph @

pkg/apis/dpf/v1/core/zone_test.go

raw
1 package core_test
2
3 import (
4 "context"
5 "net/http"
6 "net/url"
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/api"
13 "github.com/mimuret/golang-iij-dpf/pkg/apis/dpf/v1/core"
14 "github.com/mimuret/golang-iij-dpf/pkg/testtool"
15 "github.com/mimuret/golang-iij-dpf/pkg/types"
16 )
17
18 var _ = Describe("zones", func() {
19 var (
20 c core.Zone
21 cl *testtool.TestClient
22 err error
23 reqId string
24 s1, s2 core.Zone
25 slist core.ZoneList
26 )
27 BeforeEach(func() {
28 cl = testtool.NewTestClient("", "http://localhost", nil)
29 s1 = core.Zone{
30 ID: "m1",
31 CommonConfigID: 1,
32 ServiceCode: "dpm0000001",
33 State: types.StateBeforeStart,
34 Favorite: types.FavoriteHighPriority,
35 Name: "example.jp.",
36 Network: "",
37 Description: "zone 1",
38 ZoneProxyEnabled: types.Disabled,
39 }
40 s2 = core.Zone{
41 ID: "m2",
42 CommonConfigID: 2,
43 ServiceCode: "dpm0000002",
44 State: types.StateRunning,
45 Favorite: types.FavoriteLowPriority,
46 Name: "168.192.in-addr.arpa.",
47 Network: "192.168.0.0/16",
48 Description: "zone 2",
49 ZoneProxyEnabled: types.Enabled,
50 }
51 slist = core.ZoneList{
52 Items: []core.Zone{s1, s2},
53 }
54 })
55 Describe("Contract", func() {
56 Context("Read", func() {
57 BeforeEach(func() {
58 httpmock.RegisterResponder(http.MethodGet, "http://localhost/zones/m1", httpmock.NewBytesResponder(200, []byte(`{
59 "request_id": "76D7462F50E3417C96898A827E0D1EC1",
60 "result": {
61 "id": "m1",
62 "common_config_id": 1,
63 "service_code": "dpm0000001",
64 "state": 1,
65 "favorite": 1,
66 "name": "example.jp.",
67 "network": "",
68 "description": "zone 1",
69 "zone_proxy_enabled": 0
70 }
71 }`)))
72 httpmock.RegisterResponder(http.MethodGet, "http://localhost/zones/m2", httpmock.NewBytesResponder(200, []byte(`{
73 "request_id": "B10F0042282A46C18BFB440B5B58BF8C",
74 "result": {
75 "id": "m2",
76 "common_config_id": 2,
77 "service_code": "dpm0000002",
78 "state": 2,
79 "favorite":2,
80 "name": "168.192.in-addr.arpa.",
81 "network": "192.168.0.0/16",
82 "description": "zone 2",
83 "zone_proxy_enabled": 1
84 }
85 }`)))
86 })
87 AfterEach(func() {
88 httpmock.Reset()
89 })
90 When("returns Zone m1", func() {
91 BeforeEach(func() {
92 c = core.Zone{
93 ID: "m1",
94 }
95 reqId, err = cl.Read(context.Background(), &c)
96 })
97 It("returns normal", func() {
98 Expect(err).To(Succeed())
99 Expect(reqId).To(Equal("76D7462F50E3417C96898A827E0D1EC1"))
100 Expect(c).To(Equal(s1))
101 })
102 })
103 When("returns Zone m2", func() {
104 BeforeEach(func() {
105 c = core.Zone{
106 ID: "m2",
107 }
108 reqId, err = cl.Read(context.Background(), &c)
109 })
110 It("returns normal", func() {
111 Expect(err).To(Succeed())
112 Expect(reqId).To(Equal("B10F0042282A46C18BFB440B5B58BF8C"))
113 Expect(c).To(Equal(s2))
114 })
115 })
116 })
117 Context("Update", func() {
118 id1, bs1 := testtool.CreateAsyncResponse()
119 BeforeEach(func() {
120 httpmock.RegisterResponder(http.MethodPatch, "http://localhost/zones/m1", httpmock.NewBytesResponder(202, bs1))
121 reqId, err = cl.Update(context.Background(), &s1, nil)
122 })
123 AfterEach(func() {
124 httpmock.Reset()
125 })
126 It("returns job_id", func() {
127 Expect(err).To(Succeed())
128 Expect(reqId).To(Equal(id1))
129 })
130 It("post json", func() {
131 Expect(cl.RequestBody["/zones/m1"]).To(MatchJSON(`{
132 "favorite": 1,
133 "description": "zone 1"
134 }`))
135 })
136 })
137 Context("SetPathParams", func() {
138 When("no arguments, nothing to do", func() {
139 BeforeEach(func() {
140 err = s1.SetPathParams()
141 })
142 It("returns error", func() {
143 Expect(err).To(Succeed())
144 })
145 })
146 When("enough arguments", func() {
147 BeforeEach(func() {
148 err = s1.SetPathParams("m100")
149 })
150 It("not returns error", func() {
151 Expect(err).To(Succeed())
152 })
153 It("can set ContractID", func() {
154 Expect(s1.ID).To(Equal("m100"))
155 })
156 })
157 When("arguments has extra value", func() {
158 BeforeEach(func() {
159 err = s1.SetPathParams("m100", 2)
160 })
161 It("returns error", func() {
162 Expect(err).To(HaveOccurred())
163 })
164 })
165 When("arguments type missmatch (ZoneID)", func() {
166 BeforeEach(func() {
167 err = s1.SetPathParams(2)
168 })
169 It("returns error", func() {
170 Expect(err).To(HaveOccurred())
171 })
172 })
173 })
174 Context("api.Spec common test", func() {
175 var nilSpec *core.Zone
176 testtool.TestDeepCopyObject(&s1, nilSpec)
177 testtool.TestGetName(&s1, "zones")
178
179 Context("GetPathMethod", func() {
180 When("action is ActionRead", func() {
181 testtool.TestGetPathMethod(&s1, api.ActionRead, http.MethodGet, "/zones/m1")
182 })
183 When("action is ActionUpdate", func() {
184 testtool.TestGetPathMethod(&s1, api.ActionUpdate, http.MethodPatch, "/zones/m1")
185 })
186 When("action is other", func() {
187 testtool.TestGetPathMethod(&s1, api.ActionApply, "", "")
188 })
189 })
190 })
191 })
192 Describe("ZoneList", func() {
193 var c core.ZoneList
194 Context("List", func() {
195 BeforeEach(func() {
196 httpmock.RegisterResponder(http.MethodGet, "http://localhost/zones", httpmock.NewBytesResponder(200, []byte(`{
197 "request_id": "2C18922432DC48D485613F5383A7ED8E",
198 "results": [
199 {
200 "id": "m1",
201 "common_config_id": 1,
202 "service_code": "dpm0000001",
203 "state": 1,
204 "favorite": 1,
205 "name": "example.jp.",
206 "network": "",
207 "description": "zone 1",
208 "zone_proxy_enabled": 0
209 },
210 {
211 "id": "m2",
212 "common_config_id": 2,
213 "service_code": "dpm0000002",
214 "state": 2,
215 "favorite":2,
216 "name": "168.192.in-addr.arpa.",
217 "network": "192.168.0.0/16",
218 "description": "zone 2",
219 "zone_proxy_enabled": 1
220 }
221 ]
222 }`)))
223 httpmock.RegisterResponder(http.MethodGet, "http://localhost/zones/count", httpmock.NewBytesResponder(200, []byte(`{
224 "request_id": "9C518C729E5541D999389C686FE8987D",
225 "result": {
226 "count": 2
227 }
228 }`)))
229 })
230 AfterEach(func() {
231 httpmock.Reset()
232 })
233 When("returns list ", func() {
234 BeforeEach(func() {
235 c = core.ZoneList{}
236 reqId, err = cl.List(context.Background(), &c, nil)
237 })
238 It("returns normal", func() {
239 Expect(err).To(Succeed())
240 Expect(reqId).To(Equal("2C18922432DC48D485613F5383A7ED8E"))
241 Expect(c).To(Equal(slist))
242 })
243 })
244 })
245 Context("SetPathParams", func() {
246 BeforeEach(func() {
247 err = slist.SetPathParams()
248 })
249 It("nothing todo", func() {
250 Expect(err).To(Succeed())
251 })
252 })
253 Context("api.Spec common test", func() {
254 var nilSpec *core.ZoneList
255 testtool.TestDeepCopyObject(&slist, nilSpec)
256 testtool.TestGetName(&slist, "zones")
257
258 testtool.TestGetPathMethodForCountableList(&slist, "/zones")
259 })
260 Context("api.ListSpec common test", func() {
261 testtool.TestGetItems(&slist, &slist.Items)
262 testtool.TestLen(&slist, 2)
263 Context("Index", func() {
264 When("index exist", func() {
265 It("returns item", func() {
266 Expect(slist.Index(0)).To(Equal(s1))
267 })
268 })
269 })
270 })
271 Context("api.CountableListSpec common test", func() {
272 testtool.TestGetMaxLimit(&slist, 10000)
273 testtool.TestClearItems(&slist)
274 testtool.TestAddItem(&slist, s1)
275 })
276 })
277 Describe("ZoneListSearchKeywords", func() {
278 Context("GetValues", func() {
279 testcase := []struct {
280 keyword core.ZoneListSearchKeywords
281 values url.Values
282 }{
283 {
284 core.ZoneListSearchKeywords{
285 CommonSearchParams: api.CommonSearchParams{
286 Type: api.SearchTypeAND,
287 Offset: int32(10),
288 Limit: int32(100),
289 },
290 },
291 url.Values{
292 "type": []string{"AND"},
293 "offset": []string{"10"},
294 "limit": []string{"100"},
295 },
296 },
297 {
298 core.ZoneListSearchKeywords{
299 CommonSearchParams: api.CommonSearchParams{
300 Type: api.SearchTypeOR,
301 Offset: int32(10),
302 Limit: int32(100),
303 },
304 FullText: api.KeywordsString{"hogehoge", "🐰"},
305 ServiceCode: api.KeywordsString{"mxxxxxx", "mxxxxxx1"},
306 Name: api.KeywordsString{"example.jp", "example.net"},
307 Network: api.KeywordsString{"192.168.0.0/24"},
308 State: api.KeywordsState{types.StateBeforeStart, types.StateRunning},
309 Favorite: api.KeywordsFavorite{types.FavoriteHighPriority, types.FavoriteLowPriority},
310 Description: api.KeywordsString{"あああ", "🍺"},
311 CommonConfigID: api.KeywordsID{100, 200},
312 ZoneProxyEnabled: api.KeywordsBoolean{types.Enabled, types.Disabled},
313 },
314 /*
315 _keywords_full_text[]
316 _keywords_service_code[]
317 _keywords_name[]
318 _keywords_network[]
319 _keywords_state[]
320 _keywords_favorite[]
321 _keywords_description[]
322 _keywords_common_config_id[]
323 _keywords_zone_proxy_enabled[]
324 */
325 url.Values{
326 "type": []string{"OR"},
327 "offset": []string{"10"},
328 "limit": []string{"100"},
329 "_keywords_full_text[]": []string{"hogehoge", "🐰"},
330 "_keywords_service_code[]": []string{"mxxxxxx", "mxxxxxx1"},
331 "_keywords_name[]": []string{"example.jp", "example.net"},
332 "_keywords_network[]": []string{"192.168.0.0/24"},
333 "_keywords_state[]": []string{"1", "2"},
334 "_keywords_favorite[]": []string{"1", "2"},
335 "_keywords_description[]": []string{"あああ", "🍺"},
336 "_keywords_common_config_id[]": []string{"100", "200"},
337 "_keywords_zone_proxy_enabled[]": []string{"1", "0"},
338 },
339 },
340 }
341 It("can convert url.Value", func() {
342 for _, tc := range testcase {
343 s, err := tc.keyword.GetValues()
344 Expect(err).To(Succeed())
345 Expect(s).To(Equal(tc.values))
346 }
347 })
348 })
349 })
350 })