| 1 | package zones_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | net "net" |
| 6 | "net/http" |
| 7 | |
| 8 | "github.com/jarcoal/httpmock" |
| 9 | . "github.com/onsi/ginkgo" |
| 10 | . "github.com/onsi/gomega" |
| 11 | |
| 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.ZoneProxyHealthCheckList |
| 20 | cl *testtool.TestClient |
| 21 | err error |
| 22 | reqId string |
| 23 | s1, s2 zones.ZoneProxyHealthCheck |
| 24 | slist zones.ZoneProxyHealthCheckList |
| 25 | ) |
| 26 | BeforeEach(func() { |
| 27 | cl = testtool.NewTestClient("", "http://localhost", nil) |
| 28 | s1 = zones.ZoneProxyHealthCheck{ |
| 29 | Address: net.ParseIP("192.168.0.1"), |
| 30 | Status: zones.ZoneProxyStatusSuccess, |
| 31 | TsigName: "", |
| 32 | Enabled: types.Disabled, |
| 33 | } |
| 34 | s2 = zones.ZoneProxyHealthCheck{ |
| 35 | Address: net.ParseIP("2001:db8::1"), |
| 36 | Status: zones.ZoneProxyStatusFail, |
| 37 | TsigName: "tsig2", |
| 38 | Enabled: types.Enabled, |
| 39 | } |
| 40 | slist = zones.ZoneProxyHealthCheckList{ |
| 41 | AttributeMeta: zones.AttributeMeta{ |
| 42 | ZoneID: "m1", |
| 43 | }, |
| 44 | Items: []zones.ZoneProxyHealthCheck{s1, s2}, |
| 45 | } |
| 46 | }) |
| 47 | Describe("ZoneProxy", func() { |
| 48 | Context("List", func() { |
| 49 | BeforeEach(func() { |
| 50 | httpmock.RegisterResponder(http.MethodGet, "http://localhost/zones/m1/zone_proxy/health_check", httpmock.NewBytesResponder(200, []byte(`{ |
| 51 | "request_id": "751E0AB9585E48059090FA1B46E5B7A3", |
| 52 | "results": [ |
| 53 | { |
| 54 | "address": "192.168.0.1", |
| 55 | "status": "success", |
| 56 | "tsig_name": "", |
| 57 | "enabled": 0 |
| 58 | }, |
| 59 | { |
| 60 | "address": "2001:db8::1", |
| 61 | "status": "fail", |
| 62 | "tsig_name": "tsig2", |
| 63 | "enabled": 1 |
| 64 | } |
| 65 | ] |
| 66 | }`))) |
| 67 | }) |
| 68 | AfterEach(func() { |
| 69 | httpmock.Reset() |
| 70 | }) |
| 71 | AfterEach(func() { |
| 72 | httpmock.Reset() |
| 73 | }) |
| 74 | When("returns list ", func() { |
| 75 | BeforeEach(func() { |
| 76 | c = zones.ZoneProxyHealthCheckList{ |
| 77 | AttributeMeta: zones.AttributeMeta{ |
| 78 | ZoneID: "m1", |
| 79 | }, |
| 80 | } |
| 81 | reqId, err = cl.List(context.Background(), &c, nil) |
| 82 | }) |
| 83 | It("returns normal", func() { |
| 84 | Expect(err).To(Succeed()) |
| 85 | Expect(reqId).To(Equal("751E0AB9585E48059090FA1B46E5B7A3")) |
| 86 | Expect(c).To(Equal(slist)) |
| 87 | }) |
| 88 | }) |
| 89 | }) |
| 90 | Context("SetPathParams", func() { |
| 91 | When("no arguments, nothing to do", func() { |
| 92 | BeforeEach(func() { |
| 93 | err = slist.SetPathParams() |
| 94 | }) |
| 95 | It("returns error", func() { |
| 96 | Expect(err).To(Succeed()) |
| 97 | }) |
| 98 | }) |
| 99 | When("enough arguments", func() { |
| 100 | BeforeEach(func() { |
| 101 | err = slist.SetPathParams("m100") |
| 102 | }) |
| 103 | It("not returns error", func() { |
| 104 | Expect(err).To(Succeed()) |
| 105 | }) |
| 106 | It("can set ZonetId", func() { |
| 107 | Expect(slist.ZoneID).To(Equal("m100")) |
| 108 | }) |
| 109 | }) |
| 110 | When("arguments has extra value", func() { |
| 111 | BeforeEach(func() { |
| 112 | err = slist.SetPathParams("m100", 2) |
| 113 | }) |
| 114 | It("returns error", func() { |
| 115 | Expect(err).To(HaveOccurred()) |
| 116 | }) |
| 117 | }) |
| 118 | When("arguments type missmatch (ZoneID)", func() { |
| 119 | BeforeEach(func() { |
| 120 | err = slist.SetPathParams(2) |
| 121 | }) |
| 122 | It("returns error", func() { |
| 123 | Expect(err).To(HaveOccurred()) |
| 124 | }) |
| 125 | }) |
| 126 | }) |
| 127 | Context("api.Spec common test", func() { |
| 128 | var nilSpec *zones.ZoneProxyHealthCheckList |
| 129 | testtool.TestDeepCopyObject(&slist, nilSpec) |
| 130 | testtool.TestGetName(&slist, "zone_proxy/health_check") |
| 131 | |
| 132 | testtool.TestGetPathMethodForList(&slist, "/zones/m1/zone_proxy/health_check") |
| 133 | }) |
| 134 | Context("api.ListSpec common test", func() { |
| 135 | testtool.TestGetItems(&slist, &slist.Items) |
| 136 | testtool.TestLen(&slist, 2) |
| 137 | Context("Index", func() { |
| 138 | When("index exist", func() { |
| 139 | It("returns item", func() { |
| 140 | Expect(slist.Index(0)).To(Equal(s1)) |
| 141 | }) |
| 142 | }) |
| 143 | }) |
| 144 | }) |
| 145 | }) |
| 146 | Describe("ZoneProxyHealthCheck", func() { |
| 147 | var s, copy, nilSpec *zones.ZoneProxyHealthCheck |
| 148 | BeforeEach(func() { |
| 149 | s = &zones.ZoneProxyHealthCheck{} |
| 150 | }) |
| 151 | Context("DeepCopy", func() { |
| 152 | When("object is not nil", func() { |
| 153 | BeforeEach(func() { |
| 154 | copy = s.DeepCopy() |
| 155 | }) |
| 156 | It("returns copy", func() { |
| 157 | Expect(copy).NotTo(BeNil()) |
| 158 | Expect(copy).To(Equal(s)) |
| 159 | }) |
| 160 | }) |
| 161 | When("object is not nil", func() { |
| 162 | BeforeEach(func() { |
| 163 | copy = nilSpec.DeepCopy() |
| 164 | }) |
| 165 | It("returns nil", func() { |
| 166 | Expect(copy).To(BeNil()) |
| 167 | }) |
| 168 | }) |
| 169 | }) |
| 170 | }) |
| 171 | }) |