-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathcustom_server_create_test.go
More file actions
946 lines (895 loc) · 28.2 KB
/
custom_server_create_test.go
File metadata and controls
946 lines (895 loc) · 28.2 KB
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
package instance_test
import (
"fmt"
"testing"
"github.com/scaleway/scaleway-cli/v2/core"
block "github.com/scaleway/scaleway-cli/v2/internal/namespaces/block/v1alpha1"
file "github.com/scaleway/scaleway-cli/v2/internal/namespaces/file/v1alpha1"
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/instance/v1"
"github.com/scaleway/scaleway-cli/v2/internal/testhelpers"
blockSDK "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
instanceSDK "github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// deleteServerAfterFunc deletes the created server and its attached volumes and IPs.
func deleteServerAfterFunc() core.AfterFunc {
return core.ExecAfterCmd(
"scw instance server delete {{ .CmdResult.ID }} with-volumes=all with-ip=true force-shutdown=true",
)
}
// All test below should succeed to create an instance.
func Test_CreateServer(t *testing.T) {
////
// Simple use cases
////
t.Run("Simple", func(t *testing.T) {
t.Run("Default", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=ubuntu_jammy stopped=true"),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
server := ctx.Result.(*instance.ServerWithWarningsResponse).Server
assert.NotNil(t, ctx.Result)
assert.Equal(
t,
"Ubuntu 22.04 Jammy Jellyfish",
server.Image.Name,
)
},
core.TestCheckExitCode(0),
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("GP1-XS", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: "scw instance server create type=GP1-XS image=ubuntu_jammy stopped=true",
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
server := ctx.Result.(*instance.ServerWithWarningsResponse).Server
assert.NotNil(t, ctx.Result)
assert.Equal(t, "GP1-XS", server.CommercialType)
},
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("With name", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=ubuntu_jammy name=yo stopped=true"),
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
server := ctx.Result.(*instance.ServerWithWarningsResponse).Server
assert.NotNil(t, ctx.Result)
assert.Equal(t, "yo", server.Name)
},
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("With start", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=ubuntu_jammy -w"),
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
server := ctx.Result.(*instance.ServerWithWarningsResponse).Server
assert.NotNil(t, ctx.Result)
assert.Equal(
t,
instanceSDK.ServerStateRunning,
server.State,
)
},
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("Image UUID", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=f974feac-abae-4365-b988-8ec7d1cec10d stopped=true"),
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
server := ctx.Result.(*instance.ServerWithWarningsResponse).Server
assert.NotNil(t, ctx.Result)
assert.Equal(
t,
"Ubuntu Bionic Beaver",
server.Image.Name,
)
},
core.TestCheckExitCode(0),
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("Tags", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=ubuntu_jammy tags.0=prod tags.1=blue stopped=true"),
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
server := ctx.Result.(*instance.ServerWithWarningsResponse).Server
assert.NotNil(t, ctx.Result)
assert.Equal(t, "prod", server.Tags[0])
assert.Equal(t, "blue", server.Tags[1])
},
),
AfterFunc: deleteServerAfterFunc(),
}))
})
////
// Volume use cases
////
t.Run("Volumes", func(t *testing.T) {
t.Run("valid single local volume", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=ubuntu_bionic root-volume=local:20GB stopped=true"),
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
volume := testhelpers.MapTValue(t, server.Volumes, "0")
size := volume.Size
assert.Equal(
t,
20*scw.GB,
instance.SizeValue(size),
"Size of volume should be 20 GB",
)
},
core.TestCheckExitCode(0),
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("valid single local snapshot", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
core.ExecStoreBeforeCmd(
"Server",
testServerCommand("image=ubuntu_bionic root-volume=local:20GB stopped=true"),
),
core.ExecStoreBeforeCmd(
"Snapshot",
`scw instance snapshot create volume-id={{ (index .Server.Volumes "0").ID }}`,
),
),
Cmd: testServerCommand(
"image=ubuntu_bionic root-volume=local:{{ .Snapshot.Snapshot.ID }} stopped=true",
),
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
volume := testhelpers.MapTValue(t, server.Volumes, "0")
size := volume.Size
assert.Equal(
t,
20*scw.GB,
instance.SizeValue(size),
"Size of volume should be 20 GB",
)
},
),
AfterFunc: core.AfterFuncCombine(
deleteServer("Server"),
deleteServerAfterFunc(),
deleteSnapshot("Snapshot"),
),
}))
t.Run("valid single local snapshot without image", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
core.ExecStoreBeforeCmd(
"Server",
testServerCommand("image=ubuntu_bionic root-volume=local:20GB stopped=true"),
),
core.ExecStoreBeforeCmd(
"Snapshot",
`scw instance snapshot create volume-id={{ (index .Server.Volumes "0").ID }}`,
),
),
Cmd: testServerCommand(
"image=none root-volume=local:{{ .Snapshot.Snapshot.ID }} stopped=true",
),
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
volume := testhelpers.MapTValue(t, server.Volumes, "0")
size := volume.Size
assert.Equal(
t,
20*scw.GB,
instance.SizeValue(size),
"Size of volume should be 20 GB",
)
},
),
AfterFunc: core.AfterFuncCombine(
deleteServer("Server"),
deleteServerAfterFunc(),
deleteSnapshot("Snapshot"),
),
}))
t.Run("valid double local volumes", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand(
"image=ubuntu_bionic root-volume=local:10GB additional-volumes.0=l:10G stopped=true",
),
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
size0 := testhelpers.MapTValue(t, server.Volumes, "0").Size
size1 := testhelpers.MapTValue(t, server.Volumes, "1").Size
assert.Equal(
t,
10*scw.GB,
instance.SizeValue(size0),
"Size of volume should be 10 GB",
)
assert.Equal(
t,
10*scw.GB,
instance.SizeValue(size1),
"Size of volume should be 10 GB",
)
},
core.TestCheckExitCode(0),
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("valid double snapshot", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
instance.GetCommands(),
block.GetCommands(),
),
BeforeFunc: core.BeforeFuncCombine(
core.ExecStoreBeforeCmd(
"Server",
testServerCommand("image=ubuntu_jammy root-volume=block:20GB stopped=true"),
),
core.ExecStoreBeforeCmd(
"Snapshot",
`scw block snapshot create volume-id={{ (index .Server.Volumes "0").ID }} -w`,
),
),
Cmd: testServerCommand(
"image=ubuntu_jammy root-volume=block:{{ .Snapshot.ID }} additional-volumes.0=block:{{ .Snapshot.ID }} stopped=true",
),
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
testServerSBSVolumeSize("0", 20),
testServerSBSVolumeSize("1", 20),
),
AfterFunc: core.AfterFuncCombine(
deleteServer("Server"),
deleteServerAfterFunc(),
deleteBlockSnapshot("Snapshot"),
),
}))
t.Run("valid additional block volumes", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand(
"image=ubuntu_jammy additional-volumes.0=b:1G additional-volumes.1=b:5G additional-volumes.2=b:10G stopped=true",
),
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
testServerSBSVolumeSize("1", 1),
testServerSBSVolumeSize("2", 5),
testServerSBSVolumeSize("3", 10),
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("sbs additional volumes from id", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
instance.GetCommands(),
block.GetCommands(),
),
BeforeFunc: core.BeforeFuncCombine(
createSbsVolume(20),
),
Cmd: testServerCommand(
"image=ubuntu_jammy additional-volumes.0={{.Volume.ID}} stopped=true",
),
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
volume := testhelpers.MapTValue(t, server.Volumes, "1")
assert.Equal(t, instanceSDK.VolumeServerVolumeTypeSbsVolume, volume.VolumeType)
},
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
deleteServerAfterFunc(),
),
}))
t.Run("sbs additional volumes", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
instance.GetCommands(),
block.GetCommands(),
),
Cmd: testServerCommand("image=ubuntu_jammy additional-volumes.0=sbs:20G stopped=true"),
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
volume := testhelpers.MapTValue(t, server.Volumes, "1")
assert.Equal(t, instanceSDK.VolumeServerVolumeTypeSbsVolume, volume.VolumeType)
},
),
AfterFunc: core.AfterFuncCombine(
deleteServerAfterFunc(),
),
}))
t.Run("use sbs root volume", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
instance.GetCommands(),
block.GetCommands(),
),
BeforeFunc: core.BeforeFuncCombine(
createSbsVolume(20),
),
Cmd: testServerCommand("image=none root-volume={{.Volume.ID}} stopped=true"),
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
volume := testhelpers.MapTValue(t, server.Volumes, "0")
assert.Equal(t, instanceSDK.VolumeServerVolumeTypeSbsVolume, volume.VolumeType)
},
),
AfterFunc: core.AfterFuncCombine(
deleteServerAfterFunc(),
),
}))
t.Run("create sbs root volume", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
instance.GetCommands(),
block.GetCommands(),
),
Cmd: testServerCommand("image=ubuntu_jammy root-volume=sbs:20GB stopped=true"),
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
volume := testhelpers.MapTValue(t, server.Volumes, "0")
assert.Equal(t, instanceSDK.VolumeServerVolumeTypeSbsVolume, volume.VolumeType)
},
),
AfterFunc: core.AfterFuncCombine(
deleteServerAfterFunc(),
),
}))
t.Run("create sbs root volume with iops", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
instance.GetCommands(),
block.GetCommands(),
),
Cmd: testServerCommand(
"image=ubuntu_jammy root-volume=sbs:20GB:15000 stopped=true --debug",
),
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
rootVolume, rootVolumeExists := server.Volumes["0"]
assert.True(t, rootVolumeExists)
assert.Equal(
t,
instanceSDK.VolumeServerVolumeTypeSbsVolume,
rootVolume.VolumeType,
)
api := blockSDK.NewAPI(ctx.Client)
vol, err := api.WaitForVolume(&blockSDK.WaitForVolumeRequest{
VolumeID: rootVolume.ID,
Zone: rootVolume.Zone,
RetryInterval: core.DefaultRetryInterval,
})
require.NoError(t, err)
assert.NotNil(t, vol.Specs)
assert.NotNil(t, vol.Specs.PerfIops)
assert.Equal(t, uint32(15000), *vol.Specs.PerfIops)
},
),
AfterFunc: core.AfterFuncCombine(
deleteServerAfterFunc(),
),
}))
})
////
// IP use cases
////
t.Run("IPs", func(t *testing.T) {
t.Run("explicit new IP", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("ip=new stopped=true"),
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
assert.NotNil(t, server.PublicIP)
assert.NotEmpty(t, server.PublicIP.Address)
assert.False(t, server.PublicIP.Dynamic)
},
core.TestCheckExitCode(0),
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("run with dynamic IP", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("ip=dynamic -w"), // dynamic IP is created at runtime
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
require.NoError(t, ctx.Err)
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
assert.NotNil(t, server.PublicIP)
assert.NotEmpty(t, server.PublicIP.Address)
assert.True(t, server.DynamicIPRequired)
},
core.TestCheckExitCode(0),
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("existing IP", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: createIP("IP"),
Cmd: testServerCommand("ip={{ .IP.Address }} stopped=true"),
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
assert.NotNil(t, server.PublicIP)
assert.NotEmpty(t, server.PublicIP.Address)
assert.False(t, server.PublicIP.Dynamic)
},
core.TestCheckExitCode(0),
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("existing IP ID", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: createIP("IP"),
Cmd: testServerCommand("ip={{ .IP.ID }} stopped=true"),
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result)
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
assert.NotNil(t, server.PublicIP)
assert.NotEmpty(t, server.PublicIP.Address)
assert.False(t, server.PublicIP.Dynamic)
},
core.TestCheckExitCode(0),
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("with ipv6", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand(
"ip=ipv6 dynamic-ip-required=false -w",
), // IPv6 is created at runtime
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
require.NotNil(t, ctx.Result, "Server is nil")
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
assert.Len(t, server.PublicIPs, 1)
assert.Equal(t, instanceSDK.ServerIPIPFamilyInet6, server.PublicIPs[0].Family)
},
core.TestCheckExitCode(0),
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("with ipv6 and dynamic ip", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand(
"dynamic-ip-required=true ip=ipv6 -w",
), // IPv6 is created at runtime
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result, "server is nil")
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
assert.Len(t, server.PublicIPs, 2)
assert.Equal(t, instanceSDK.ServerIPIPFamilyInet, server.PublicIPs[0].Family)
assert.True(t, server.PublicIPs[0].Dynamic)
assert.Equal(t, instanceSDK.ServerIPIPFamilyInet6, server.PublicIPs[1].Family)
},
),
AfterFunc: deleteServerAfterFunc(),
}))
t.Run("with ipv6 and ipv4", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("ip=both -w"), // IPv6 is created at runtime
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.NotNil(t, ctx.Result, "server is nil")
server := testhelpers.Value[*instance.ServerWithWarningsResponse](
t,
ctx.Result,
).Server
assert.Len(t, server.PublicIPs, 2)
assert.Equal(t, instanceSDK.ServerIPIPFamilyInet, server.PublicIPs[0].Family)
assert.Equal(t, instanceSDK.ServerIPIPFamilyInet6, server.PublicIPs[1].Family)
},
),
AfterFunc: deleteServerAfterFunc(),
}))
})
}
// None of the tests below should succeed to create an instance.
// These tests need to be run in sequence since they are having warnings in the stderr
// and these warnings can be captured by other tests
func Test_CreateServerErrors(t *testing.T) {
////
// Image errors
////
t.Run("Error: invalid image label", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=macos"),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
t.Run("Error: invalid image UUID", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=7a892c1a-bbdc-491f-9974-4008e3708664"),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
////
// Instance type errors
////
t.Run("Error: invalid instance type", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: "scw instance server create type=MACBOOK1-S image=ubuntu_jammy",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
////
// Volume errors
////
t.Run("Error: invalid total local volumes size: too low 1", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=ubuntu_jammy root-volume=l:5GB"),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
t.Run("Error: invalid total local volumes size: too low 2", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand(
"image=ubuntu_jammy root-volume=l:5GB additional-volumes.0=block:10GB",
),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
t.Run("Error: invalid total local volumes size: too low 3", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: createVolume(5),
Cmd: testServerCommand("image=ubuntu_jammy root-volume={{ .Volume.ID }}"),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
AfterFunc: deleteVolume(),
DisableParallel: true,
}))
t.Run("Error: invalid total local volumes size: too high 1", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand(
"image=ubuntu_jammy root-volume=local:10GB additional-volumes.0=local:20GB",
),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
t.Run("Error: invalid total local volumes size: too high 2", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=ubuntu_jammy additional-volumes.0=local:30GB"),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
t.Run("Error: invalid total local volumes size: too high 3", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: createVolume(20),
Cmd: testServerCommand(
"image=ubuntu_jammy root-volume={{ .Volume.ID }} additional-volumes.0=local:10GB",
),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
AfterFunc: deleteVolume(),
DisableParallel: true,
}))
t.Run("Error: invalid root volume size", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand(
"image=ubuntu_jammy root-volume=local:2GB additional-volumes.0=local:18GB",
),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
t.Run("Error: disallow existing root volume ID", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: createVolume(20),
Cmd: testServerCommand("image=ubuntu_jammy root-volume={{ .Volume.ID }}"),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
AfterFunc: deleteVolume(),
DisableParallel: true,
}))
t.Run("Error: invalid root volume ID", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand(
"image=ubuntu_jammy root-volume=29da9ad9-e759-4a56-82c8-f0607f93055c",
),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
t.Run("Error: already attached additional volume ID", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.ExecStoreBeforeCmd(
"Server",
testServerCommand(
"name=cli-test image=ubuntu_jammy root-volume=l:10G additional-volumes.0=l:10G stopped=true",
),
),
Cmd: testServerCommand(
`image=ubuntu_jammy root-volume=l:10G additional-volumes.0={{ (index .Server.Volumes "1").ID }} stopped=true`,
),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
AfterFunc: deleteServer("Server"),
DisableParallel: true,
}))
t.Run("Error: invalid root volume format", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=ubuntu_jammy root-volume=20GB"),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
t.Run("Error: invalid root volume snapshot ID", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand(
"image=ubuntu_jammy root-volume=local:29da9ad9-e759-4a56-82c8-f0607f93055c",
),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
t.Run("Error: invalid additional volume snapshot ID", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand(
"image=ubuntu_jammy additional-volumes.0=block:29da9ad9-e759-4a56-82c8-f0607f93055c",
),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
DisableParallel: true,
}))
////
// IP errors
////
t.Run("Error: not found ip ID", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=ubuntu_jammy ip=23165951-13fd-4a3b-84ed-22c2e96658f2"),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
}))
t.Run("Error: forbidden IP", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=ubuntu_jammy ip=51.15.242.82"),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
}))
t.Run("Error: invalid ip", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: testServerCommand("image=ubuntu_jammy ip=yo"),
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
}))
t.Run("Error: image size is incompatible with instance type", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: "scw instance server create image=d4067cdc-dc9d-4810-8a26-0dae51d7df42 type=DEV1-S",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
}))
////
// Windows
////
t.Run("Error: ssh key id is required", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: "scw instance server create image=windows_server_2022 type=POP2-2C-8G-WIN",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
}))
}
func Test_CreateServerScratchStorage(t *testing.T) {
t.Run("Default scratch storage", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
Cmd: "scw instance server create type=H100-1-80G image=ubuntu_jammy_gpu_os_12 zone=fr-par-2",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
func(_ *testing.T, ctx *core.CheckFuncCtx) {
fmt.Println(ctx.LogBuffer)
},
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
serverResponse, isServerResponse := ctx.Result.(*instance.ServerWithWarningsResponse)
if !isServerResponse {
t.Fatalf("Result is not a server")
}
server := serverResponse.Server
additionalVolume, exist := server.Volumes["1"]
if !exist {
t.Fatalf("Expected an additional scratch volume, found none")
}
assert.Equal(
t,
instanceSDK.VolumeServerVolumeTypeScratch,
additionalVolume.VolumeType,
)
},
),
AfterFunc: core.ExecAfterCmd(
"scw instance server delete {{ .CmdResult.ID }} zone=fr-par-2 with-volumes=all with-ip=true force-shutdown=true",
),
DisableParallel: true,
}))
}
func Test_AttachFilesystem(t *testing.T) {
t.Run("attach filesystem", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
instance.GetCommands(),
file.GetCommands(),
),
BeforeFunc: core.BeforeFuncCombine(
core.ExecStoreBeforeCmd(
"FileSystem",
"scw file filesystem create name=instance-fs-cli size=100000000000",
),
core.ExecStoreBeforeCmd(
"Server",
testServerCommand("stopped=true image=ubuntu-jammy type=POP2-2C-8G"),
),
),
Cmd: "scw instance server attach-filesystem server-id={{ .Server.ID }} filesystem-id={{ .FileSystem.ID }}",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
core.ExecAfterCmd(
"scw instance server detach-filesystem server-id={{ .Server.ID }} filesystem-id={{ .FileSystem.ID }}",
),
deleteServer("Server"),
core.ExecAfterCmd(
"scw file filesystem delete {{ .FileSystem.ID }}",
),
),
DisableParallel: true,
}))
}