From d5b855fd1243b61ce706840a1c3820d09bb8b46f Mon Sep 17 00:00:00 2001 From: Daniel Milchev Date: Tue, 7 Feb 2023 11:15:23 +0200 Subject: [PATCH] [#118] Provide integration test for the CLI get command Created golden file and test case scenarios, created an init function and an add fuction for the customResultFns map and unmarshaled the data. Signed-off-by: Daniel Milchev --- integration/ctr_management_cli_test.go | 20 +++++++++++ integration/framework/cli/cmd_base.go | 17 +++++++--- integration/testdata/get-help.golden | 18 ++++++++++ integration/testdata/get-test.yaml | 47 ++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 integration/testdata/get-help.golden create mode 100644 integration/testdata/get-test.yaml diff --git a/integration/ctr_management_cli_test.go b/integration/ctr_management_cli_test.go index 5df1b30..ee8a623 100644 --- a/integration/ctr_management_cli_test.go +++ b/integration/ctr_management_cli_test.go @@ -15,14 +15,19 @@ package integration import ( "embed" + "encoding/json" + "errors" "io/fs" "os" "path/filepath" "testing" + "github.com/eclipse-kanto/container-management/containerm/containers/types" "github.com/eclipse-kanto/container-management/containerm/pkg/testutil" "github.com/eclipse-kanto/container-management/containerm/util" . "github.com/eclipse-kanto/container-management/integration/framework/cli" + "gotest.tools/v3/assert" + "gotest.tools/v3/icmd" "github.com/caarlos0/env/v6" "github.com/stretchr/testify/require" @@ -35,6 +40,10 @@ type cliTestConfiguration struct { KantoHost string `env:"KANTO_HOST" envDefault:"/run/container-management/container-management.sock"` } +func init() { + AddCustomResult("ASSERT_JSON_CONTAINER", assertJSONContainer) +} + func TestCtrMgrCLI(t *testing.T) { cliTestConfiguration := &cliTestConfiguration{} require.NoError(t, env.Parse(cliTestConfiguration, env.Options{RequiredIfNoDef: true})) @@ -80,3 +89,14 @@ func dumpTestdata() error { } return nil } + +func assertJSONContainer(result icmd.Result, args ...string) assert.BoolOrComparison { + if result.Stdout() == "" { + return errors.New("stdout result is empty") + } + var container *types.Container + if err := json.Unmarshal([]byte(result.Stdout()), &container); err != nil { + return err + } + return true +} diff --git a/integration/framework/cli/cmd_base.go b/integration/framework/cli/cmd_base.go index 258f969..951fa18 100644 --- a/integration/framework/cli/cmd_base.go +++ b/integration/framework/cli/cmd_base.go @@ -27,15 +27,15 @@ import ( "gotest.tools/v3/icmd" ) -// TestData constant for the testdata directory +// TestData constant for the testdata directory. const TestData = "testdata" -var customResultDefaultFns = map[string]func(result icmd.Result, args ...string) assert.BoolOrComparison{ +var customResultFns = map[string]func(result icmd.Result, args ...string) assert.BoolOrComparison{ "REGEX": regex, "LOGS_JSON": logs, } -// TestCaseCMD represents a command and expected result +// TestCaseCMD represents a command and expected result. type TestCaseCMD struct { name string icmd icmd.Cmd @@ -47,6 +47,15 @@ type TestCaseCMD struct { onExit *[]icmd.Cmd } +// AddCustomResult adds custom result to customResultFns map or adds an error. +func AddCustomResult(name string, f func(result icmd.Result, args ...string) assert.BoolOrComparison) error { + if _, ok := customResultFns[name]; ok { + return fmt.Errorf("function with name %s already exist", name) + } + customResultFns[name] = f + return nil +} + // GetTestCaseFromYamlFile parses yaml file to TestCaseCMD array. func GetTestCaseFromYamlFile(filePath string) ([]TestCaseCMD, error) { f, err := os.Open(filePath) @@ -135,7 +144,7 @@ func buildCmd(binary string, args ...string) *icmd.Cmd { } func assertCustomResult(t *testing.T, result icmd.Result, name string, args ...string) { - f, ok := customResultDefaultFns[name] + f, ok := customResultFns[name] assert.Equal(t, ok, true, fmt.Sprintf("function %s not found", name)) assert.Assert(t, f(result, args...)) } diff --git a/integration/testdata/get-help.golden b/integration/testdata/get-help.golden new file mode 100644 index 0000000..0b0189e --- /dev/null +++ b/integration/testdata/get-help.golden @@ -0,0 +1,18 @@ +Get detailed information about a given container. + +Usage: + kanto-cm get + +Examples: +get + get --name + get -n + +Flags: + -h, --help help for get + -n, --name string Get the information about a container with a specific name. + +Global Flags: + --debug Switch commands log level to DEBUG mode + --host string Specify the address path to the Eclipse Kanto container management (default "/run/container-management/container-management.sock") + --timeout int Specify the connection timeout in seconds to the Eclipse Kanto container management (default 30) diff --git a/integration/testdata/get-test.yaml b/integration/testdata/get-test.yaml new file mode 100644 index 0000000..752cb66 --- /dev/null +++ b/integration/testdata/get-test.yaml @@ -0,0 +1,47 @@ +name: get_help +command: + binary: kanto-cm + args: ["get", "-h"] +expected: + exitCode: 0 +goldenFile: "get-help.golden" +--- +name: get_no_args +command: + binary: kanto-cm + args: ["get"] +expected: + exitCode: 1 + err: "Error: You must provide either an ID or a name for the container via --name (-n)" +--- +name: get_invalid_id +command: + binary: kanto-cm + args: ["get", "invalid"] +expected: + exitCode: 1 + err: "Error: The requested container with ID = invalid was not found" +--- +name: get_invalid_name +command: + binary: kanto-cm + args: ["get", "-n", "invalid"] +expected: + exitCode: 1 + err: "Error: The requested container with name = invalid was not found. Try using an ID instead." +--- +name: get_container +setupCmd: + - binary: kanto-cm + args: ["create", "--host", "$KANTO_HOST", "-n", "get_container", "docker.io/library/influxdb:1.8.4"] +command: + binary: kanto-cm + args: ["get", "-n", "get_container"] +expected: + exitCode: 0 +customResult: + type: ASSERT_JSON_CONTAINER + args: [] +onExit: + - binary: "kanto-cm" + args: ["remove", "--host", "$KANTO_HOST", "-n", "get_container", "-f"] \ No newline at end of file