tests: Add cli.Context helper functions

Created two new helper functions to create a `cli.Context` with and without a
`cli.App`.

Calling these functions simplifies a lot of test code.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
James O. D. Hunt 2018-08-09 14:15:07 +01:00
parent 41d1c14c68
commit 0ede467256
19 changed files with 147 additions and 214 deletions

View File

@ -206,12 +206,9 @@ func TestCreatePIDFileUnableToCreate(t *testing.T) {
func TestCreateCLIFunctionNoRuntimeConfig(t *testing.T) {
assert := assert.New(t)
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"foo": "bar",
}
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
ctx.App.Metadata["foo"] = "bar"
fn, ok := createCLICommand.Action.(func(context *cli.Context) error)
assert.True(ok)
@ -241,13 +238,10 @@ func TestCreateCLIFunctionSetupConsoleFail(t *testing.T) {
set.String("console-socket", consoleSocketPath, "")
app := cli.NewApp()
ctx := cli.NewContext(app, set, nil)
app.Name = "foo"
ctx := createCLIContext(set)
ctx.App.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"runtimeConfig": runtimeConfig,
}
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
fn, ok := createCLICommand.Action.(func(context *cli.Context) error)
assert.True(ok)
@ -272,13 +266,10 @@ func TestCreateCLIFunctionCreateFail(t *testing.T) {
set.String("console-socket", "", "")
app := cli.NewApp()
ctx := cli.NewContext(app, set, nil)
app.Name = "foo"
ctx := createCLIContext(set)
ctx.App.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"runtimeConfig": runtimeConfig,
}
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
fn, ok := createCLICommand.Action.(func(context *cli.Context) error)
assert.True(ok)

View File

@ -489,9 +489,7 @@ func TestDeleteCLIFunction(t *testing.T) {
assert := assert.New(t)
flagSet := &flag.FlagSet{}
app := cli.NewApp()
ctx := cli.NewContext(app, flagSet, nil)
ctx := createCLIContext(flagSet)
fn, ok := deleteCLICommand.Action.(func(context *cli.Context) error)
assert.True(ok)
@ -507,7 +505,7 @@ func TestDeleteCLIFunction(t *testing.T) {
flagSet = flag.NewFlagSet("container-id", flag.ContinueOnError)
flagSet.Parse([]string{"xyz"})
ctx = cli.NewContext(app, flagSet, nil)
ctx = createCLIContext(flagSet)
err = fn(ctx)
assert.Error(err)
@ -574,9 +572,7 @@ func TestDeleteCLIFunctionSuccess(t *testing.T) {
}()
flagSet := &flag.FlagSet{}
app := cli.NewApp()
ctx := cli.NewContext(app, flagSet, nil)
ctx := createCLIContext(flagSet)
fn, ok := deleteCLICommand.Action.(func(context *cli.Context) error)
assert.True(ok)
@ -587,7 +583,7 @@ func TestDeleteCLIFunctionSuccess(t *testing.T) {
flagSet = flag.NewFlagSet("container-id", flag.ContinueOnError)
flagSet.Parse([]string{sandbox.ID()})
ctx = cli.NewContext(app, flagSet, nil)
ctx = createCLIContext(flagSet)
assert.NotNil(ctx)
err = fn(ctx)

View File

@ -28,7 +28,7 @@ func TestEventsCliAction(t *testing.T) {
flagSet := flag.NewFlagSet("events", flag.ContinueOnError)
// create a new fake context
ctx := cli.NewContext(&cli.App{}, flagSet, nil)
ctx := createCLIContext(flagSet)
err := actionFunc(ctx)
assert.Error(err, "Missing container ID")
@ -38,7 +38,7 @@ func TestEventsCLIFailure(t *testing.T) {
assert := assert.New(t)
flagSet := flag.NewFlagSet("events", flag.ContinueOnError)
ctx := cli.NewContext(&cli.App{}, flagSet, nil)
ctx := createCLIContext(flagSet)
actionFunc, ok := eventsCLICommand.Action.(func(ctx *cli.Context) error)
assert.True(ok)
@ -50,7 +50,7 @@ func TestEventsCLIFailure(t *testing.T) {
// interval is negative
flagSet.Parse([]string{testContainerID})
flagSet.Duration("interval", (-1)*time.Second, "")
ctx = cli.NewContext(&cli.App{}, flagSet, nil)
ctx = createCLIContext(flagSet)
err = actionFunc(ctx)
assert.Error(err)
@ -58,7 +58,7 @@ func TestEventsCLIFailure(t *testing.T) {
flagSet = flag.NewFlagSet("events", flag.ContinueOnError)
flagSet.Parse([]string{testContainerID})
flagSet.Duration("interval", 0*time.Second, "")
ctx = cli.NewContext(&cli.App{}, flagSet, nil)
ctx = createCLIContext(flagSet)
err = actionFunc(ctx)
assert.Error(err)
@ -136,7 +136,7 @@ func TestEventsCLISuccessful(t *testing.T) {
flagSet.Parse([]string{testContainerID})
flagSet.Duration("interval", 5*time.Second, "")
flagSet.Bool("stats", true, "")
ctx := cli.NewContext(&cli.App{}, flagSet, nil)
ctx := createCLIContext(flagSet)
err = actionFunc(ctx)
assert.NoError(err)
}

View File

@ -25,8 +25,7 @@ func TestExecCLIFunction(t *testing.T) {
assert := assert.New(t)
flagSet := &flag.FlagSet{}
app := cli.NewApp()
ctx := cli.NewContext(app, flagSet, nil)
ctx := createCLIContext(flagSet)
fn, ok := startCLICommand.Action.(func(context *cli.Context) error)
assert.True(ok)
@ -43,7 +42,7 @@ func TestExecCLIFunction(t *testing.T) {
// pass container-id
flagSet = flag.NewFlagSet("container-id", flag.ContinueOnError)
flagSet.Parse([]string{"xyz"})
ctx = cli.NewContext(app, flagSet, nil)
ctx = createCLIContext(flagSet)
err = fn(ctx)
assert.Error(err)
@ -54,7 +53,7 @@ func TestExecuteErrors(t *testing.T) {
assert := assert.New(t)
flagSet := flag.NewFlagSet("", 0)
ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
ctx := createCLIContext(flagSet)
// missing container id
err := execute(ctx)
@ -146,7 +145,7 @@ func TestExecuteErrorReadingProcessJson(t *testing.T) {
flagSet := flag.NewFlagSet("", 0)
flagSet.String("process", processPath, "")
flagSet.Parse([]string{testContainerID})
ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
ctx := createCLIContext(flagSet)
rootPath, configPath := testConfigSetup(t)
defer os.RemoveAll(rootPath)
@ -195,7 +194,7 @@ func TestExecuteErrorOpeningConsole(t *testing.T) {
flagSet := flag.NewFlagSet("", 0)
flagSet.String("console-socket", consoleSock, "")
flagSet.Parse([]string{testContainerID})
ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
ctx := createCLIContext(flagSet)
rootPath, configPath := testConfigSetup(t)
defer os.RemoveAll(rootPath)
@ -262,7 +261,7 @@ func TestExecuteWithFlags(t *testing.T) {
flagSet.Bool("no-new-privs", false, "")
flagSet.Parse([]string{testContainerID, "/tmp/foo"})
ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
ctx := createCLIContext(flagSet)
rootPath, configPath := testConfigSetup(t)
defer os.RemoveAll(rootPath)
@ -351,7 +350,7 @@ func TestExecuteWithFlagsDetached(t *testing.T) {
flagSet := testExecParamsSetup(t, pidFilePath, consolePath, detach)
flagSet.Parse([]string{testContainerID, "/tmp/foo"})
ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
ctx := createCLIContext(flagSet)
rootPath, configPath := testConfigSetup(t)
defer os.RemoveAll(rootPath)
@ -430,7 +429,7 @@ func TestExecuteWithInvalidProcessJson(t *testing.T) {
defer os.Remove(processPath)
flagSet.Parse([]string{testContainerID})
ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
ctx := createCLIContext(flagSet)
rootPath, configPath := testConfigSetup(t)
defer os.RemoveAll(rootPath)
@ -482,7 +481,7 @@ func TestExecuteWithValidProcessJson(t *testing.T) {
flagSet.String("process", processPath, "")
flagSet.Parse([]string{testContainerID, "/tmp/foo"})
ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
ctx := createCLIContext(flagSet)
rootPath, configPath := testConfigSetup(t)
defer os.RemoveAll(rootPath)
@ -583,7 +582,7 @@ func TestExecuteWithEmptyEnvironmentValue(t *testing.T) {
flagSet.String("process", processPath, "")
flagSet.Parse([]string{testContainerID})
ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
ctx := createCLIContext(flagSet)
rootPath, configPath := testConfigSetup(t)
defer os.RemoveAll(rootPath)
@ -695,7 +694,7 @@ func TestGenerateExecParams(t *testing.T) {
flagSet.String("cwd", cwd, "")
flagSet.String("apparmor", apparmor, "")
ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
ctx := createCLIContext(flagSet)
process := &oci.CompatOCIProcess{}
params, err := generateExecParams(ctx, process)
assert.NoError(err)
@ -738,7 +737,7 @@ func TestGenerateExecParamsWithProcessJsonFile(t *testing.T) {
flagSet.String("process", processPath, "")
flagSet.Parse([]string{testContainerID})
ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
ctx := createCLIContext(flagSet)
processJSON := `{
"consoleSize": {

View File

@ -20,12 +20,9 @@ import (
func TestFactoryCLIFunctionNoRuntimeConfig(t *testing.T) {
assert := assert.New(t)
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"foo": "bar",
}
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
ctx.App.Metadata["foo"] = "bar"
fn, ok := initFactoryCommand.Action.(func(context *cli.Context) error)
assert.True(ok)
@ -54,14 +51,12 @@ func TestFactoryCLIFunctionInit(t *testing.T) {
set.String("console-socket", "", "")
app := cli.NewApp()
ctx := cli.NewContext(app, set, nil)
app.Name = "foo"
ctx := createCLIContext(set)
ctx.App.Name = "foo"
// No template
ctx.App.Metadata = map[string]interface{}{
"runtimeConfig": runtimeConfig,
}
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
fn, ok := initFactoryCommand.Action.(func(context *cli.Context) error)
assert.True(ok)
err = fn(ctx)
@ -92,14 +87,11 @@ func TestFactoryCLIFunctionDestroy(t *testing.T) {
set.String("console-socket", "", "")
app := cli.NewApp()
ctx := cli.NewContext(app, set, nil)
app.Name = "foo"
ctx := createCLIContext(set)
ctx.App.Name = "foo"
// No template
ctx.App.Metadata = map[string]interface{}{
"runtimeConfig": runtimeConfig,
}
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
fn, ok := destroyFactoryCommand.Action.(func(context *cli.Context) error)
assert.True(ok)
err = fn(ctx)
@ -130,14 +122,12 @@ func TestFactoryCLIFunctionStatus(t *testing.T) {
set.String("console-socket", "", "")
app := cli.NewApp()
ctx := cli.NewContext(app, set, nil)
app.Name = "foo"
ctx := createCLIContext(set)
ctx.App.Name = "foo"
// No template
ctx.App.Metadata = map[string]interface{}{
"runtimeConfig": runtimeConfig,
}
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
fn, ok := statusFactoryCommand.Action.(func(context *cli.Context) error)
assert.True(ok)
err = fn(ctx)

View File

@ -110,9 +110,8 @@ func TestCCCheckCLIFunction(t *testing.T) {
setupCheckHostIsVMContainerCapable(assert, cpuInfoFile, cpuData, moduleData)
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
// create buffer to save logger output
buf := &bytes.Buffer{}

View File

@ -76,9 +76,8 @@ func TestCCCheckCLIFunction(t *testing.T) {
setupCheckHostIsVMContainerCapable(assert, cpuInfoFile, moduleData)
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
// create buffer to save logger output
buf := &bytes.Buffer{}

View File

@ -95,9 +95,8 @@ func TestCCCheckCLIFunction(t *testing.T) {
setupCheckHostIsVMContainerCapable(assert, cpuInfoFile, cpuData, moduleData)
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
// create buffer to save logger output
buf := &bytes.Buffer{}

View File

@ -681,9 +681,8 @@ func TestCheckCLIFunctionFail(t *testing.T) {
procCPUInfo = oldProcCPUInfo
}()
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
fn, ok := kataCheckCLICommand.Action.(func(context *cli.Context) error)
assert.True(ok)

View File

@ -868,14 +868,11 @@ func TestEnvHandleSettings(t *testing.T) {
_, err = getExpectedSettings(config, tmpdir, configFile)
assert.NoError(t, err)
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,
}
ctx := createCLIContext(set)
ctx.App.Name = "foo"
ctx.App.Metadata["configFile"] = configFile
ctx.App.Metadata["runtimeConfig"] = config
tmpfile, err := ioutil.TempFile("", "")
assert.NoError(t, err)
@ -905,13 +902,10 @@ func TestEnvHandleSettingsInvalidShimConfig(t *testing.T) {
config.ShimConfig = "invalid shim config"
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": configFile,
"runtimeConfig": config,
}
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
ctx.App.Metadata["configFile"] = configFile
ctx.App.Metadata["runtimeConfig"] = config
tmpfile, err := ioutil.TempFile("", "")
assert.NoError(err)
@ -931,59 +925,47 @@ func TestEnvHandleSettingsInvalidParams(t *testing.T) {
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,
}
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
ctx.App.Metadata["configFile"] = configFile
err = handleSettings(nil, ctx)
assert.Error(err)
}
func TestEnvHandleSettingsEmptyMap(t *testing.T) {
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
ctx.App.Metadata = map[string]interface{}{}
err := handleSettings(os.Stdout, ctx)
assert.Error(t, err)
}
func TestEnvHandleSettingsInvalidFile(t *testing.T) {
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": "foo",
"runtimeConfig": oci.RuntimeConfig{},
}
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
ctx.App.Metadata["configFile"] = "foo"
ctx.App.Metadata["runtimeConfig"] = oci.RuntimeConfig{}
err := handleSettings(nil, ctx)
assert.Error(t, err)
}
func TestEnvHandleSettingsInvalidConfigFileType(t *testing.T) {
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": 123,
"runtimeConfig": oci.RuntimeConfig{},
}
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
ctx.App.Metadata["configFile"] = 123
ctx.App.Metadata["runtimeConfig"] = oci.RuntimeConfig{}
err := handleSettings(os.Stderr, ctx)
assert.Error(t, err)
}
func TestEnvHandleSettingsInvalidRuntimeConfigType(t *testing.T) {
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": "/some/where",
"runtimeConfig": true,
}
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
ctx.App.Metadata["configFile"] = "/some/where"
ctx.App.Metadata["runtimeConfig"] = true
err := handleSettings(os.Stderr, ctx)
assert.Error(t, err)
@ -1004,13 +986,11 @@ func TestEnvCLIFunction(t *testing.T) {
app := cli.NewApp()
set := flag.NewFlagSet("test", flag.ContinueOnError)
ctx := cli.NewContext(app, set, nil)
ctx := createCLIContextWithApp(set, app)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": configFile,
"runtimeConfig": config,
}
ctx.App.Metadata["configFile"] = configFile
ctx.App.Metadata["runtimeConfig"] = config
fn, ok := kataEnvCLICommand.Action.(func(context *cli.Context) error)
assert.True(t, ok)
@ -1030,7 +1010,7 @@ func TestEnvCLIFunction(t *testing.T) {
assert.NoError(t, err)
set.Bool("json", true, "")
ctx = cli.NewContext(app, set, nil)
ctx = createCLIContextWithApp(set, app)
err = fn(ctx)
assert.NoError(t, err)
@ -1049,14 +1029,11 @@ func TestEnvCLIFunctionFail(t *testing.T) {
_, err = getExpectedSettings(config, tmpdir, configFile)
assert.NoError(t, err)
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"configFile": configFile,
"runtimeConfig": config,
}
ctx.App.Metadata["configFile"] = configFile
ctx.App.Metadata["runtimeConfig"] = config
fn, ok := kataEnvCLICommand.Action.(func(context *cli.Context) error)
assert.True(t, ok)

View File

@ -350,12 +350,9 @@ func TestStateToJSON(t *testing.T) {
}
func TestListCLIFunctionNoContainers(t *testing.T) {
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"foo": "bar",
}
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
ctx.App.Metadata["foo"] = "bar"
fn, ok := listCLICommand.Action.(func(context *cli.Context) error)
assert.True(t, ok)
@ -373,9 +370,8 @@ func TestListGetContainersListSandboxFail(t *testing.T) {
assert.NoError(err)
defer os.RemoveAll(tmpdir)
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
assert.NoError(err)
@ -405,9 +401,8 @@ func TestListGetContainers(t *testing.T) {
assert.NoError(err)
defer os.RemoveAll(tmpdir)
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
assert.NoError(err)
@ -445,9 +440,8 @@ func TestListGetContainersSandboxWithoutContainers(t *testing.T) {
assert.NoError(err)
defer os.RemoveAll(tmpdir)
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
assert.NoError(err)
@ -495,16 +489,13 @@ func TestListGetContainersSandboxWithContainer(t *testing.T) {
testingImpl.ListSandboxFunc = nil
}()
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = "foo"
ctx := createCLIContext(nil)
ctx.App.Name = "foo"
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
assert.NoError(err)
ctx.App.Metadata = map[string]interface{}{
"runtimeConfig": runtimeConfig,
}
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
_, err = getContainers(ctx)
assert.NoError(err)
@ -580,12 +571,9 @@ func TestListCLIFunctionFormatFail(t *testing.T) {
// start off with an invalid output file
defaultOutputFile = invalidFile
app := cli.NewApp()
ctx := cli.NewContext(app, d.flags, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"foo": "bar",
}
ctx := createCLIContext(d.flags)
ctx.App.Name = "foo"
ctx.App.Metadata["foo"] = "bar"
fn, ok := listCLICommand.Action.(func(context *cli.Context) error)
assert.True(ok, d)
@ -674,12 +662,9 @@ func TestListCLIFunctionQuiet(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Bool("quiet", true, "")
app := cli.NewApp()
ctx := cli.NewContext(app, set, nil)
app.Name = "foo"
ctx.App.Metadata = map[string]interface{}{
"runtimeConfig": runtimeConfig,
}
ctx := createCLIContext(set)
ctx.App.Name = "foo"
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
savedOutputFile := defaultOutputFile
defer func() {

View File

@ -451,9 +451,8 @@ func newSingleContainerStatus(containerID string, containerState vc.State, annot
}
func execCLICommandFunc(assertHandler *assert.Assertions, cliCommand cli.Command, set *flag.FlagSet, expectedErr bool) {
app := cli.NewApp()
ctx := cli.NewContext(app, set, nil)
app.Name = "foo"
ctx := createCLIContext(set)
ctx.App.Name = "foo"
fn, ok := cliCommand.Action.(func(context *cli.Context) error)
assertHandler.True(ok)
@ -467,6 +466,21 @@ func execCLICommandFunc(assertHandler *assert.Assertions, cliCommand cli.Command
}
}
func createCLIContextWithApp(flagSet *flag.FlagSet, app *cli.App) *cli.Context {
ctx := cli.NewContext(app, flagSet, nil)
// create the map if required
if ctx.App.Metadata == nil {
ctx.App.Metadata = map[string]interface{}{}
}
return ctx
}
func createCLIContext(flagset *flag.FlagSet) *cli.Context {
return createCLIContextWithApp(flagset, cli.NewApp())
}
func TestMakeOCIBundle(t *testing.T) {
assert := assert.New(t)
@ -532,7 +546,6 @@ func TestCreateRootfs(t *testing.T) {
func TestMainUserWantsUsage(t *testing.T) {
assert := assert.New(t)
app := cli.NewApp()
type testData struct {
arguments []string
@ -558,7 +571,7 @@ func TestMainUserWantsUsage(t *testing.T) {
set := flag.NewFlagSet("", 0)
set.Parse(d.arguments)
ctx := cli.NewContext(app, set, nil)
ctx := createCLIContext(set)
result := userWantsUsage(ctx)
if d.expectTrue {
@ -571,7 +584,6 @@ func TestMainUserWantsUsage(t *testing.T) {
func TestMainBeforeSubCommands(t *testing.T) {
assert := assert.New(t)
app := cli.NewApp()
type testData struct {
arguments []string
@ -591,7 +603,7 @@ func TestMainBeforeSubCommands(t *testing.T) {
set := flag.NewFlagSet("", 0)
set.Parse(d.arguments)
ctx := cli.NewContext(app, set, nil)
ctx := createCLIContext(set)
err := beforeSubcommands(ctx)
if d.expectError {
@ -615,13 +627,11 @@ func TestMainBeforeSubCommandsInvalidLogFile(t *testing.T) {
err = os.MkdirAll(logFile, testDirMode)
assert.NoError(err)
app := cli.NewApp()
set := flag.NewFlagSet("", 0)
set.String("log", logFile, "")
set.Parse([]string{"create"})
ctx := cli.NewContext(app, set, nil)
ctx := createCLIContext(set)
err = beforeSubcommands(ctx)
assert.Error(err)
@ -636,8 +646,6 @@ func TestMainBeforeSubCommandsInvalidLogFormat(t *testing.T) {
logFile := filepath.Join(tmpdir, "log")
app := cli.NewApp()
set := flag.NewFlagSet("", 0)
set.Bool("debug", true, "")
set.String("log", logFile, "")
@ -651,7 +659,7 @@ func TestMainBeforeSubCommandsInvalidLogFormat(t *testing.T) {
kataLog.Logger.Out = logOut
}()
ctx := cli.NewContext(app, set, nil)
ctx := createCLIContext(set)
err = beforeSubcommands(ctx)
assert.Error(err)
@ -668,8 +676,6 @@ func TestMainBeforeSubCommandsLoadConfigurationFail(t *testing.T) {
logFile := filepath.Join(tmpdir, "log")
configFile := filepath.Join(tmpdir, "config")
app := cli.NewApp()
for _, logFormat := range []string{"json", "text"} {
set := flag.NewFlagSet("", 0)
set.Bool("debug", true, "")
@ -678,7 +684,7 @@ func TestMainBeforeSubCommandsLoadConfigurationFail(t *testing.T) {
set.String("kata-config", configFile, "")
set.Parse([]string{"kata-env"})
ctx := cli.NewContext(app, set, nil)
ctx := createCLIContext(set)
savedExitFunc := exitFunc
@ -702,12 +708,10 @@ func TestMainBeforeSubCommandsShowCCConfigPaths(t *testing.T) {
assert.NoError(err)
defer os.RemoveAll(tmpdir)
app := cli.NewApp()
set := flag.NewFlagSet("", 0)
set.Bool("kata-show-default-config-paths", true, "")
ctx := cli.NewContext(app, set, nil)
ctx := createCLIContext(set)
savedExitFunc := exitFunc

View File

@ -24,7 +24,7 @@ func TestPSCLIAction(t *testing.T) {
flagSet.Parse([]string{"runtime"})
// create a new fake context
ctx := cli.NewContext(&cli.App{Metadata: map[string]interface{}{}}, flagSet, nil)
ctx := createCLIContext(flagSet)
// get Action function
actionFunc, ok := psCLICommand.Action.(func(ctx *cli.Context) error)

View File

@ -29,7 +29,7 @@ func TestRunCliAction(t *testing.T) {
flagSet.Parse([]string{"runtime"})
// create a new fake context
ctx := cli.NewContext(&cli.App{Metadata: map[string]interface{}{}}, flagSet, nil)
ctx := createCLIContext(flagSet)
// get Action function
actionFunc, ok := runCLICommand.Action.(func(ctx *cli.Context) error)

View File

@ -21,7 +21,7 @@ func TestSpecCliAction(t *testing.T) {
assert.True(ok)
flagSet := flag.NewFlagSet("flag", flag.ContinueOnError)
ctx := cli.NewContext(&cli.App{}, flagSet, nil)
ctx := createCLIContext(flagSet)
defer os.Remove(specConfig)
err := actionFunc(ctx)
assert.NoError(err)

View File

@ -164,9 +164,7 @@ func TestStartCLIFunction(t *testing.T) {
assert := assert.New(t)
flagSet := &flag.FlagSet{}
app := cli.NewApp()
ctx := cli.NewContext(app, flagSet, nil)
ctx := createCLIContext(flagSet)
fn, ok := startCLICommand.Action.(func(context *cli.Context) error)
assert.True(ok)
@ -182,7 +180,7 @@ func TestStartCLIFunction(t *testing.T) {
flagSet = flag.NewFlagSet("container-id", flag.ContinueOnError)
flagSet.Parse([]string{"xyz"})
ctx = cli.NewContext(app, flagSet, nil)
ctx = createCLIContext(flagSet)
err = fn(ctx)
assert.Error(err)
@ -225,14 +223,12 @@ func TestStartCLIFunctionSuccess(t *testing.T) {
testingImpl.StartContainerFunc = nil
}()
app := cli.NewApp()
fn, ok := startCLICommand.Action.(func(context *cli.Context) error)
assert.True(ok)
flagSet := flag.NewFlagSet("test", 0)
flagSet.Parse([]string{testContainerID})
ctx := cli.NewContext(app, flagSet, nil)
ctx := createCLIContext(flagSet)
assert.NotNil(ctx)
err = fn(ctx)

View File

@ -28,13 +28,13 @@ func TestStateCliAction(t *testing.T) {
// without container id
flagSet.Parse([]string{"runtime"})
ctx := cli.NewContext(&cli.App{}, flagSet, nil)
ctx := createCLIContext(flagSet)
err := actionFunc(ctx)
assert.Error(err)
// with container id
flagSet.Parse([]string{"runtime", testContainerID})
ctx = cli.NewContext(&cli.App{}, flagSet, nil)
ctx = createCLIContext(flagSet)
err = actionFunc(ctx)
assert.Error(err)
}

View File

@ -26,7 +26,7 @@ func TestUpdateCLIAction(t *testing.T) {
flagSet.Parse([]string{"resources"})
// create a new fake context
ctx := cli.NewContext(&cli.App{}, flagSet, nil)
ctx := createCLIContext(flagSet)
// get Action function
actionFunc, ok := updateCLICommand.Action.(func(ctx *cli.Context) error)
@ -40,7 +40,7 @@ func TestUpdateCLIFailure(t *testing.T) {
assert := assert.New(t)
flagSet := flag.NewFlagSet("update", flag.ContinueOnError)
ctx := cli.NewContext(&cli.App{}, flagSet, nil)
ctx := createCLIContext(flagSet)
actionFunc, ok := updateCLICommand.Action.(func(ctx *cli.Context) error)
assert.True(ok)
@ -51,7 +51,7 @@ func TestUpdateCLIFailure(t *testing.T) {
// container info
flagSet.Parse([]string{testContainerID})
ctx = cli.NewContext(&cli.App{}, flagSet, nil)
ctx = createCLIContext(flagSet)
err = actionFunc(ctx)
assert.Error(err)
@ -105,7 +105,7 @@ func TestUpdateCLIFailure(t *testing.T) {
testingImpl.UpdateContainerFunc = nil
}()
flagSet.String("resources", "/abc/123/xyz/rgb", "")
ctx = cli.NewContext(&cli.App{}, flagSet, nil)
ctx = createCLIContext(flagSet)
err = actionFunc(ctx)
assert.Error(err)
@ -117,7 +117,7 @@ func TestUpdateCLIFailure(t *testing.T) {
f.WriteString("no json")
f.Close()
flagSet.Set("resources", f.Name())
ctx = cli.NewContext(&cli.App{}, flagSet, nil)
ctx = createCLIContext(flagSet)
err = actionFunc(ctx)
assert.Error(err)
@ -125,7 +125,7 @@ func TestUpdateCLIFailure(t *testing.T) {
flagSet = flag.NewFlagSet("update", flag.ContinueOnError)
flagSet.Parse([]string{testContainerID})
flagSet.String("cpu-period", "abcxyz", "")
ctx = cli.NewContext(&cli.App{}, flagSet, nil)
ctx = createCLIContext(flagSet)
err = actionFunc(ctx)
assert.Error(err)
@ -133,7 +133,7 @@ func TestUpdateCLIFailure(t *testing.T) {
flagSet = flag.NewFlagSet("update", flag.ContinueOnError)
flagSet.Parse([]string{testContainerID})
flagSet.String("cpu-quota", "abcxyz", "")
ctx = cli.NewContext(&cli.App{}, flagSet, nil)
ctx = createCLIContext(flagSet)
err = actionFunc(ctx)
assert.Error(err)
@ -141,7 +141,7 @@ func TestUpdateCLIFailure(t *testing.T) {
flagSet = flag.NewFlagSet("update", flag.ContinueOnError)
flagSet.Parse([]string{testContainerID})
flagSet.String("memory", "abcxyz", "")
ctx = cli.NewContext(&cli.App{}, flagSet, nil)
ctx = createCLIContext(flagSet)
err = actionFunc(ctx)
assert.Error(err)
}
@ -200,7 +200,7 @@ func TestUpdateCLISuccessful(t *testing.T) {
flagSet.String("kernel-memory", "100M", "")
flagSet.String("kernel-memory-tcp", "100M", "")
flagSet.String("memory-reservation", "100M", "")
ctx := cli.NewContext(&cli.App{}, flagSet, nil)
ctx := createCLIContext(flagSet)
err = actionFunc(ctx)
assert.NoError(err)
}

View File

@ -31,10 +31,9 @@ func TestVersion(t *testing.T) {
return testAppVersion
}
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
app.Name = testAppName
app.Version = runtimeVersion()
ctx := createCLIContext(nil)
ctx.App.Name = testAppName
ctx.App.Version = runtimeVersion()
fn, ok := versionCLICommand.Action.(func(context *cli.Context) error)
assert.True(t, ok)