From e5a36671cfcc1cdbf49b3a7e494006c77e206c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Wed, 15 Apr 2020 15:50:38 +0200 Subject: [PATCH] refacto(testing): Use Args as a []string Used to handle arguments with space in them --- internal/core/testing.go | 52 ++++++++++++------- internal/e2e/human_test.go | 39 +++++--------- internal/e2e/sdk_errors_test.go | 4 +- .../instance/v1/custom_image_test.go | 6 +-- .../instance/v1/custom_server_create_test.go | 35 ++++++------- .../instance/v1/custom_server_test.go | 8 +-- .../namespaces/instance/v1/helpers_test.go | 14 +++-- .../instance/v1/instance_cli_test.go | 10 +--- internal/namespaces/instance/v1/ip_test.go | 6 +-- internal/namespaces/k8s/v1/helpers_test.go | 13 ++--- 10 files changed, 87 insertions(+), 100 deletions(-) diff --git a/internal/core/testing.go b/internal/core/testing.go index 2c4a7542f6..7dc15e9307 100644 --- a/internal/core/testing.go +++ b/internal/core/testing.go @@ -61,14 +61,16 @@ type BeforeFunc func(ctx *BeforeFuncCtx) error type AfterFunc func(ctx *AfterFuncCtx) error type BeforeFuncCtx struct { + T *testing.T Client *scw.Client - ExecuteCmd func(cmd string) interface{} + ExecuteCmd func(args []string) interface{} Meta map[string]interface{} } type AfterFuncCtx struct { + T *testing.T Client *scw.Client - ExecuteCmd func(cmd string) interface{} + ExecuteCmd func(args []string) interface{} Meta map[string]interface{} CmdResult interface{} } @@ -92,8 +94,13 @@ type TestConfig struct { BeforeFunc BeforeFunc // The command line you want to test + // Conflict with Args Cmd string + // Args represents a program arguments and should be used, when you cannot Cmd because your arguments include space characters + // Conflict with Cmd + Args []string + // A list of check function that will be run on result. Check TestCheck @@ -194,18 +201,12 @@ func Test(config *TestConfig) func(t *testing.T) { meta := map[string]interface{}{} - cmdTemplate := func(cmd string) string { - cmdBuf := &bytes.Buffer{} - require.NoError(t, template.Must(template.New("cmd").Parse(cmd)).Execute(cmdBuf, meta)) - return cmdBuf.String() - } - - executeCmd := func(cmd string) interface{} { + executeCmd := func(args []string) interface{} { stdoutBuffer := &bytes.Buffer{} stderrBuffer := &bytes.Buffer{} - logger.Debugf("command: %s", cmdTemplate(cmd)) + logger.Debugf("command: %s", args) _, result, err := Bootstrap(&BootstrapConfig{ - Args: strings.Split(cmdTemplate(cmd), " "), + Args: args, Commands: config.Commands, BuildInfo: &config.BuildInfo, Stdout: stdoutBuffer, @@ -222,6 +223,7 @@ func Test(config *TestConfig) func(t *testing.T) { // Run config.BeforeFunc if config.BeforeFunc != nil { require.NoError(t, config.BeforeFunc(&BeforeFuncCtx{ + T: t, Client: client, ExecuteCmd: executeCmd, Meta: meta, @@ -232,13 +234,16 @@ func Test(config *TestConfig) func(t *testing.T) { var result interface{} var exitCode int var err error - + args := config.Args if config.Cmd != "" { + args = cmdToArgs(t, meta, config.Cmd) + } + if len(args) > 0 { stdout := &bytes.Buffer{} stderr := &bytes.Buffer{} - logger.Debugf("command: %s", cmdTemplate(config.Cmd)) + logger.Debugf("command: %s", args) exitCode, result, err = Bootstrap(&BootstrapConfig{ - Args: strings.Split(cmdTemplate(config.Cmd), " "), + Args: args, Commands: config.Commands, BuildInfo: &config.BuildInfo, Stdout: stdout, @@ -248,6 +253,7 @@ func Test(config *TestConfig) func(t *testing.T) { OverrideEnv: config.OverrideEnv, }) + meta["CmdResult"] = result config.Check(t, &CheckFuncCtx{ ExitCode: exitCode, Stdout: stdout.Bytes(), @@ -261,6 +267,7 @@ func Test(config *TestConfig) func(t *testing.T) { // Run config.AfterFunc if config.AfterFunc != nil { require.NoError(t, config.AfterFunc(&AfterFuncCtx{ + T: t, Client: client, ExecuteCmd: executeCmd, Meta: meta, @@ -270,6 +277,12 @@ func Test(config *TestConfig) func(t *testing.T) { } } +func cmdToArgs(t *testing.T, meta map[string]interface{}, s string) []string { + cmdBuf := &bytes.Buffer{} + require.NoError(t, template.Must(template.New("cmd").Parse(s)).Execute(cmdBuf, meta)) + return strings.Split(cmdBuf.String(), " ") +} + // BeforeFuncCombine combines multiple before functions into one. func BeforeFuncCombine(beforeFuncs ...BeforeFunc) BeforeFunc { return func(ctx *BeforeFuncCtx) error { @@ -306,7 +319,8 @@ func AfterFuncCombine(afterFuncs ...AfterFunc) AfterFunc { // in the context Meta at metaKey. func ExecStoreBeforeCmd(metaKey, cmd string) BeforeFunc { return func(ctx *BeforeFuncCtx) error { - ctx.Meta[metaKey] = ctx.ExecuteCmd(cmd) + args := cmdToArgs(ctx.T, ctx.Meta, cmd) + ctx.Meta[metaKey] = ctx.ExecuteCmd(args) return nil } } @@ -314,7 +328,8 @@ func ExecStoreBeforeCmd(metaKey, cmd string) BeforeFunc { // ExecBeforeCmd executes the given before command. func ExecBeforeCmd(cmd string) BeforeFunc { return func(ctx *BeforeFuncCtx) error { - ctx.ExecuteCmd(cmd) + args := cmdToArgs(ctx.T, ctx.Meta, cmd) + ctx.ExecuteCmd(args) return nil } } @@ -322,7 +337,8 @@ func ExecBeforeCmd(cmd string) BeforeFunc { // ExecAfterCmd executes the given before command. func ExecAfterCmd(cmd string) AfterFunc { return func(ctx *AfterFuncCtx) error { - ctx.ExecuteCmd(cmd) + args := cmdToArgs(ctx.T, ctx.Meta, cmd) + ctx.ExecuteCmd(args) return nil } } @@ -398,7 +414,7 @@ func testGolden(t *testing.T, goldenPath string, actual []byte) { if actualIsEmpty { assert.NotNil(t, err) } else { - require.NoError(t, err) + require.NoError(t, err, "expected to find golden file with %s", string(actual)) // Replace Windows return carriage. expected = bytes.ReplaceAll(expected, []byte("\r"), []byte("")) diff --git a/internal/e2e/human_test.go b/internal/e2e/human_test.go index fb5ecdc32f..97d1e45908 100644 --- a/internal/e2e/human_test.go +++ b/internal/e2e/human_test.go @@ -78,12 +78,11 @@ func TestHumanList(t *testing.T) { Commands: test.GetCommands(), UseE2EClient: true, DisableParallel: true, // because e2e client is used - BeforeFunc: func(ctx *core.BeforeFuncCtx) error { - ctx.ExecuteCmd("scw test human create") - ctx.ExecuteCmd("scw test human create") - ctx.ExecuteCmd("scw test human create") - return nil - }, + BeforeFunc: core.BeforeFuncCombine( + core.ExecBeforeCmd("scw test human create"), + core.ExecBeforeCmd("scw test human create"), + core.ExecBeforeCmd("scw test human create"), + ), Cmd: "scw test human list", Check: core.TestCheckCombine( core.TestCheckExitCode(0), @@ -106,11 +105,8 @@ func TestHumanUpdate(t *testing.T) { Commands: test.GetCommands(), UseE2EClient: true, DisableParallel: true, // because e2e client is used - BeforeFunc: func(ctx *core.BeforeFuncCtx) error { - ctx.ExecuteCmd("scw test human create height=170.5 shoe-size=35.1 altitude-in-meter=-12 altitude-in-millimeter=-12050 fingers-count=21 hair-count=9223372036854775808 is-happy=true eyes-color=amber organization-id=b3ba839a-dcf2-4b0a-ac81-fc32370052a0") - return nil - }, - Cmd: "scw test human update 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8 is-happy=false", + BeforeFunc: core.ExecBeforeCmd("scw test human create height=170.5 shoe-size=35.1 altitude-in-meter=-12 altitude-in-millimeter=-12050 fingers-count=21 hair-count=9223372036854775808 is-happy=true eyes-color=amber organization-id=b3ba839a-dcf2-4b0a-ac81-fc32370052a0"), + Cmd: "scw test human update 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8 is-happy=false", Check: core.TestCheckCombine( core.TestCheckExitCode(0), core.TestCheckGolden(), @@ -121,11 +117,8 @@ func TestHumanUpdate(t *testing.T) { Commands: test.GetCommands(), UseE2EClient: true, DisableParallel: true, // because e2e client is used - BeforeFunc: func(ctx *core.BeforeFuncCtx) error { - ctx.ExecuteCmd("scw test human create") - return nil - }, - Cmd: "scw test human update 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8 height=155.666 shoe-size=36.0 altitude-in-meter=2147483647 altitude-in-millimeter=2147483647285 fingers-count=20 hair-count=9223372036854775809 is-happy=true eyes-color=blue", + BeforeFunc: core.ExecBeforeCmd("scw test human create"), + Cmd: "scw test human update 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8 height=155.666 shoe-size=36.0 altitude-in-meter=2147483647 altitude-in-millimeter=2147483647285 fingers-count=20 hair-count=9223372036854775809 is-happy=true eyes-color=blue", Check: core.TestCheckCombine( core.TestCheckExitCode(0), core.TestCheckGolden(), @@ -147,11 +140,8 @@ func TestHumanGet(t *testing.T) { Commands: test.GetCommands(), UseE2EClient: true, DisableParallel: true, // because e2e client is used - BeforeFunc: func(ctx *core.BeforeFuncCtx) error { - ctx.ExecuteCmd("scw test human create height=155.666 shoe-size=36.0 altitude-in-meter=2147483647 altitude-in-millimeter=2147483647285 fingers-count=20 hair-count=9223372036854775809 is-happy=true eyes-color=blue organization-id=b3ba839a-dcf2-4b0a-ac81-fc32370052a0") - return nil - }, - Cmd: "scw test human get 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8", + BeforeFunc: core.ExecBeforeCmd("scw test human create height=155.666 shoe-size=36.0 altitude-in-meter=2147483647 altitude-in-millimeter=2147483647285 fingers-count=20 hair-count=9223372036854775809 is-happy=true eyes-color=blue organization-id=b3ba839a-dcf2-4b0a-ac81-fc32370052a0"), + Cmd: "scw test human get 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8", Check: core.TestCheckCombine( core.TestCheckExitCode(0), core.TestCheckGolden(), @@ -195,11 +185,8 @@ func TestHumanDelete(t *testing.T) { Commands: test.GetCommands(), UseE2EClient: true, DisableParallel: true, // because e2e client is used - BeforeFunc: func(ctx *core.BeforeFuncCtx) error { - ctx.ExecuteCmd("scw test human create height=155.666 shoe-size=36.0 altitude-in-meter=2147483647 altitude-in-millimeter=2147483647285 fingers-count=20 hair-count=9223372036854775809 is-happy=true eyes-color=blue organization-id=b3ba839a-dcf2-4b0a-ac81-fc32370052a0") - return nil - }, - Cmd: "scw test human delete 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8", + BeforeFunc: core.ExecBeforeCmd("scw test human create height=155.666 shoe-size=36.0 altitude-in-meter=2147483647 altitude-in-millimeter=2147483647285 fingers-count=20 hair-count=9223372036854775809 is-happy=true eyes-color=blue organization-id=b3ba839a-dcf2-4b0a-ac81-fc32370052a0"), + Cmd: "scw test human delete 0194fdc2-fa2f-fcc0-41d3-ff12045b73c8", Check: core.TestCheckCombine( core.TestCheckExitCode(0), core.TestCheckGolden(), diff --git a/internal/e2e/sdk_errors_test.go b/internal/e2e/sdk_errors_test.go index 1c670fce95..c4a898eef1 100644 --- a/internal/e2e/sdk_errors_test.go +++ b/internal/e2e/sdk_errors_test.go @@ -32,7 +32,7 @@ func TestSdkStandardErrors(t *testing.T) { DisableParallel: true, // because e2e client is used BeforeFunc: func(ctx *core.BeforeFuncCtx) error { for i := 0; i < 10; i++ { - ctx.ExecuteCmd("scw test human create") + ctx.ExecuteCmd([]string{"scw", "test", "human", "create"}) } return nil }, @@ -48,7 +48,7 @@ func TestSdkStandardErrors(t *testing.T) { UseE2EClient: true, DisableParallel: true, // because e2e client is used BeforeFunc: func(ctx *core.BeforeFuncCtx) error { - ctx.ExecuteCmd("scw test human create") + ctx.ExecuteCmd([]string{"scw", "test", "human", "create"}) api := sdktest.NewAPI(ctx.Client) _, err := api.RunHuman(&sdktest.RunHumanRequest{ HumanID: "0194fdc2-fa2f-fcc0-41d3-ff12045b73c8", diff --git a/internal/namespaces/instance/v1/custom_image_test.go b/internal/namespaces/instance/v1/custom_image_test.go index 54a0068cd2..9d490e4667 100644 --- a/internal/namespaces/instance/v1/custom_image_test.go +++ b/internal/namespaces/instance/v1/custom_image_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/scaleway/scaleway-cli/internal/core" - "github.com/scaleway/scaleway-sdk-go/api/instance/v1" ) func Test_ImageCreate(t *testing.T) { @@ -21,10 +20,7 @@ func Test_ImageCreate(t *testing.T) { ), AfterFunc: core.AfterFuncCombine( deleteServer("Server"), - func(ctx *core.AfterFuncCtx) error { - ctx.ExecuteCmd("scw instance image delete " + ctx.CmdResult.(*instance.CreateImageResponse).Image.ID) - return nil - }, + core.ExecAfterCmd("scw instance image delete {{ .CmdResult.Image.ID }}"), deleteSnapshot("Snapshot"), ), })) diff --git a/internal/namespaces/instance/v1/custom_server_create_test.go b/internal/namespaces/instance/v1/custom_server_create_test.go index 9cac532738..9cdd0547ee 100644 --- a/internal/namespaces/instance/v1/custom_server_create_test.go +++ b/internal/namespaces/instance/v1/custom_server_create_test.go @@ -10,9 +10,8 @@ import ( ) // deleteServerAfterFunc deletes the created server and its attached volumes and IPs. -func deleteServerAfterFunc(ctx *core.AfterFuncCtx) error { - ctx.ExecuteCmd("scw instance server delete " + ctx.CmdResult.(*instance.Server).ID + " with-volumes=all with-ip=true force-shutdown=true") - return nil +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. @@ -32,7 +31,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("GP1-XS", core.Test(&core.TestConfig{ @@ -44,7 +43,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("With name", core.Test(&core.TestConfig{ @@ -56,7 +55,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("With start", core.Test(&core.TestConfig{ @@ -68,7 +67,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("With bootscript", core.Test(&core.TestConfig{ @@ -80,7 +79,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("Image UUID", core.Test(&core.TestConfig{ @@ -92,7 +91,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("Tags", core.Test(&core.TestConfig{ @@ -105,7 +104,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) }) @@ -123,7 +122,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("valid double local volumes", core.Test(&core.TestConfig{ @@ -136,7 +135,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("valid additional block volumes", core.Test(&core.TestConfig{ @@ -150,7 +149,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) }) @@ -169,7 +168,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("run with dynamic IP", core.Test(&core.TestConfig{ @@ -183,7 +182,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("existing IP", core.Test(&core.TestConfig{ @@ -197,7 +196,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("existing IP ID", core.Test(&core.TestConfig{ @@ -211,7 +210,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) t.Run("with ipv6", core.Test(&core.TestConfig{ @@ -223,7 +222,7 @@ func Test_CreateServer(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: deleteServerAfterFunc, + AfterFunc: deleteServerAfterFunc(), })) }) diff --git a/internal/namespaces/instance/v1/custom_server_test.go b/internal/namespaces/instance/v1/custom_server_test.go index 751e8cab39..563e76aca3 100644 --- a/internal/namespaces/instance/v1/custom_server_test.go +++ b/internal/namespaces/instance/v1/custom_server_test.go @@ -71,10 +71,10 @@ func Test_ServerVolumeUpdate(t *testing.T) { assert.Nil(t, ctx.Result.(*instance.DetachVolumeResponse).Server.Volumes["1"]) assert.Equal(t, 1, len(ctx.Result.(*instance.DetachVolumeResponse).Server.Volumes)) }, - AfterFunc: func(ctx *core.AfterFuncCtx) error { - ctx.ExecuteCmd(`scw instance volume delete {{ (index .Server.Volumes "1").ID }}`) - return deleteServer("Server")(ctx) - }, + AfterFunc: core.AfterFuncCombine( + core.ExecAfterCmd(`scw instance volume delete {{ (index .Server.Volumes "1").ID }}`), + deleteServer("Server"), + ), DisableParallel: true, })) diff --git a/internal/namespaces/instance/v1/helpers_test.go b/internal/namespaces/instance/v1/helpers_test.go index 2a1f8003d8..8e6286a24a 100644 --- a/internal/namespaces/instance/v1/helpers_test.go +++ b/internal/namespaces/instance/v1/helpers_test.go @@ -2,6 +2,7 @@ package instance import ( "fmt" + "strings" "github.com/scaleway/scaleway-cli/internal/core" "github.com/scaleway/scaleway-sdk-go/api/instance/v1" @@ -32,7 +33,7 @@ func deleteServer(metaKey string) core.AfterFunc { func createVolume(metaKey string, sizeInGb int, volumeType instance.VolumeType) core.BeforeFunc { return func(ctx *core.BeforeFuncCtx) error { cmd := fmt.Sprintf("scw instance volume create name=cli-test size=%dGB volume-type=%s", sizeInGb, volumeType) - res := ctx.ExecuteCmd(cmd) + res := ctx.ExecuteCmd(strings.Split(cmd, " ")) createVolumeResponse := res.(*instance.CreateVolumeResponse) ctx.Meta[metaKey] = createVolumeResponse.Volume return nil @@ -51,7 +52,7 @@ func deleteVolume(metaKey string) core.AfterFunc { // createIP creates an IP and register it in the context Meta at metaKey. func createIP(metaKey string) core.BeforeFunc { return func(ctx *core.BeforeFuncCtx) error { - res := ctx.ExecuteCmd("scw instance ip create") + res := ctx.ExecuteCmd(strings.Split("scw instance ip create", " ")) createIPResponse := res.(*instance.CreateIPResponse) ctx.Meta[metaKey] = createIPResponse.IP return nil @@ -60,10 +61,7 @@ func createIP(metaKey string) core.BeforeFunc { // deleteIP deletes an IP previously registered in the context Meta at metaKey. func deleteIP(metaKey string) core.AfterFunc { - return func(ctx *core.AfterFuncCtx) error { - ctx.ExecuteCmd("scw instance ip delete {{ ." + metaKey + ".Address }}") - return nil - } + return core.ExecAfterCmd("scw instance ip delete {{ ." + metaKey + ".Address }}") } // @@ -74,7 +72,7 @@ func deleteIP(metaKey string) core.AfterFunc { // register it in the context Meta at metaKey. func createPlacementGroup(metaKey string) core.BeforeFunc { return func(ctx *core.BeforeFuncCtx) error { - res := ctx.ExecuteCmd("scw instance placement-group create") + res := ctx.ExecuteCmd([]string{"scw", "instance", "placement-group", "create"}) createPlacementGroupResponse := res.(*instance.CreatePlacementGroupResponse) ctx.Meta[metaKey] = createPlacementGroupResponse.PlacementGroup return nil @@ -95,7 +93,7 @@ func deletePlacementGroup(metaKey string) core.AfterFunc { // register it in the context Meta at metaKey. func createSecurityGroup(metaKey string) core.BeforeFunc { return func(ctx *core.BeforeFuncCtx) error { - res := ctx.ExecuteCmd("scw instance security-group create") + res := ctx.ExecuteCmd([]string{"scw", "instance", "security-group", "create"}) createSecurityGroupResponse := res.(*instance.CreateSecurityGroupResponse) ctx.Meta[metaKey] = createSecurityGroupResponse.SecurityGroup return nil diff --git a/internal/namespaces/instance/v1/instance_cli_test.go b/internal/namespaces/instance/v1/instance_cli_test.go index ce2191f40c..79e0b92ce3 100644 --- a/internal/namespaces/instance/v1/instance_cli_test.go +++ b/internal/namespaces/instance/v1/instance_cli_test.go @@ -55,10 +55,7 @@ func Test_CreateVolume(t *testing.T) { }, core.TestCheckExitCode(0), ), - AfterFunc: func(ctx *core.AfterFuncCtx) error { - ctx.ExecuteCmd("scw instance volume delete " + ctx.CmdResult.(*instance.CreateVolumeResponse).Volume.ID) - return nil - }, + AfterFunc: core.ExecAfterCmd("scw instance volume delete {{ .CmdResult.Volume.ID }}"), })) t.Run("Bad size unit", core.Test(&core.TestConfig{ @@ -224,10 +221,7 @@ func Test_SnapshotCreate(t *testing.T) { core.TestCheckExitCode(0), ), AfterFunc: core.AfterFuncCombine( - func(ctx *core.AfterFuncCtx) error { - ctx.ExecuteCmd("scw instance snapshot delete " + ctx.CmdResult.(*instance.CreateSnapshotResponse).Snapshot.ID) - return nil - }, + core.ExecAfterCmd("scw instance snapshot delete {{ .CmdResult.Snapshot.ID }}"), deleteServer("Server"), ), })) diff --git a/internal/namespaces/instance/v1/ip_test.go b/internal/namespaces/instance/v1/ip_test.go index 12a9e626de..d2316424d9 100644 --- a/internal/namespaces/instance/v1/ip_test.go +++ b/internal/namespaces/instance/v1/ip_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/scaleway/scaleway-cli/internal/core" - "github.com/scaleway/scaleway-sdk-go/api/instance/v1" ) func Test_IpCreate(t *testing.T) { @@ -15,10 +14,7 @@ func Test_IpCreate(t *testing.T) { core.TestCheckGolden(), core.TestCheckExitCode(0), ), - AfterFunc: func(ctx *core.AfterFuncCtx) error { - ctx.ExecuteCmd("scw instance ip delete " + ctx.CmdResult.(*instance.CreateIPResponse).IP.ID) - return nil - }, + AfterFunc: core.ExecAfterCmd("scw instance ip delete {{ .CmdResult.IP.ID }}"), })) } diff --git a/internal/namespaces/k8s/v1/helpers_test.go b/internal/namespaces/k8s/v1/helpers_test.go index 4a0887b4bb..fb080c5606 100644 --- a/internal/namespaces/k8s/v1/helpers_test.go +++ b/internal/namespaces/k8s/v1/helpers_test.go @@ -3,6 +3,7 @@ package k8s import ( "fmt" "io/ioutil" + "strings" "github.com/scaleway/scaleway-cli/internal/core" k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1" @@ -27,7 +28,7 @@ const ( func createClusterAndWaitAndKubeconfig(metaKey string, kubeconfigMetaKey string, version string) core.BeforeFunc { return func(ctx *core.BeforeFuncCtx) error { cmd := fmt.Sprintf("scw k8s cluster create name=cli-test version=%s cni=cilium pools.0.node-type=DEV1-M pools.0.size=1 pools.0.name=default --wait", version) - res := ctx.ExecuteCmd(cmd) + res := ctx.ExecuteCmd(strings.Split(cmd, " ")) cluster := res.(*k8s.Cluster) ctx.Meta[metaKey] = cluster api := k8s.NewAPI(ctx.Client) @@ -48,7 +49,7 @@ func createClusterAndWaitAndKubeconfig(metaKey string, kubeconfigMetaKey string, func createClusterAndWaitAndInstallKubeconfig(metaKey string, kubeconfigMetaKey string, version string) core.BeforeFunc { return func(ctx *core.BeforeFuncCtx) error { cmd := fmt.Sprintf("scw k8s cluster create name=cli-test version=%s cni=cilium pools.0.node-type=DEV1-M pools.0.size=1 pools.0.name=default --wait", version) - res := ctx.ExecuteCmd(cmd) + res := ctx.ExecuteCmd(strings.Split(cmd, " ")) cluster := res.(*k8s.Cluster) ctx.Meta[metaKey] = cluster api := k8s.NewAPI(ctx.Client) @@ -61,7 +62,7 @@ func createClusterAndWaitAndInstallKubeconfig(metaKey string, kubeconfigMetaKey } ctx.Meta[kubeconfigMetaKey] = kubeconfig cmd = fmt.Sprintf("scw k8s kubeconfig install %s", cluster.ID) - _ = ctx.ExecuteCmd(cmd) + _ = ctx.ExecuteCmd(strings.Split(cmd, " ")) return nil } } @@ -71,7 +72,7 @@ func createClusterAndWaitAndInstallKubeconfig(metaKey string, kubeconfigMetaKey func createClusterAndWaitAndKubeconfigAndPopulateFile(metaKey string, kubeconfigMetaKey string, version string, file string, content []byte) core.BeforeFunc { return func(ctx *core.BeforeFuncCtx) error { cmd := fmt.Sprintf("scw k8s cluster create name=cli-test version=%s cni=cilium pools.0.node-type=DEV1-M pools.0.size=1 pools.0.name=default --wait", version) - res := ctx.ExecuteCmd(cmd) + res := ctx.ExecuteCmd(strings.Split(cmd, " ")) cluster := res.(*k8s.Cluster) ctx.Meta[metaKey] = cluster api := k8s.NewAPI(ctx.Client) @@ -93,7 +94,7 @@ func createClusterAndWaitAndKubeconfigAndPopulateFile(metaKey string, kubeconfig func createClusterAndWaitAndKubeconfigAndPopulateFileAndInstall(metaKey string, kubeconfigMetaKey string, version string, file string, content []byte) core.BeforeFunc { return func(ctx *core.BeforeFuncCtx) error { cmd := fmt.Sprintf("scw k8s cluster create name=cli-test version=%s cni=cilium pools.0.node-type=DEV1-M pools.0.size=1 pools.0.name=default --wait", version) - res := ctx.ExecuteCmd(cmd) + res := ctx.ExecuteCmd(strings.Split(cmd, " ")) cluster := res.(*k8s.Cluster) ctx.Meta[metaKey] = cluster api := k8s.NewAPI(ctx.Client) @@ -110,7 +111,7 @@ func createClusterAndWaitAndKubeconfigAndPopulateFileAndInstall(metaKey string, return err } cmd = fmt.Sprintf("scw k8s kubeconfig install %s", cluster.ID) - _ = ctx.ExecuteCmd(cmd) + _ = ctx.ExecuteCmd(strings.Split(cmd, " ")) return nil }