-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathcustom_server_test.go
More file actions
481 lines (457 loc) · 15.7 KB
/
custom_server_test.go
File metadata and controls
481 lines (457 loc) · 15.7 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
package instance_test
import (
"testing"
"github.com/scaleway/scaleway-cli/v2/core"
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
block "github.com/scaleway/scaleway-cli/v2/internal/namespaces/block/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"
)
func Test_ServerVolumeUpdate(t *testing.T) {
t.Skip("Skipping temporarily")
t.Run("Attach", func(t *testing.T) {
t.Run("simple block volume", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
block.GetCommands(),
instance.GetCommands(),
),
BeforeFunc: core.BeforeFuncCombine(
createServer("Server"),
createSbsVolume(10),
),
Cmd: "scw instance server attach-volume server-id={{ .Server.ID }} volume-id={{ .Volume.ID }}",
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
require.NoError(t, ctx.Err)
resp := testhelpers.Value[*instanceSDK.AttachVolumeResponse](t, ctx.Result)
addedVolume := testhelpers.MapTValue(t, resp.Server.Volumes, "1")
assert.Equal(
t,
instanceSDK.VolumeServerVolumeTypeSbsVolume,
addedVolume.VolumeType,
)
},
testAttachVolumeServerSBSVolumeSize("0", 10),
testAttachVolumeServerSBSVolumeSize("1", 10),
),
AfterFunc: deleteServer("Server"),
DisableParallel: true,
}))
t.Run("simple local volume", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
createServer("Server"),
createVolume(10),
),
Cmd: "scw instance server attach-volume server-id={{ .Server.ID }} volume-id={{ .Volume.ID }}",
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
require.NoError(t, ctx.Err)
resp := testhelpers.Value[*instanceSDK.AttachVolumeResponse](t, ctx.Result)
addedVolume := testhelpers.MapTValue(t, resp.Server.Volumes, "1")
assert.Equal(
t,
10*scw.GB,
instance.SizeValue(addedVolume.Size),
"Size of volume should be 10 GB",
)
assert.Equal(t, instanceSDK.VolumeServerVolumeTypeLSSD, addedVolume.VolumeType)
},
testAttachVolumeServerSBSVolumeSize("0", 10),
),
AfterFunc: deleteServer("Server"),
DisableParallel: true,
}))
t.Run("invalid volume UUID", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: createServer("Server"),
Cmd: "scw instance server attach-volume server-id={{ .Server.ID }} volume-id=11111111-1111-1111-1111-111111111111",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
AfterFunc: deleteServer("Server"),
DisableParallel: true,
}))
})
t.Run("Detach", func(t *testing.T) {
t.Run("simple block volume", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
block.GetCommands(),
instance.GetCommands(),
),
BeforeFunc: core.ExecStoreBeforeCmd(
"Server",
testServerCommand("stopped=true image=ubuntu-jammy additional-volumes.0=block:10G"),
),
Cmd: `scw instance server detach-volume volume-id={{ (index .Server.Volumes "1").ID }} server-id={{ .Server.ID }}`,
Check: func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
require.NoError(t, ctx.Err)
resp := testhelpers.Value[*instanceSDK.DetachServerVolumeResponse](t, ctx.Result)
assert.NotZero(t, resp.Server.Volumes["0"])
assert.Nil(t, resp.Server.Volumes["1"])
assert.Len(
t,
ctx.Result.(*instanceSDK.DetachServerVolumeResponse).Server.Volumes,
1,
)
},
AfterFunc: core.AfterFuncCombine(
core.ExecAfterCmd(
`scw block volume wait terminal-status=available {{ (index .Server.Volumes "1").ID }}`,
),
core.ExecAfterCmd(`scw block volume delete {{ (index .Server.Volumes "1").ID }}`),
deleteServer("Server"),
),
DisableParallel: true,
}))
t.Run("invalid volume UUID", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: createServer("Server"),
Cmd: "scw instance server detach-volume volume-id=11111111-1111-1111-1111-111111111111 server-id={{ .Server.ID }}",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
AfterFunc: deleteServer("Server"),
DisableParallel: true,
}))
})
}
func Test_ServerUpdateCustom(t *testing.T) {
// IP cases.
t.Run("Try to remove ip from server without ip", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: createServer("Server"),
Cmd: "scw instance server update {{ .Server.ID }} ip=none",
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
resp := testhelpers.Value[*instance.ServerWithWarningsResponse](t, ctx.Result)
assert.Equal(t, (*instanceSDK.ServerIP)(nil), resp.Server.PublicIP)
},
core.TestCheckExitCode(0),
),
AfterFunc: deleteServer("Server"),
}))
t.Run("Update server ip from server without ip", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
createServer("Server"),
createIP("IP"),
),
Cmd: "scw instance server update {{ .Server.ID }} ip={{ .IP.Address }}",
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
ip := testhelpers.MapValue[*instanceSDK.IP](t, ctx.Meta, "IP")
resp := testhelpers.Value[*instance.ServerWithWarningsResponse](t, ctx.Result)
assert.NotNil(t, resp.Server)
assert.NotNil(t, resp.Server.PublicIP)
assert.Equal(t, ip.Address, resp.Server.PublicIP.Address)
},
core.TestCheckExitCode(0),
),
AfterFunc: deleteServer("Server"),
}))
t.Run("Update server ip from server with ip", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
createServer("Server"),
createIP("IP1"),
createIP("IP2"),
// Attach IP1 to Server.
core.ExecStoreBeforeCmd(
"UpdatedServer",
"scw instance server update {{ .Server.ID }} ip={{ .IP1.Address }}",
),
),
Cmd: "scw instance server update {{ .Server.ID }} ip={{ .IP2.Address }}",
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
// Test that the Server WAS attached to IP1.
assert.Equal(
t,
ctx.Meta["IP1"].(*instanceSDK.IP).Address,
ctx.Meta["UpdatedServer"].(*instance.ServerWithWarningsResponse).Server.PublicIP.Address,
)
// Test that the Server IS attached to IP2.
assert.Equal(t,
ctx.Meta["IP2"].(*instanceSDK.IP).Address,
ctx.Result.(*instance.ServerWithWarningsResponse).Server.PublicIP.Address)
},
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
deleteServer("Server"),
deleteIP("IP1"),
),
}))
// Placement group cases.
t.Run(
"Update server placement-group-id from server with placement-group-id",
core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
createPlacementGroup("PlacementGroup1"),
createPlacementGroup("PlacementGroup2"),
core.ExecStoreBeforeCmd(
"Server",
testServerCommand(
"stopped=true image=ubuntu-jammy placement-group-id={{ .PlacementGroup1.ID }}",
),
),
),
Cmd: "scw instance server update {{ .Server.ID }} placement-group-id={{ .PlacementGroup2.ID }}",
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.Equal(t,
ctx.Meta["PlacementGroup2"].(*instanceSDK.PlacementGroup).ID,
ctx.Result.(*instance.ServerWithWarningsResponse).Server.PlacementGroup.ID)
},
),
AfterFunc: core.AfterFuncCombine(
deleteServer("Server"),
deletePlacementGroup("PlacementGroup1"),
deletePlacementGroup("PlacementGroup2"),
),
}),
)
// Security group cases.
t.Run(
"Update server security-group-id from server with security-group-id",
core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
createSecurityGroup("SecurityGroup1"),
createSecurityGroup("SecurityGroup2"),
core.ExecStoreBeforeCmd(
"Server",
testServerCommand(
"stopped=true image=ubuntu-jammy security-group-id={{ .SecurityGroup1.ID }}",
),
),
),
Cmd: "scw instance server update {{ .Server.ID }} security-group-id={{ .SecurityGroup2.ID }}",
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.Equal(t,
ctx.Meta["SecurityGroup2"].(*instanceSDK.SecurityGroup).ID,
ctx.Result.(*instance.ServerWithWarningsResponse).Server.SecurityGroup.ID)
},
),
AfterFunc: core.AfterFuncCombine(
deleteServer("Server"),
deleteSecurityGroup("SecurityGroup1"),
deleteSecurityGroup("SecurityGroup2"),
),
}),
)
// Volumes cases.
t.Run("Volumes", func(t *testing.T) {
t.Run("valid simple block volume", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
block.GetCommands(),
instance.GetCommands(),
),
BeforeFunc: core.BeforeFuncCombine(
createServer("Server"),
createSbsVolume(10),
),
Cmd: `scw instance server update {{ .Server.ID }} volume-ids.0={{ (index .Server.Volumes "0").ID }} volume-ids.1={{ .Volume.ID }}`,
Check: core.TestCheckCombine(
testServerUpdateServerSBSVolumeSize("0", 10),
testServerUpdateServerSBSVolumeSize("1", 10),
),
AfterFunc: deleteServer("Server"),
}))
t.Run("detach all volumes", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
block.GetCommands(),
instance.GetCommands(),
),
BeforeFunc: core.ExecStoreBeforeCmd(
"Server",
testServerCommand("stopped=true image=ubuntu-jammy additional-volumes.0=block:10G"),
),
Cmd: `scw instance server update {{ .Server.ID }} volume-ids=none`,
Check: func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
require.NoError(t, ctx.Err)
assert.Empty(t, ctx.Result.(*instance.ServerWithWarningsResponse).Server.Volumes)
},
AfterFunc: core.AfterFuncCombine(
core.ExecAfterCmd(
`scw block volume wait terminal-status=available {{ (index .Server.Volumes "0").ID }}`,
),
core.ExecAfterCmd(`scw block volume delete {{ (index .Server.Volumes "0").ID }}`),
core.ExecAfterCmd(
`scw block volume wait terminal-status=available {{ (index .Server.Volumes "1").ID }}`,
),
core.ExecAfterCmd(`scw block volume delete {{ (index .Server.Volumes "1").ID }}`),
deleteServer("Server"),
),
}))
})
}
// These tests needs to be run in sequence
// since they are using the interactive print
func Test_ServerDelete(t *testing.T) {
interactive.IsInteractive = true
t.Run("with all volumes", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.ExecStoreBeforeCmd(
"Server",
testServerCommand("stopped=true additional-volumes.0=block:10G"),
),
Cmd: `scw instance server delete {{ .Server.ID }} with-ip=true with-volumes=all`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
DisableParallel: true,
}))
t.Run("only block volumes", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.ExecStoreBeforeCmd(
"Server",
testServerCommand("stopped=true root-volume=l:20G additional-volumes.0=block:10G"),
),
Cmd: `scw instance server delete {{ .Server.ID }} with-ip=true with-volumes=block`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.ExecAfterCmd(
`scw instance volume delete {{ (index .Server.Volumes "0").ID }}`,
),
DisableParallel: true,
}))
t.Run("only local volumes", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
block.GetCommands(),
instance.GetCommands(),
),
BeforeFunc: core.ExecStoreBeforeCmd(
"Server",
testServerCommand("stopped=true root-volume=l:20G additional-volumes.0=block:10G"),
),
Cmd: `scw instance server delete {{ .Server.ID }} with-ip=true with-volumes=local`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
core.ExecAfterCmd(
`scw block volume wait terminal-status=available {{ (index .Server.Volumes "1").ID }}`,
),
core.ExecAfterCmd(`scw block volume delete {{ (index .Server.Volumes "1").ID }}`),
),
DisableParallel: true,
}))
t.Run("with none volumes", core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
block.GetCommands(),
instance.GetCommands(),
),
BeforeFunc: core.ExecStoreBeforeCmd(
"Server",
testServerCommand("stopped=true root-volume=l:20G additional-volumes.0=block:10G"),
),
Cmd: `scw instance server delete {{ .Server.ID }} with-ip=true with-volumes=none`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
api := instanceSDK.NewAPI(ctx.Client)
server := ctx.Meta["Server"].(*instance.ServerWithWarningsResponse).Server
_, err := api.GetVolume(&instanceSDK.GetVolumeRequest{
VolumeID: server.Volumes["0"].ID,
})
assert.NoError(t, err)
},
),
AfterFunc: core.AfterFuncCombine(
core.ExecAfterCmd(`scw instance volume delete {{ (index .Server.Volumes "0").ID }}`),
core.ExecAfterCmd(`scw block volume delete {{ (index .Server.Volumes "1").ID }}`),
),
DisableParallel: true,
}))
t.Run("with sbs volumes", func(t *testing.T) {
t.Skip("Skipping 'with sbs volumes' test temporarily")
core.Test(&core.TestConfig{
Commands: core.NewCommandsMerge(
instance.GetCommands(),
block.GetCommands(),
),
BeforeFunc: core.BeforeFuncCombine(
core.ExecStoreBeforeCmd(
"BlockVolume",
"scw block volume create perf-iops=5000 from-empty.size=10G name=cli-test-server-delete-with-sbs-volumes",
),
core.ExecStoreBeforeCmd(
"Server",
testServerCommand("stopped=true image=ubuntu-jammy"),
),
core.ExecBeforeCmd(
"scw instance server attach-volume server-id={{ .Server.ID }} volume-id={{ .BlockVolume.ID }}",
),
),
Cmd: `scw instance server delete {{ .Server.ID }} with-ip=true with-volumes=all`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
api := blockSDK.NewAPI(ctx.Client)
blockVolume := ctx.Meta["BlockVolume"].(*blockSDK.Volume)
resp, err := api.GetVolume(&blockSDK.GetVolumeRequest{
Zone: blockVolume.Zone,
VolumeID: blockVolume.ID,
})
assert.Error(t, err, "%v", resp)
},
),
DisableParallel: true,
})
})
t.Run("with multiple IPs", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.ExecStoreBeforeCmd("Server", testServerCommand("stopped=true ip=both")),
Cmd: `scw instance server delete {{ .Server.ID }} with-ip=true with-volumes=all`,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
require.NotNil(t, ctx.Meta["Server"])
server := ctx.Meta["Server"].(*instance.ServerWithWarningsResponse).Server
assert.Len(t, server.PublicIPs, 2)
api := instanceSDK.NewAPI(ctx.Client)
for _, ip := range server.PublicIPs {
_, err := api.GetIP(&instanceSDK.GetIPRequest{
Zone: server.Zone,
IP: ip.ID,
})
assert.Error(t, err, "expected IP to be deleted")
}
},
),
DisableParallel: true,
}))
interactive.IsInteractive = false
}