Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cmd/kpod/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
18 changes: 12 additions & 6 deletions cmd/kpod/create_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ 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")
}
// Process KEY=VALUE stringslice in string map for WithLabels func
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]
Expand All @@ -27,15 +26,22 @@ 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")
}
// Add default environment variables if nothing defined
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
}

Expand Down
99 changes: 99 additions & 0 deletions cmd/kpod/create_cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

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)
}
2 changes: 1 addition & 1 deletion cmd/kpod/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/kpod/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions cmd/kpod/spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"reflect"
"testing"

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"},
}
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]))

}
54 changes: 54 additions & 0 deletions libpod/runtime_img_test.go
Original file line number Diff line number Diff line change
@@ -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"}))
}
19 changes: 19 additions & 0 deletions libpod/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package libpod

import (
"github.com/stretchr/testify/assert"
"testing"
)

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{}))
}
2 changes: 2 additions & 0 deletions vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@ github.com/go-zoo/bone 031b4005dfe248ccba241a0c9de0f9e112fd6b7c
github.com/hashicorp/go-multierror 83588e72410abfbe4df460eeb6f30841ae47d4c4
github.com/hashicorp/errwrap 7554cd9344cec97297fa6649b055a8c98c2a1e55
github.com/pquerna/ffjson d49c2bc1aa135aad0c6f4fc2056623ec78f5d5ac
github.com/stretchr/testify 4d4bfba8f1d1027c4fdbe371823030df51419987
github.com/pmezard/go-difflib 792786c7400a136282c1664665ae0a8db921c6c2
27 changes: 27 additions & 0 deletions vendor/github.com/pmezard/go-difflib/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading