summaryrefslogtreecommitdiff
path: root/cmd/podman/create_cli_test.go
blob: 63a1e5dd3b4b8971e193291bb950255b43ff4834 (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
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)
}