From 46d762176eafb748b2094bd518ecf66d86388779 Mon Sep 17 00:00:00 2001 From: baude Date: Wed, 1 Nov 2017 16:44:08 -0500 Subject: create_cli_test.go: Unittests Create unittests for the create_cli helper functions. As such, remove cli context usage from the functions to further divide between cli input and parsing. Also, simplifies unit testing. Signed-off-by: baude --- cmd/kpod/create.go | 5 +-- cmd/kpod/create_cli.go | 18 ++++++--- cmd/kpod/create_cli_test.go | 98 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 cmd/kpod/create_cli_test.go (limited to 'cmd') diff --git a/cmd/kpod/create.go b/cmd/kpod/create.go index 9d4d08b08..545714c68 100644 --- a/cmd/kpod/create.go +++ b/cmd/kpod/create.go @@ -252,13 +252,12 @@ func parseCreateOpts(c *cli.Context, runtime *libpod.Runtime) (*createConfig, er } // LABEL VARIABLES - labels, err := getAllLabels(c) + labels, err := getAllLabels(c.StringSlice("label-file"), c.StringSlice("labels")) if err != nil { return &createConfig{}, errors.Wrapf(err, "unable to process labels") } // ENVIRONMENT VARIABLES - // TODO where should env variables be verified to be x=y format - env, err := getAllEnvironmentVariables(c) + env, err := getAllEnvironmentVariables(c.StringSlice("env-file"), c.StringSlice("env")) if err != nil { return &createConfig{}, errors.Wrapf(err, "unable to process environment variables") } diff --git a/cmd/kpod/create_cli.go b/cmd/kpod/create_cli.go index 996155ba0..1cc00adc5 100644 --- a/cmd/kpod/create_cli.go +++ b/cmd/kpod/create_cli.go @@ -4,13 +4,12 @@ import ( "strings" "github.com/pkg/errors" - "github.com/urfave/cli" ) -func getAllLabels(cli *cli.Context) (map[string]string, error) { +func getAllLabels(labelFile, inputLabels []string) (map[string]string, error) { var labelValues []string labels := make(map[string]string) - labelValues, labelErr := readKVStrings(cli.StringSlice("label-file"), cli.StringSlice("label")) + labelValues, labelErr := readKVStrings(labelFile, inputLabels) if labelErr != nil { return labels, errors.Wrapf(labelErr, "unable to process labels from --label and label-file") } @@ -18,7 +17,7 @@ func getAllLabels(cli *cli.Context) (map[string]string, error) { if len(labelValues) > 0 { for _, i := range labelValues { spliti := strings.Split(i, "=") - if len(spliti) > 1 { + if len(spliti) < 2 { return labels, errors.Errorf("labels must be in KEY=VALUE format: %s is invalid", i) } labels[spliti[0]] = spliti[1] @@ -27,8 +26,8 @@ func getAllLabels(cli *cli.Context) (map[string]string, error) { return labels, nil } -func getAllEnvironmentVariables(cli *cli.Context) ([]string, error) { - env, err := readKVStrings(cli.StringSlice("env-file"), cli.StringSlice("env")) +func getAllEnvironmentVariables(envFiles, envInput []string) ([]string, error) { + env, err := readKVStrings(envFiles, envInput) if err != nil { return []string{}, errors.Wrapf(err, "unable to process variables from --env and --env-file") } @@ -36,6 +35,13 @@ func getAllEnvironmentVariables(cli *cli.Context) ([]string, error) { if len(env) == 0 { env = append(env, defaultEnvVariables...) } + // Each environment variable must be in the K=V format + for _,i := range env{ + spliti := strings.Split(i, "=") + if len(spliti) != 2 { + return env, errors.Errorf("environment variables must be in the format KEY=VALUE: %s is invalid", i) + } + } return env, nil } diff --git a/cmd/kpod/create_cli_test.go b/cmd/kpod/create_cli_test.go new file mode 100644 index 000000000..928a5ce05 --- /dev/null +++ b/cmd/kpod/create_cli_test.go @@ -0,0 +1,98 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + "io/ioutil" + "os" + "testing" +) + +var ( + Var1 = []string{"ONE=1", "TWO=2"} +) + +func createTmpFile(content []byte) (string, error) { + tmpfile, err := ioutil.TempFile(os.TempDir(), "unittest") + if err != nil { + return "", err + } + + if _, err := tmpfile.Write(content); err != nil { + return "", err + + } + if err := tmpfile.Close(); err != nil { + return "", err + } + return tmpfile.Name(), nil +} + +func TestConvertStringSliceToMap(t *testing.T) { + strSlice := []string{"BLAU=BLUE", "GELB=YELLOW"} + result, _ := convertStringSliceToMap(strSlice, "=") + assert.Equal(t, result["BLAU"], "BLUE") +} + +func TestConvertStringSliceToMapBadData(t *testing.T) { + strSlice := []string{"BLAU=BLUE", "GELB^YELLOW"} + _, err := convertStringSliceToMap(strSlice, "=") + assert.Error(t, err) +} + +func TestGetAllLabels(t *testing.T) { + fileLabels := []string{} + labels, _ := getAllLabels(fileLabels, Var1) + assert.Equal(t, len(labels), 2) +} + +func TestGetAllLabelsBadKeyValue(t *testing.T) { + inLabels := []string{"ONE1", "TWO=2"} + fileLabels := []string{} + _, err := getAllLabels(fileLabels, inLabels) + assert.Error(t, err, assert.AnError) +} + +func TestGetAllLabelsBadLabelFile(t *testing.T) { + fileLabels := []string{"/foobar5001/be"} + _, err := getAllLabels(fileLabels, Var1) + assert.Error(t, err, assert.AnError) +} + +func TestGetAllLabelsFile(t *testing.T) { + content := []byte("THREE=3") + tFile, err := createTmpFile(content) + defer os.Remove(tFile) + assert.NoError(t, err) + fileLabels := []string{tFile} + result, _ := getAllLabels(fileLabels, Var1) + assert.Equal(t, len(result), 3) +} + +func TestGetAllEnvironmentVariables(t *testing.T) { + fileEnvs := []string{} + result, _ := getAllEnvironmentVariables(fileEnvs, Var1) + assert.Equal(t, len(result), 2) +} + +func TestGetAllEnvironmentVariablesBadKeyValue(t *testing.T) { + inEnvs := []string{"ONE1", "TWO=2"} + fileEnvs := []string{} + _, err := getAllEnvironmentVariables(fileEnvs, inEnvs) + assert.Error(t, err, assert.AnError) +} + +func TestGetAllEnvironmentVariablesBadEnvFile(t *testing.T) { + fileEnvs := []string{"/foobar5001/be"} + _, err := getAllEnvironmentVariables(fileEnvs, Var1) + assert.Error(t, err, assert.AnError) +} + +func TestGetAllEnvironmentVariablesFile(t *testing.T) { + content := []byte("THREE=3") + tFile, err := createTmpFile(content) + defer os.Remove(tFile) + assert.NoError(t, err) + fileEnvs := []string{tFile} + result, _ := getAllEnvironmentVariables(fileEnvs, Var1) + assert.Equal(t, len(result), 3) +} -- cgit v1.2.3-54-g00ecf From 99ca35f18598f77bcf260f91044116365d8e3c26 Mon Sep 17 00:00:00 2001 From: baude Date: Thu, 2 Nov 2017 08:09:17 -0500 Subject: util_test.go: Unittests for util.go Add unit tests for func StringInSlice. Signed-off-by: baude --- cmd/kpod/create_cli_test.go | 3 ++- libpod/util_test.go | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 libpod/util_test.go (limited to 'cmd') diff --git a/cmd/kpod/create_cli_test.go b/cmd/kpod/create_cli_test.go index 928a5ce05..af5c5afae 100644 --- a/cmd/kpod/create_cli_test.go +++ b/cmd/kpod/create_cli_test.go @@ -1,10 +1,11 @@ package main import ( - "github.com/stretchr/testify/assert" "io/ioutil" "os" "testing" + + "github.com/stretchr/testify/assert" ) var ( diff --git a/libpod/util_test.go b/libpod/util_test.go new file mode 100644 index 000000000..b3d336d1f --- /dev/null +++ b/libpod/util_test.go @@ -0,0 +1,19 @@ +package libpod + + +import ( + "testing" + "github.com/stretchr/testify/assert" +) +var ( + sliceData = []string{"one", "two", "three", "four"} +) + +func TestStringInSlice(t *testing.T) { + // string is in the slice + assert.True(t, StringInSlice("one", sliceData)) + // string is not in the slice + assert.False(t, StringInSlice("five", sliceData)) + // string is not in empty slice + assert.False(t, StringInSlice("one", []string{})) +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf From aa19565d8d86701191bb2bf15fe0ab019da590dc Mon Sep 17 00:00:00 2001 From: baude Date: Thu, 2 Nov 2017 08:25:06 -0500 Subject: spec.go: Remove cli context as func arg Remove cli context as a func arg to make unit tests easier. Signed-off-by: baude --- cmd/kpod/create.go | 2 +- cmd/kpod/run.go | 2 +- cmd/kpod/spec.go | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) (limited to 'cmd') diff --git a/cmd/kpod/create.go b/cmd/kpod/create.go index 545714c68..caee0c7f0 100644 --- a/cmd/kpod/create.go +++ b/cmd/kpod/create.go @@ -170,7 +170,7 @@ func createCmd(c *cli.Context) error { if err != nil { return err } - options, err := createConfig.GetContainerCreateOptions(c) + options, err := createConfig.GetContainerCreateOptions() if err != nil { return errors.Wrapf(err, "unable to parse new container options") } diff --git a/cmd/kpod/run.go b/cmd/kpod/run.go index cefa1d2d0..904977940 100644 --- a/cmd/kpod/run.go +++ b/cmd/kpod/run.go @@ -63,7 +63,7 @@ func runCmd(c *cli.Context) error { } logrus.Debug("imageID is ", imageID) - options, err := createConfig.GetContainerCreateOptions(c) + options, err := createConfig.GetContainerCreateOptions() if err != nil { return errors.Wrapf(err, "unable to parse new container options") } diff --git a/cmd/kpod/spec.go b/cmd/kpod/spec.go index d30c0d1a5..b990d8463 100644 --- a/cmd/kpod/spec.go +++ b/cmd/kpod/spec.go @@ -11,7 +11,6 @@ import ( "github.com/projectatomic/libpod/libpod" ann "github.com/projectatomic/libpod/pkg/annotations" "github.com/sirupsen/logrus" - "github.com/urfave/cli" "golang.org/x/sys/unix" ) @@ -464,7 +463,7 @@ func (c *createConfig) GetTmpfsMounts() []spec.Mount { return m } -func (c *createConfig) GetContainerCreateOptions(cli *cli.Context) ([]libpod.CtrCreateOption, error) { +func (c *createConfig) GetContainerCreateOptions() ([]libpod.CtrCreateOption, error) { var options []libpod.CtrCreateOption // Uncomment after talking to mheon about unimplemented funcs -- cgit v1.2.3-54-g00ecf From 69cecb049aaf37fcc9a086b3f3f84e6e63174b14 Mon Sep 17 00:00:00 2001 From: baude Date: Thu, 2 Nov 2017 09:15:10 -0500 Subject: spec_test.go: Unit tests for spec.go Unit tests for spec.go which includes testing parts of the oci spec. Signed-off-by: baude --- cmd/kpod/spec_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 cmd/kpod/spec_test.go (limited to 'cmd') diff --git a/cmd/kpod/spec_test.go b/cmd/kpod/spec_test.go new file mode 100644 index 000000000..2f023f201 --- /dev/null +++ b/cmd/kpod/spec_test.go @@ -0,0 +1,39 @@ +package main + + +import ( + "testing" + "reflect" + + "github.com/stretchr/testify/assert" + spec "github.com/opencontainers/runtime-spec/specs-go" +) + +func TestCreateConfig_GetVolumeMounts(t *testing.T) { + data := spec.Mount{ + Destination: "/foobar", + Type: "bind", + Source: "foobar", + Options: []string{"ro", "rbind"}, + } + config := createConfig{ + volumes: []string{"foobar:/foobar:ro"}, + } + specMount := config.GetVolumeMounts() + assert.True(t, reflect.DeepEqual(data, specMount[0])) +} + +func TestCreateConfig_GetTmpfsMounts(t *testing.T) { + data := spec.Mount{ + Destination: "/homer", + Type: "tmpfs", + Source: "tmpfs", + Options: []string{"rw", "size=787448k", "mode=1777"}, + } + config:= createConfig{ + tmpfs: []string{"/homer:rw,size=787448k,mode=1777"}, + } + tmpfsMount := config.GetTmpfsMounts() + assert.True(t, reflect.DeepEqual(data, tmpfsMount[0])) + +} -- cgit v1.2.3-54-g00ecf From 0026075d59b5e6e90786ed21825ac43d4f59fa5a Mon Sep 17 00:00:00 2001 From: baude Date: Thu, 2 Nov 2017 13:17:09 -0500 Subject: libpod/runtime_img_test.go Unit Tests Unit tests for getRegistry related functions. Signed-off-by: baude --- cmd/kpod/create_cli.go | 2 +- cmd/kpod/spec_test.go | 19 ++++++++-------- libpod/runtime_img_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ libpod/util_test.go | 6 +++--- 4 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 libpod/runtime_img_test.go (limited to 'cmd') diff --git a/cmd/kpod/create_cli.go b/cmd/kpod/create_cli.go index 1cc00adc5..eaad46591 100644 --- a/cmd/kpod/create_cli.go +++ b/cmd/kpod/create_cli.go @@ -36,7 +36,7 @@ func getAllEnvironmentVariables(envFiles, envInput []string) ([]string, error) { env = append(env, defaultEnvVariables...) } // Each environment variable must be in the K=V format - for _,i := range env{ + for _, i := range env { spliti := strings.Split(i, "=") if len(spliti) != 2 { return env, errors.Errorf("environment variables must be in the format KEY=VALUE: %s is invalid", i) diff --git a/cmd/kpod/spec_test.go b/cmd/kpod/spec_test.go index 2f023f201..c3de84324 100644 --- a/cmd/kpod/spec_test.go +++ b/cmd/kpod/spec_test.go @@ -1,20 +1,19 @@ package main - import ( - "testing" "reflect" + "testing" - "github.com/stretchr/testify/assert" spec "github.com/opencontainers/runtime-spec/specs-go" + "github.com/stretchr/testify/assert" ) func TestCreateConfig_GetVolumeMounts(t *testing.T) { data := spec.Mount{ Destination: "/foobar", - Type: "bind", - Source: "foobar", - Options: []string{"ro", "rbind"}, + Type: "bind", + Source: "foobar", + Options: []string{"ro", "rbind"}, } config := createConfig{ volumes: []string{"foobar:/foobar:ro"}, @@ -26,11 +25,11 @@ func TestCreateConfig_GetVolumeMounts(t *testing.T) { func TestCreateConfig_GetTmpfsMounts(t *testing.T) { data := spec.Mount{ Destination: "/homer", - Type: "tmpfs", - Source: "tmpfs", - Options: []string{"rw", "size=787448k", "mode=1777"}, + Type: "tmpfs", + Source: "tmpfs", + Options: []string{"rw", "size=787448k", "mode=1777"}, } - config:= createConfig{ + config := createConfig{ tmpfs: []string{"/homer:rw,size=787448k,mode=1777"}, } tmpfsMount := config.GetTmpfsMounts() diff --git a/libpod/runtime_img_test.go b/libpod/runtime_img_test.go new file mode 100644 index 000000000..f7f7128a4 --- /dev/null +++ b/libpod/runtime_img_test.go @@ -0,0 +1,54 @@ +package libpod + +import ( + "io/ioutil" + "os" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +var ( + registry = `[registries.search] +registries = ['one'] + +[registries.insecure] +registries = ['two']` +) + +func createTmpFile(content []byte) (string, error) { + tmpfile, err := ioutil.TempFile(os.TempDir(), "unittest") + if err != nil { + return "", err + } + + if _, err := tmpfile.Write(content); err != nil { + return "", err + + } + if err := tmpfile.Close(); err != nil { + return "", err + } + return tmpfile.Name(), nil +} + +func TestGetRegistries(t *testing.T) { + registryPath, err := createTmpFile([]byte(registry)) + assert.NoError(t, err) + defer os.Remove(registryPath) + os.Setenv("REGISTRIES_CONFIG_PATH", registryPath) + registries, err := GetRegistries() + assert.NoError(t, err) + assert.True(t, reflect.DeepEqual(registries, []string{"one"})) +} + +func TestGetInsecureRegistries(t *testing.T) { + registryPath, err := createTmpFile([]byte(registry)) + assert.NoError(t, err) + os.Setenv("REGISTRIES_CONFIG_PATH", registryPath) + defer os.Remove(registryPath) + registries, err := GetInsecureRegistries() + assert.NoError(t, err) + assert.True(t, reflect.DeepEqual(registries, []string{"two"})) +} diff --git a/libpod/util_test.go b/libpod/util_test.go index b3d336d1f..24e5fdfac 100644 --- a/libpod/util_test.go +++ b/libpod/util_test.go @@ -1,10 +1,10 @@ package libpod - import ( - "testing" "github.com/stretchr/testify/assert" + "testing" ) + var ( sliceData = []string{"one", "two", "three", "four"} ) @@ -16,4 +16,4 @@ func TestStringInSlice(t *testing.T) { assert.False(t, StringInSlice("five", sliceData)) // string is not in empty slice assert.False(t, StringInSlice("one", []string{})) -} \ No newline at end of file +} -- cgit v1.2.3-54-g00ecf