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

Provide integration test for the CLI get command #129

Merged
merged 2 commits into from
Mar 20, 2023
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
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 returns 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"]