/
fixtures.go
455 lines (421 loc) · 15 KB
/
fixtures.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package testing
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
th "github.com/gophercloud/gophercloud/testhelper"
"github.com/gophercloud/gophercloud/testhelper/client"
)
// PoolsListBody contains the canned body of a pool list response.
const PoolsListBody = `
{
"pools":[
{
"lb_algorithm":"ROUND_ROBIN",
"protocol":"HTTP",
"description":"",
"healthmonitor_id": "466c8345-28d8-4f84-a246-e04380b0461d",
"members":[{"id": "53306cda-815d-4354-9fe4-59e09da9c3c5"}],
"listeners":[{"id": "2a280670-c202-4b0b-a562-34077415aabf"}],
"loadbalancers":[{"id": "79e05663-7f03-45d2-a092-8b94062f22ab"}],
"id":"72741b06-df4d-4715-b142-276b6bce75ab",
"name":"web",
"admin_state_up":true,
"project_id":"83657cfcdfe44cd5920adaf26c48ceea",
"provider": "haproxy"
},
{
"lb_algorithm":"LEAST_CONNECTION",
"protocol":"HTTP",
"description":"",
"healthmonitor_id": "5f6c8345-28d8-4f84-a246-e04380b0461d",
"members":[{"id": "67306cda-815d-4354-9fe4-59e09da9c3c5"}],
"listeners":[{"id": "2a280670-c202-4b0b-a562-34077415aabf"}],
"loadbalancers":[{"id": "79e05663-7f03-45d2-a092-8b94062f22ab"}],
"id":"c3741b06-df4d-4715-b142-276b6bce75ab",
"name":"db",
"admin_state_up":true,
"project_id":"83657cfcdfe44cd5920adaf26c48ceea",
"provider": "haproxy"
}
]
}
`
// SinglePoolBody is the canned body of a Get request on an existing pool.
const SinglePoolBody = `
{
"pool": {
"lb_algorithm":"LEAST_CONNECTION",
"protocol":"HTTP",
"description":"",
"healthmonitor_id": "5f6c8345-28d8-4f84-a246-e04380b0461d",
"members":[{"id": "67306cda-815d-4354-9fe4-59e09da9c3c5"}],
"listeners":[{"id": "2a280670-c202-4b0b-a562-34077415aabf"}],
"loadbalancers":[{"id": "79e05663-7f03-45d2-a092-8b94062f22ab"}],
"id":"c3741b06-df4d-4715-b142-276b6bce75ab",
"name":"db",
"admin_state_up":true,
"project_id":"83657cfcdfe44cd5920adaf26c48ceea",
"provider": "haproxy"
}
}
`
// PostUpdatePoolBody is the canned response body of a Update request on an existing pool.
const PostUpdatePoolBody = `
{
"pool": {
"lb_algorithm":"LEAST_CONNECTION",
"protocol":"HTTP",
"description":"",
"healthmonitor_id": "5f6c8345-28d8-4f84-a246-e04380b0461d",
"members":[{"id": "67306cda-815d-4354-9fe4-59e09da9c3c5"}],
"listeners":[{"id": "2a280670-c202-4b0b-a562-34077415aabf"}],
"loadbalancers":[{"id": "79e05663-7f03-45d2-a092-8b94062f22ab"}],
"id":"c3741b06-df4d-4715-b142-276b6bce75ab",
"name":"db",
"admin_state_up":true,
"project_id":"83657cfcdfe44cd5920adaf26c48ceea",
"provider": "haproxy"
}
}
`
var (
PoolWeb = pools.Pool{
LBMethod: "ROUND_ROBIN",
Protocol: "HTTP",
Description: "",
MonitorID: "466c8345-28d8-4f84-a246-e04380b0461d",
ProjectID: "83657cfcdfe44cd5920adaf26c48ceea",
AdminStateUp: true,
Name: "web",
Members: []pools.Member{{ID: "53306cda-815d-4354-9fe4-59e09da9c3c5"}},
ID: "72741b06-df4d-4715-b142-276b6bce75ab",
Loadbalancers: []pools.LoadBalancerID{{ID: "79e05663-7f03-45d2-a092-8b94062f22ab"}},
Listeners: []pools.ListenerID{{ID: "2a280670-c202-4b0b-a562-34077415aabf"}},
Provider: "haproxy",
}
PoolDb = pools.Pool{
LBMethod: "LEAST_CONNECTION",
Protocol: "HTTP",
Description: "",
MonitorID: "5f6c8345-28d8-4f84-a246-e04380b0461d",
ProjectID: "83657cfcdfe44cd5920adaf26c48ceea",
AdminStateUp: true,
Name: "db",
Members: []pools.Member{{ID: "67306cda-815d-4354-9fe4-59e09da9c3c5"}},
ID: "c3741b06-df4d-4715-b142-276b6bce75ab",
Loadbalancers: []pools.LoadBalancerID{{ID: "79e05663-7f03-45d2-a092-8b94062f22ab"}},
Listeners: []pools.ListenerID{{ID: "2a280670-c202-4b0b-a562-34077415aabf"}},
Provider: "haproxy",
}
PoolUpdated = pools.Pool{
LBMethod: "LEAST_CONNECTION",
Protocol: "HTTP",
Description: "",
MonitorID: "5f6c8345-28d8-4f84-a246-e04380b0461d",
ProjectID: "83657cfcdfe44cd5920adaf26c48ceea",
AdminStateUp: true,
Name: "db",
Members: []pools.Member{{ID: "67306cda-815d-4354-9fe4-59e09da9c3c5"}},
ID: "c3741b06-df4d-4715-b142-276b6bce75ab",
Loadbalancers: []pools.LoadBalancerID{{ID: "79e05663-7f03-45d2-a092-8b94062f22ab"}},
Listeners: []pools.ListenerID{{ID: "2a280670-c202-4b0b-a562-34077415aabf"}},
Provider: "haproxy",
}
)
// HandlePoolListSuccessfully sets up the test server to respond to a pool List request.
func HandlePoolListSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/pools", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
w.Header().Add("Content-Type", "application/json")
r.ParseForm()
marker := r.Form.Get("marker")
switch marker {
case "":
fmt.Fprintf(w, PoolsListBody)
case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
fmt.Fprintf(w, `{ "pools": [] }`)
default:
t.Fatalf("/v2.0/lbaas/pools invoked with unexpected marker=[%s]", marker)
}
})
}
// HandlePoolCreationSuccessfully sets up the test server to respond to a pool creation request
// with a given response.
func HandlePoolCreationSuccessfully(t *testing.T, response string) {
th.Mux.HandleFunc("/v2.0/lbaas/pools", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestJSONRequest(t, r, `{
"pool": {
"lb_algorithm": "ROUND_ROBIN",
"protocol": "HTTP",
"name": "Example pool",
"project_id": "2ffc6e22aae24e4795f87155d24c896f",
"loadbalancer_id": "79e05663-7f03-45d2-a092-8b94062f22ab"
}
}`)
w.WriteHeader(http.StatusAccepted)
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, response)
})
}
// HandlePoolGetSuccessfully sets up the test server to respond to a pool Get request.
func HandlePoolGetSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/pools/c3741b06-df4d-4715-b142-276b6bce75ab", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestHeader(t, r, "Accept", "application/json")
fmt.Fprintf(w, SinglePoolBody)
})
}
// HandlePoolDeletionSuccessfully sets up the test server to respond to a pool deletion request.
func HandlePoolDeletionSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/pools/c3741b06-df4d-4715-b142-276b6bce75ab", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
w.WriteHeader(http.StatusNoContent)
})
}
// HandlePoolUpdateSuccessfully sets up the test server to respond to a pool Update request.
func HandlePoolUpdateSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/pools/c3741b06-df4d-4715-b142-276b6bce75ab", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestHeader(t, r, "Accept", "application/json")
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestJSONRequest(t, r, `{
"pool": {
"name": "NewPoolName",
"lb_algorithm": "LEAST_CONNECTIONS"
}
}`)
fmt.Fprintf(w, PostUpdatePoolBody)
})
}
// MembersListBody contains the canned body of a member list response.
const MembersListBody = `
{
"members":[
{
"id": "2a280670-c202-4b0b-a562-34077415aabf",
"address": "10.0.2.10",
"weight": 5,
"name": "web",
"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
"project_id": "2ffc6e22aae24e4795f87155d24c896f",
"admin_state_up":true,
"protocol_port": 80
},
{
"id": "fad389a3-9a4a-4762-a365-8c7038508b5d",
"address": "10.0.2.11",
"weight": 10,
"name": "db",
"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
"project_id": "2ffc6e22aae24e4795f87155d24c896f",
"admin_state_up":false,
"protocol_port": 80,
"provisioning_status": "ACTIVE",
"created_at": "2018-08-23T20:05:21",
"updated_at": "2018-08-23T21:22:53",
"operating_status": "ONLINE",
"backup": false,
"monitor_address": "192.168.1.111",
"monitor_port": 80
}
]
}
`
// SingleMemberBody is the canned body of a Get request on an existing member.
const SingleMemberBody = `
{
"member": {
"id": "fad389a3-9a4a-4762-a365-8c7038508b5d",
"address": "10.0.2.11",
"weight": 10,
"name": "db",
"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
"project_id": "2ffc6e22aae24e4795f87155d24c896f",
"admin_state_up":false,
"protocol_port": 80,
"provisioning_status": "ACTIVE",
"created_at": "2018-08-23T20:05:21",
"updated_at": "2018-08-23T21:22:53",
"operating_status": "ONLINE",
"backup": false,
"monitor_address": "192.168.1.111",
"monitor_port": 80
}
}
`
// PostUpdateMemberBody is the canned response body of a Update request on an existing member.
const PostUpdateMemberBody = `
{
"member": {
"id": "fad389a3-9a4a-4762-a365-8c7038508b5d",
"address": "10.0.2.11",
"weight": 10,
"name": "db",
"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
"project_id": "2ffc6e22aae24e4795f87155d24c896f",
"admin_state_up":false,
"protocol_port": 80
}
}
`
var (
MemberWeb = pools.Member{
SubnetID: "1981f108-3c48-48d2-b908-30f7d28532c9",
ProjectID: "2ffc6e22aae24e4795f87155d24c896f",
AdminStateUp: true,
Name: "web",
ID: "2a280670-c202-4b0b-a562-34077415aabf",
Address: "10.0.2.10",
Weight: 5,
ProtocolPort: 80,
}
MemberDb = pools.Member{
SubnetID: "1981f108-3c48-48d2-b908-30f7d28532c9",
ProjectID: "2ffc6e22aae24e4795f87155d24c896f",
AdminStateUp: false,
Name: "db",
ID: "fad389a3-9a4a-4762-a365-8c7038508b5d",
Address: "10.0.2.11",
Weight: 10,
ProtocolPort: 80,
ProvisioningStatus: "ACTIVE",
CreatedAt: time.Date(2018, 8, 23, 20, 05, 21, 0, time.UTC),
UpdatedAt: time.Date(2018, 8, 23, 21, 22, 53, 0, time.UTC),
OperatingStatus: "ONLINE",
Backup: false,
MonitorAddress: "192.168.1.111",
MonitorPort: 80,
}
MemberUpdated = pools.Member{
SubnetID: "1981f108-3c48-48d2-b908-30f7d28532c9",
ProjectID: "2ffc6e22aae24e4795f87155d24c896f",
AdminStateUp: false,
Name: "db",
ID: "fad389a3-9a4a-4762-a365-8c7038508b5d",
Address: "10.0.2.11",
Weight: 10,
ProtocolPort: 80,
}
)
// HandleMemberListSuccessfully sets up the test server to respond to a member List request.
func HandleMemberListSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
w.Header().Add("Content-Type", "application/json")
r.ParseForm()
marker := r.Form.Get("marker")
switch marker {
case "":
fmt.Fprintf(w, MembersListBody)
case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
fmt.Fprintf(w, `{ "members": [] }`)
default:
t.Fatalf("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members invoked with unexpected marker=[%s]", marker)
}
})
}
// HandleMemberCreationSuccessfully sets up the test server to respond to a member creation request
// with a given response.
func HandleMemberCreationSuccessfully(t *testing.T, response string) {
th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestJSONRequest(t, r, `{
"member": {
"address": "10.0.2.11",
"weight": 10,
"name": "db",
"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
"project_id": "2ffc6e22aae24e4795f87155d24c896f",
"protocol_port": 80
}
}`)
w.WriteHeader(http.StatusAccepted)
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, response)
})
}
// HandleMemberGetSuccessfully sets up the test server to respond to a member Get request.
func HandleMemberGetSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members/2a280670-c202-4b0b-a562-34077415aabf", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestHeader(t, r, "Accept", "application/json")
fmt.Fprintf(w, SingleMemberBody)
})
}
// HandleMemberDeletionSuccessfully sets up the test server to respond to a member deletion request.
func HandleMemberDeletionSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members/2a280670-c202-4b0b-a562-34077415aabf", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
w.WriteHeader(http.StatusNoContent)
})
}
// HandleMemberUpdateSuccessfully sets up the test server to respond to a member Update request.
func HandleMemberUpdateSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members/2a280670-c202-4b0b-a562-34077415aabf", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestHeader(t, r, "Accept", "application/json")
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestJSONRequest(t, r, `{
"member": {
"name": "newMemberName",
"weight": 4
}
}`)
fmt.Fprintf(w, PostUpdateMemberBody)
})
}
// HandleMembersUpdateSuccessfully sets up the test server to respond to a batch member Update request.
func HandleMembersUpdateSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestHeader(t, r, "Accept", "application/json")
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestJSONRequest(t, r, `{
"members": [
{
"name": "web-server-1",
"weight": 20,
"subnet_id": "bbb35f84-35cc-4b2f-84c2-a6a29bba68aa",
"address": "192.0.2.16",
"protocol_port": 80
},
{
"name": "web-server-2",
"weight": 10,
"subnet_id": "bbb35f84-35cc-4b2f-84c2-a6a29bba68aa",
"address": "192.0.2.17",
"protocol_port": 80
}
]
}`)
w.WriteHeader(http.StatusAccepted)
})
}
// HandleEmptyMembersUpdateSuccessfully sets up the test server to respond to an empty batch member Update request.
func HandleEmptyMembersUpdateSuccessfully(t *testing.T) {
th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
th.TestHeader(t, r, "Accept", "application/json")
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestJSONRequest(t, r, `{
"members": []
}`)
w.WriteHeader(http.StatusAccepted)
})
}