| 1 |
package core_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net/http" |
| 6 |
"net/url" |
| 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/api" |
| 13 |
"github.com/mimuret/golang-iij-dpf/pkg/apis/dpf/v1/core" |
| 14 |
"github.com/mimuret/golang-iij-dpf/pkg/testtool" |
| 15 |
"github.com/mimuret/golang-iij-dpf/pkg/types" |
| 16 |
) |
| 17 |
|
| 18 |
var _ = Describe("lb_domains", func() { |
| 19 |
var ( |
| 20 |
c core.LBDomain |
| 21 |
cl *testtool.TestClient |
| 22 |
err error |
| 23 |
reqId string |
| 24 |
s1, s2 core.LBDomain |
| 25 |
slist core.LBDomainList |
| 26 |
) |
| 27 |
BeforeEach(func() { |
| 28 |
cl = testtool.NewTestClient("", "http://localhost", nil) |
| 29 |
s1 = core.LBDomain{ |
| 30 |
ID: "b0000000000001", |
| 31 |
CommonConfigID: 1, |
| 32 |
ServiceCode: "dpl0000001", |
| 33 |
State: types.StateBeforeStart, |
| 34 |
Favorite: types.FavoriteHighPriority, |
| 35 |
Name: "lb.xxxxxxxxxxxxxxxx.x.d-16.jp.", |
| 36 |
Description: "lb_domains 1", |
| 37 |
RuleResourceName: "rule-1", |
| 38 |
} |
| 39 |
s2 = core.LBDomain{ |
| 40 |
ID: "b0000000000002", |
| 41 |
CommonConfigID: 2, |
| 42 |
ServiceCode: "dpl0000002", |
| 43 |
State: types.StateRunning, |
| 44 |
Favorite: types.FavoriteLowPriority, |
| 45 |
Name: "lb.xxxxxxxxxxxxxxxx.x.d-16.jp.", |
| 46 |
Description: "lb_domains 2", |
| 47 |
RuleResourceName: "rule-2", |
| 48 |
} |
| 49 |
slist = core.LBDomainList{ |
| 50 |
Items: []core.LBDomain{s1, s2}, |
| 51 |
} |
| 52 |
}) |
| 53 |
Describe("Contract", func() { |
| 54 |
Context("Read", func() { |
| 55 |
BeforeEach(func() { |
| 56 |
httpmock.RegisterResponder(http.MethodGet, "http://localhost/lb_domains/b0000000000001", httpmock.NewBytesResponder(200, []byte(`{ |
| 57 |
"request_id": "76D7462F50E3417C96898A827E0D1EC1", |
| 58 |
"result": { |
| 59 |
"id": "b0000000000001", |
| 60 |
"common_config_id": 1, |
| 61 |
"service_code": "dpl0000001", |
| 62 |
"state": 1, |
| 63 |
"favorite": 1, |
| 64 |
"name": "lb.xxxxxxxxxxxxxxxx.x.d-16.jp.", |
| 65 |
"description": "lb_domains 1", |
| 66 |
"rule_resource_name": "rule-1" |
| 67 |
} |
| 68 |
}`))) |
| 69 |
httpmock.RegisterResponder(http.MethodGet, "http://localhost/lb_domains/b0000000000002", httpmock.NewBytesResponder(200, []byte(`{ |
| 70 |
"request_id": "B10F0042282A46C18BFB440B5B58BF8C", |
| 71 |
"result": { |
| 72 |
"id": "b0000000000002", |
| 73 |
"common_config_id": 2, |
| 74 |
"service_code": "dpl0000002", |
| 75 |
"state": 2, |
| 76 |
"favorite":2, |
| 77 |
"name": "lb.xxxxxxxxxxxxxxxx.x.d-16.jp.", |
| 78 |
"description": "lb_domains 2", |
| 79 |
"rule_resource_name": "rule-2" |
| 80 |
} |
| 81 |
}`))) |
| 82 |
}) |
| 83 |
AfterEach(func() { |
| 84 |
httpmock.Reset() |
| 85 |
}) |
| 86 |
When("returns LBDomain m1", func() { |
| 87 |
BeforeEach(func() { |
| 88 |
c = core.LBDomain{ |
| 89 |
ID: "b0000000000001", |
| 90 |
} |
| 91 |
reqId, err = cl.Read(context.Background(), &c) |
| 92 |
}) |
| 93 |
It("returns normal", func() { |
| 94 |
Expect(err).To(Succeed()) |
| 95 |
Expect(reqId).To(Equal("76D7462F50E3417C96898A827E0D1EC1")) |
| 96 |
Expect(c).To(Equal(s1)) |
| 97 |
}) |
| 98 |
}) |
| 99 |
When("returns LBDomain m2", func() { |
| 100 |
BeforeEach(func() { |
| 101 |
c = core.LBDomain{ |
| 102 |
ID: "b0000000000002", |
| 103 |
} |
| 104 |
reqId, err = cl.Read(context.Background(), &c) |
| 105 |
}) |
| 106 |
It("returns normal", func() { |
| 107 |
Expect(err).To(Succeed()) |
| 108 |
Expect(reqId).To(Equal("B10F0042282A46C18BFB440B5B58BF8C")) |
| 109 |
Expect(c).To(Equal(s2)) |
| 110 |
}) |
| 111 |
}) |
| 112 |
}) |
| 113 |
Context("Update", func() { |
| 114 |
id1, bs1 := testtool.CreateAsyncResponse() |
| 115 |
BeforeEach(func() { |
| 116 |
httpmock.RegisterResponder(http.MethodPatch, "http://localhost/lb_domains/b0000000000001", httpmock.NewBytesResponder(202, bs1)) |
| 117 |
reqId, err = cl.Update(context.Background(), &s1, nil) |
| 118 |
}) |
| 119 |
AfterEach(func() { |
| 120 |
httpmock.Reset() |
| 121 |
}) |
| 122 |
It("returns job_id", func() { |
| 123 |
Expect(err).To(Succeed()) |
| 124 |
Expect(reqId).To(Equal(id1)) |
| 125 |
}) |
| 126 |
It("post json", func() { |
| 127 |
Expect(cl.RequestBody["/lb_domains/b0000000000001"]).To(MatchJSON(`{ |
| 128 |
"favorite": 1, |
| 129 |
"description": "lb_domains 1", |
| 130 |
"rule_resource_name": "rule-1" |
| 131 |
}`)) |
| 132 |
}) |
| 133 |
}) |
| 134 |
Context("SetPathParams", func() { |
| 135 |
When("no arguments, nothing to do", func() { |
| 136 |
BeforeEach(func() { |
| 137 |
err = s1.SetPathParams() |
| 138 |
}) |
| 139 |
It("returns error", func() { |
| 140 |
Expect(err).To(Succeed()) |
| 141 |
}) |
| 142 |
}) |
| 143 |
When("enough arguments", func() { |
| 144 |
BeforeEach(func() { |
| 145 |
err = s1.SetPathParams("b0000000000010") |
| 146 |
}) |
| 147 |
It("not returns error", func() { |
| 148 |
Expect(err).To(Succeed()) |
| 149 |
}) |
| 150 |
It("can set ContractID", func() { |
| 151 |
Expect(s1.ID).To(Equal("b0000000000010")) |
| 152 |
}) |
| 153 |
}) |
| 154 |
When("arguments has extra value", func() { |
| 155 |
BeforeEach(func() { |
| 156 |
err = s1.SetPathParams("b0000000000010", 2) |
| 157 |
}) |
| 158 |
It("returns error", func() { |
| 159 |
Expect(err).To(HaveOccurred()) |
| 160 |
}) |
| 161 |
}) |
| 162 |
When("arguments type missmatch", func() { |
| 163 |
BeforeEach(func() { |
| 164 |
err = s1.SetPathParams(2) |
| 165 |
}) |
| 166 |
It("returns error", func() { |
| 167 |
Expect(err).To(HaveOccurred()) |
| 168 |
}) |
| 169 |
}) |
| 170 |
}) |
| 171 |
Context("api.Spec common test", func() { |
| 172 |
var nilSpec *core.LBDomain |
| 173 |
testtool.TestDeepCopyObject(&s1, nilSpec) |
| 174 |
testtool.TestGetName(&s1, "lb_domains") |
| 175 |
|
| 176 |
Context("GetPathMethod", func() { |
| 177 |
When("action is ActionRead", func() { |
| 178 |
testtool.TestGetPathMethod(&s1, api.ActionRead, http.MethodGet, "/lb_domains/b0000000000001") |
| 179 |
}) |
| 180 |
When("action is ActionUpdate", func() { |
| 181 |
testtool.TestGetPathMethod(&s1, api.ActionUpdate, http.MethodPatch, "/lb_domains/b0000000000001") |
| 182 |
}) |
| 183 |
When("action is other", func() { |
| 184 |
testtool.TestGetPathMethod(&s1, api.ActionApply, "", "") |
| 185 |
}) |
| 186 |
}) |
| 187 |
}) |
| 188 |
}) |
| 189 |
Describe("LBDomainList", func() { |
| 190 |
var c core.LBDomainList |
| 191 |
Context("List", func() { |
| 192 |
BeforeEach(func() { |
| 193 |
httpmock.RegisterResponder(http.MethodGet, "http://localhost/lb_domains", httpmock.NewBytesResponder(200, []byte(`{ |
| 194 |
"request_id": "2C18922432DC48D485613F5383A7ED8E", |
| 195 |
"results": [ |
| 196 |
{ |
| 197 |
"id": "b0000000000001", |
| 198 |
"common_config_id": 1, |
| 199 |
"service_code": "dpl0000001", |
| 200 |
"state": 1, |
| 201 |
"favorite": 1, |
| 202 |
"name": "lb.xxxxxxxxxxxxxxxx.x.d-16.jp.", |
| 203 |
"description": "lb_domains 1", |
| 204 |
"rule_resource_name": "rule-1" |
| 205 |
}, |
| 206 |
{ |
| 207 |
"id": "b0000000000002", |
| 208 |
"common_config_id": 2, |
| 209 |
"service_code": "dpl0000002", |
| 210 |
"state": 2, |
| 211 |
"favorite":2, |
| 212 |
"name": "lb.xxxxxxxxxxxxxxxx.x.d-16.jp.", |
| 213 |
"description": "lb_domains 2", |
| 214 |
"rule_resource_name": "rule-2" |
| 215 |
} |
| 216 |
] |
| 217 |
}`))) |
| 218 |
httpmock.RegisterResponder(http.MethodGet, "http://localhost/lb_domains/count", httpmock.NewBytesResponder(200, []byte(`{ |
| 219 |
"request_id": "9C518C729E5541D999389C686FE8987D", |
| 220 |
"result": { |
| 221 |
"count": 2 |
| 222 |
} |
| 223 |
}`))) |
| 224 |
}) |
| 225 |
AfterEach(func() { |
| 226 |
httpmock.Reset() |
| 227 |
}) |
| 228 |
When("returns list ", func() { |
| 229 |
BeforeEach(func() { |
| 230 |
c = core.LBDomainList{} |
| 231 |
reqId, err = cl.List(context.Background(), &c, nil) |
| 232 |
}) |
| 233 |
It("returns normal", func() { |
| 234 |
Expect(err).To(Succeed()) |
| 235 |
Expect(reqId).To(Equal("2C18922432DC48D485613F5383A7ED8E")) |
| 236 |
Expect(c).To(Equal(slist)) |
| 237 |
}) |
| 238 |
}) |
| 239 |
}) |
| 240 |
Context("SetPathParams", func() { |
| 241 |
BeforeEach(func() { |
| 242 |
err = slist.SetPathParams() |
| 243 |
}) |
| 244 |
It("nothing todo", func() { |
| 245 |
Expect(err).To(Succeed()) |
| 246 |
}) |
| 247 |
}) |
| 248 |
Context("api.Spec common test", func() { |
| 249 |
var nilSpec *core.LBDomainList |
| 250 |
testtool.TestDeepCopyObject(&slist, nilSpec) |
| 251 |
testtool.TestGetName(&slist, "lb_domains") |
| 252 |
|
| 253 |
testtool.TestGetPathMethodForCountableList(&slist, "/lb_domains") |
| 254 |
}) |
| 255 |
Context("api.ListSpec common test", func() { |
| 256 |
testtool.TestGetItems(&slist, &slist.Items) |
| 257 |
testtool.TestLen(&slist, 2) |
| 258 |
Context("Index", func() { |
| 259 |
When("index exist", func() { |
| 260 |
It("returns item", func() { |
| 261 |
Expect(slist.Index(0)).To(Equal(s1)) |
| 262 |
}) |
| 263 |
}) |
| 264 |
}) |
| 265 |
}) |
| 266 |
Context("api.CountableListSpec common test", func() { |
| 267 |
testtool.TestGetMaxLimit(&slist, 10000) |
| 268 |
testtool.TestClearItems(&slist) |
| 269 |
testtool.TestAddItem(&slist, s1) |
| 270 |
}) |
| 271 |
}) |
| 272 |
Describe("LBDomainListSearchKeywords", func() { |
| 273 |
Context("GetValues", func() { |
| 274 |
testcase := []struct { |
| 275 |
keyword core.LBDomainListSearchKeywords |
| 276 |
values url.Values |
| 277 |
}{ |
| 278 |
{ |
| 279 |
core.LBDomainListSearchKeywords{ |
| 280 |
CommonSearchParams: api.CommonSearchParams{ |
| 281 |
Type: api.SearchTypeAND, |
| 282 |
Offset: int32(10), |
| 283 |
Limit: int32(100), |
| 284 |
}, |
| 285 |
}, |
| 286 |
url.Values{ |
| 287 |
"type": []string{"AND"}, |
| 288 |
"offset": []string{"10"}, |
| 289 |
"limit": []string{"100"}, |
| 290 |
}, |
| 291 |
}, |
| 292 |
{ |
| 293 |
core.LBDomainListSearchKeywords{ |
| 294 |
CommonSearchParams: api.CommonSearchParams{ |
| 295 |
Type: api.SearchTypeOR, |
| 296 |
Offset: int32(10), |
| 297 |
Limit: int32(100), |
| 298 |
}, |
| 299 |
FullText: api.KeywordsString{"hogehoge", "🐰"}, |
| 300 |
ServiceCode: api.KeywordsString{"mxxxxxx", "mxxxxxx1"}, |
| 301 |
Name: api.KeywordsString{"example.jp", "example.net"}, |
| 302 |
State: api.KeywordsState{types.StateBeforeStart, types.StateRunning}, |
| 303 |
Favorite: api.KeywordsFavorite{types.FavoriteHighPriority, types.FavoriteLowPriority}, |
| 304 |
Description: api.KeywordsString{"あああ", "🍺"}, |
| 305 |
CommonConfigID: api.KeywordsID{100, 200}, |
| 306 |
Label: api.KeywordsLabels{api.Label{Key: "hogehoge", Value: "hugahuga"}}, |
| 307 |
}, |
| 308 |
/* |
| 309 |
_keywords_full_text[] |
| 310 |
_keywords_service_code[] |
| 311 |
_keywords_name[] |
| 312 |
_keywords_state[] |
| 313 |
_keywords_favorite[] |
| 314 |
_keywords_description[] |
| 315 |
_keywords_common_config_id[] |
| 316 |
_keywords_label[] |
| 317 |
*/ |
| 318 |
url.Values{ |
| 319 |
"type": []string{"OR"}, |
| 320 |
"offset": []string{"10"}, |
| 321 |
"limit": []string{"100"}, |
| 322 |
"_keywords_full_text[]": []string{"hogehoge", "🐰"}, |
| 323 |
"_keywords_service_code[]": []string{"mxxxxxx", "mxxxxxx1"}, |
| 324 |
"_keywords_name[]": []string{"example.jp", "example.net"}, |
| 325 |
"_keywords_state[]": []string{"1", "2"}, |
| 326 |
"_keywords_favorite[]": []string{"1", "2"}, |
| 327 |
"_keywords_description[]": []string{"あああ", "🍺"}, |
| 328 |
"_keywords_common_config_id[]": []string{"100", "200"}, |
| 329 |
"_keywords_label[]": []string{"hogehoge=hugahuga"}, |
| 330 |
}, |
| 331 |
}, |
| 332 |
} |
| 333 |
It("can convert url.Value", func() { |
| 334 |
for _, tc := range testcase { |
| 335 |
s, err := tc.keyword.GetValues() |
| 336 |
Expect(err).To(Succeed()) |
| 337 |
Expect(s).To(Equal(tc.values)) |
| 338 |
} |
| 339 |
}) |
| 340 |
}) |
| 341 |
}) |
| 342 |
}) |