Skip to content

Commit 21fe18d

Browse files
Codelaxjremy42
andauthored
test(instance): default server creation to ip=none (scaleway#4352)
Co-authored-by: Jonathan R. <[email protected]>
1 parent a0ca9f7 commit 21fe18d

11 files changed

+3879
-4339
lines changed

core/testing.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,27 @@ func (meta testMetadata) render(strTpl string) string {
8989
return buf.String()
9090
}
9191

92+
func GetFromMeta[T any](t *testing.T, ctx *CheckFuncCtx, key string) T {
93+
t.Helper()
94+
95+
item, ok := ctx.Meta[key]
96+
assert.True(t, ok)
97+
typedItem, typeIsCorrect := item.(T)
98+
assert.True(t, typeIsCorrect)
99+
100+
return typedItem
101+
}
102+
103+
func GetTestResult[T any](t *testing.T, ctx *CheckFuncCtx) T {
104+
t.Helper()
105+
106+
typedItem, typeIsCorrect := ctx.Result.(T)
107+
assert.True(t, typeIsCorrect)
108+
assert.NotNil(t, typedItem)
109+
110+
return typedItem
111+
}
112+
92113
func BeforeFuncStoreInMeta(key string, value interface{}) BeforeFunc {
93114
return func(ctx *BeforeFuncCtx) error {
94115
ctx.Meta[key] = value

internal/namespaces/instance/v1/custom_image_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func Test_ImageCreate(t *testing.T) {
3232
t.Run("Use additional snapshots", core.Test(&core.TestConfig{
3333
Commands: instance.GetCommands(),
3434
BeforeFunc: core.BeforeFuncCombine(
35-
core.ExecStoreBeforeCmd("Server", "scw instance server create type=DEV1-S image=ubuntu_focal root-volume=local:10GB additional-volumes.0=local:10GB -w"),
35+
core.ExecStoreBeforeCmd("Server", "scw instance server create type=DEV1-S ip=none image=ubuntu_focal root-volume=local:10GB additional-volumes.0=local:10GB -w"),
3636
core.ExecStoreBeforeCmd("SnapshotA", `scw instance snapshot create -w name=cli-test-image-create-snapshotA volume-id={{ (index .Server.Volumes "0").ID }}`),
3737
core.ExecStoreBeforeCmd("SnapshotB", `scw instance snapshot create -w name=cli-test-image-create-snapshotB volume-id={{ (index .Server.Volumes "1").ID }}`),
3838
),

internal/namespaces/instance/v1/custom_placement_group_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func Test_GetPlacementGroup(t *testing.T) {
1111
t.Run("simple", core.Test(&core.TestConfig{
1212
BeforeFunc: core.BeforeFuncCombine(
1313
core.ExecStoreBeforeCmd("PlacementGroup", "scw instance placement-group create"),
14-
core.ExecStoreBeforeCmd("ServerA", "scw instance server create type=DEV1-S image=ubuntu_jammy stopped=true placement-group-id={{ .PlacementGroup.PlacementGroup.ID }}"),
14+
core.ExecStoreBeforeCmd("ServerA", "scw instance server create type=DEV1-S image=ubuntu_jammy ip=none stopped=true placement-group-id={{ .PlacementGroup.PlacementGroup.ID }}"),
1515
),
1616
Commands: instance.GetCommands(),
1717
Cmd: "scw instance placement-group get {{ .PlacementGroup.PlacementGroup.ID }}",

internal/namespaces/instance/v1/custom_server_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ func Test_ServerUpdateCustom(t *testing.T) {
127127
Check: core.TestCheckCombine(
128128
func(t *testing.T, ctx *core.CheckFuncCtx) {
129129
t.Helper()
130-
assert.Equal(t, ctx.Meta["IP"].(*instanceSDK.IP).Address, ctx.Result.(*instanceSDK.UpdateServerResponse).Server.PublicIP.Address)
130+
ip := core.GetFromMeta[*instanceSDK.IP](t, ctx, "IP")
131+
resp := core.GetTestResult[*instanceSDK.UpdateServerResponse](t, ctx)
132+
133+
assert.NotNil(t, resp.Server)
134+
assert.NotNil(t, resp.Server.PublicIP)
135+
assert.Equal(t, ip.Address, resp.Server.PublicIP.Address)
131136
},
132137
core.TestCheckExitCode(0),
133138
),

internal/namespaces/instance/v1/helpers_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,22 @@ func createServerBionic(metaKey string) core.BeforeFunc {
2121
return core.ExecStoreBeforeCmd(metaKey, testServerCommand("stopped=true image=ubuntu-bionic"))
2222
}
2323

24+
// createServer creates a stopped ubuntu server without IP and
25+
// register it in the context Meta at given metaKey
26+
//
2427
//nolint:unparam
2528
func createServer(metaKey string) core.BeforeFunc {
2629
return core.ExecStoreBeforeCmd(metaKey, testServerCommand("stopped=true image=ubuntu-jammy"))
2730
}
2831

2932
// testServerCommand creates returns a create server command with the instance type and the given arguments
3033
func testServerCommand(params string) string {
31-
return "scw instance server create type=DEV1-S " + params
34+
baseCommand := "scw instance server create type=DEV1-S "
35+
if !strings.Contains(params, "ip=") {
36+
baseCommand += "ip=none "
37+
}
38+
39+
return baseCommand + params
3240
}
3341

3442
// createServer creates a stopped ubuntu-bionic server and

internal/namespaces/instance/v1/testdata/test-image-create-create-simple-image.cassette.yaml

Lines changed: 1043 additions & 1045 deletions
Large diffs are not rendered by default.

internal/namespaces/instance/v1/testdata/test-image-create-create-simple-image.golden

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
22
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3-
ID 6d717fb0-83a8-4111-a322-2cb9ddd995cc
4-
Name cli-img-frosty-neumann
3+
ID 925aa80b-9410-4385-961d-3b258706effe
4+
Name cli-img-nifty-maxwell
55
Arch x86_64
66
CreationDate few seconds ago
77
ModificationDate few seconds ago
88
ExtraVolumes 0
99
FromServer -
10-
Organization ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b
10+
Organization 105bdce1-64c0-48ab-899d-868455867ecf
1111
Public false
12-
RootVolume ffbf1933-5935-4300-9dda-8dc42c63df66
12+
RootVolume ab31f24e-1e04-4c7e-818a-5fca7e14c704
1313
State available
14-
Project ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b
14+
Project 105bdce1-64c0-48ab-899d-868455867ecf
1515
Zone fr-par-1
1616
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
1717
{
1818
"image": {
19-
"id": "6d717fb0-83a8-4111-a322-2cb9ddd995cc",
20-
"name": "cli-img-frosty-neumann",
19+
"id": "925aa80b-9410-4385-961d-3b258706effe",
20+
"name": "cli-img-nifty-maxwell",
2121
"arch": "x86_64",
2222
"creation_date": "1970-01-01T00:00:00.0Z",
2323
"modification_date": "1970-01-01T00:00:00.0Z",
2424
"default_bootscript": null,
2525
"extra_volumes": {},
2626
"from_server": "",
27-
"organization": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b",
27+
"organization": "105bdce1-64c0-48ab-899d-868455867ecf",
2828
"public": false,
2929
"root_volume": {
30-
"id": "ffbf1933-5935-4300-9dda-8dc42c63df66",
31-
"name": "cli-snp-focused-davinci",
30+
"id": "ab31f24e-1e04-4c7e-818a-5fca7e14c704",
31+
"name": "cli-snp-competent-keller",
3232
"size": 20000000000,
3333
"volume_type": "l_ssd"
3434
},
3535
"state": "available",
36-
"project": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b",
36+
"project": "105bdce1-64c0-48ab-899d-868455867ecf",
3737
"tags": [],
3838
"zone": "fr-par-1"
3939
}

0 commit comments

Comments
 (0)