aboutsummaryrefslogtreecommitdiff
path: root/pkg/util/utils_test.go
blob: c938dc59259a273f0ad73018d4787b415cb60f72 (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
package util

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{}))
}

func TestParseChanges(t *testing.T) {
	// CMD=/bin/sh
	_, vals, err := ParseChanges("CMD=/bin/sh")
	assert.EqualValues(t, []string{"/bin/sh"}, vals)
	assert.NoError(t, err)

	// CMD [/bin/sh]
	_, vals, err = ParseChanges("CMD [/bin/sh]")
	assert.EqualValues(t, []string{"/bin/sh"}, vals)
	assert.NoError(t, err)

	// CMD ["/bin/sh"]
	_, vals, err = ParseChanges(`CMD ["/bin/sh"]`)
	assert.EqualValues(t, []string{`"/bin/sh"`}, vals)
	assert.NoError(t, err)

	// CMD ["/bin/sh","-c","ls"]
	_, vals, err = ParseChanges(`CMD ["/bin/sh","c","ls"]`)
	assert.EqualValues(t, []string{`"/bin/sh"`, `"c"`, `"ls"`}, vals)
	assert.NoError(t, err)

	// CMD ["/bin/sh","arg-with,comma"]
	_, vals, err = ParseChanges(`CMD ["/bin/sh","arg-with,comma"]`)
	assert.EqualValues(t, []string{`"/bin/sh"`, `"arg-with`, `comma"`}, vals)
	assert.NoError(t, err)

	// CMD "/bin/sh"]
	_, _, err = ParseChanges(`CMD "/bin/sh"]`)
	assert.Error(t, err)
	assert.Equal(t, `invalid value "/bin/sh"]`, err.Error())

	// CMD [bin/sh
	_, _, err = ParseChanges(`CMD "/bin/sh"]`)
	assert.Error(t, err)
	assert.Equal(t, `invalid value "/bin/sh"]`, err.Error())

	// CMD ["/bin /sh"]
	_, _, err = ParseChanges(`CMD ["/bin /sh"]`)
	assert.Error(t, err)
	assert.Equal(t, `invalid value "/bin /sh"`, err.Error())

	// CMD ["/bin/sh", "-c","ls"] whitespace between values
	_, vals, err = ParseChanges(`CMD ["/bin/sh", "c","ls"]`)
	assert.Error(t, err)
	assert.Equal(t, `invalid value  "c"`, err.Error())

	// CMD?
	_, _, err = ParseChanges(`CMD?`)
	assert.Error(t, err)
	assert.Equal(t, `invalid format CMD?`, err.Error())

	// empty values for CMD
	_, _, err = ParseChanges(`CMD `)
	assert.Error(t, err)
	assert.Equal(t, `invalid value `, err.Error())

	// LABEL=blue=image
	_, vals, err = ParseChanges(`LABEL=blue=image`)
	assert.EqualValues(t, []string{"blue", "image"}, vals)
	assert.NoError(t, err)

	// LABEL = blue=image
	_, vals, err = ParseChanges(`LABEL = blue=image`)
	assert.Error(t, err)
	assert.Equal(t, `invalid value = blue=image`, err.Error())

}