Browse code

Builder/dockerfile/dispatchers.go tests

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

Tomasz Kopczynski authored on 2016/06/18 19:49:17
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,208 @@
0
+package dockerfile
1
+
2
+import (
3
+	"fmt"
4
+	"runtime"
5
+	"strings"
6
+	"testing"
7
+
8
+	"github.com/docker/engine-api/types/container"
9
+)
10
+
11
+type commandWithFunction struct {
12
+	name     string
13
+	function func(args []string) error
14
+}
15
+
16
+func TestCommandsExactlyOneArgument(t *testing.T) {
17
+	commands := []commandWithFunction{
18
+		{"MAINTAINER", func(args []string) error { return maintainer(nil, args, nil, "") }},
19
+		{"FROM", func(args []string) error { return from(nil, args, nil, "") }},
20
+		{"WORKDIR", func(args []string) error { return workdir(nil, args, nil, "") }},
21
+		{"USER", func(args []string) error { return user(nil, args, nil, "") }}}
22
+
23
+	for _, command := range commands {
24
+		err := command.function([]string{})
25
+
26
+		if err == nil {
27
+			t.Fatalf("Error should be present for %s command", command.name)
28
+		}
29
+
30
+		expectedError := fmt.Sprintf("%s requires exactly one argument", command.name)
31
+
32
+		if err.Error() != expectedError {
33
+			t.Fatalf("Wrong error message for %s. Got: %s. Should be: %s", command.name, err.Error(), expectedError)
34
+		}
35
+	}
36
+}
37
+
38
+func TestCommandsAtLeastOneArgument(t *testing.T) {
39
+	commands := []commandWithFunction{
40
+		{"ENV", func(args []string) error { return env(nil, args, nil, "") }},
41
+		{"LABEL", func(args []string) error { return label(nil, args, nil, "") }},
42
+		{"ADD", func(args []string) error { return add(nil, args, nil, "") }},
43
+		{"COPY", func(args []string) error { return dispatchCopy(nil, args, nil, "") }},
44
+		{"ONBUILD", func(args []string) error { return onbuild(nil, args, nil, "") }},
45
+		{"EXPOSE", func(args []string) error { return expose(nil, args, nil, "") }},
46
+		{"VOLUME", func(args []string) error { return volume(nil, args, nil, "") }}}
47
+
48
+	for _, command := range commands {
49
+		err := command.function([]string{})
50
+
51
+		if err == nil {
52
+			t.Fatalf("Error should be present for %s command", command.name)
53
+		}
54
+
55
+		expectedError := fmt.Sprintf("%s requires at least one argument", command.name)
56
+
57
+		if err.Error() != expectedError {
58
+			t.Fatalf("Wrong error message for %s. Got: %s. Should be: %s", command.name, err.Error(), expectedError)
59
+		}
60
+	}
61
+}
62
+
63
+func TestCommandsTooManyArguments(t *testing.T) {
64
+	commands := []commandWithFunction{
65
+		{"ENV", func(args []string) error { return env(nil, args, nil, "") }},
66
+		{"LABEL", func(args []string) error { return label(nil, args, nil, "") }}}
67
+
68
+	for _, command := range commands {
69
+		err := command.function([]string{"arg1", "arg2", "arg3"})
70
+
71
+		if err == nil {
72
+			t.Fatalf("Error should be present for %s command", command.name)
73
+		}
74
+
75
+		expectedError := fmt.Sprintf("Bad input to %s, too many arguments", command.name)
76
+
77
+		if err.Error() != expectedError {
78
+			t.Fatalf("Wrong error message for %s. Got: %s. Should be: %s", command.name, err.Error(), expectedError)
79
+		}
80
+	}
81
+}
82
+
83
+func TestEnv2Variables(t *testing.T) {
84
+	variables := []string{"var1", "val1", "var2", "val2"}
85
+
86
+	bflags := &BFlags{}
87
+	config := &container.Config{}
88
+
89
+	b := &Builder{flags: bflags, runConfig: config, disableCommit: true}
90
+
91
+	if err := env(b, variables, nil, ""); err != nil {
92
+		t.Fatalf("Error when executing env: %s", err.Error())
93
+	}
94
+
95
+	expectedVar1 := fmt.Sprintf("%s=%s", variables[0], variables[1])
96
+	expectedVar2 := fmt.Sprintf("%s=%s", variables[2], variables[3])
97
+
98
+	if b.runConfig.Env[0] != expectedVar1 {
99
+		t.Fatalf("Wrong env output for first variable. Got: %s. Should be: %s", b.runConfig.Env[0], expectedVar1)
100
+	}
101
+
102
+	if b.runConfig.Env[1] != expectedVar2 {
103
+		t.Fatalf("Wrong env output for second variable. Got: %s, Should be: %s", b.runConfig.Env[1], expectedVar2)
104
+	}
105
+}
106
+
107
+func TestMaintainer(t *testing.T) {
108
+	maintainerEntry := "Some Maintainer <maintainer@example.com>"
109
+
110
+	b := &Builder{flags: &BFlags{}, runConfig: &container.Config{}, disableCommit: true}
111
+
112
+	if err := maintainer(b, []string{maintainerEntry}, nil, ""); err != nil {
113
+		t.Fatalf("Error when executing maintainer: %s", err.Error())
114
+	}
115
+
116
+	if b.maintainer != maintainerEntry {
117
+		t.Fatalf("Maintainer in builder should be set to %s. Got: %s", maintainerEntry, b.maintainer)
118
+	}
119
+}
120
+
121
+func TestLabel(t *testing.T) {
122
+	labelName := "label"
123
+	labelValue := "value"
124
+
125
+	labelEntry := []string{labelName, labelValue}
126
+
127
+	b := &Builder{flags: &BFlags{}, runConfig: &container.Config{}, disableCommit: true}
128
+
129
+	if err := label(b, labelEntry, nil, ""); err != nil {
130
+		t.Fatalf("Error when executing label: %s", err.Error())
131
+	}
132
+
133
+	if val, ok := b.runConfig.Labels[labelName]; ok {
134
+		if val != labelValue {
135
+			t.Fatalf("Label %s should have value %s, had %s instead", labelName, labelValue, val)
136
+		}
137
+	} else {
138
+		t.Fatalf("Label %s should be present but it is not", labelName)
139
+	}
140
+}
141
+
142
+func TestFrom(t *testing.T) {
143
+	b := &Builder{flags: &BFlags{}, runConfig: &container.Config{}, disableCommit: true}
144
+
145
+	err := from(b, []string{"scratch"}, nil, "")
146
+
147
+	if runtime.GOOS == "windows" {
148
+		if err == nil {
149
+			t.Fatalf("Error not set on Windows")
150
+		}
151
+
152
+		expectedError := "Windows does not support FROM scratch"
153
+
154
+		if !strings.Contains(err.Error(), expectedError) {
155
+			t.Fatalf("Error message not correct on Windows. Should be: %s, got: %s", expectedError, err.Error())
156
+		}
157
+	} else {
158
+		if err != nil {
159
+			t.Fatalf("Error when executing from: %s", err.Error())
160
+		}
161
+
162
+		if b.image != "" {
163
+			t.Fatalf("Image shoule be empty, got: %s", b.image)
164
+		}
165
+
166
+		if b.noBaseImage != true {
167
+			t.Fatalf("Image should not have any base image, got: %s", b.noBaseImage)
168
+		}
169
+	}
170
+}
171
+
172
+func TestOnbuildIllegalTriggers(t *testing.T) {
173
+	triggers := []struct{ command, expectedError string }{
174
+		{"ONBUILD", "Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed"},
175
+		{"MAINTAINER", "MAINTAINER isn't allowed as an ONBUILD trigger"},
176
+		{"FROM", "FROM isn't allowed as an ONBUILD trigger"}}
177
+
178
+	for _, trigger := range triggers {
179
+		b := &Builder{flags: &BFlags{}, runConfig: &container.Config{}, disableCommit: true}
180
+
181
+		err := onbuild(b, []string{trigger.command}, nil, "")
182
+
183
+		if err == nil {
184
+			t.Fatalf("Error should not be nil")
185
+		}
186
+
187
+		if !strings.Contains(err.Error(), trigger.expectedError) {
188
+			t.Fatalf("Error message not correct. Should be: %s, got: %s", trigger.expectedError, err.Error())
189
+		}
190
+	}
191
+}
192
+
193
+func TestOnbuild(t *testing.T) {
194
+	b := &Builder{flags: &BFlags{}, runConfig: &container.Config{}, disableCommit: true}
195
+
196
+	err := onbuild(b, []string{"ADD", ".", "/app/src"}, nil, "ONBUILD ADD . /app/src")
197
+
198
+	if err != nil {
199
+		t.Fatalf("Error should be empty, got: %s", err.Error())
200
+	}
201
+
202
+	expectedOnbuild := "ADD . /app/src"
203
+
204
+	if b.runConfig.OnBuild[0] != expectedOnbuild {
205
+		t.Fatalf("Wrong ONBUILD command. Expected: %s, got: %s", expectedOnbuild, b.runConfig.OnBuild[0])
206
+	}
207
+}