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
Removed the new CLITest struct, reverted some changes, created an init func and made a setter for customResultFns map

Signed-off-by: Daniel Milchev <fixed-term.daniel.milchev@bosch.io>
  • Loading branch information
daniel-milchev committed Mar 20, 2023
1 parent d5367af commit 8c8c8fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
17 changes: 7 additions & 10 deletions integration/ctr_management_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type cliTestConfiguration struct {
KantoHost string `env:"KANTO_HOST" envDefault:"/run/container-management/container-management.sock"`
}

func init() {
SetCustomResult("ASSERT_JSON_CONTAINER", assertJSONContainer)
}

func TestCtrMgrCLI(t *testing.T) {
cliTestConfiguration := &cliTestConfiguration{}
require.NoError(t, env.Parse(cliTestConfiguration, env.Options{RequiredIfNoDef: true}))
Expand All @@ -52,14 +56,8 @@ 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) {
framework.RunCmdTestCases(t)
RunCmdTestCases(t, tc)
})
}
}
Expand Down Expand Up @@ -91,14 +89,13 @@ 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")
return errors.New("stdout result is empty")
}
var container *types.Container
if err := json.Unmarshal([]byte(result.Stdout()), &container); err != nil {
return err
}
return false
return true
}
31 changes: 16 additions & 15 deletions integration/framework/cli/cmd_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,11 @@ import (
// 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,
}

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 All @@ -52,6 +47,15 @@ type TestCaseCMD struct {
onExit *[]icmd.Cmd
}

// Setter
func SetCustomResult(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 @@ -95,8 +99,8 @@ func GetAllTestCasesFromTestdataDir() (map[string][]TestCaseCMD, error) {
}

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

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))
func assertCustomResult(t *testing.T, result icmd.Result, name string, args ...string) {
f, ok := customResultFns[name]
assert.Equal(t, ok, true, fmt.Sprintf("function %s not found", name))
assert.Assert(t, f(result, args...))
}

Expand Down

0 comments on commit 8c8c8fa

Please sign in to comment.