Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
kata-env: Fix test cases for kata-env JSON
Browse files Browse the repository at this point in the history
With the addition of the JSON kata-env output, we need
to fix up the tests:
 - add a test for the JSON flag
 - fix the format/layout of the other tests to take into
  account the change in function API and the additon of a
  flagset to the cmdline ctx.

Signed-off-by: Graham Whaley <[email protected]>
  • Loading branch information
Graham Whaley authored and Eric Ernst committed Aug 23, 2018
1 parent b21646a commit 89e22e5
Showing 1 changed file with 50 additions and 15 deletions.
65 changes: 50 additions & 15 deletions cli/kata-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,11 @@ func TestEnvHandleSettings(t *testing.T) {
_, err = getExpectedSettings(config, tmpdir, configFile)
assert.NoError(t, err)

m := map[string]interface{}{
app := cli.NewApp()
set := flag.NewFlagSet("test", flag.ContinueOnError)
ctx := cli.NewContext(app, set, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": configFile,
"runtimeConfig": config,
}
Expand All @@ -792,7 +796,7 @@ func TestEnvHandleSettings(t *testing.T) {
assert.NoError(t, err)
defer os.Remove(tmpfile.Name())

err = handleSettings(tmpfile, m)
err = handleSettings(tmpfile, ctx)
assert.NoError(t, err)

var env EnvInfo
Expand All @@ -816,7 +820,10 @@ func TestEnvHandleSettingsInvalidShimConfig(t *testing.T) {

config.ShimConfig = "invalid shim config"

m := map[string]interface{}{
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": configFile,
"runtimeConfig": config,
}
Expand All @@ -825,47 +832,75 @@ func TestEnvHandleSettingsInvalidShimConfig(t *testing.T) {
assert.NoError(err)
defer os.Remove(tmpfile.Name())

err = handleSettings(tmpfile, m)
err = handleSettings(tmpfile, ctx)
assert.Error(err)
}

func TestEnvHandleSettingsInvalidParams(t *testing.T) {
err := handleSettings(nil, map[string]interface{}{})
assert.Error(t, err)
assert := assert.New(t)

tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
defer os.RemoveAll(tmpdir)

configFile, _, err := makeRuntimeConfig(tmpdir)
assert.NoError(err)

app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": configFile,
}
err = handleSettings(nil, ctx)
assert.Error(err)
}

func TestEnvHandleSettingsEmptyMap(t *testing.T) {
err := handleSettings(os.Stdout, map[string]interface{}{})
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{}
err := handleSettings(os.Stdout, ctx)
assert.Error(t, err)
}

func TestEnvHandleSettingsInvalidFile(t *testing.T) {
m := map[string]interface{}{
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": "foo",
"runtimeConfig": oci.RuntimeConfig{},
}

err := handleSettings(nil, m)
err := handleSettings(nil, ctx)
assert.Error(t, err)
}

func TestEnvHandleSettingsInvalidConfigFileType(t *testing.T) {
m := map[string]interface{}{
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": 123,
"runtimeConfig": oci.RuntimeConfig{},
}

err := handleSettings(os.Stderr, m)
err := handleSettings(os.Stderr, ctx)
assert.Error(t, err)
}

func TestEnvHandleSettingsInvalidRuntimeConfigType(t *testing.T) {
m := map[string]interface{}{
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": "/some/where",
"runtimeConfig": true,
}

err := handleSettings(os.Stderr, m)
err := handleSettings(os.Stderr, ctx)
assert.Error(t, err)
}

Expand All @@ -883,7 +918,8 @@ func TestEnvCLIFunction(t *testing.T) {
assert.NoError(t, err)

app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
set := flag.NewFlagSet("test", flag.ContinueOnError)
ctx := cli.NewContext(app, set, nil)
app.Name = "foo"

ctx.App.Metadata = map[string]interface{}{
Expand All @@ -908,7 +944,6 @@ func TestEnvCLIFunction(t *testing.T) {
err = fn(ctx)
assert.NoError(t, err)

set := flag.NewFlagSet("", 0)
set.Bool("json", true, "")
ctx = cli.NewContext(app, set, nil)

Expand Down

0 comments on commit 89e22e5

Please sign in to comment.