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