aboutsummaryrefslogtreecommitdiff
path: root/cmd/kpod/create_cli_test.go
blob: 928a5ce0566ed3359f96cb9934780769f9e98667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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)
}