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

Commit

Permalink
tests: Add cli.Context helper functions
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jodh-intel committed Aug 10, 2018
1 parent 41d1c14 commit 0ede467
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 214 deletions.
27 changes: 9 additions & 18 deletions cli/create_test.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 4 additions & 8 deletions cli/delete_test.go
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions cli/events_test.go
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -50,15 +50,15 @@ 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)

// interval is zero
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)

Expand Down Expand Up @@ -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)
}
25 changes: 12 additions & 13 deletions cli/exec_test.go
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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": {
Expand Down
38 changes: 14 additions & 24 deletions cli/factory_test.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions cli/kata-check_amd64_test.go
Expand Up @@ -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{}
Expand Down
5 changes: 2 additions & 3 deletions cli/kata-check_arm64_test.go
Expand Up @@ -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{}
Expand Down
5 changes: 2 additions & 3 deletions cli/kata-check_ppc64le_test.go
Expand Up @@ -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{}
Expand Down
5 changes: 2 additions & 3 deletions cli/kata-check_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit 0ede467

Please sign in to comment.