Browse code

builder: handle escapes without swallowing all of them.

Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <github@hollensbe.org> (github: erikh)

Erik Hollensbe authored on 2014/10/26 02:58:57
Showing 2 changed files
... ...
@@ -24,8 +24,9 @@ func (b *Builder) replaceEnv(str string) string {
24 24
 				continue
25 25
 			}
26 26
 
27
+			prefix := match[:idx]
27 28
 			stripped := match[idx+2:]
28
-			str = strings.Replace(str, match, "$"+stripped, -1)
29
+			str = strings.Replace(str, match, prefix+"$"+stripped, -1)
29 30
 			continue
30 31
 		}
31 32
 
... ...
@@ -15,6 +15,92 @@ import (
15 15
 	"github.com/docker/docker/pkg/archive"
16 16
 )
17 17
 
18
+func TestBuildHandleEscapes(t *testing.T) {
19
+	name := "testbuildhandleescapes"
20
+
21
+	defer deleteImages(name)
22
+
23
+	_, err := buildImage(name,
24
+		`
25
+  FROM scratch
26
+  ENV FOO bar
27
+  VOLUME ${FOO}
28
+  `, true)
29
+
30
+	if err != nil {
31
+		t.Fatal(err)
32
+	}
33
+
34
+	var result map[string]map[string]struct{}
35
+
36
+	res, err := inspectFieldJSON(name, "Config.Volumes")
37
+	if err != nil {
38
+		t.Fatal(err)
39
+	}
40
+
41
+	if err = unmarshalJSON([]byte(res), &result); err != nil {
42
+		t.Fatal(err)
43
+	}
44
+
45
+	if _, ok := result["bar"]; !ok {
46
+		t.Fatal("Could not find volume bar set from env foo in volumes table")
47
+	}
48
+
49
+	_, err = buildImage(name,
50
+		`
51
+  FROM scratch
52
+  ENV FOO bar
53
+  VOLUME \${FOO}
54
+  `, true)
55
+
56
+	if err != nil {
57
+		t.Fatal(err)
58
+	}
59
+
60
+	res, err = inspectFieldJSON(name, "Config.Volumes")
61
+	if err != nil {
62
+		t.Fatal(err)
63
+	}
64
+
65
+	if err = unmarshalJSON([]byte(res), &result); err != nil {
66
+		t.Fatal(err)
67
+	}
68
+
69
+	if _, ok := result["${FOO}"]; !ok {
70
+		t.Fatal("Could not find volume ${FOO} set from env foo in volumes table")
71
+	}
72
+
73
+	// this test in particular provides *7* backslashes and expects 6 to come back.
74
+	// Like above, the first escape is swallowed and the rest are treated as
75
+	// literals, this one is just less obvious because of all the character noise.
76
+
77
+	_, err = buildImage(name,
78
+		`
79
+  FROM scratch
80
+  ENV FOO bar
81
+  VOLUME \\\\\\\${FOO}
82
+  `, true)
83
+
84
+	if err != nil {
85
+		t.Fatal(err)
86
+	}
87
+
88
+	res, err = inspectFieldJSON(name, "Config.Volumes")
89
+	if err != nil {
90
+		t.Fatal(err)
91
+	}
92
+
93
+	if err = unmarshalJSON([]byte(res), &result); err != nil {
94
+		t.Fatal(err)
95
+	}
96
+
97
+	if _, ok := result[`\\\\\\${FOO}`]; !ok {
98
+		t.Fatal(`Could not find volume \\\\\\${FOO} set from env foo in volumes table`)
99
+	}
100
+
101
+	logDone("build - handle escapes")
102
+}
103
+
18 104
 func TestBuildOnBuildLowercase(t *testing.T) {
19 105
 	name := "testbuildonbuildlowercase"
20 106
 	name2 := "testbuildonbuildlowercase2"