Debian Codegraph @

pkg/apis/dpf/v1/zones/zone_proxy_test.go

raw
1 package zones_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 "github.com/mimuret/golang-iij-dpf/pkg/api"
12 "github.com/mimuret/golang-iij-dpf/pkg/apis/dpf/v1/zones"
13 "github.com/mimuret/golang-iij-dpf/pkg/testtool"
14 "github.com/mimuret/golang-iij-dpf/pkg/types"
15 )
16
17 var _ = Describe("zone_proxy", func() {
18 var (
19 c zones.ZoneProxy
20 cl *testtool.TestClient
21 err error
22 reqId string
23 s1, s2 zones.ZoneProxy
24 )
25 BeforeEach(func() {
26 cl = testtool.NewTestClient("", "http://localhost", nil)
27 s1 = zones.ZoneProxy{
28 AttributeMeta: zones.AttributeMeta{
29 ZoneID: "m1",
30 },
31 Enabled: types.Enabled,
32 }
33 s2 = zones.ZoneProxy{
34 AttributeMeta: zones.AttributeMeta{
35 ZoneID: "m2",
36 },
37 Enabled: types.Disabled,
38 }
39 })
40 Describe("ZoneProxy", func() {
41 Context("Read", func() {
42 BeforeEach(func() {
43 httpmock.RegisterResponder(http.MethodGet, "http://localhost/zones/m1/zone_proxy", httpmock.NewBytesResponder(200, []byte(`{
44 "request_id": "0607103482984C3B9EC782480423CE63",
45 "result": {
46 "enabled": 1
47 }
48 }`)))
49 httpmock.RegisterResponder(http.MethodGet, "http://localhost/zones/m2/zone_proxy", httpmock.NewBytesResponder(200, []byte(`{
50 "request_id": "63896F8889174DF59FF79F5C5F4B173A",
51 "result": {
52 "enabled": 0
53 }
54 }`)))
55 })
56 AfterEach(func() {
57 httpmock.Reset()
58 })
59 When("returns id=r1", func() {
60 BeforeEach(func() {
61 c = zones.ZoneProxy{
62 AttributeMeta: zones.AttributeMeta{
63 ZoneID: "m1",
64 },
65 }
66 reqId, err = cl.Read(context.Background(), &c)
67 })
68 It("returns normal", func() {
69 Expect(err).To(Succeed())
70 Expect(reqId).To(Equal("0607103482984C3B9EC782480423CE63"))
71 Expect(c).To(Equal(s1))
72 })
73 })
74 When("returns id=r2", func() {
75 BeforeEach(func() {
76 c = zones.ZoneProxy{
77 AttributeMeta: zones.AttributeMeta{
78 ZoneID: "m2",
79 },
80 }
81 reqId, err = cl.Read(context.Background(), &c)
82 })
83 It("returns normal", func() {
84 Expect(err).To(Succeed())
85 Expect(reqId).To(Equal("63896F8889174DF59FF79F5C5F4B173A"))
86 Expect(c).To(Equal(s2))
87 })
88 })
89 })
90 Context("Update", func() {
91 var (
92 id1, bs1 = testtool.CreateAsyncResponse()
93 id2, bs2 = testtool.CreateAsyncResponse()
94 )
95 BeforeEach(func() {
96 httpmock.RegisterResponder(http.MethodPatch, "http://localhost/zones/m1/zone_proxy", httpmock.NewBytesResponder(202, bs1))
97 httpmock.RegisterResponder(http.MethodPatch, "http://localhost/zones/m2/zone_proxy", httpmock.NewBytesResponder(202, bs2))
98 })
99 AfterEach(func() {
100 httpmock.Reset()
101 })
102 When("enable", func() {
103 BeforeEach(func() {
104 reqId, err = cl.Update(context.Background(), &s1, nil)
105 })
106 It("returns job_id", func() {
107 Expect(err).To(Succeed())
108 Expect(reqId).To(Equal(id1))
109 })
110 It("post json", func() {
111 Expect(cl.RequestBody["/zones/m1/zone_proxy"]).To(MatchJSON(`{
112 "enabled": 1
113 }`))
114 })
115 })
116 When("disabled", func() {
117 BeforeEach(func() {
118 reqId, err = cl.Update(context.Background(), &s2, nil)
119 })
120 It("returns job_id", func() {
121 Expect(err).To(Succeed())
122 Expect(reqId).To(Equal(id2))
123 })
124 It("post json", func() {
125 Expect(cl.RequestBody["/zones/m2/zone_proxy"]).To(MatchJSON(`{
126 "enabled": 0
127 }`))
128 })
129 })
130 })
131 Context("SetPathParams", func() {
132 When("no arguments, nothing to do", func() {
133 BeforeEach(func() {
134 err = s1.SetPathParams()
135 })
136 It("returns error", func() {
137 Expect(err).To(Succeed())
138 })
139 })
140 When("enough arguments", func() {
141 BeforeEach(func() {
142 err = s1.SetPathParams("m100")
143 })
144 It("not returns error", func() {
145 Expect(err).To(Succeed())
146 })
147 It("can set ZonetId", func() {
148 Expect(s1.ZoneID).To(Equal("m100"))
149 })
150 })
151 When("arguments has extra value", func() {
152 BeforeEach(func() {
153 err = s1.SetPathParams("m100", 2)
154 })
155 It("returns error", func() {
156 Expect(err).To(HaveOccurred())
157 })
158 })
159 When("arguments type missmatch (ZoneID)", func() {
160 BeforeEach(func() {
161 err = s1.SetPathParams(2)
162 })
163 It("returns error", func() {
164 Expect(err).To(HaveOccurred())
165 })
166 })
167 })
168 Context("api.Spec common test", func() {
169 var nilSpec *zones.ZoneProxy
170 testtool.TestDeepCopyObject(&s1, nilSpec)
171 testtool.TestGetName(&s1, "zone_proxy")
172
173 Context("GetPathMethod", func() {
174 When("action is ActionRead", func() {
175 testtool.TestGetPathMethod(&s1, api.ActionRead, http.MethodGet, "/zones/m1/zone_proxy")
176 })
177 When("action is ActionUpdate", func() {
178 testtool.TestGetPathMethod(&s1, api.ActionUpdate, http.MethodPatch, "/zones/m1/zone_proxy")
179 })
180 When("other", func() {
181 testtool.TestGetPathMethod(&s1, api.ActionApply, "", "")
182 })
183 })
184 })
185 })
186 })