Skip to content

Commit

Permalink
[eclipse-kanto#118] Provide integration test for the CLI get command
Browse files Browse the repository at this point in the history
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 <fixed-term.daniel.milchev@bosch.io>
  • Loading branch information
daniel-milchev committed Mar 20, 2023
1 parent f4bc857 commit 5c8c584
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
20 changes: 20 additions & 0 deletions integration/ctr_management_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ 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"

"github.com/caarlos0/env/v6"
"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)

//go:embed testdata
Expand All @@ -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}))
Expand Down Expand Up @@ -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
}
17 changes: 13 additions & 4 deletions integration/framework/cli/cmd_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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...))
}
Expand Down
18 changes: 18 additions & 0 deletions integration/testdata/get-help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Get detailed information about a given container.

Usage:
kanto-cm get <container-id>

Examples:
get <container-id>
get --name <container-name>
get -n <container-name>

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)
47 changes: 47 additions & 0 deletions integration/testdata/get-test.yaml
Original file line number Diff line number Diff line change
@@ -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", "--host", "$KANTO_HOST"]
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", "--host", "$KANTO_HOST", "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", "--host", "$KANTO_HOST", "-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", "--host", "$KANTO_HOST", "-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"]

0 comments on commit 5c8c584

Please sign in to comment.