Debian Codegraph @

pkg/apis/dpf/v1/lb_domains/rule_test.go

raw symbols SCIP
1 package lb_domains_test
2
3 import (
4 "context"
5 "net/http"
6
7 "github.com/jarcoal/httpmock"
8 . "github.com/onsi/ginkgo"
9 . "github.com/onsi/gomega"
10
11 api "github.com/mimuret/golang-iij-dpf/pkg/api"
12 "github.com/mimuret/golang-iij-dpf/pkg/apis/dpf/v1/lb_domains"
13 "github.com/mimuret/golang-iij-dpf/pkg/testtool"
14 )
15
16 var _ = Describe("Rule", func() {
17 var (
18 c lb_domains.Rule
19 cl *testtool.TestClient
20 err error
21 reqId string
22 s1, s2 lb_domains.Rule
23 slist lb_domains.RuleList
24 )
25 BeforeEach(func() {
26 cl = testtool.NewTestClient("", "http://localhost", nil)
27 s1 = lb_domains.Rule{
28 AttributeMeta: lb_domains.AttributeMeta{
29 LBDomainID: "b0000000000001",
30 },
31 ResourceName: "rule-1",
32 Name: "rule#1",
33 Description: "rule 1 comment",
34 Methods: []lb_domains.RuleMethod{},
35 }
36 s2 = lb_domains.Rule{
37 AttributeMeta: lb_domains.AttributeMeta{
38 LBDomainID: "b0000000000001",
39 },
40 ResourceName: "rule-2",
41 Name: "rule#2",
42 Description: "rule 2 comment",
43 Methods: []lb_domains.RuleMethod{},
44 }
45 slist = lb_domains.RuleList{
46 AttributeMeta: lb_domains.AttributeMeta{
47 LBDomainID: "b0000000000001",
48 },
49 Items: []lb_domains.Rule{s1, s2},
50 }
51 })
52 Describe("Rule", func() {
53 Context("Read", func() {
54 BeforeEach(func() {
55 httpmock.RegisterResponder(http.MethodGet, "http://localhost/lb_domains/b0000000000001/rules/rule-1", httpmock.NewBytesResponder(200, []byte(`{
56 "request_id": "76D7462F50E3417C96898A827E0D1EC1",
57 "result": {
58 "resource_name": "rule-1",
59 "name": "rule#1",
60 "description": "rule 1 comment",
61 "methods": []
62 }
63 }`)))
64 httpmock.RegisterResponder(http.MethodGet, "http://localhost/lb_domains/b0000000000001/rules/rule-2", httpmock.NewBytesResponder(200, []byte(`{
65 "request_id": "B10F0042282A46C18BFB440B5B58BF8C",
66 "result": {
67 "resource_name": "rule-2",
68 "name": "rule#2",
69 "description": "rule 2 comment",
70 "methods": []
71 }
72 }`)))
73 })
74 AfterEach(func() {
75 httpmock.Reset()
76 })
77 When("returns LBDomain m1", func() {
78 BeforeEach(func() {
79 c = lb_domains.Rule{
80 AttributeMeta: lb_domains.AttributeMeta{
81 LBDomainID: "b0000000000001",
82 },
83 ResourceName: "rule-1",
84 }
85 reqId, err = cl.Read(context.Background(), &c)
86 })
87 It("returns normal", func() {
88 Expect(err).To(Succeed())
89 Expect(reqId).To(Equal("76D7462F50E3417C96898A827E0D1EC1"))
90 Expect(c).To(Equal(s1))
91 })
92 })
93 When("returns LBDomain m2", func() {
94 BeforeEach(func() {
95 c = lb_domains.Rule{
96 AttributeMeta: lb_domains.AttributeMeta{
97 LBDomainID: "b0000000000001",
98 },
99 ResourceName: "rule-2",
100 }
101 reqId, err = cl.Read(context.Background(), &c)
102 })
103 It("returns normal", func() {
104 Expect(err).To(Succeed())
105 Expect(reqId).To(Equal("B10F0042282A46C18BFB440B5B58BF8C"))
106 Expect(c).To(Equal(s2))
107 })
108 })
109 })
110 Context("Create", func() {
111 id1, bs1 := testtool.CreateAsyncResponse()
112 BeforeEach(func() {
113 httpmock.RegisterResponder(http.MethodPost, "http://localhost/lb_domains/b0000000000001/rules", httpmock.NewBytesResponder(202, bs1))
114 reqId, err = cl.Create(context.Background(), &s1, nil)
115 })
116 AfterEach(func() {
117 httpmock.Reset()
118 })
119 It("returns job_id", func() {
120 Expect(err).To(Succeed())
121 Expect(reqId).To(Equal(id1))
122 })
123 It("post json", func() {
124 Expect(cl.RequestBody["/lb_domains/b0000000000001/rules"]).To(MatchJSON(`{
125 "resource_name": "rule-1",
126 "name": "rule#1",
127 "description": "rule 1 comment"
128 }`))
129 })
130 })
131 Context("Update", func() {
132 id1, bs1 := testtool.CreateAsyncResponse()
133 BeforeEach(func() {
134 httpmock.RegisterResponder(http.MethodPatch, "http://localhost/lb_domains/b0000000000001/rules/rule-1", httpmock.NewBytesResponder(202, bs1))
135 reqId, err = cl.Update(context.Background(), &s1, nil)
136 })
137 AfterEach(func() {
138 httpmock.Reset()
139 })
140 It("returns job_id", func() {
141 Expect(err).To(Succeed())
142 Expect(reqId).To(Equal(id1))
143 })
144 It("post json", func() {
145 Expect(cl.RequestBody["/lb_domains/b0000000000001/rules/rule-1"]).To(MatchJSON(`{
146 "name": "rule#1",
147 "description": "rule 1 comment"
148 }`))
149 })
150 })
151 Context("SetPathParams", func() {
152 When("no arguments, nothing to do", func() {
153 BeforeEach(func() {
154 err = s1.SetPathParams()
155 })
156 It("returns error", func() {
157 Expect(err).To(Succeed())
158 })
159 })
160 When("enough arguments", func() {
161 BeforeEach(func() {
162 err = s1.SetPathParams("b0000000000010", "id1")
163 })
164 It("not returns error", func() {
165 Expect(err).To(Succeed())
166 })
167 It("can set LBDomainID", func() {
168 Expect(s1.LBDomainID).To(Equal("b0000000000010"))
169 Expect(s1.ResourceName).To(Equal("id1"))
170 })
171 })
172 When("arguments has extra value", func() {
173 BeforeEach(func() {
174 err = s1.SetPathParams("b0000000000010", "id1", 2)
175 })
176 It("returns error", func() {
177 Expect(err).To(HaveOccurred())
178 })
179 })
180 When("arguments type missmatch (LBDomainID)", func() {
181 BeforeEach(func() {
182 err = s1.SetPathParams(2, "id1")
183 })
184 It("returns error", func() {
185 Expect(err).To(HaveOccurred())
186 })
187 })
188 })
189 Context("api.Spec common test", func() {
190 var nilSpec *lb_domains.Rule
191 testtool.TestDeepCopyObject(&s1, nilSpec)
192 testtool.TestGetName(&s1, "rules")
193
194 Context("GetPathMethod", func() {
195 When("action is ActionRead", func() {
196 testtool.TestGetPathMethod(&s1, api.ActionRead, http.MethodGet, "/lb_domains/b0000000000001/rules/rule-1")
197 })
198 When("action is ActionUpdate", func() {
199 testtool.TestGetPathMethod(&s1, api.ActionUpdate, http.MethodPatch, "/lb_domains/b0000000000001/rules/rule-1")
200 })
201 When("action is ActionDelete", func() {
202 testtool.TestGetPathMethod(&s1, api.ActionDelete, http.MethodDelete, "/lb_domains/b0000000000001/rules/rule-1")
203 })
204 When("action is ActionCreate", func() {
205 testtool.TestGetPathMethod(&s1, api.ActionCreate, http.MethodPost, "/lb_domains/b0000000000001/rules")
206 })
207 When("action is other", func() {
208 testtool.TestGetPathMethod(&s1, api.ActionApply, "", "")
209 })
210 })
211 })
212 })
213 Describe("RuleList", func() {
214 var c lb_domains.RuleList
215 Context("List", func() {
216 BeforeEach(func() {
217 httpmock.RegisterResponder(http.MethodGet, "http://localhost/lb_domains/b0000000000001/rules", httpmock.NewBytesResponder(200, []byte(`{
218 "request_id": "2C18922432DC48D485613F5383A7ED8E",
219 "results": [
220 {
221 "resource_name": "rule-1",
222 "name": "rule#1",
223 "description": "rule 1 comment",
224 "Methods": []
225 },
226 {
227 "resource_name": "rule-2",
228 "name": "rule#2",
229 "description": "rule 2 comment",
230 "Methods": []
231 }
232 ]
233 }`)))
234 })
235 AfterEach(func() {
236 httpmock.Reset()
237 })
238 When("returns list ", func() {
239 BeforeEach(func() {
240 c = lb_domains.RuleList{
241 AttributeMeta: lb_domains.AttributeMeta{
242 LBDomainID: "b0000000000001",
243 },
244 }
245 reqId, err = cl.List(context.Background(), &c, nil)
246 })
247 It("returns normal", func() {
248 Expect(err).To(Succeed())
249 Expect(reqId).To(Equal("2C18922432DC48D485613F5383A7ED8E"))
250 Expect(c).To(Equal(slist))
251 })
252 })
253 })
254 Context("SetPathParams", func() {
255 When("no arguments, nothing to do", func() {
256 BeforeEach(func() {
257 err = slist.SetPathParams()
258 })
259 It("returns error", func() {
260 Expect(err).To(Succeed())
261 })
262 })
263 When("enough arguments", func() {
264 BeforeEach(func() {
265 err = slist.SetPathParams("b0000000000010")
266 })
267 It("not returns error", func() {
268 Expect(err).To(Succeed())
269 })
270 It("can set LBDomainID", func() {
271 Expect(slist.LBDomainID).To(Equal("b0000000000010"))
272 })
273 })
274 When("arguments has extra value", func() {
275 BeforeEach(func() {
276 err = slist.SetPathParams("b0000000000010", 2)
277 })
278 It("returns error", func() {
279 Expect(err).To(HaveOccurred())
280 })
281 })
282 When("arguments type missmatch (LBDomainID)", func() {
283 BeforeEach(func() {
284 err = slist.SetPathParams(2)
285 })
286 It("returns error", func() {
287 Expect(err).To(HaveOccurred())
288 })
289 })
290 })
291 Context("api.Spec common test", func() {
292 var nilSpec *lb_domains.RuleList
293 testtool.TestDeepCopyObject(&slist, nilSpec)
294 testtool.TestGetName(&slist, "rules")
295 })
296 Context("api.ListSpec common test", func() {
297 testtool.TestGetItems(&slist, &slist.Items)
298 testtool.TestLen(&slist, 2)
299 Context("Index", func() {
300 When("index exist", func() {
301 It("returns item", func() {
302 Expect(slist.Index(0)).To(Equal(s1))
303 })
304 })
305 })
306 })
307 })
308 })