Skip to content

Commit

Permalink
[#118] Provide integration test for the CLI get command
Browse files Browse the repository at this point in the history
Added more tests

Signed-off-by: Daniel Milchev <fixed-term.daniel.milchev@bosch.io>
  • Loading branch information
daniel-milchev committed Mar 7, 2023
1 parent 470c916 commit d5367af
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
26 changes: 24 additions & 2 deletions integration/ctr_management_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//go:build integration

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"
Expand All @@ -48,8 +52,14 @@ func TestCtrMgrCLI(t *testing.T) {
testCases, err := GetAllTestCasesFromTestdataDir()
testutil.AssertNil(t, err)
for name, tc := range testCases {
// init fwork
framework := CLITest{
CustomResultFuncs: map[string]func(result icmd.Result, args ...string) assert.BoolOrComparison{
"ASSERT_JSON_CONTAINER": assertJSONContainer,
},
TestCases: tc}
t.Run(name, func(t *testing.T) {
RunCmdTestCases(t, tc)
framework.RunCmdTestCases(t)
})
}
}
Expand Down Expand Up @@ -80,3 +90,15 @@ func dumpTestdata() error {
}
return nil
}

// TODO - finish the unmarshal
func assertJSONContainer(result icmd.Result, args ...string) assert.BoolOrComparison {
if result.Stdout() == "" {
return errors.New("Empty string")
}
var container *types.Container
if err := json.Unmarshal([]byte(result.Stdout()), &container); err != nil {
return err
}
return false
}
20 changes: 14 additions & 6 deletions integration/framework/cli/cmd_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var customResultDefaultFns = map[string]func(result icmd.Result, args ...string)
"LOGS_JSON": logs,
}

type CLITest struct {
CustomResultFuncs map[string]func(result icmd.Result, args ...string) assert.BoolOrComparison
TestCases []TestCaseCMD
}

// TestCaseCMD represents a command and expected result
type TestCaseCMD struct {
name string
Expand Down Expand Up @@ -90,8 +95,8 @@ func GetAllTestCasesFromTestdataDir() (map[string][]TestCaseCMD, error) {
}

// RunCmdTestCases runs the provided test cases and asserts the result according to the provided parameters.
func RunCmdTestCases(t *testing.T, cmdList []TestCaseCMD) {
for _, cmd := range cmdList {
func (c *CLITest) RunCmdTestCases(t *testing.T) {
for _, cmd := range c.TestCases {
t.Run(cmd.name, func(t *testing.T) {
if cmd.setupCmd != nil {
runMultipleCommands(t, *cmd.setupCmd)
Expand All @@ -101,7 +106,7 @@ func RunCmdTestCases(t *testing.T, cmdList []TestCaseCMD) {
assert.Assert(t, golden.String(result.Stdout(), cmd.goldenFile))
}
if cmd.customResult != "" {
assertCustomResult(t, *result, cmd.customResult, cmd.customResultArgs...)
c.assertCustomResult(t, *result, cmd.customResult, cmd.customResultArgs...)
}
result.Assert(t, cmd.expected)
})
Expand Down Expand Up @@ -134,9 +139,12 @@ func buildCmd(binary string, args ...string) *icmd.Cmd {
return &cmd
}

func assertCustomResult(t *testing.T, result icmd.Result, name string, args ...string) {
f, ok := customResultDefaultFns[name]
assert.Equal(t, ok, true, fmt.Sprintf("function %s not found", name))
func (c *CLITest) assertCustomResult(t *testing.T, result icmd.Result, name string, args ...string) {
f, exist := c.CustomResultFuncs[name]
if !exist {
f, exist = customResultDefaultFns[name]
}
assert.Equal(t, exist, true, fmt.Sprintf("function %s not found", name))
assert.Assert(t, f(result, args...))
}

Expand Down
26 changes: 25 additions & 1 deletion integration/testdata/get-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ 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
Expand All @@ -20,4 +28,20 @@ command:
args: ["get", "-n", "invalid"]
expected:
exitCode: 1
err: "Error: The requested container with name = invalid was not found. Try using an ID instead."
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"]

0 comments on commit d5367af

Please sign in to comment.