Browse code

Unit tests for builder/dockerfile/support

Signed-off-by: Tomasz Kopczynski <tomek@kopczynski.net.pl>

Tomasz Kopczynski authored on 2016/04/06 06:00:37
Showing 2 changed files
... ...
@@ -2,6 +2,9 @@ package dockerfile
2 2
 
3 3
 import "strings"
4 4
 
5
+// handleJSONArgs parses command passed to CMD, ENTRYPOINT or RUN instruction in Dockerfile
6
+// for exec form it returns untouched args slice
7
+// for shell form it returns concatenated args as the first element of a slice
5 8
 func handleJSONArgs(args []string, attributes map[string]bool) []string {
6 9
 	if len(args) == 0 {
7 10
 		return []string{}
8 11
new file mode 100644
... ...
@@ -0,0 +1,65 @@
0
+package dockerfile
1
+
2
+import "testing"
3
+
4
+type testCase struct {
5
+	name       string
6
+	args       []string
7
+	attributes map[string]bool
8
+	expected   []string
9
+}
10
+
11
+func initTestCases() []testCase {
12
+	testCases := []testCase{}
13
+
14
+	testCases = append(testCases, testCase{
15
+		name:       "empty args",
16
+		args:       []string{},
17
+		attributes: make(map[string]bool),
18
+		expected:   []string{},
19
+	})
20
+
21
+	jsonAttributes := make(map[string]bool)
22
+	jsonAttributes["json"] = true
23
+
24
+	testCases = append(testCases, testCase{
25
+		name:       "json attribute with one element",
26
+		args:       []string{"foo"},
27
+		attributes: jsonAttributes,
28
+		expected:   []string{"foo"},
29
+	})
30
+
31
+	testCases = append(testCases, testCase{
32
+		name:       "json attribute with two elements",
33
+		args:       []string{"foo", "bar"},
34
+		attributes: jsonAttributes,
35
+		expected:   []string{"foo", "bar"},
36
+	})
37
+
38
+	testCases = append(testCases, testCase{
39
+		name:       "no attributes",
40
+		args:       []string{"foo", "bar"},
41
+		attributes: nil,
42
+		expected:   []string{"foo bar"},
43
+	})
44
+
45
+	return testCases
46
+}
47
+
48
+func TestHandleJSONArgs(t *testing.T) {
49
+	testCases := initTestCases()
50
+
51
+	for _, test := range testCases {
52
+		arguments := handleJSONArgs(test.args, test.attributes)
53
+
54
+		if len(arguments) != len(test.expected) {
55
+			t.Fatalf("In test \"%s\": length of returned slice is incorrect. Expected: %d, got: %d", test.name, len(test.expected), len(arguments))
56
+		}
57
+
58
+		for i := range test.expected {
59
+			if arguments[i] != test.expected[i] {
60
+				t.Fatalf("In test \"%s\": element as position %d is incorrect. Expected: %s, got: %s", test.name, i, test.expected[i], arguments[i])
61
+			}
62
+		}
63
+	}
64
+}