-
Notifications
You must be signed in to change notification settings - Fork 5
/
loadbalancers_test.go
414 lines (356 loc) · 10.9 KB
/
loadbalancers_test.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
package oneandone
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
)
var (
set_lb sync.Once
test_lb_name string
test_lb_desc string
test_lb *LoadBalancer
)
const (
lb_round_robin = "ROUND_ROBIN"
lb_least_con = "LEAST_CONNECTIONS"
)
// Helper functions
func create_load_balancer() *LoadBalancer {
rand.Seed(time.Now().UnixNano())
rint := rand.Intn(999)
test_lb_name = fmt.Sprintf("LoadBalancer_%d", rint)
test_lb_desc = fmt.Sprintf("LoadBalancer_%d description", rint)
req := LoadBalancerRequest{
Name: test_lb_name,
Description: test_lb_desc,
Method: lb_round_robin,
Persistence: Bool2Pointer(true),
PersistenceTime: Int2Pointer(60),
HealthCheckTest: "TCP",
HealthCheckInterval: Int2Pointer(300),
Rules: []LoadBalancerRule{
{
Protocol: "TCP",
PortBalancer: 8080,
PortServer: 8089,
Source: "0.0.0.0",
},
},
}
fmt.Printf("Creating new load balancer '%s'...\n", test_lb_name)
lb_id, lb, err := api.CreateLoadBalancer(&req)
if err != nil {
fmt.Printf("Unable to create a load balancer. Error: %s", err.Error())
return nil
}
if lb_id == "" || lb.Id == "" {
fmt.Printf("Unable to create load balancer '%s'.", test_lb_name)
return nil
}
api.WaitForState(lb, "ACTIVE", 10, 30)
return lb
}
func set_load_balancer() {
test_lb = create_load_balancer()
}
// /load_balancers tests
func TestCreateLoadBalancer(t *testing.T) {
set_lb.Do(set_load_balancer)
if test_lb == nil {
t.Errorf("CreateLoadBalancer failed.")
}
if test_lb.Id == "" {
t.Errorf("Missing load balancer ID.")
}
if test_lb.Name != test_lb_name {
t.Errorf("Wrong name of the load balancer.")
}
if test_lb.Description != test_lb_desc {
t.Errorf("Wrong load balancer description.")
}
if !test_lb.Persistence {
t.Errorf("Wrong load balancer persistence.")
}
if test_lb.PersistenceTime != 60 {
t.Errorf("Wrong persistence time for load balancer '%s'.", test_lb.Name)
}
if test_lb.HealthCheckInterval != 300 {
t.Errorf("Wrong health check interval for load balancer '%s'.", test_lb.Name)
}
if test_lb.HealthCheckTest != "TCP" {
t.Errorf("Wrong health check test for load balancer '%s'.", test_lb.Name)
}
if test_lb.Method != lb_round_robin {
t.Errorf("Wrong method for load balancer '%s'.", test_lb.Name)
}
if len(test_lb.Rules) != 1 {
t.Errorf("Wrong number of load balancer '%s' rules.", test_lb.Name)
} else {
if test_lb.Rules[0].Protocol != "TCP" {
t.Errorf("Wrong protocol of load balancer '%s' rule.", test_lb.Name)
}
if test_lb.Rules[0].PortBalancer != 8080 {
t.Errorf("Wrong rule balancer port of load balancer '%s'.", test_lb.Name)
}
if test_lb.Rules[0].PortServer != 8089 {
t.Errorf("Wrong rule server port of load balancer '%s'.", test_lb.Name)
}
if test_lb.Rules[0].Source != "0.0.0.0" {
t.Errorf("Wrong source in load balancer '%s' rule.", test_lb.Name)
}
}
}
func TestGetLoadBalancer(t *testing.T) {
set_lb.Do(set_load_balancer)
fmt.Printf("Getting load balancer '%s'...\n", test_lb.Name)
lb, err := api.GetLoadBalancer(test_lb.Id)
if err != nil {
t.Errorf("GetLoadBalancer failed. Error: " + err.Error())
}
if lb.Id != test_lb.Id {
t.Errorf("Wrong load balancer ID.")
}
}
func TestListLoadBalancers(t *testing.T) {
set_lb.Do(set_load_balancer)
fmt.Println("Listing all load balancers...")
res, err := api.ListLoadBalancers()
if err != nil {
t.Errorf("ListLoadBalancers failed. Error: " + err.Error())
}
if len(res) == 0 {
t.Errorf("No load balancer found.")
}
res, err = api.ListLoadBalancers(1, 1, "", "", "id,name")
if err != nil {
t.Errorf("ListLoadBalancers with parameter options failed. Error: " + err.Error())
}
if len(res) == 0 {
t.Errorf("No load balancer found.")
}
if len(res) > 1 {
t.Errorf("Wrong number of objects per page.")
}
if res[0].Id == "" {
t.Errorf("Filtering parameters failed.")
}
if res[0].Name == "" {
t.Errorf("Filtering parameters failed.")
}
if res[0].State != "" {
t.Errorf("Filtering parameters failed.")
}
// Test for error response
res, err = api.ListLoadBalancers(0, "name")
if res != nil || err == nil {
t.Errorf("ListLoadBalancers failed to handle incorrect argument type.")
}
res, err = api.ListLoadBalancers(0, 0, "", test_lb.Name, "")
if err != nil {
t.Errorf("ListLoadBalancers with parameter options failed. Error: " + err.Error())
}
if len(res) != 1 {
t.Errorf("Search parameter failed.")
}
if res[0].Name != test_lb.Name {
t.Errorf("Search parameter failed.")
}
}
func TestAddLoadBalancerServerIps(t *testing.T) {
set_lb.Do(set_load_balancer)
sync_server.Do(func() { deploy_test_server(false) })
fmt.Printf("Assigning server IPs to load balancer '%s'...\n", test_lb.Name)
test_server, _ = api.GetServer(test_server.Id)
ips := []string{test_server.Ips[0].Id}
lb, err := api.AddLoadBalancerServerIps(test_lb.Id, ips)
if err != nil {
t.Errorf("AddLoadBalancerServerIps failed. Error: " + err.Error())
}
api.WaitForState(lb, "ACTIVE", 10, 60)
lb, _ = api.GetLoadBalancer(lb.Id)
if len(lb.ServerIps) != 1 {
t.Errorf("Found no server IP attached to the load balancer.")
}
if lb.ServerIps[0].Id != test_server.Ips[0].Id {
t.Errorf("Wrong server IP attached to the load balancer.")
}
test_lb = lb
}
func TestGetLoadBalancerServerIp(t *testing.T) {
set_lb.Do(set_load_balancer)
fmt.Printf("Getting server IPs of load balancer '%s'...\n", test_lb.Name)
lb_ser_ip, err := api.GetLoadBalancerServerIp(test_lb.Id, test_lb.ServerIps[0].Id)
if err != nil {
t.Errorf("GetLoadBalancerServerIp failed. Error: " + err.Error())
}
if lb_ser_ip.Id != test_lb.ServerIps[0].Id {
t.Errorf("Wrong ID of the server IP attached to load balancer '%s'.", test_lb.Name)
}
if lb_ser_ip.Ip != test_lb.ServerIps[0].Ip {
t.Errorf("Wrong server IP address attached to load balancer '%s'.", test_lb.Name)
}
}
func TestListLoadBalancerServerIps(t *testing.T) {
set_lb.Do(set_load_balancer)
sync_server.Do(func() { deploy_test_server(false) })
fmt.Printf("Listing server IPs of load balancer '%s'...\n", test_lb.Name)
lb_ips, err := api.ListLoadBalancerServerIps(test_lb.Id)
if err != nil {
t.Errorf("ListLoadBalancerServerIps failed. Error: " + err.Error())
}
if len(lb_ips) != 1 {
t.Errorf("Wrong number of server IPs added to load balancer '%s'.", test_lb.Name)
}
if lb_ips[0].Id != test_server.Ips[0].Id {
t.Errorf("Wrong server IP added to load balancer '%s'.", test_lb.Name)
}
}
func TestDeleteLoadBalancerServerIp(t *testing.T) {
set_lb.Do(set_load_balancer)
sync_server.Do(func() { deploy_test_server(false) })
fmt.Printf("Deleting server IP from load balancer '%s'...\n", test_lb.Name)
lb, err := api.DeleteLoadBalancerServerIp(test_lb.Id, test_server.Ips[0].Id)
if err != nil {
t.Errorf("DeleteLoadBalancerServerIp failed. Error: " + err.Error())
}
api.WaitForState(lb, "ACTIVE", 10, 60)
lb, err = api.GetLoadBalancer(lb.Id)
if err != nil {
t.Errorf("Deleting server IP from the load balancer failed.")
}
if len(lb.ServerIps) > 0 {
t.Errorf("IP not deleted from the load balancer.")
}
}
func TestGetLoadBalancerRule(t *testing.T) {
set_lb.Do(set_load_balancer)
fmt.Printf("Getting the rule of load balancer '%s'...\n", test_lb.Name)
lb_rule, err := api.GetLoadBalancerRule(test_lb.Id, test_lb.Rules[0].Id)
if err != nil {
t.Errorf("GetLoadBalancerRule failed. Error: " + err.Error())
}
if lb_rule.Id != test_lb.Rules[0].Id {
t.Errorf("Wrong rule ID.")
}
if lb_rule.PortBalancer != test_lb.Rules[0].PortBalancer {
t.Errorf("Wrong rule port_balancer field.")
}
if lb_rule.PortServer != test_lb.Rules[0].PortServer {
t.Errorf("Wrong rule port_server field.")
}
if lb_rule.Protocol != test_lb.Rules[0].Protocol {
t.Errorf("Wrong rule protocol.")
}
if lb_rule.Source != test_lb.Rules[0].Source {
t.Errorf("Wrong rule source IP.")
}
}
func TestAddLoadBalancerRules(t *testing.T) {
set_lb.Do(set_load_balancer)
fmt.Printf("Adding rules to load balancer '%s'...\n", test_lb.Name)
rules := []LoadBalancerRule{
{
Protocol: "TCP",
PortBalancer: 35367,
PortServer: 35367,
Source: "0.0.0.0",
},
{
Protocol: "TCP",
PortBalancer: 815,
PortServer: 815,
},
}
lb, err := api.AddLoadBalancerRules(test_lb.Id, rules)
if err != nil {
t.Errorf("AddLoadBalancerRules failed. Error: " + err.Error())
} else {
api.WaitForState(lb, "ACTIVE", 10, 60)
}
lb, _ = api.GetLoadBalancer(lb.Id)
if len(lb.Rules) != 3 {
t.Errorf("Unable to add rules to load balancer '%s'.\n", test_lb.Name)
}
}
func TestListLoadBalancerRules(t *testing.T) {
set_lb.Do(set_load_balancer)
fmt.Printf("Listing load balancer '%s' rules...\n", test_lb.Name)
lb_rules, err := api.ListLoadBalancerRules(test_lb.Id)
if err != nil {
t.Errorf("ListLoadBalancerRules failed. Error: " + err.Error())
}
if len(lb_rules) != 3 {
t.Errorf("Wrong number of rules found at load balancer '%s'.", test_lb.Name)
}
}
func TestDeleteLoadBalancerRule(t *testing.T) {
set_lb.Do(set_load_balancer)
lbr, _ := api.ListLoadBalancerRules(test_lb.Id)
fmt.Printf("Deleting rule '%s' from load balancer '%s'...\n", lbr[0].Id, test_lb.Name)
lb, err := api.DeleteLoadBalancerRule(test_lb.Id, lbr[0].Id)
if err != nil {
t.Errorf("DeleteLoadBalancerRule failed. Error: " + err.Error())
}
api.WaitForState(lb, "ACTIVE", 10, 60)
lb, err = api.GetLoadBalancer(lb.Id)
if err != nil {
t.Errorf("Deleting rule from the load balancer failed.")
}
if len(lb.Rules) != 2 {
t.Errorf("Rule not deleted from the load balancer.")
}
for _, rule := range lb.Rules {
if rule.Id == lbr[0].Id {
t.Errorf("Rule not deleted from the load balancer.")
}
}
}
func TestUpdateLoadBalancer(t *testing.T) {
set_lb.Do(set_load_balancer)
fmt.Printf("Updating load balancer '%s'...\n", test_lb.Name)
new_name := test_lb.Name + "_updated"
new_desc := test_lb.Description + "_updated"
lbu := LoadBalancerRequest{
Name: new_name,
Description: new_desc,
Method: lb_least_con,
Persistence: Bool2Pointer(false),
}
lb, err := api.UpdateLoadBalancer(test_lb.Id, &lbu)
if err != nil {
t.Errorf("UpdateLoadBalancer failed. Error: " + err.Error())
} else {
api.WaitForState(lb, "ACTIVE", 10, 30)
}
lb, _ = api.GetLoadBalancer(lb.Id)
if lb.Name != new_name {
t.Errorf("Failed to update load balancer name.")
}
if lb.Description != new_desc {
t.Errorf("Failed to update load balancer description.")
}
if lb.Method != lb_least_con {
t.Errorf("Failed to update load balancer method.")
}
if lb.Persistence {
t.Errorf("Failed to update load balancer persistence.")
}
}
func TestDeleteLoadBalancer(t *testing.T) {
set_lb.Do(set_load_balancer)
fmt.Printf("Deleting load balancer '%s'...\n", test_lb.Name)
lb, err := api.DeleteLoadBalancer(test_lb.Id)
if err != nil {
t.Errorf("DeleteLoadBalancer failed. Error: " + err.Error())
} else {
api.WaitUntilDeleted(lb)
}
lb, err = api.GetLoadBalancer(lb.Id)
if lb != nil {
t.Errorf("Unable to delete the load balancer.")
} else {
test_lb = nil
}
}