Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(instance): default server creation to ip=none #4352

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@ func (meta testMetadata) render(strTpl string) string {
return buf.String()
}

func GetFromMeta[T any](t *testing.T, ctx *CheckFuncCtx, key string) T {
t.Helper()

item, ok := ctx.Meta[key]
assert.True(t, ok)
typedItem, typeIsCorrect := item.(T)
assert.True(t, typeIsCorrect)

return typedItem
}

func GetTestResult[T any](t *testing.T, ctx *CheckFuncCtx) T {
t.Helper()

typedItem, typeIsCorrect := ctx.Result.(T)
assert.True(t, typeIsCorrect)
assert.NotNil(t, typedItem)

return typedItem
}

func BeforeFuncStoreInMeta(key string, value interface{}) BeforeFunc {
return func(ctx *BeforeFuncCtx) error {
ctx.Meta[key] = value
Expand Down
2 changes: 1 addition & 1 deletion internal/namespaces/instance/v1/custom_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Test_ImageCreate(t *testing.T) {
t.Run("Use additional snapshots", core.Test(&core.TestConfig{
Commands: instance.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
core.ExecStoreBeforeCmd("Server", "scw instance server create type=DEV1-S image=ubuntu_focal root-volume=local:10GB additional-volumes.0=local:10GB -w"),
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"),
core.ExecStoreBeforeCmd("SnapshotA", `scw instance snapshot create -w name=cli-test-image-create-snapshotA volume-id={{ (index .Server.Volumes "0").ID }}`),
core.ExecStoreBeforeCmd("SnapshotB", `scw instance snapshot create -w name=cli-test-image-create-snapshotB volume-id={{ (index .Server.Volumes "1").ID }}`),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func Test_GetPlacementGroup(t *testing.T) {
t.Run("simple", core.Test(&core.TestConfig{
BeforeFunc: core.BeforeFuncCombine(
core.ExecStoreBeforeCmd("PlacementGroup", "scw instance placement-group create"),
core.ExecStoreBeforeCmd("ServerA", "scw instance server create type=DEV1-S image=ubuntu_jammy stopped=true placement-group-id={{ .PlacementGroup.PlacementGroup.ID }}"),
core.ExecStoreBeforeCmd("ServerA", "scw instance server create type=DEV1-S image=ubuntu_jammy ip=none stopped=true placement-group-id={{ .PlacementGroup.PlacementGroup.ID }}"),
),
Commands: instance.GetCommands(),
Cmd: "scw instance placement-group get {{ .PlacementGroup.PlacementGroup.ID }}",
Expand Down
7 changes: 6 additions & 1 deletion internal/namespaces/instance/v1/custom_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ func Test_ServerUpdateCustom(t *testing.T) {
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
assert.Equal(t, ctx.Meta["IP"].(*instanceSDK.IP).Address, ctx.Result.(*instanceSDK.UpdateServerResponse).Server.PublicIP.Address)
ip := core.GetFromMeta[*instanceSDK.IP](t, ctx, "IP")
resp := core.GetTestResult[*instanceSDK.UpdateServerResponse](t, ctx)

assert.NotNil(t, resp.Server)
assert.NotNil(t, resp.Server.PublicIP)
assert.Equal(t, ip.Address, resp.Server.PublicIP.Address)
},
core.TestCheckExitCode(0),
),
Expand Down
10 changes: 9 additions & 1 deletion internal/namespaces/instance/v1/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,22 @@ func createServerBionic(metaKey string) core.BeforeFunc {
return core.ExecStoreBeforeCmd(metaKey, testServerCommand("stopped=true image=ubuntu-bionic"))
}

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

// testServerCommand creates returns a create server command with the instance type and the given arguments
func testServerCommand(params string) string {
return "scw instance server create type=DEV1-S " + params
baseCommand := "scw instance server create type=DEV1-S "
if !strings.Contains(params, "ip=") {
baseCommand += "ip=none "
}

return baseCommand + params
}

// createServer creates a stopped ubuntu-bionic server and
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
ID 6d717fb0-83a8-4111-a322-2cb9ddd995cc
Name cli-img-frosty-neumann
ID 925aa80b-9410-4385-961d-3b258706effe
Name cli-img-nifty-maxwell
Arch x86_64
CreationDate few seconds ago
ModificationDate few seconds ago
ExtraVolumes 0
FromServer -
Organization ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b
Organization 105bdce1-64c0-48ab-899d-868455867ecf
Public false
RootVolume ffbf1933-5935-4300-9dda-8dc42c63df66
RootVolume ab31f24e-1e04-4c7e-818a-5fca7e14c704
State available
Project ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b
Project 105bdce1-64c0-48ab-899d-868455867ecf
Zone fr-par-1
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
{
"image": {
"id": "6d717fb0-83a8-4111-a322-2cb9ddd995cc",
"name": "cli-img-frosty-neumann",
"id": "925aa80b-9410-4385-961d-3b258706effe",
"name": "cli-img-nifty-maxwell",
"arch": "x86_64",
"creation_date": "1970-01-01T00:00:00.0Z",
"modification_date": "1970-01-01T00:00:00.0Z",
"default_bootscript": null,
"extra_volumes": {},
"from_server": "",
"organization": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b",
"organization": "105bdce1-64c0-48ab-899d-868455867ecf",
"public": false,
"root_volume": {
"id": "ffbf1933-5935-4300-9dda-8dc42c63df66",
"name": "cli-snp-focused-davinci",
"id": "ab31f24e-1e04-4c7e-818a-5fca7e14c704",
"name": "cli-snp-competent-keller",
"size": 20000000000,
"volume_type": "l_ssd"
},
"state": "available",
"project": "ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b",
"project": "105bdce1-64c0-48ab-899d-868455867ecf",
"tags": [],
"zone": "fr-par-1"
}
Expand Down
Loading
Loading