| 1 | package zones_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "time" |
| 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/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("ds_records", func() { |
| 18 | var ( |
| 19 | c zones.DsRecordList |
| 20 | cl *testtool.TestClient |
| 21 | err error |
| 22 | reqId string |
| 23 | s1, s2 zones.DsRecord |
| 24 | slist zones.DsRecordList |
| 25 | ) |
| 26 | BeforeEach(func() { |
| 27 | atTime, err := types.ParseTime(time.RFC3339Nano, "2021-06-20T10:23:51.071Z") |
| 28 | Expect(err).To(Succeed()) |
| 29 | |
| 30 | cl = testtool.NewTestClient("", "http://localhost", nil) |
| 31 | s1 = zones.DsRecord{ |
| 32 | RRSet: "46369 8 2 39F054DCB3EC1E93D8AE6D8F1AAAD91794055EA36895045FAF6F65F0 2FEBC579", |
| 33 | TransitAt: atTime, |
| 34 | } |
| 35 | s2 = zones.DsRecord{ |
| 36 | RRSet: "57367 8 2 C069902A17BC8E19F1D5809272219B694C09EC5BCF4AAD4911FC765B 1C233A69", |
| 37 | TransitAt: atTime, |
| 38 | } |
| 39 | slist = zones.DsRecordList{ |
| 40 | AttributeMeta: zones.AttributeMeta{ |
| 41 | ZoneID: "m1", |
| 42 | }, |
| 43 | Items: []zones.DsRecord{s1, s2}, |
| 44 | } |
| 45 | }) |
| 46 | Describe("DsRecordList", func() { |
| 47 | Context("List", func() { |
| 48 | BeforeEach(func() { |
| 49 | httpmock.RegisterResponder(http.MethodGet, "http://localhost/zones/m1/ds_records", httpmock.NewBytesResponder(200, []byte(`{ |
| 50 | "request_id": "CD445FC65BBA4977B78B347A87633947", |
| 51 | "results": [ |
| 52 | { |
| 53 | "rrset": "46369 8 2 39F054DCB3EC1E93D8AE6D8F1AAAD91794055EA36895045FAF6F65F0 2FEBC579", |
| 54 | "transited_at": "2021-06-20T10:23:51.071Z" |
| 55 | }, |
| 56 | { |
| 57 | "rrset": "57367 8 2 C069902A17BC8E19F1D5809272219B694C09EC5BCF4AAD4911FC765B 1C233A69", |
| 58 | "transited_at": "2021-06-20T10:23:51.071Z" |
| 59 | } |
| 60 | ] |
| 61 | }`))) |
| 62 | }) |
| 63 | AfterEach(func() { |
| 64 | httpmock.Reset() |
| 65 | }) |
| 66 | When("returns m1", func() { |
| 67 | BeforeEach(func() { |
| 68 | c = zones.DsRecordList{ |
| 69 | AttributeMeta: zones.AttributeMeta{ |
| 70 | ZoneID: "m1", |
| 71 | }, |
| 72 | } |
| 73 | reqId, err = cl.List(context.Background(), &c, nil) |
| 74 | }) |
| 75 | It("returns normal", func() { |
| 76 | Expect(err).To(Succeed()) |
| 77 | Expect(reqId).To(Equal("CD445FC65BBA4977B78B347A87633947")) |
| 78 | Expect(c).To(Equal(slist)) |
| 79 | }) |
| 80 | }) |
| 81 | }) |
| 82 | Context("SetPathParams", func() { |
| 83 | When("no arguments, nothing to do", func() { |
| 84 | BeforeEach(func() { |
| 85 | err = slist.SetPathParams() |
| 86 | }) |
| 87 | It("returns error", func() { |
| 88 | Expect(err).To(Succeed()) |
| 89 | }) |
| 90 | }) |
| 91 | When("enough arguments", func() { |
| 92 | BeforeEach(func() { |
| 93 | err = slist.SetPathParams("m10") |
| 94 | }) |
| 95 | It("not returns error", func() { |
| 96 | Expect(err).To(Succeed()) |
| 97 | }) |
| 98 | It("can set ContractID", func() { |
| 99 | Expect(slist.GetZoneID()).To(Equal("m10")) |
| 100 | }) |
| 101 | }) |
| 102 | When("arguments has extra value", func() { |
| 103 | BeforeEach(func() { |
| 104 | err = slist.SetPathParams("m10", 2) |
| 105 | }) |
| 106 | It("returns error", func() { |
| 107 | Expect(err).To(HaveOccurred()) |
| 108 | }) |
| 109 | }) |
| 110 | When("arguments type missmatch (ZoneID)", func() { |
| 111 | BeforeEach(func() { |
| 112 | err = slist.SetPathParams(2) |
| 113 | }) |
| 114 | It("returns error", func() { |
| 115 | Expect(err).To(HaveOccurred()) |
| 116 | }) |
| 117 | }) |
| 118 | }) |
| 119 | Context("api.Spec common test", func() { |
| 120 | var nilSpec *zones.DsRecordList |
| 121 | testtool.TestDeepCopyObject(&slist, nilSpec) |
| 122 | testtool.TestGetName(&slist, "ds_records") |
| 123 | testtool.TestGetPathMethodForList(&slist, "/zones/m1/ds_records") |
| 124 | }) |
| 125 | Context("api.ListSpec common test", func() { |
| 126 | testtool.TestGetItems(&slist, &slist.Items) |
| 127 | testtool.TestLen(&slist, 2) |
| 128 | Context("Index", func() { |
| 129 | When("index exist", func() { |
| 130 | It("returns item", func() { |
| 131 | Expect(slist.Index(0)).To(Equal(s1)) |
| 132 | }) |
| 133 | }) |
| 134 | }) |
| 135 | }) |
| 136 | }) |
| 137 | Describe("DsRecord", func() { |
| 138 | var s, copy, nilSpec *zones.DsRecord |
| 139 | BeforeEach(func() { |
| 140 | s = &zones.DsRecord{} |
| 141 | }) |
| 142 | Context("DeepCopy", func() { |
| 143 | When("object is not nil", func() { |
| 144 | BeforeEach(func() { |
| 145 | copy = s.DeepCopy() |
| 146 | }) |
| 147 | It("returns copy", func() { |
| 148 | Expect(copy).NotTo(BeNil()) |
| 149 | Expect(copy).To(Equal(s)) |
| 150 | }) |
| 151 | }) |
| 152 | When("object is not nil", func() { |
| 153 | BeforeEach(func() { |
| 154 | copy = nilSpec.DeepCopy() |
| 155 | }) |
| 156 | It("returns nil", func() { |
| 157 | Expect(copy).To(BeNil()) |
| 158 | }) |
| 159 | }) |
| 160 | }) |
| 161 | }) |
| 162 | }) |