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