summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Demeester <vincent@sbr.pm>2016-04-16 13:34:35 +0200
committerVincent Demeester <vincent@sbr.pm>2016-04-16 13:34:35 +0200
commit27dd6a10b8515ed464e11ba409dd8c03b2287d1d (patch)
tree6ec7bacce368b330fdfa688161c47f596f0b2532
parent5314296c693d0d4d7f622940b48bc15e400032e3 (diff)
parentd0ebc58b9c82d2baf0c672b514ba9733ff79a3a1 (diff)
downloaddocker-27dd6a10b8515ed464e11ba409dd8c03b2287d1d.tar.gz
Merge pull request #21817 from tkopczynski/20784-builder-dockerfile-support
Unit tests for builder/dockerfile/support
-rw-r--r--builder/dockerfile/support.go3
-rw-r--r--builder/dockerfile/support_test.go65
2 files changed, 68 insertions, 0 deletions
diff --git a/builder/dockerfile/support.go b/builder/dockerfile/support.go
index 38897b2cad..d2fdada874 100644
--- a/builder/dockerfile/support.go
+++ b/builder/dockerfile/support.go
@@ -2,6 +2,9 @@ package dockerfile
import "strings"
+// handleJSONArgs parses command passed to CMD, ENTRYPOINT or RUN instruction in Dockerfile
+// for exec form it returns untouched args slice
+// for shell form it returns concatenated args as the first element of a slice
func handleJSONArgs(args []string, attributes map[string]bool) []string {
if len(args) == 0 {
return []string{}
diff --git a/builder/dockerfile/support_test.go b/builder/dockerfile/support_test.go
new file mode 100644
index 0000000000..7cc6fe9dcb
--- /dev/null
+++ b/builder/dockerfile/support_test.go
@@ -0,0 +1,65 @@
+package dockerfile
+
+import "testing"
+
+type testCase struct {
+ name string
+ args []string
+ attributes map[string]bool
+ expected []string
+}
+
+func initTestCases() []testCase {
+ testCases := []testCase{}
+
+ testCases = append(testCases, testCase{
+ name: "empty args",
+ args: []string{},
+ attributes: make(map[string]bool),
+ expected: []string{},
+ })
+
+ jsonAttributes := make(map[string]bool)
+ jsonAttributes["json"] = true
+
+ testCases = append(testCases, testCase{
+ name: "json attribute with one element",
+ args: []string{"foo"},
+ attributes: jsonAttributes,
+ expected: []string{"foo"},
+ })
+
+ testCases = append(testCases, testCase{
+ name: "json attribute with two elements",
+ args: []string{"foo", "bar"},
+ attributes: jsonAttributes,
+ expected: []string{"foo", "bar"},
+ })
+
+ testCases = append(testCases, testCase{
+ name: "no attributes",
+ args: []string{"foo", "bar"},
+ attributes: nil,
+ expected: []string{"foo bar"},
+ })
+
+ return testCases
+}
+
+func TestHandleJSONArgs(t *testing.T) {
+ testCases := initTestCases()
+
+ for _, test := range testCases {
+ arguments := handleJSONArgs(test.args, test.attributes)
+
+ if len(arguments) != len(test.expected) {
+ t.Fatalf("In test \"%s\": length of returned slice is incorrect. Expected: %d, got: %d", test.name, len(test.expected), len(arguments))
+ }
+
+ for i := range test.expected {
+ if arguments[i] != test.expected[i] {
+ t.Fatalf("In test \"%s\": element as position %d is incorrect. Expected: %s, got: %s", test.name, i, test.expected[i], arguments[i])
+ }
+ }
+ }
+}