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

refacto(testing): Use Args as a []string #867

Merged
merged 1 commit into from
Apr 15, 2020
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
52 changes: 34 additions & 18 deletions internal/core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -306,23 +319,26 @@ 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
}
}

// 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
}
}

// 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
}
}
Expand Down Expand Up @@ -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(""))
Expand Down
39 changes: 13 additions & 26 deletions internal/e2e/human_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions internal/e2e/sdk_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -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",
Expand Down
6 changes: 1 addition & 5 deletions internal/namespaces/instance/v1/custom_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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"),
),
}))
Expand Down
Loading