-
Notifications
You must be signed in to change notification settings - Fork 5
/
baremetal_server_test.go
643 lines (543 loc) · 17.3 KB
/
baremetal_server_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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package oneandone
import (
"fmt"
"math/rand"
"strconv"
"strings"
"sync"
"testing"
"time"
)
var (
set_baremetal_server sync.Once
baremetal_server_id string
baremetal_server_name string
baremetal_ser_app_id string
baremetal_server *Server
baremetal_ser_lb *LoadBalancer
baremetalModel BaremetalModel
)
func setup_baremetal_server() {
fmt.Println("Deploying a baremetal test server...")
b_srv_id, b_srv, err := create_baremetal_test_server(false)
if err != nil {
fmt.Printf("Unable to create the baremetal server. Error: %s", err.Error())
return
}
if b_srv_id == "" || b_srv.Id == "" {
fmt.Printf("Unable to create the baremetal server.")
return
} else {
baremetal_server_id = b_srv.Id
}
err = api.WaitForState(b_srv, "POWERED_OFF", 20, 90)
if err != nil {
fmt.Printf("Error: %s", err.Error())
}
baremetal_server = b_srv
}
func get_baremetal_appliance(max_disk_size int) ServerAppliance {
saps, _ := api.ListServerAppliances(1, 500, "", "baremetal", "")
for i, sp := range saps {
if strings.Contains(strings.ToLower(sp.Os), "centos") {
return saps[i]
}
}
return saps[0]
}
func create_baremetal_test_server(power_on bool) (string, *Server, error) {
rand.Seed(time.Now().UnixNano())
baremetal_server_name = fmt.Sprintf("GOBAREMETAL_%d", rand.Intn(1000000))
fmt.Printf("Creating test server '%s'...\n", baremetal_server_name)
sap := get_baremetal_appliance(hdd_size)
baremetal_ser_app_id = sap.Id
//mp := get_default_mon_policy()
baremetalModels, err := api.ListBaremetalModels()
baremetalModel = baremetalModels[0]
baremetalModelId := baremetalModels[0].Id
req := ServerRequest{
Name: baremetal_server_name,
Description: baremetal_server_name + " description",
ApplianceId: baremetal_ser_app_id,
DatacenterId: get_random_datacenterID(),
PowerOn: power_on,
ServerType: "baremetal",
Hardware: Hardware{
BaremetalModelId: baremetalModelId,
},
}
b_ser_id, b_server, err := api.CreateServer(&req)
return b_ser_id, b_server, err
}
// /servers tests
func TestCreateBaremetalServer(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
if baremetal_server == nil {
t.Errorf("CreateServer failed.")
return
}
if baremetal_server.Name != baremetal_server_name {
t.Errorf("Wrong server name.")
}
if baremetal_server.Image.Id != baremetal_ser_app_id {
t.Errorf("Wrong server image on server '%s'.", baremetal_server.Name)
}
}
func TestListAllServers(t *testing.T) {
//set_baremetal_server.Do(setup_baremetal_server)
fmt.Println("Listing all servers...")
res, err := api.ListServers()
if err != nil {
t.Errorf("ListServers failed. Error: " + err.Error())
}
if len(res) == 0 {
t.Errorf("No server found.")
}
res, err = api.ListServers(1, 2, "name", "", "id,name")
if err != nil {
t.Errorf("ListServers with parameter options failed. Error: " + err.Error())
return
}
if len(res) == 0 {
t.Errorf("No server found.")
}
if len(res) > 2 {
t.Errorf("Wrong number of objects per page.")
}
if res[0].Hardware != nil {
t.Errorf("Filtering parameters failed.")
}
if res[0].Name == "" {
t.Errorf("Filtering parameters failed.")
}
if len(res) == 2 && res[0].Name >= res[1].Name {
t.Errorf("Sorting parameters failed.")
}
// Test for error response
res, err = api.ListServers(0, 0, true, "name", "")
if res != nil || err == nil {
t.Errorf("ListServers failed to handle incorrect argument type.")
}
res, err = api.ListServers(0, 0, "", baremetal_server_name, "")
if err != nil {
t.Errorf("ListServers with parameter options failed. Error: " + err.Error())
return
}
if len(res) != 1 {
t.Errorf("Search parameter failed.")
}
if res[0].Name != baremetal_server_name {
t.Errorf("Search parameter failed.")
}
}
func TestGetBaremetalServer(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
fmt.Println("Getting the server...")
b_srv, err := api.GetServer(baremetal_server_id)
if err != nil {
t.Errorf("GetServer failed. Error: " + err.Error())
return
}
if b_srv.Id != baremetal_server_id {
t.Errorf("Wrong server ID.")
}
}
func TestGetBaremetalServerStatus(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
fmt.Println("Getting the server's status...")
status, err := api.GetServerStatus(baremetal_server_id)
if err != nil {
t.Errorf("GetServerStatus failed. Error: " + err.Error())
return
}
if status.State != "POWERED_OFF" {
t.Errorf("Wrong server status.")
}
}
func TestStartBaremetalServer(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
fmt.Println("Starting the server...")
b_srv, err := api.StartServer(baremetal_server_id)
if err != nil {
t.Errorf("StartServer failed. Error: " + err.Error())
return
}
err = api.WaitForState(b_srv, "POWERED_ON", 10, 60)
if err != nil {
t.Errorf("Starting the server failed. Error: " + err.Error())
}
server, err = api.GetServer(b_srv.Id)
if err != nil {
t.Errorf("GetServer failed. Error: " + err.Error())
} else if server.Status.State != "POWERED_ON" {
t.Errorf("Wrong server state. Expected: POWERED_ON. Found: %s.", server.Status.State)
}
}
func TestRebootBaremetalServer(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
for i := 1; i < 3; i++ {
is_hardware := i%2 == 0
var method string
if is_hardware {
method = "HARDWARE"
} else {
method = "SOFTWARE"
}
fmt.Printf("Rebooting the server using '%s' method...\n", method)
b_srv, err := api.RebootServer(baremetal_server_id, is_hardware)
if err != nil {
t.Errorf("RebootServer using '%s' method failed. Error: %s", method, err.Error())
return
}
err = api.WaitForState(b_srv, "REBOOTING", 5, 60)
if err != nil {
t.Errorf("Rebooting the server using '%s' method failed. Error: %s", method, err.Error())
}
err = api.WaitForState(b_srv, "POWERED_ON", 10, 60)
if err != nil {
t.Errorf("Rebooting the server using '%s' method failed. Error: %s", method, err.Error())
}
}
}
func TestRenameBaremetalServer(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
b_srvs, err := api.GetServer(baremetal_server_id)
if err != nil {
t.Errorf("get server failed. Error: " + err.Error())
}
api.WaitForState(b_srvs, "POWERED_ON", 10, 60)
fmt.Println("Renaming the server...")
new_name := server.Name + "_renamed"
new_desc := server.Description + "_renamed"
b_srv, err := api.RenameServer(baremetal_server_id, new_name, new_desc)
if err != nil {
t.Errorf("Renaming server failed. Error: " + err.Error())
return
}
if b_srv.Name != new_name {
t.Errorf("Wrong server name.")
}
if b_srv.Description != new_desc {
t.Errorf("Wrong server description.")
}
}
func TestGetBaremetalServerHardware(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
fmt.Println("Getting the server's hardware...")
hardware, err := api.GetServerHardware(baremetal_server_id)
baremetalModel, err := api.GetBaremetalModel("81504C620D98BCEBAA5202D145203B4B")
if err != nil {
t.Errorf("GetServerHardware failed. Error: " + err.Error())
return
}
if hardware == nil {
t.Errorf("Unable to get the server's hardware.")
} else {
if hardware.Vcores != baremetalModel.Hardware.CoresPerProcessor {
t.Errorf("Wrong number of processor cores on server '%s'.", server.Name)
}
if hardware.CoresPerProcessor != baremetalModel.Hardware.Cores {
t.Errorf("Wrong number of cores per processor on server '%s'.", server.Name)
}
if hardware.Ram != baremetalModel.Hardware.Ram {
t.Errorf("Wrong RAM size on server '%s'.", server.Name)
}
}
}
func TestListBaremetalServerHdds(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
fmt.Println("Listing all the server's HDDs...")
hdds, err := api.ListServerHdds(baremetal_server_id)
if err != nil {
t.Errorf("ListServerHdds failed. Error: " + err.Error())
return
}
if len(hdds) != 1 {
t.Errorf("Wrong number of the server's hard disks.")
}
if hdds[0].Id == "" {
t.Errorf("Wrong HDD id.")
}
if hdds[0].Size != 1600 {
t.Errorf("Wrong HDD size.")
}
if hdds[0].IsMain != true {
t.Errorf("Wrong main HDD.")
}
server_hdd = &hdds[0]
}
func TestGetBaremetalServerHdd(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
hdds, _ := api.ListServerHdds(baremetal_server_id)
fmt.Println("Getting server HDD...")
hdd, err := api.GetServerHdd(baremetal_server_id, hdds[0].Id)
if err != nil {
t.Errorf("GetServerHdd failed. Error: " + err.Error())
return
}
if hdd.Id != hdds[0].Id {
t.Errorf("Wrong HDD id.")
}
if hdd.Size != hdds[0].Size {
t.Errorf("Wrong HDD size.")
}
if hdd.IsMain != hdds[0].IsMain {
t.Errorf("Wrong main HDD.")
}
}
func TestGetBaremetalServerImage(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
fmt.Println("Getting the server's image...")
img, err := api.GetServerImage(baremetal_server_id)
if err != nil {
t.Errorf("GetServerImage failed. Error: " + err.Error())
return
}
if img.Id != baremetal_server.Image.Id {
t.Errorf("Wrong image ID.")
}
if img.Name != baremetal_server.Image.Name {
t.Errorf("Wrong image name.")
}
}
func TestStopBaremetalServer(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
fmt.Println("Stopping the server...")
b_srv, err := api.ShutdownServer(baremetal_server_id, false)
if err != nil {
t.Errorf("ShutdownServer failed. Error: " + err.Error())
} else {
err = api.WaitForState(b_srv, "POWERED_OFF", 10, 60)
if err != nil {
t.Errorf("Stopping the server failed. Error: " + err.Error())
}
baremetal_server, err = api.GetServer(baremetal_server_id)
if err != nil {
t.Errorf("GetServer failed. Error: " + err.Error())
}
if baremetal_server.Status.State != "POWERED_OFF" {
t.Errorf("Wrong baremetal_server state. Expected: POWERED_OFF. Found: %s.", baremetal_server.Status.State)
}
}
}
func TestListBaremetalServerIps(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
b_srv, e := api.GetServer(baremetal_server_id)
if e == nil {
baremetal_server = b_srv
}
fmt.Println("Listing the baremetal_server's IPs...")
ips, err := api.ListServerIps(baremetal_server_id)
if err != nil {
t.Errorf("ListServerIps failed. Error: " + err.Error())
return
}
if len(ips) != len(baremetal_server.Ips) {
t.Errorf("Not all IPs were obtained.")
}
if ips[0].Id != baremetal_server.Ips[0].Id {
t.Errorf("Wrong IP ID.")
}
if ips[0].Ip != baremetal_server.Ips[0].Ip {
t.Errorf("Wrong IP address.")
}
}
func TestAssignBaremetalServerIp(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
fmt.Println("Assigning new IP addresses to the baremetal_server...")
for i := 2; i < 4; i++ {
time.Sleep(time.Second)
b_srv, err := api.AssignServerIp(baremetal_server_id, "IPV4")
if err != nil {
t.Errorf("AssignServerIp failed. Error: " + err.Error())
return
}
b_srv = wait_for_action_done(b_srv, 10, 30)
if len(b_srv.Ips) != i {
t.Errorf("IP address not assigned to the baremetal_server.")
}
baremetal_server = b_srv
}
}
func TestGetBaremetalServerIp(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
fmt.Println("Getting the baremetal_server's IP...")
baremetal_server, _ = api.GetServer(baremetal_server_id)
if baremetal_server == nil {
t.Errorf("GetServer failed.")
return
}
time.Sleep(time.Second)
ip, err := api.GetServerIp(baremetal_server_id, baremetal_server.Ips[0].Id)
if err != nil {
t.Errorf("GetServerIps failed. Error: " + err.Error())
return
}
if ip.Id != baremetal_server.Ips[0].Id {
t.Errorf("Wrong IP ID.")
}
if ip.Ip != baremetal_server.Ips[0].Ip {
t.Errorf("Wrong IP address.")
}
}
func TestDeleteBaremetalServerIp(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
if len(baremetal_server.Ips) <= 1 {
for i := 0; i < 2; i++ {
time.Sleep(10 * time.Second)
s, e := api.AssignServerIp(baremetal_server_id, "IPV4")
if s != nil && e == nil {
s = wait_for_action_done(s, 10, 30)
baremetal_server = s
time.Sleep(120 * time.Second)
}
}
}
ip_no := len(baremetal_server.Ips)
for i := 1; i < ip_no-1; i++ {
keep_ip := i%2 == 0
fmt.Printf("Deleting the baremetal_server's IP '%s' (keep_ip = %s)...\n", baremetal_server.Ips[i].Ip, strconv.FormatBool(keep_ip))
api.DeleteServerIp(baremetal_server_id, baremetal_server.Ips[i].Id, keep_ip)
time.Sleep(280 * time.Second)
}
}
func TestAssignBaremetalServerIpLoadBalancer(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
ips, _ := api.ListServerIps(baremetal_server_id)
lb := create_load_balancer()
fmt.Printf("Assigning a load balancer to the baremetal_server's IP '%s'...\n", ips[0].Ip)
b_srv, err := api.AssignServerIpLoadBalancer(baremetal_server_id, ips[0].Id, lb.Id)
if err != nil {
t.Errorf("AssignServerIpLoadBalancer failed. Error: " + err.Error())
return
}
if len(b_srv.Ips[0].LoadBalancers) == 0 {
t.Errorf("Load balancer not assigned.")
}
if b_srv.Ips[0].LoadBalancers[0].Id != lb.Id {
t.Errorf("Wrong load balancer assigned.")
}
baremetal_ser_lb = lb
}
func TestListBaremetalServerIpLoadBalancers(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
ips, _ := api.ListServerIps(baremetal_server_id)
fmt.Println("Listing load balancers assigned to the baremetal_server's IP...")
lbs, err := api.ListServerIpLoadBalancers(baremetal_server_id, ips[0].Id)
if err != nil {
t.Errorf("ListServerIpLoadBalancers failed. Error: " + err.Error())
return
}
if len(lbs) == 0 {
t.Errorf("No load balancer was assigned to the baremetal_server's IP.")
return
}
if lbs[0].Id != baremetal_ser_lb.Id {
t.Errorf("Wrong load balancer assigned.")
}
}
func TestUnassignBaremetalServerIpLoadBalancer(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
ips, _ := api.ListServerIps(baremetal_server_id)
fmt.Println("Unassigning the load balancer from the baremetal_server's IP...")
b_srv, err := api.UnassignServerIpLoadBalancer(baremetal_server_id, ips[0].Id, baremetal_ser_lb.Id)
if err != nil {
t.Errorf("UnassignServerIpLoadBalancer failed. Error: " + err.Error())
return
}
if len(b_srv.Ips[0].LoadBalancers) > 0 {
t.Errorf("Unassigning the load balancer failed.")
}
baremetal_ser_lb, err = api.DeleteLoadBalancer(baremetal_ser_lb.Id)
if err == nil {
api.WaitUntilDeleted(baremetal_ser_lb)
}
baremetal_ser_lb, _ = api.GetLoadBalancer(baremetal_ser_lb.Id)
}
func TestAssignBaremetalServerIpFirewallPolicy(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
ips, _ := api.ListServerIps(baremetal_server_id)
fmt.Println("Assigning a firewall policy to the baremetal_server's IP...")
fps, err := api.ListFirewallPolicies(0, 1, "creation_date", "linux", "id,name")
if err != nil {
t.Errorf("ListFirewallPolicies failed. Error: " + err.Error())
return
}
b_srv, err := api.AssignServerIpFirewallPolicy(baremetal_server_id, ips[0].Id, fps[0].Id)
if err != nil {
t.Errorf("AssignServerIpFirewallPolicy failed. Error: " + err.Error())
return
}
if b_srv.Ips[0].Firewall == nil {
t.Errorf("Firewall policy not assigned.")
}
if b_srv.Ips[0].Firewall.Id != fps[0].Id {
t.Errorf("Wrong firewall policy assigned.")
}
}
func TestGetBaremetalServerIpFirewallPolicy(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
ips, _ := api.ListServerIps(baremetal_server_id)
fmt.Println("Getting the firewall policy assigned to the baremetal_server's IP...")
fps, err := api.ListFirewallPolicies(0, 1, "creation_date", "linux", "id,name")
if err != nil {
t.Errorf("ListFirewallPolicies failed. Error: " + err.Error())
}
fp, err := api.GetServerIpFirewallPolicy(baremetal_server_id, ips[0].Id)
if err != nil {
t.Errorf("GetServerIpFirewallPolicy failed. Error: " + err.Error())
}
if fp == nil {
t.Errorf("No firewall policy assigned to the baremetal_server's IP.")
}
if fp.Id != fps[0].Id {
t.Errorf("Wrong firewall policy assigned to the baremetal_server's IP.")
}
}
func TestRecoveryRebootServer(t *testing.T) {
set_server.Do(setup_server)
fmt.Print("Rebooting the server using recovery boot")
srv, err := api.RecoveryRebootServer(baremetal_server_id, false, get_random_recoveryImage().Id)
if err != nil {
t.Errorf("Rebooting the server using recovery boot. Error: %s", err.Error())
return
}
err = api.WaitForState(srv, "REBOOTING", 10, 60)
if err != nil {
t.Errorf("Rebooting the server using recovery boot. Error: %s", err.Error())
}
err = api.WaitForState(srv, "POWERED_ON", 20, 60)
if err != nil {
t.Errorf("Rebooting the server using recovery boot. Error: %s", err.Error())
}
}
func TestDeleteBaremetalServer(t *testing.T) {
set_baremetal_server.Do(setup_baremetal_server)
time.Sleep(120 * time.Second)
b_srv, err := api.DeleteServer(baremetal_server_id, true)
time.Sleep(420 * time.Second)
if err != nil {
t.Errorf("DeleteServer server failed. Error: " + err.Error())
return
}
fmt.Printf("Deleting baremetal_server '%s', keeping baremetal_server's IP '%s'...\n", b_srv.Name, b_srv.Ips[0].Ip)
ip_id := b_srv.Ips[0].Id
b_srv, err = api.GetServer(baremetal_server_id)
if b_srv != nil {
t.Errorf("Unable to delete the baremetal_server.")
} else {
baremetal_server = nil
}
ip, _ := api.GetPublicIp(ip_id)
if ip == nil {
t.Errorf("Failed to keep IP after deleting the baremetal_server.")
} else {
fmt.Printf("Deleting baremetal_server's IP '%s' after deleting the baremetal_server...\n", ip.IpAddress)
ip, err = api.DeletePublicIp(ip_id)
if err != nil {
t.Errorf("Unable to delete baremetal_server's IP after deleting the baremetal_server.")
} else {
api.WaitUntilDeleted(ip)
}
}
}