Browse code

Refactor docker_cli_build_test.go

Use `testutil/cmd` for `buildCommand`.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2017/01/11 03:16:25
Showing 4 changed files
... ...
@@ -27,49 +27,29 @@ import (
27 27
 )
28 28
 
29 29
 func (s *DockerSuite) TestBuildJSONEmptyRun(c *check.C) {
30
-	name := "testbuildjsonemptyrun"
31
-
32
-	_, err := buildImage(
33
-		name,
34
-		`
30
+	buildImageSuccessfully(c, "testbuildjsonemptyrun", withDockerfile(`
35 31
     FROM busybox
36 32
     RUN []
37
-    `,
38
-		true)
39
-
40
-	if err != nil {
41
-		c.Fatal("error when dealing with a RUN statement with empty JSON array")
42
-	}
43
-
33
+    `))
44 34
 }
45 35
 
46 36
 func (s *DockerSuite) TestBuildShCmdJSONEntrypoint(c *check.C) {
47 37
 	name := "testbuildshcmdjsonentrypoint"
38
+	expected := "/bin/sh -c echo test"
39
+	if daemonPlatform == "windows" {
40
+		expected = "cmd /S /C echo test"
41
+	}
48 42
 
49
-	_, err := buildImage(
50
-		name,
51
-		`
43
+	buildImageSuccessfully(c, name, withDockerfile(`
52 44
     FROM busybox
53 45
     ENTRYPOINT ["echo"]
54 46
     CMD echo test
55
-    `,
56
-		true)
57
-	if err != nil {
58
-		c.Fatal(err)
59
-	}
60
-
47
+    `))
61 48
 	out, _ := dockerCmd(c, "run", "--rm", name)
62 49
 
63
-	if daemonPlatform == "windows" {
64
-		if !strings.Contains(out, "cmd /S /C echo test") {
65
-			c.Fatalf("CMD did not contain cmd /S /C echo test : %q", out)
66
-		}
67
-	} else {
68
-		if strings.TrimSpace(out) != "/bin/sh -c echo test" {
69
-			c.Fatalf("CMD did not contain /bin/sh -c : %q", out)
70
-		}
50
+	if strings.TrimSpace(out) != expected {
51
+		c.Fatalf("CMD did not contain %q : %q", expected, out)
71 52
 	}
72
-
73 53
 }
74 54
 
75 55
 func (s *DockerSuite) TestBuildEnvironmentReplacementUser(c *check.C) {
... ...
@@ -77,21 +57,16 @@ func (s *DockerSuite) TestBuildEnvironmentReplacementUser(c *check.C) {
77 77
 	testRequires(c, DaemonIsLinux)
78 78
 	name := "testbuildenvironmentreplacement"
79 79
 
80
-	_, err := buildImage(name, `
80
+	buildImageSuccessfully(c, name, withDockerfile(`
81 81
   FROM scratch
82 82
   ENV user foo
83 83
   USER ${user}
84
-  `, true)
85
-	if err != nil {
86
-		c.Fatal(err)
87
-	}
88
-
84
+  `))
89 85
 	res := inspectFieldJSON(c, name, "Config.User")
90 86
 
91 87
 	if res != `"foo"` {
92 88
 		c.Fatal("User foo from environment not in Config.User on image")
93 89
 	}
94
-
95 90
 }
96 91
 
97 92
 func (s *DockerSuite) TestBuildEnvironmentReplacementVolume(c *check.C) {
... ...
@@ -105,23 +80,14 @@ func (s *DockerSuite) TestBuildEnvironmentReplacementVolume(c *check.C) {
105 105
 		volumePath = "/quux"
106 106
 	}
107 107
 
108
-	_, err := buildImage(name, `
108
+	buildImageSuccessfully(c, name, withDockerfile(`
109 109
   FROM `+minimalBaseImage()+`
110 110
   ENV volume `+volumePath+`
111 111
   VOLUME ${volume}
112
-  `, true)
113
-	if err != nil {
114
-		c.Fatal(err)
115
-	}
116
-
117
-	res := inspectFieldJSON(c, name, "Config.Volumes")
112
+  `))
118 113
 
119 114
 	var volumes map[string]interface{}
120
-
121
-	if err := json.Unmarshal([]byte(res), &volumes); err != nil {
122
-		c.Fatal(err)
123
-	}
124
-
115
+	inspectFieldAndUnmarshall(c, name, "Config.Volumes", &volumes)
125 116
 	if _, ok := volumes[volumePath]; !ok {
126 117
 		c.Fatal("Volume " + volumePath + " from environment not in Config.Volumes on image")
127 118
 	}
... ...
@@ -133,27 +99,17 @@ func (s *DockerSuite) TestBuildEnvironmentReplacementExpose(c *check.C) {
133 133
 	testRequires(c, DaemonIsLinux)
134 134
 	name := "testbuildenvironmentreplacement"
135 135
 
136
-	_, err := buildImage(name, `
136
+	buildImageSuccessfully(c, name, withDockerfile(`
137 137
   FROM scratch
138 138
   ENV port 80
139 139
   EXPOSE ${port}
140 140
   ENV ports "  99   100 "
141 141
   EXPOSE ${ports}
142
-  `, true)
143
-	if err != nil {
144
-		c.Fatal(err)
145
-	}
146
-
147
-	res := inspectFieldJSON(c, name, "Config.ExposedPorts")
142
+  `))
148 143
 
149 144
 	var exposedPorts map[string]interface{}
150
-
151
-	if err := json.Unmarshal([]byte(res), &exposedPorts); err != nil {
152
-		c.Fatal(err)
153
-	}
154
-
145
+	inspectFieldAndUnmarshall(c, name, "Config.ExposedPorts", &exposedPorts)
155 146
 	exp := []int{80, 99, 100}
156
-
157 147
 	for _, p := range exp {
158 148
 		tmp := fmt.Sprintf("%d/tcp", p)
159 149
 		if _, ok := exposedPorts[tmp]; !ok {
... ...
@@ -166,23 +122,28 @@ func (s *DockerSuite) TestBuildEnvironmentReplacementExpose(c *check.C) {
166 166
 func (s *DockerSuite) TestBuildEnvironmentReplacementWorkdir(c *check.C) {
167 167
 	name := "testbuildenvironmentreplacement"
168 168
 
169
-	_, err := buildImage(name, `
169
+	buildImageSuccessfully(c, name, withDockerfile(`
170 170
   FROM busybox
171 171
   ENV MYWORKDIR /work
172 172
   RUN mkdir ${MYWORKDIR}
173 173
   WORKDIR ${MYWORKDIR}
174
-  `, true)
174
+  `))
175
+	res := inspectFieldJSON(c, name, "Config.WorkingDir")
175 176
 
176
-	if err != nil {
177
-		c.Fatal(err)
177
+	expected := `"/work"`
178
+	if daemonPlatform == "windows" {
179
+		expected = `"C:\\work"`
180
+	}
181
+	if res != expected {
182
+		c.Fatalf("Workdir /workdir from environment not in Config.WorkingDir on image: %s", res)
178 183
 	}
179
-
180 184
 }
181 185
 
182 186
 func (s *DockerSuite) TestBuildEnvironmentReplacementAddCopy(c *check.C) {
183 187
 	name := "testbuildenvironmentreplacement"
184 188
 
185
-	ctx, err := fakeContext(`
189
+	buildImageSuccessfully(c, name, withBuildContext(c,
190
+		withFile("Dockerfile", `
186 191
   FROM `+minimalBaseImage()+`
187 192
   ENV baz foo
188 193
   ENV quux bar
... ...
@@ -194,23 +155,12 @@ func (s *DockerSuite) TestBuildEnvironmentReplacementAddCopy(c *check.C) {
194 194
   COPY ${quux} ${dot}
195 195
   ADD ${zzz:-${fee}} ${dot}
196 196
   COPY ${zzz:-${gee}} ${dot}
197
-  `,
198
-		map[string]string{
199
-			"foo": "test1",
200
-			"bar": "test2",
201
-			"fff": "test3",
202
-			"ggg": "test4",
203
-		})
204
-
205
-	if err != nil {
206
-		c.Fatal(err)
207
-	}
208
-	defer ctx.Close()
209
-
210
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
211
-		c.Fatal(err)
212
-	}
213
-
197
+  `),
198
+		withFile("foo", "test1"),
199
+		withFile("bar", "test2"),
200
+		withFile("fff", "test3"),
201
+		withFile("ggg", "test4"),
202
+	))
214 203
 }
215 204
 
216 205
 func (s *DockerSuite) TestBuildEnvironmentReplacementEnv(c *check.C) {
... ...
@@ -218,8 +168,7 @@ func (s *DockerSuite) TestBuildEnvironmentReplacementEnv(c *check.C) {
218 218
 	testRequires(c, DaemonIsLinux)
219 219
 	name := "testbuildenvironmentreplacement"
220 220
 
221
-	_, err := buildImage(name,
222
-		`
221
+	buildImageSuccessfully(c, name, withDockerfile(`
223 222
   FROM busybox
224 223
   ENV foo zzz
225 224
   ENV bar ${foo}
... ...
@@ -232,20 +181,10 @@ func (s *DockerSuite) TestBuildEnvironmentReplacementEnv(c *check.C) {
232 232
   RUN [ "$abc3" = '$foo' ] && (echo "$abc3" | grep -q foo)
233 233
   ENV abc4 "\$foo"
234 234
   RUN [ "$abc4" = '$foo' ] && (echo "$abc4" | grep -q foo)
235
-  `, true)
236
-
237
-	if err != nil {
238
-		c.Fatal(err)
239
-	}
240
-
241
-	res := inspectFieldJSON(c, name, "Config.Env")
235
+  `))
242 236
 
243 237
 	envResult := []string{}
244
-
245
-	if err = json.Unmarshal([]byte(res), &envResult); err != nil {
246
-		c.Fatal(err)
247
-	}
248
-
238
+	inspectFieldAndUnmarshall(c, name, "Config.Env", &envResult)
249 239
 	found := false
250 240
 	envCount := 0
251 241
 
... ...
@@ -279,114 +218,70 @@ func (s *DockerSuite) TestBuildEnvironmentReplacementEnv(c *check.C) {
279 279
 
280 280
 }
281 281
 
282
-func (s *DockerSuite) TestBuildHandleEscapes(c *check.C) {
282
+func (s *DockerSuite) TestBuildHandleEscapesInVolume(c *check.C) {
283 283
 	// The volume paths used in this test are invalid on Windows
284 284
 	testRequires(c, DaemonIsLinux)
285 285
 	name := "testbuildhandleescapes"
286 286
 
287
-	_, err := buildImage(name,
288
-		`
289
-  FROM scratch
290
-  ENV FOO bar
291
-  VOLUME ${FOO}
292
-  `, true)
293
-
294
-	if err != nil {
295
-		c.Fatal(err)
296
-	}
297
-
298
-	var result map[string]map[string]struct{}
299
-
300
-	res := inspectFieldJSON(c, name, "Config.Volumes")
301
-
302
-	if err = json.Unmarshal([]byte(res), &result); err != nil {
303
-		c.Fatal(err)
304
-	}
305
-
306
-	if _, ok := result["bar"]; !ok {
307
-		c.Fatalf("Could not find volume bar set from env foo in volumes table, got %q", result)
308
-	}
309
-
310
-	deleteImages(name)
311
-
312
-	_, err = buildImage(name,
313
-		`
314
-  FROM scratch
315
-  ENV FOO bar
316
-  VOLUME \${FOO}
317
-  `, true)
318
-
319
-	if err != nil {
320
-		c.Fatal(err)
321
-	}
322
-
323
-	res = inspectFieldJSON(c, name, "Config.Volumes")
324
-
325
-	if err = json.Unmarshal([]byte(res), &result); err != nil {
326
-		c.Fatal(err)
327
-	}
328
-
329
-	if _, ok := result["${FOO}"]; !ok {
330
-		c.Fatalf("Could not find volume ${FOO} set from env foo in volumes table, got %q", result)
287
+	testCases := []struct {
288
+		volumeValue string
289
+		expected    string
290
+	}{
291
+		{
292
+			volumeValue: "${FOO}",
293
+			expected:    "bar",
294
+		},
295
+		{
296
+			volumeValue: `\${FOO}`,
297
+			expected:    "${FOO}",
298
+		},
299
+		// this test in particular provides *7* backslashes and expects 6 to come back.
300
+		// Like above, the first escape is swallowed and the rest are treated as
301
+		// literals, this one is just less obvious because of all the character noise.
302
+		{
303
+			volumeValue: `\\\\\\\${FOO}`,
304
+			expected:    `\\\${FOO}`,
305
+		},
331 306
 	}
332 307
 
333
-	deleteImages(name)
334
-
335
-	// this test in particular provides *7* backslashes and expects 6 to come back.
336
-	// Like above, the first escape is swallowed and the rest are treated as
337
-	// literals, this one is just less obvious because of all the character noise.
338
-
339
-	_, err = buildImage(name,
340
-		`
308
+	for _, tc := range testCases {
309
+		buildImageSuccessfully(c, name, withDockerfile(fmt.Sprintf(`
341 310
   FROM scratch
342 311
   ENV FOO bar
343
-  VOLUME \\\\\\\${FOO}
344
-  `, true)
345
-
346
-	if err != nil {
347
-		c.Fatal(err)
348
-	}
312
+  VOLUME %s
313
+  `, tc.volumeValue)))
349 314
 
350
-	res = inspectFieldJSON(c, name, "Config.Volumes")
351
-
352
-	if err = json.Unmarshal([]byte(res), &result); err != nil {
353
-		c.Fatal(err)
354
-	}
315
+		var result map[string]map[string]struct{}
316
+		inspectFieldAndUnmarshall(c, name, "Config.Volumes", &result)
317
+		if _, ok := result[tc.expected]; !ok {
318
+			c.Fatalf("Could not find volume %s set from env foo in volumes table, got %q", tc.expected, result)
319
+		}
355 320
 
356
-	if _, ok := result[`\\\${FOO}`]; !ok {
357
-		c.Fatalf(`Could not find volume \\\${FOO} set from env foo in volumes table, got %q`, result)
321
+		// Remove the image for the next iteration
322
+		dockerCmd(c, "rmi", name)
358 323
 	}
359
-
360 324
 }
361 325
 
362 326
 func (s *DockerSuite) TestBuildOnBuildLowercase(c *check.C) {
363 327
 	name := "testbuildonbuildlowercase"
364 328
 	name2 := "testbuildonbuildlowercase2"
365 329
 
366
-	_, err := buildImage(name,
367
-		`
330
+	buildImageSuccessfully(c, name, withDockerfile(`
368 331
   FROM busybox
369 332
   onbuild run echo quux
370
-  `, true)
333
+  `))
371 334
 
372
-	if err != nil {
373
-		c.Fatal(err)
374
-	}
375
-
376
-	_, out, err := buildImageWithOut(name2, fmt.Sprintf(`
335
+	result := buildImageNew(name2, withDockerfile(fmt.Sprintf(`
377 336
   FROM %s
378
-  `, name), true)
337
+  `, name)))
338
+	result.Assert(c, icmd.Success)
379 339
 
380
-	if err != nil {
381
-		c.Fatal(err)
340
+	if !strings.Contains(result.Combined(), "quux") {
341
+		c.Fatalf("Did not receive the expected echo text, got %s", result.Combined())
382 342
 	}
383 343
 
384
-	if !strings.Contains(out, "quux") {
385
-		c.Fatalf("Did not receive the expected echo text, got %s", out)
386
-	}
387
-
388
-	if strings.Contains(out, "ONBUILD ONBUILD") {
389
-		c.Fatalf("Got an ONBUILD ONBUILD error with no error: got %s", out)
344
+	if strings.Contains(result.Combined(), "ONBUILD ONBUILD") {
345
+		c.Fatalf("Got an ONBUILD ONBUILD error with no error: got %s", result.Combined())
390 346
 	}
391 347
 
392 348
 }
... ...
@@ -395,20 +290,13 @@ func (s *DockerSuite) TestBuildEnvEscapes(c *check.C) {
395 395
 	// ENV expansions work differently in Windows
396 396
 	testRequires(c, DaemonIsLinux)
397 397
 	name := "testbuildenvescapes"
398
-	_, err := buildImage(name,
399
-		`
398
+	buildImageSuccessfully(c, name, withDockerfile(`
400 399
     FROM busybox
401 400
     ENV TEST foo
402 401
     CMD echo \$
403
-    `,
404
-		true)
405
-
406
-	if err != nil {
407
-		c.Fatal(err)
408
-	}
402
+    `))
409 403
 
410 404
 	out, _ := dockerCmd(c, "run", "-t", name)
411
-
412 405
 	if strings.TrimSpace(out) != "$" {
413 406
 		c.Fatalf("Env TEST was not overwritten with bar when foo was supplied to dockerfile: was %q", strings.TrimSpace(out))
414 407
 	}
... ...
@@ -419,77 +307,51 @@ func (s *DockerSuite) TestBuildEnvOverwrite(c *check.C) {
419 419
 	// ENV expansions work differently in Windows
420 420
 	testRequires(c, DaemonIsLinux)
421 421
 	name := "testbuildenvoverwrite"
422
-
423
-	_, err := buildImage(name,
424
-		`
422
+	buildImageSuccessfully(c, name, withDockerfile(`
425 423
     FROM busybox
426 424
     ENV TEST foo
427 425
     CMD echo ${TEST}
428
-    `,
429
-		true)
430
-
431
-	if err != nil {
432
-		c.Fatal(err)
433
-	}
426
+    `))
434 427
 
435 428
 	out, _ := dockerCmd(c, "run", "-e", "TEST=bar", "-t", name)
436
-
437 429
 	if strings.TrimSpace(out) != "bar" {
438 430
 		c.Fatalf("Env TEST was not overwritten with bar when foo was supplied to dockerfile: was %q", strings.TrimSpace(out))
439 431
 	}
440 432
 
441 433
 }
442 434
 
435
+// FIXME(vdemeester) why we disabled cache here ?
443 436
 func (s *DockerSuite) TestBuildOnBuildCmdEntrypointJSON(c *check.C) {
444 437
 	name1 := "onbuildcmd"
445 438
 	name2 := "onbuildgenerated"
446 439
 
447
-	_, err := buildImage(name1, `
440
+	buildImageSuccessfully(c, name1, withDockerfile(`
448 441
 FROM busybox
449 442
 ONBUILD CMD ["hello world"]
450 443
 ONBUILD ENTRYPOINT ["echo"]
451
-ONBUILD RUN ["true"]`,
452
-		false)
453
-
454
-	if err != nil {
455
-		c.Fatal(err)
456
-	}
457
-
458
-	_, err = buildImage(name2, fmt.Sprintf(`FROM %s`, name1), false)
444
+ONBUILD RUN ["true"]`))
459 445
 
460
-	if err != nil {
461
-		c.Fatal(err)
462
-	}
446
+	buildImageSuccessfully(c, name2, withDockerfile(fmt.Sprintf(`FROM %s`, name1)))
463 447
 
464 448
 	out, _ := dockerCmd(c, "run", name2)
465
-
466 449
 	if !regexp.MustCompile(`(?m)^hello world`).MatchString(out) {
467 450
 		c.Fatalf("did not get echo output from onbuild. Got: %q", out)
468 451
 	}
469 452
 
470 453
 }
471 454
 
455
+// FIXME(vdemeester) why we disabled cache here ?
472 456
 func (s *DockerSuite) TestBuildOnBuildEntrypointJSON(c *check.C) {
473 457
 	name1 := "onbuildcmd"
474 458
 	name2 := "onbuildgenerated"
475 459
 
476
-	_, err := buildImage(name1, `
460
+	buildImageSuccessfully(c, name1, withDockerfile(`
477 461
 FROM busybox
478
-ONBUILD ENTRYPOINT ["echo"]`,
479
-		false)
462
+ONBUILD ENTRYPOINT ["echo"]`))
480 463
 
481
-	if err != nil {
482
-		c.Fatal(err)
483
-	}
484
-
485
-	_, err = buildImage(name2, fmt.Sprintf("FROM %s\nCMD [\"hello world\"]\n", name1), false)
486
-
487
-	if err != nil {
488
-		c.Fatal(err)
489
-	}
464
+	buildImageSuccessfully(c, name2, withDockerfile(fmt.Sprintf("FROM %s\nCMD [\"hello world\"]\n", name1)))
490 465
 
491 466
 	out, _ := dockerCmd(c, "run", name2)
492
-
493 467
 	if !regexp.MustCompile(`(?m)^hello world`).MatchString(out) {
494 468
 		c.Fatal("got malformed output from onbuild", out)
495 469
 	}
... ...
@@ -626,8 +488,8 @@ ADD folder/file /test/changetarget`,
626 626
 
627 627
 func (s *DockerSuite) TestBuildAddSingleFileToRoot(c *check.C) {
628 628
 	testRequires(c, DaemonIsLinux) // Linux specific test
629
-	name := "testaddimg"
630
-	ctx, err := fakeContext(fmt.Sprintf(`FROM busybox
629
+	buildImageSuccessfully(c, "testaddimg", withBuildContext(c,
630
+		withFile("Dockerfile", fmt.Sprintf(`FROM busybox
631 631
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
632 632
 RUN echo 'dockerio:x:1001:' >> /etc/group
633 633
 RUN touch /exists
... ...
@@ -635,18 +497,8 @@ RUN chown dockerio.dockerio /exists
635 635
 ADD test_file /
636 636
 RUN [ $(ls -l /test_file | awk '{print $3":"$4}') = 'root:root' ]
637 637
 RUN [ $(ls -l /test_file | awk '{print $1}') = '%s' ]
638
-RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`, expectedFileChmod),
639
-		map[string]string{
640
-			"test_file": "test1",
641
-		})
642
-	if err != nil {
643
-		c.Fatal(err)
644
-	}
645
-	defer ctx.Close()
646
-
647
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
648
-		c.Fatal(err)
649
-	}
638
+RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`, expectedFileChmod)),
639
+		withFile("test_file", "test1")))
650 640
 }
651 641
 
652 642
 // Issue #3960: "ADD src ." hangs
... ...
@@ -678,8 +530,8 @@ ADD test_file .`,
678 678
 
679 679
 func (s *DockerSuite) TestBuildAddSingleFileToExistDir(c *check.C) {
680 680
 	testRequires(c, DaemonIsLinux) // Linux specific test
681
-	name := "testaddsinglefiletoexistdir"
682
-	ctx, err := fakeContext(`FROM busybox
681
+	buildImageSuccessfully(c, "testaddsinglefiletoexistdir", withBuildContext(c,
682
+		withFile("Dockerfile", `FROM busybox
683 683
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
684 684
 RUN echo 'dockerio:x:1001:' >> /etc/group
685 685
 RUN mkdir /exists
... ...
@@ -688,18 +540,8 @@ RUN chown -R dockerio.dockerio /exists
688 688
 ADD test_file /exists/
689 689
 RUN [ $(ls -l / | grep exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
690 690
 RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]
691
-RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
692
-		map[string]string{
693
-			"test_file": "test1",
694
-		})
695
-	if err != nil {
696
-		c.Fatal(err)
697
-	}
698
-	defer ctx.Close()
699
-
700
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
701
-		c.Fatal(err)
702
-	}
691
+RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`),
692
+		withFile("test_file", "test1")))
703 693
 }
704 694
 
705 695
 func (s *DockerSuite) TestBuildCopyAddMultipleFiles(c *check.C) {
... ...
@@ -712,8 +554,8 @@ func (s *DockerSuite) TestBuildCopyAddMultipleFiles(c *check.C) {
712 712
 	}
713 713
 	defer server.Close()
714 714
 
715
-	name := "testcopymultiplefilestofile"
716
-	ctx, err := fakeContext(fmt.Sprintf(`FROM busybox
715
+	buildImageSuccessfully(c, "testcopymultiplefilestofile", withBuildContext(c,
716
+		withFile("Dockerfile", fmt.Sprintf(`FROM busybox
717 717
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
718 718
 RUN echo 'dockerio:x:1001:' >> /etc/group
719 719
 RUN mkdir /exists
... ...
@@ -724,141 +566,75 @@ ADD test_file3 test_file4 %s/robots.txt /exists/
724 724
 RUN [ $(ls -l / | grep exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
725 725
 RUN [ $(ls -l /exists/test_file1 | awk '{print $3":"$4}') = 'root:root' ]
726 726
 RUN [ $(ls -l /exists/test_file2 | awk '{print $3":"$4}') = 'root:root' ]
727
-
728 727
 RUN [ $(ls -l /exists/test_file3 | awk '{print $3":"$4}') = 'root:root' ]
729 728
 RUN [ $(ls -l /exists/test_file4 | awk '{print $3":"$4}') = 'root:root' ]
730 729
 RUN [ $(ls -l /exists/robots.txt | awk '{print $3":"$4}') = 'root:root' ]
731
-
732 730
 RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
733
-`, server.URL()),
734
-		map[string]string{
735
-			"test_file1": "test1",
736
-			"test_file2": "test2",
737
-			"test_file3": "test3",
738
-			"test_file4": "test4",
739
-		})
740
-	if err != nil {
741
-		c.Fatal(err)
742
-	}
743
-	defer ctx.Close()
744
-
745
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
746
-		c.Fatal(err)
747
-	}
748
-}
749
-
750
-// This test is mainly for user namespaces to verify that new directories
751
-// are created as the remapped root uid/gid pair
752
-func (s *DockerSuite) TestBuildAddToNewDestination(c *check.C) {
753
-	testRequires(c, DaemonIsLinux) // Linux specific test
754
-	name := "testaddtonewdest"
755
-	ctx, err := fakeContext(`FROM busybox
756
-ADD . /new_dir
757
-RUN ls -l /
758
-RUN [ $(ls -l / | grep new_dir | awk '{print $3":"$4}') = 'root:root' ]`,
759
-		map[string]string{
760
-			"test_dir/test_file": "test file",
761
-		})
762
-	if err != nil {
763
-		c.Fatal(err)
764
-	}
765
-	defer ctx.Close()
766
-
767
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
768
-		c.Fatal(err)
769
-	}
731
+`, server.URL())),
732
+		withFile("test_file1", "test1"),
733
+		withFile("test_file2", "test2"),
734
+		withFile("test_file3", "test3"),
735
+		withFile("test_file3", "test3"),
736
+		withFile("test_file4", "test4")))
770 737
 }
771 738
 
772
-// This test is mainly for user namespaces to verify that new directories
739
+// These tests are mainly for user namespaces to verify that new directories
773 740
 // are created as the remapped root uid/gid pair
774
-func (s *DockerSuite) TestBuildCopyToNewParentDirectory(c *check.C) {
775
-	testRequires(c, DaemonIsLinux) // Linux specific test
776
-	name := "testcopytonewdir"
777
-	ctx, err := fakeContext(`FROM busybox
778
-COPY test_dir /new_dir
779
-RUN ls -l /new_dir
780
-RUN [ $(ls -l / | grep new_dir | awk '{print $3":"$4}') = 'root:root' ]`,
781
-		map[string]string{
782
-			"test_dir/test_file": "test file",
783
-		})
784
-	if err != nil {
785
-		c.Fatal(err)
786
-	}
787
-	defer ctx.Close()
788
-
789
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
790
-		c.Fatal(err)
741
+func (s *DockerSuite) TestBuildUsernamespaceValidateRemappedRoot(c *check.C) {
742
+	testRequires(c, DaemonIsLinux)
743
+	testCases := []string{
744
+		"ADD . /new_dir",
745
+		"COPY test_dir /new_dir",
746
+		"WORKDIR /new_dir",
791 747
 	}
792
-}
748
+	name := "testbuildusernamespacevalidateremappedroot"
749
+	for _, tc := range testCases {
750
+		buildImageSuccessfully(c, name, withBuildContext(c,
751
+			withFile("Dockerfile", fmt.Sprintf(`FROM busybox
752
+%s
753
+RUN [ $(ls -l / | grep new_dir | awk '{print $3":"$4}') = 'root:root' ]`, tc)),
754
+			withFile("test_dir/test_file", "test file")))
793 755
 
794
-// This test is mainly for user namespaces to verify that new directories
795
-// are created as the remapped root uid/gid pair
796
-func (s *DockerSuite) TestBuildWorkdirIsContainerRoot(c *check.C) {
797
-	testRequires(c, DaemonIsLinux) // Linux specific test
798
-	name := "testworkdirownership"
799
-	if _, err := buildImage(name, `FROM busybox
800
-WORKDIR /new_dir
801
-RUN ls -l /
802
-RUN [ $(ls -l / | grep new_dir | awk '{print $3":"$4}') = 'root:root' ]`, true); err != nil {
803
-		c.Fatal(err)
756
+		dockerCmd(c, "rmi", name)
804 757
 	}
805 758
 }
806 759
 
807
-func (s *DockerSuite) TestBuildAddFileWithWhitespace(c *check.C) {
760
+func (s *DockerSuite) TestBuildAddAndCopyFileWithWhitespace(c *check.C) {
808 761
 	testRequires(c, DaemonIsLinux) // Not currently passing on Windows
809 762
 	name := "testaddfilewithwhitespace"
810
-	ctx, err := fakeContext(`FROM busybox
763
+
764
+	for _, command := range []string{"ADD", "COPY"} {
765
+		buildImageSuccessfully(c, name, withBuildContext(c,
766
+			withFile("Dockerfile", fmt.Sprintf(`FROM busybox
811 767
 RUN mkdir "/test dir"
812 768
 RUN mkdir "/test_dir"
813
-ADD [ "test file1", "/test_file1" ]
814
-ADD [ "test_file2", "/test file2" ]
815
-ADD [ "test file3", "/test file3" ]
816
-ADD [ "test dir/test_file4", "/test_dir/test_file4" ]
817
-ADD [ "test_dir/test_file5", "/test dir/test_file5" ]
818
-ADD [ "test dir/test_file6", "/test dir/test_file6" ]
769
+%s [ "test file1", "/test_file1" ]
770
+%s [ "test_file2", "/test file2" ]
771
+%s [ "test file3", "/test file3" ]
772
+%s [ "test dir/test_file4", "/test_dir/test_file4" ]
773
+%s [ "test_dir/test_file5", "/test dir/test_file5" ]
774
+%s [ "test dir/test_file6", "/test dir/test_file6" ]
819 775
 RUN [ $(cat "/test_file1") = 'test1' ]
820 776
 RUN [ $(cat "/test file2") = 'test2' ]
821 777
 RUN [ $(cat "/test file3") = 'test3' ]
822 778
 RUN [ $(cat "/test_dir/test_file4") = 'test4' ]
823 779
 RUN [ $(cat "/test dir/test_file5") = 'test5' ]
824
-RUN [ $(cat "/test dir/test_file6") = 'test6' ]`,
825
-		map[string]string{
826
-			"test file1":          "test1",
827
-			"test_file2":          "test2",
828
-			"test file3":          "test3",
829
-			"test dir/test_file4": "test4",
830
-			"test_dir/test_file5": "test5",
831
-			"test dir/test_file6": "test6",
832
-		})
833
-	if err != nil {
834
-		c.Fatal(err)
835
-	}
836
-	defer ctx.Close()
780
+RUN [ $(cat "/test dir/test_file6") = 'test6' ]`, command, command, command, command, command, command)),
781
+			withFile("test file1", "test1"),
782
+			withFile("test_file2", "test2"),
783
+			withFile("test file3", "test3"),
784
+			withFile("test dir/test_file4", "test4"),
785
+			withFile("test_dir/test_file5", "test5"),
786
+			withFile("test dir/test_file6", "test6"),
787
+		))
837 788
 
838
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
839
-		c.Fatal(err)
789
+		dockerCmd(c, "rmi", name)
840 790
 	}
841 791
 }
842 792
 
843
-func (s *DockerSuite) TestBuildCopyFileWithWhitespace(c *check.C) {
844
-	dockerfile := `FROM busybox
845
-RUN mkdir "/test dir"
846
-RUN mkdir "/test_dir"
847
-COPY [ "test file1", "/test_file1" ]
848
-COPY [ "test_file2", "/test file2" ]
849
-COPY [ "test file3", "/test file3" ]
850
-COPY [ "test dir/test_file4", "/test_dir/test_file4" ]
851
-COPY [ "test_dir/test_file5", "/test dir/test_file5" ]
852
-COPY [ "test dir/test_file6", "/test dir/test_file6" ]
853
-RUN [ $(cat "/test_file1") = 'test1' ]
854
-RUN [ $(cat "/test file2") = 'test2' ]
855
-RUN [ $(cat "/test file3") = 'test3' ]
856
-RUN [ $(cat "/test_dir/test_file4") = 'test4' ]
857
-RUN [ $(cat "/test dir/test_file5") = 'test5' ]
858
-RUN [ $(cat "/test dir/test_file6") = 'test6' ]`
859
-
860
-	if daemonPlatform == "windows" {
861
-		dockerfile = `FROM ` + WindowsBaseImage + `
793
+func (s *DockerSuite) TestBuildCopyFileWithWhitespaceOnWindows(c *check.C) {
794
+	testRequires(c, DaemonIsWindows)
795
+	dockerfile := `FROM ` + WindowsBaseImage + `
862 796
 RUN mkdir "C:/test dir"
863 797
 RUN mkdir "C:/test_dir"
864 798
 COPY [ "test file1", "/test_file1" ]
... ...
@@ -873,26 +649,17 @@ RUN find "test3" "C:/test file3"
873 873
 RUN find "test4" "C:/test_dir/test_file4"
874 874
 RUN find "test5" "C:/test dir/test_file5"
875 875
 RUN find "test6" "C:/test dir/test_file6"`
876
-	}
877 876
 
878 877
 	name := "testcopyfilewithwhitespace"
879
-	ctx, err := fakeContext(dockerfile,
880
-		map[string]string{
881
-			"test file1":          "test1",
882
-			"test_file2":          "test2",
883
-			"test file3":          "test3",
884
-			"test dir/test_file4": "test4",
885
-			"test_dir/test_file5": "test5",
886
-			"test dir/test_file6": "test6",
887
-		})
888
-	if err != nil {
889
-		c.Fatal(err)
890
-	}
891
-	defer ctx.Close()
892
-
893
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
894
-		c.Fatal(err)
895
-	}
878
+	buildImageSuccessfully(c, name, withBuildContext(c,
879
+		withFile("Dockerfile", dockerfile),
880
+		withFile("test file1", "test1"),
881
+		withFile("test_file2", "test2"),
882
+		withFile("test file3", "test3"),
883
+		withFile("test dir/test_file4", "test4"),
884
+		withFile("test_dir/test_file5", "test5"),
885
+		withFile("test dir/test_file6", "test6"),
886
+	))
896 887
 }
897 888
 
898 889
 func (s *DockerSuite) TestBuildCopyWildcard(c *check.C) {
... ...
@@ -946,27 +713,23 @@ func (s *DockerSuite) TestBuildCopyWildcard(c *check.C) {
946 946
 }
947 947
 
948 948
 func (s *DockerSuite) TestBuildCopyWildcardInName(c *check.C) {
949
-	name := "testcopywildcardinname"
950
-	ctx, err := fakeContext(`FROM busybox
949
+	// Run this only on Linux
950
+	// Below is the original comment (that I don't agree with — vdemeester)
951
+	// Normally we would do c.Fatal(err) here but given that
952
+	// the odds of this failing are so rare, it must be because
953
+	// the OS we're running the client on doesn't support * in
954
+	// filenames (like windows).  So, instead of failing the test
955
+	// just let it pass. Then we don't need to explicitly
956
+	// say which OSs this works on or not.
957
+	testRequires(c, DaemonIsLinux)
958
+
959
+	buildImageSuccessfully(c, "testcopywildcardinname", withBuildContext(c,
960
+		withFile("Dockerfile", `FROM busybox
951 961
 	COPY *.txt /tmp/
952 962
 	RUN [ "$(cat /tmp/\*.txt)" = 'hi there' ]
953
-	`, map[string]string{"*.txt": "hi there"})
954
-
955
-	if err != nil {
956
-		// Normally we would do c.Fatal(err) here but given that
957
-		// the odds of this failing are so rare, it must be because
958
-		// the OS we're running the client on doesn't support * in
959
-		// filenames (like windows).  So, instead of failing the test
960
-		// just let it pass. Then we don't need to explicitly
961
-		// say which OSs this works on or not.
962
-		return
963
-	}
964
-	defer ctx.Close()
965
-
966
-	_, err = buildImageFromContext(name, ctx, true)
967
-	if err != nil {
968
-		c.Fatalf("should have built: %q", err)
969
-	}
963
+	`),
964
+		withFile("*.txt", "hi there"),
965
+	))
970 966
 }
971 967
 
972 968
 func (s *DockerSuite) TestBuildCopyWildcardCache(c *check.C) {
... ...
@@ -1004,8 +767,8 @@ func (s *DockerSuite) TestBuildCopyWildcardCache(c *check.C) {
1004 1004
 
1005 1005
 func (s *DockerSuite) TestBuildAddSingleFileToNonExistingDir(c *check.C) {
1006 1006
 	testRequires(c, DaemonIsLinux) // Linux specific test
1007
-	name := "testaddsinglefiletononexistingdir"
1008
-	ctx, err := fakeContext(`FROM busybox
1007
+	buildImageSuccessfully(c, "testaddsinglefiletononexistingdir", withBuildContext(c,
1008
+		withFile("Dockerfile", `FROM busybox
1009 1009
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
1010 1010
 RUN echo 'dockerio:x:1001:' >> /etc/group
1011 1011
 RUN touch /exists
... ...
@@ -1013,49 +776,28 @@ RUN chown dockerio.dockerio /exists
1013 1013
 ADD test_file /test_dir/
1014 1014
 RUN [ $(ls -l / | grep test_dir | awk '{print $3":"$4}') = 'root:root' ]
1015 1015
 RUN [ $(ls -l /test_dir/test_file | awk '{print $3":"$4}') = 'root:root' ]
1016
-RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
1017
-		map[string]string{
1018
-			"test_file": "test1",
1019
-		})
1020
-	if err != nil {
1021
-		c.Fatal(err)
1022
-	}
1023
-	defer ctx.Close()
1024
-
1025
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1026
-		c.Fatal(err)
1027
-	}
1028
-
1016
+RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`),
1017
+		withFile("test_file", "test1")))
1029 1018
 }
1030 1019
 
1031 1020
 func (s *DockerSuite) TestBuildAddDirContentToRoot(c *check.C) {
1032 1021
 	testRequires(c, DaemonIsLinux) // Linux specific test
1033
-	name := "testadddircontenttoroot"
1034
-	ctx, err := fakeContext(`FROM busybox
1022
+	buildImageSuccessfully(c, "testadddircontenttoroot", withBuildContext(c,
1023
+		withFile("Dockerfile", `FROM busybox
1035 1024
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
1036 1025
 RUN echo 'dockerio:x:1001:' >> /etc/group
1037 1026
 RUN touch /exists
1038 1027
 RUN chown dockerio.dockerio exists
1039 1028
 ADD test_dir /
1040 1029
 RUN [ $(ls -l /test_file | awk '{print $3":"$4}') = 'root:root' ]
1041
-RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
1042
-		map[string]string{
1043
-			"test_dir/test_file": "test1",
1044
-		})
1045
-	if err != nil {
1046
-		c.Fatal(err)
1047
-	}
1048
-	defer ctx.Close()
1049
-
1050
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1051
-		c.Fatal(err)
1052
-	}
1030
+RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`),
1031
+		withFile("test_dir/test_file", "test1")))
1053 1032
 }
1054 1033
 
1055 1034
 func (s *DockerSuite) TestBuildAddDirContentToExistingDir(c *check.C) {
1056 1035
 	testRequires(c, DaemonIsLinux) // Linux specific test
1057
-	name := "testadddircontenttoexistingdir"
1058
-	ctx, err := fakeContext(`FROM busybox
1036
+	buildImageSuccessfully(c, "testadddircontenttoexistingdir", withBuildContext(c,
1037
+		withFile("Dockerfile", `FROM busybox
1059 1038
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
1060 1039
 RUN echo 'dockerio:x:1001:' >> /etc/group
1061 1040
 RUN mkdir /exists
... ...
@@ -1064,24 +806,14 @@ RUN chown -R dockerio.dockerio /exists
1064 1064
 ADD test_dir/ /exists/
1065 1065
 RUN [ $(ls -l / | grep exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
1066 1066
 RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
1067
-RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]`,
1068
-		map[string]string{
1069
-			"test_dir/test_file": "test1",
1070
-		})
1071
-	if err != nil {
1072
-		c.Fatal(err)
1073
-	}
1074
-	defer ctx.Close()
1075
-
1076
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1077
-		c.Fatal(err)
1078
-	}
1067
+RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]`),
1068
+		withFile("test_dir/test_file", "test1")))
1079 1069
 }
1080 1070
 
1081 1071
 func (s *DockerSuite) TestBuildAddWholeDirToRoot(c *check.C) {
1082 1072
 	testRequires(c, DaemonIsLinux) // Linux specific test
1083
-	name := "testaddwholedirtoroot"
1084
-	ctx, err := fakeContext(fmt.Sprintf(`FROM busybox
1073
+	buildImageSuccessfully(c, "testaddwholedirtoroot", withBuildContext(c,
1074
+		withFile("Dockerfile", fmt.Sprintf(`FROM busybox
1085 1075
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
1086 1076
 RUN echo 'dockerio:x:1001:' >> /etc/group
1087 1077
 RUN touch /exists
... ...
@@ -1091,67 +823,40 @@ RUN [ $(ls -l / | grep test_dir | awk '{print $3":"$4}') = 'root:root' ]
1091 1091
 RUN [ $(ls -l / | grep test_dir | awk '{print $1}') = 'drwxr-xr-x' ]
1092 1092
 RUN [ $(ls -l /test_dir/test_file | awk '{print $3":"$4}') = 'root:root' ]
1093 1093
 RUN [ $(ls -l /test_dir/test_file | awk '{print $1}') = '%s' ]
1094
-RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`, expectedFileChmod),
1095
-		map[string]string{
1096
-			"test_dir/test_file": "test1",
1097
-		})
1098
-	if err != nil {
1099
-		c.Fatal(err)
1100
-	}
1101
-	defer ctx.Close()
1102
-
1103
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1104
-		c.Fatal(err)
1105
-	}
1094
+RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`, expectedFileChmod)),
1095
+		withFile("test_dir/test_file", "test1")))
1106 1096
 }
1107 1097
 
1108
-// Testing #5941
1109
-func (s *DockerSuite) TestBuildAddEtcToRoot(c *check.C) {
1110
-	name := "testaddetctoroot"
1111
-
1112
-	ctx, err := fakeContext(`FROM `+minimalBaseImage()+`
1113
-ADD . /`,
1114
-		map[string]string{
1115
-			"etc/test_file": "test1",
1116
-		})
1117
-	if err != nil {
1118
-		c.Fatal(err)
1119
-	}
1120
-	defer ctx.Close()
1121
-
1122
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1123
-		c.Fatal(err)
1124
-	}
1098
+// Testing #5941 : Having an etc directory in context conflicts with the /etc/mtab
1099
+func (s *DockerSuite) TestBuildAddOrCopyEtcToRootShouldNotConflict(c *check.C) {
1100
+	buildImageSuccessfully(c, "testaddetctoroot", withBuildContext(c,
1101
+		withFile("Dockerfile", `FROM `+minimalBaseImage()+`
1102
+ADD . /`),
1103
+		withFile("etc/test_file", "test1")))
1104
+	buildImageSuccessfully(c, "testcopyetctoroot", withBuildContext(c,
1105
+		withFile("Dockerfile", `FROM `+minimalBaseImage()+`
1106
+COPY . /`),
1107
+		withFile("etc/test_file", "test1")))
1125 1108
 }
1126 1109
 
1127
-// Testing #9401
1110
+// Testing #9401 : Losing setuid flag after a ADD
1128 1111
 func (s *DockerSuite) TestBuildAddPreservesFilesSpecialBits(c *check.C) {
1129 1112
 	testRequires(c, DaemonIsLinux) // Linux specific test
1130
-	name := "testaddpreservesfilesspecialbits"
1131
-	ctx, err := fakeContext(`FROM busybox
1113
+	buildImageSuccessfully(c, "testaddetctoroot", withBuildContext(c,
1114
+		withFile("Dockerfile", `FROM busybox
1132 1115
 ADD suidbin /usr/bin/suidbin
1133 1116
 RUN chmod 4755 /usr/bin/suidbin
1134 1117
 RUN [ $(ls -l /usr/bin/suidbin | awk '{print $1}') = '-rwsr-xr-x' ]
1135 1118
 ADD ./data/ /
1136
-RUN [ $(ls -l /usr/bin/suidbin | awk '{print $1}') = '-rwsr-xr-x' ]`,
1137
-		map[string]string{
1138
-			"suidbin":             "suidbin",
1139
-			"/data/usr/test_file": "test1",
1140
-		})
1141
-	if err != nil {
1142
-		c.Fatal(err)
1143
-	}
1144
-	defer ctx.Close()
1145
-
1146
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1147
-		c.Fatal(err)
1148
-	}
1119
+RUN [ $(ls -l /usr/bin/suidbin | awk '{print $1}') = '-rwsr-xr-x' ]`),
1120
+		withFile("suidbin", "suidbin"),
1121
+		withFile("/data/usr/test_file", "test1")))
1149 1122
 }
1150 1123
 
1151 1124
 func (s *DockerSuite) TestBuildCopySingleFileToRoot(c *check.C) {
1152 1125
 	testRequires(c, DaemonIsLinux) // Linux specific test
1153
-	name := "testcopysinglefiletoroot"
1154
-	ctx, err := fakeContext(fmt.Sprintf(`FROM busybox
1126
+	buildImageSuccessfully(c, "testcopysinglefiletoroot", withBuildContext(c,
1127
+		withFile("Dockerfile", fmt.Sprintf(`FROM busybox
1155 1128
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
1156 1129
 RUN echo 'dockerio:x:1001:' >> /etc/group
1157 1130
 RUN touch /exists
... ...
@@ -1159,18 +864,8 @@ RUN chown dockerio.dockerio /exists
1159 1159
 COPY test_file /
1160 1160
 RUN [ $(ls -l /test_file | awk '{print $3":"$4}') = 'root:root' ]
1161 1161
 RUN [ $(ls -l /test_file | awk '{print $1}') = '%s' ]
1162
-RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`, expectedFileChmod),
1163
-		map[string]string{
1164
-			"test_file": "test1",
1165
-		})
1166
-	if err != nil {
1167
-		c.Fatal(err)
1168
-	}
1169
-	defer ctx.Close()
1170
-
1171
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1172
-		c.Fatal(err)
1173
-	}
1162
+RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`, expectedFileChmod)),
1163
+		withFile("test_file", "test1")))
1174 1164
 }
1175 1165
 
1176 1166
 // Issue #3960: "ADD src ." hangs - adapted for COPY
... ...
@@ -1202,8 +897,8 @@ COPY test_file .`,
1202 1202
 
1203 1203
 func (s *DockerSuite) TestBuildCopySingleFileToExistDir(c *check.C) {
1204 1204
 	testRequires(c, DaemonIsLinux) // Linux specific test
1205
-	name := "testcopysinglefiletoexistdir"
1206
-	ctx, err := fakeContext(`FROM busybox
1205
+	buildImageSuccessfully(c, "testcopysinglefiletoexistdir", withBuildContext(c,
1206
+		withFile("Dockerfile", `FROM busybox
1207 1207
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
1208 1208
 RUN echo 'dockerio:x:1001:' >> /etc/group
1209 1209
 RUN mkdir /exists
... ...
@@ -1212,24 +907,14 @@ RUN chown -R dockerio.dockerio /exists
1212 1212
 COPY test_file /exists/
1213 1213
 RUN [ $(ls -l / | grep exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
1214 1214
 RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]
1215
-RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
1216
-		map[string]string{
1217
-			"test_file": "test1",
1218
-		})
1219
-	if err != nil {
1220
-		c.Fatal(err)
1221
-	}
1222
-	defer ctx.Close()
1223
-
1224
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1225
-		c.Fatal(err)
1226
-	}
1215
+RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`),
1216
+		withFile("test_file", "test1")))
1227 1217
 }
1228 1218
 
1229 1219
 func (s *DockerSuite) TestBuildCopySingleFileToNonExistDir(c *check.C) {
1230
-	testRequires(c, DaemonIsLinux) // Linux specific test
1231
-	name := "testcopysinglefiletononexistdir"
1232
-	ctx, err := fakeContext(`FROM busybox
1220
+	testRequires(c, DaemonIsLinux) // Linux specific
1221
+	buildImageSuccessfully(c, "testcopysinglefiletononexistdir", withBuildContext(c,
1222
+		withFile("Dockerfile", `FROM busybox
1233 1223
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
1234 1224
 RUN echo 'dockerio:x:1001:' >> /etc/group
1235 1225
 RUN touch /exists
... ...
@@ -1237,48 +922,28 @@ RUN chown dockerio.dockerio /exists
1237 1237
 COPY test_file /test_dir/
1238 1238
 RUN [ $(ls -l / | grep test_dir | awk '{print $3":"$4}') = 'root:root' ]
1239 1239
 RUN [ $(ls -l /test_dir/test_file | awk '{print $3":"$4}') = 'root:root' ]
1240
-RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
1241
-		map[string]string{
1242
-			"test_file": "test1",
1243
-		})
1244
-	if err != nil {
1245
-		c.Fatal(err)
1246
-	}
1247
-	defer ctx.Close()
1248
-
1249
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1250
-		c.Fatal(err)
1251
-	}
1240
+RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`),
1241
+		withFile("test_file", "test1")))
1252 1242
 }
1253 1243
 
1254 1244
 func (s *DockerSuite) TestBuildCopyDirContentToRoot(c *check.C) {
1255 1245
 	testRequires(c, DaemonIsLinux) // Linux specific test
1256
-	name := "testcopydircontenttoroot"
1257
-	ctx, err := fakeContext(`FROM busybox
1246
+	buildImageSuccessfully(c, "testcopydircontenttoroot", withBuildContext(c,
1247
+		withFile("Dockerfile", `FROM busybox
1258 1248
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
1259 1249
 RUN echo 'dockerio:x:1001:' >> /etc/group
1260 1250
 RUN touch /exists
1261 1251
 RUN chown dockerio.dockerio exists
1262 1252
 COPY test_dir /
1263 1253
 RUN [ $(ls -l /test_file | awk '{print $3":"$4}') = 'root:root' ]
1264
-RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
1265
-		map[string]string{
1266
-			"test_dir/test_file": "test1",
1267
-		})
1268
-	if err != nil {
1269
-		c.Fatal(err)
1270
-	}
1271
-	defer ctx.Close()
1272
-
1273
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1274
-		c.Fatal(err)
1275
-	}
1254
+RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`),
1255
+		withFile("test_dir/test_file", "test1")))
1276 1256
 }
1277 1257
 
1278 1258
 func (s *DockerSuite) TestBuildCopyDirContentToExistDir(c *check.C) {
1279 1259
 	testRequires(c, DaemonIsLinux) // Linux specific test
1280
-	name := "testcopydircontenttoexistdir"
1281
-	ctx, err := fakeContext(`FROM busybox
1260
+	buildImageSuccessfully(c, "testcopydircontenttoexistdir", withBuildContext(c,
1261
+		withFile("Dockerfile", `FROM busybox
1282 1262
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
1283 1263
 RUN echo 'dockerio:x:1001:' >> /etc/group
1284 1264
 RUN mkdir /exists
... ...
@@ -1287,24 +952,14 @@ RUN chown -R dockerio.dockerio /exists
1287 1287
 COPY test_dir/ /exists/
1288 1288
 RUN [ $(ls -l / | grep exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
1289 1289
 RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
1290
-RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]`,
1291
-		map[string]string{
1292
-			"test_dir/test_file": "test1",
1293
-		})
1294
-	if err != nil {
1295
-		c.Fatal(err)
1296
-	}
1297
-	defer ctx.Close()
1298
-
1299
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1300
-		c.Fatal(err)
1301
-	}
1290
+RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]`),
1291
+		withFile("test_dir/test_file", "test1")))
1302 1292
 }
1303 1293
 
1304 1294
 func (s *DockerSuite) TestBuildCopyWholeDirToRoot(c *check.C) {
1305 1295
 	testRequires(c, DaemonIsLinux) // Linux specific test
1306
-	name := "testcopywholedirtoroot"
1307
-	ctx, err := fakeContext(fmt.Sprintf(`FROM busybox
1296
+	buildImageSuccessfully(c, "testcopywholedirtoroot", withBuildContext(c,
1297
+		withFile("Dockerfile", fmt.Sprintf(`FROM busybox
1308 1298
 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
1309 1299
 RUN echo 'dockerio:x:1001:' >> /etc/group
1310 1300
 RUN touch /exists
... ...
@@ -1314,36 +969,8 @@ RUN [ $(ls -l / | grep test_dir | awk '{print $3":"$4}') = 'root:root' ]
1314 1314
 RUN [ $(ls -l / | grep test_dir | awk '{print $1}') = 'drwxr-xr-x' ]
1315 1315
 RUN [ $(ls -l /test_dir/test_file | awk '{print $3":"$4}') = 'root:root' ]
1316 1316
 RUN [ $(ls -l /test_dir/test_file | awk '{print $1}') = '%s' ]
1317
-RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`, expectedFileChmod),
1318
-		map[string]string{
1319
-			"test_dir/test_file": "test1",
1320
-		})
1321
-	if err != nil {
1322
-		c.Fatal(err)
1323
-	}
1324
-	defer ctx.Close()
1325
-
1326
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1327
-		c.Fatal(err)
1328
-	}
1329
-}
1330
-
1331
-func (s *DockerSuite) TestBuildCopyEtcToRoot(c *check.C) {
1332
-	name := "testcopyetctoroot"
1333
-
1334
-	ctx, err := fakeContext(`FROM `+minimalBaseImage()+`
1335
-COPY . /`,
1336
-		map[string]string{
1337
-			"etc/test_file": "test1",
1338
-		})
1339
-	if err != nil {
1340
-		c.Fatal(err)
1341
-	}
1342
-	defer ctx.Close()
1343
-
1344
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1345
-		c.Fatal(err)
1346
-	}
1317
+RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`, expectedFileChmod)),
1318
+		withFile("test_dir/test_file", "test1")))
1347 1319
 }
1348 1320
 
1349 1321
 func (s *DockerSuite) TestBuildAddBadLinks(c *check.C) {
... ...
@@ -1504,19 +1131,20 @@ func (s *DockerSuite) TestBuildWithInaccessibleFilesInContext(c *check.C) {
1504 1504
 		if err = os.Chmod(pathToFileWithoutReadAccess, 0700); err != nil {
1505 1505
 			c.Fatalf("failed to chmod file to 700: %s", err)
1506 1506
 		}
1507
-		buildCmd := exec.Command("su", "unprivilegeduser", "-c", fmt.Sprintf("%s build -t %s .", dockerBinary, name))
1508
-		buildCmd.Dir = ctx.Dir
1509
-		out, _, err := runCommandWithOutput(buildCmd)
1510
-		if err == nil {
1511
-			c.Fatalf("build should have failed: %s %s", err, out)
1507
+		result := icmd.RunCmd(icmd.Cmd{
1508
+			Command: []string{"su", "unprivilegeduser", "-c", fmt.Sprintf("%s build -t %s .", dockerBinary, name)},
1509
+			Dir:     ctx.Dir,
1510
+		})
1511
+		if result.Error == nil {
1512
+			c.Fatalf("build should have failed: %s %s", result.Error, result.Combined())
1512 1513
 		}
1513 1514
 
1514 1515
 		// check if we've detected the failure before we started building
1515
-		if !strings.Contains(out, "no permission to read from ") {
1516
-			c.Fatalf("output should've contained the string: no permission to read from but contained: %s", out)
1516
+		if !strings.Contains(result.Combined(), "no permission to read from ") {
1517
+			c.Fatalf("output should've contained the string: no permission to read from but contained: %s", result.Combined())
1517 1518
 		}
1518 1519
 
1519
-		if !strings.Contains(out, "Error checking context") {
1520
+		if !strings.Contains(result.Combined(), "Error checking context") {
1520 1521
 			c.Fatalf("output should've contained the string: Error checking context")
1521 1522
 		}
1522 1523
 	}
... ...
@@ -1541,20 +1169,21 @@ func (s *DockerSuite) TestBuildWithInaccessibleFilesInContext(c *check.C) {
1541 1541
 			c.Fatalf("failed to chmod file to 700: %s", err)
1542 1542
 		}
1543 1543
 
1544
-		buildCmd := exec.Command("su", "unprivilegeduser", "-c", fmt.Sprintf("%s build -t %s .", dockerBinary, name))
1545
-		buildCmd.Dir = ctx.Dir
1546
-		out, _, err := runCommandWithOutput(buildCmd)
1547
-		if err == nil {
1548
-			c.Fatalf("build should have failed: %s %s", err, out)
1544
+		result := icmd.RunCmd(icmd.Cmd{
1545
+			Command: []string{"su", "unprivilegeduser", "-c", fmt.Sprintf("%s build -t %s .", dockerBinary, name)},
1546
+			Dir:     ctx.Dir,
1547
+		})
1548
+		if result.Error == nil {
1549
+			c.Fatalf("build should have failed: %s %s", result.Error, result.Combined())
1549 1550
 		}
1550 1551
 
1551 1552
 		// check if we've detected the failure before we started building
1552
-		if !strings.Contains(out, "can't stat") {
1553
-			c.Fatalf("output should've contained the string: can't access %s", out)
1553
+		if !strings.Contains(result.Combined(), "can't stat") {
1554
+			c.Fatalf("output should've contained the string: can't access %s", result.Combined())
1554 1555
 		}
1555 1556
 
1556
-		if !strings.Contains(out, "Error checking context") {
1557
-			c.Fatalf("output should've contained the string: Error checking context\ngot:%s", out)
1557
+		if !strings.Contains(result.Combined(), "Error checking context") {
1558
+			c.Fatalf("output should've contained the string: Error checking context\ngot:%s", result.Combined())
1558 1559
 		}
1559 1560
 
1560 1561
 	}
... ...
@@ -1617,15 +1246,12 @@ func (s *DockerSuite) TestBuildForceRm(c *check.C) {
1617 1617
 	}
1618 1618
 	name := "testbuildforcerm"
1619 1619
 
1620
-	ctx, err := fakeContext(`FROM `+minimalBaseImage()+`
1620
+	buildImageNew(name, withBuildFlags("--force-rm"), withBuildContext(c,
1621
+		withFile("Dockerfile", `FROM `+minimalBaseImage()+`
1621 1622
 	RUN true
1622
-	RUN thiswillfail`, nil)
1623
-	if err != nil {
1624
-		c.Fatal(err)
1625
-	}
1626
-	defer ctx.Close()
1627
-
1628
-	dockerCmdInDir(c, ctx.Dir, "build", "-t", name, "--force-rm", ".")
1623
+	RUN thiswillfail`))).Assert(c, icmd.Expected{
1624
+		ExitCode: 1,
1625
+	})
1629 1626
 
1630 1627
 	containerCountAfter, err := getContainerCount()
1631 1628
 	if err != nil {
... ...
@@ -1641,83 +1267,51 @@ func (s *DockerSuite) TestBuildForceRm(c *check.C) {
1641 1641
 func (s *DockerSuite) TestBuildRm(c *check.C) {
1642 1642
 	name := "testbuildrm"
1643 1643
 
1644
-	ctx, err := fakeContext(`FROM `+minimalBaseImage()+`
1645
-	ADD foo /
1646
-	ADD foo /`, map[string]string{"foo": "bar"})
1647
-	if err != nil {
1648
-		c.Fatal(err)
1649
-	}
1650
-	defer ctx.Close()
1651
-	{
1652
-		containerCountBefore, err := getContainerCount()
1653
-		if err != nil {
1654
-			c.Fatalf("failed to get the container count: %s", err)
1655
-		}
1656
-
1657
-		out, _, err := dockerCmdInDir(c, ctx.Dir, "build", "--rm", "-t", name, ".")
1658
-
1659
-		if err != nil {
1660
-			c.Fatal("failed to build the image", out)
1661
-		}
1662
-
1663
-		containerCountAfter, err := getContainerCount()
1664
-		if err != nil {
1665
-			c.Fatalf("failed to get the container count: %s", err)
1666
-		}
1667
-
1668
-		if containerCountBefore != containerCountAfter {
1669
-			c.Fatalf("-rm shouldn't have left containers behind")
1670
-		}
1671
-		deleteImages(name)
1672
-	}
1673
-
1674
-	{
1675
-		containerCountBefore, err := getContainerCount()
1676
-		if err != nil {
1677
-			c.Fatalf("failed to get the container count: %s", err)
1678
-		}
1679
-
1680
-		out, _, err := dockerCmdInDir(c, ctx.Dir, "build", "-t", name, ".")
1681
-
1682
-		if err != nil {
1683
-			c.Fatal("failed to build the image", out)
1684
-		}
1685
-
1686
-		containerCountAfter, err := getContainerCount()
1687
-		if err != nil {
1688
-			c.Fatalf("failed to get the container count: %s", err)
1689
-		}
1690
-
1691
-		if containerCountBefore != containerCountAfter {
1692
-			c.Fatalf("--rm shouldn't have left containers behind")
1693
-		}
1694
-		deleteImages(name)
1644
+	testCases := []struct {
1645
+		buildflags                []string
1646
+		shouldLeftContainerBehind bool
1647
+	}{
1648
+		// Default case (i.e. --rm=true)
1649
+		{
1650
+			buildflags:                []string{},
1651
+			shouldLeftContainerBehind: false,
1652
+		},
1653
+		{
1654
+			buildflags:                []string{"--rm"},
1655
+			shouldLeftContainerBehind: false,
1656
+		},
1657
+		{
1658
+			buildflags:                []string{"--rm=false"},
1659
+			shouldLeftContainerBehind: true,
1660
+		},
1695 1661
 	}
1696 1662
 
1697
-	{
1663
+	for _, tc := range testCases {
1698 1664
 		containerCountBefore, err := getContainerCount()
1699 1665
 		if err != nil {
1700 1666
 			c.Fatalf("failed to get the container count: %s", err)
1701 1667
 		}
1702 1668
 
1703
-		out, _, err := dockerCmdInDir(c, ctx.Dir, "build", "--rm=false", "-t", name, ".")
1704
-
1705
-		if err != nil {
1706
-			c.Fatal("failed to build the image", out)
1707
-		}
1669
+		buildImageSuccessfully(c, name, withBuildFlags(tc.buildflags...), withDockerfile(`FROM busybox
1670
+	RUN echo hello world`))
1708 1671
 
1709 1672
 		containerCountAfter, err := getContainerCount()
1710 1673
 		if err != nil {
1711 1674
 			c.Fatalf("failed to get the container count: %s", err)
1712 1675
 		}
1713 1676
 
1714
-		if containerCountBefore == containerCountAfter {
1715
-			c.Fatalf("--rm=false should have left containers behind")
1677
+		if tc.shouldLeftContainerBehind {
1678
+			if containerCountBefore == containerCountAfter {
1679
+				c.Fatalf("flags %v should have left containers behind", tc.buildflags)
1680
+			}
1681
+		} else {
1682
+			if containerCountBefore != containerCountAfter {
1683
+				c.Fatalf("flags %v shouldn't have left containers behind", tc.buildflags)
1684
+			}
1716 1685
 		}
1717
-		deleteImages(name)
1718 1686
 
1687
+		dockerCmd(c, "rmi", name)
1719 1688
 	}
1720
-
1721 1689
 }
1722 1690
 
1723 1691
 func (s *DockerSuite) TestBuildWithVolumes(c *check.C) {
... ...
@@ -1737,27 +1331,18 @@ func (s *DockerSuite) TestBuildWithVolumes(c *check.C) {
1737 1737
 			"/test8]": emptyMap,
1738 1738
 		}
1739 1739
 	)
1740
-	_, err := buildImage(name,
1741
-		`FROM scratch
1740
+
1741
+	buildImageSuccessfully(c, name, withDockerfile(`FROM scratch
1742 1742
 		VOLUME /test1
1743 1743
 		VOLUME /test2
1744 1744
     VOLUME /test3 /test4
1745 1745
     VOLUME ["/test5", "/test6"]
1746 1746
     VOLUME [/test7 /test8]
1747
-    `,
1748
-		true)
1749
-	if err != nil {
1750
-		c.Fatal(err)
1751
-	}
1752
-	res := inspectFieldJSON(c, name, "Config.Volumes")
1747
+    `))
1753 1748
 
1754
-	err = json.Unmarshal([]byte(res), &result)
1755
-	if err != nil {
1756
-		c.Fatal(err)
1757
-	}
1749
+	inspectFieldAndUnmarshall(c, name, "Config.Volumes", &result)
1758 1750
 
1759 1751
 	equal := reflect.DeepEqual(&result, &expected)
1760
-
1761 1752
 	if !equal {
1762 1753
 		c.Fatalf("Volumes %s, expected %s", result, expected)
1763 1754
 	}
... ...
@@ -1767,14 +1352,10 @@ func (s *DockerSuite) TestBuildWithVolumes(c *check.C) {
1767 1767
 func (s *DockerSuite) TestBuildMaintainer(c *check.C) {
1768 1768
 	name := "testbuildmaintainer"
1769 1769
 
1770
+	buildImageSuccessfully(c, name, withDockerfile(`FROM `+minimalBaseImage()+`
1771
+        MAINTAINER dockerio`))
1772
+
1770 1773
 	expected := "dockerio"
1771
-	_, err := buildImage(name,
1772
-		`FROM `+minimalBaseImage()+`
1773
-        MAINTAINER dockerio`,
1774
-		true)
1775
-	if err != nil {
1776
-		c.Fatal(err)
1777
-	}
1778 1774
 	res := inspectField(c, name, "Author")
1779 1775
 	if res != expected {
1780 1776
 		c.Fatalf("Maintainer %s, expected %s", res, expected)
... ...
@@ -1785,15 +1366,10 @@ func (s *DockerSuite) TestBuildUser(c *check.C) {
1785 1785
 	testRequires(c, DaemonIsLinux)
1786 1786
 	name := "testbuilduser"
1787 1787
 	expected := "dockerio"
1788
-	_, err := buildImage(name,
1789
-		`FROM busybox
1788
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
1790 1789
 		RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
1791 1790
 		USER dockerio
1792
-		RUN [ $(whoami) = 'dockerio' ]`,
1793
-		true)
1794
-	if err != nil {
1795
-		c.Fatal(err)
1796
-	}
1791
+		RUN [ $(whoami) = 'dockerio' ]`))
1797 1792
 	res := inspectField(c, name, "Config.User")
1798 1793
 	if res != expected {
1799 1794
 		c.Fatalf("User %s, expected %s", res, expected)
... ...
@@ -1825,19 +1401,15 @@ func (s *DockerSuite) TestBuildRelativeWorkdir(c *check.C) {
1825 1825
 		expectedFinal = `/test2/test3`
1826 1826
 	}
1827 1827
 
1828
-	_, err := buildImage(name,
1829
-		`FROM busybox
1828
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
1830 1829
 		RUN sh -c "[ "$PWD" = "`+expected1+`" ]"
1831 1830
 		WORKDIR test1
1832 1831
 		RUN sh -c "[ "$PWD" = "`+expected2+`" ]"
1833 1832
 		WORKDIR /test2
1834 1833
 		RUN sh -c "[ "$PWD" = "`+expected3+`" ]"
1835 1834
 		WORKDIR test3
1836
-		RUN sh -c "[ "$PWD" = "`+expected4+`" ]"`,
1837
-		true)
1838
-	if err != nil {
1839
-		c.Fatal(err)
1840
-	}
1835
+		RUN sh -c "[ "$PWD" = "`+expected4+`" ]"`))
1836
+
1841 1837
 	res := inspectField(c, name, "Config.WorkingDir")
1842 1838
 	if res != expectedFinal {
1843 1839
 		c.Fatalf("Workdir %s, expected %s", res, expectedFinal)
... ...
@@ -1848,30 +1420,23 @@ func (s *DockerSuite) TestBuildRelativeWorkdir(c *check.C) {
1848 1848
 // Windows semantics. Most path handling verifications are in unit tests
1849 1849
 func (s *DockerSuite) TestBuildWindowsWorkdirProcessing(c *check.C) {
1850 1850
 	testRequires(c, DaemonIsWindows)
1851
-	name := "testbuildwindowsworkdirprocessing"
1852
-	_, err := buildImage(name,
1853
-		`FROM busybox
1851
+	buildImageSuccessfully(c, "testbuildwindowsworkdirprocessing", withDockerfile(`FROM busybox
1854 1852
 		WORKDIR C:\\foo
1855 1853
 		WORKDIR bar
1856 1854
 		RUN sh -c "[ "$PWD" = "C:/foo/bar" ]"
1857
-		`,
1858
-		true)
1859
-	if err != nil {
1860
-		c.Fatal(err)
1861
-	}
1855
+		`))
1862 1856
 }
1863 1857
 
1864 1858
 // #22181 Regression test. Most paths handling verifications are in unit test.
1865 1859
 // One functional test for end-to-end
1866 1860
 func (s *DockerSuite) TestBuildWindowsAddCopyPathProcessing(c *check.C) {
1867 1861
 	testRequires(c, DaemonIsWindows)
1868
-	name := "testbuildwindowsaddcopypathprocessing"
1869 1862
 	// TODO Windows (@jhowardmsft). Needs a follow-up PR to 22181 to
1870 1863
 	// support backslash such as .\\ being equivalent to ./ and c:\\ being
1871 1864
 	// equivalent to c:/. This is not currently (nor ever has been) supported
1872 1865
 	// by docker on the Windows platform.
1873
-	dockerfile := `
1874
-		FROM busybox
1866
+	buildImageSuccessfully(c, "testbuildwindowsaddcopypathprocessing", withBuildContext(c,
1867
+		withFile("Dockerfile", `FROM busybox
1875 1868
 			# No trailing slash on COPY/ADD
1876 1869
 			# Results in dir being changed to a file
1877 1870
 			WORKDIR /wc1
... ...
@@ -1889,21 +1454,12 @@ func (s *DockerSuite) TestBuildWindowsAddCopyPathProcessing(c *check.C) {
1889 1889
 			ADD wd2 c:/wd2/
1890 1890
 			RUN sh -c "[ $(cat c:/wd1/wd1) = 'hellowd1' ]"
1891 1891
 			RUN sh -c "[ $(cat c:/wd2/wd2) = 'worldwd2' ]"
1892
-			`
1893
-	ctx, err := fakeContext(dockerfile, map[string]string{
1894
-		"wc1": "hellowc1",
1895
-		"wc2": "worldwc2",
1896
-		"wd1": "hellowd1",
1897
-		"wd2": "worldwd2",
1898
-	})
1899
-	if err != nil {
1900
-		c.Fatal(err)
1901
-	}
1902
-	defer ctx.Close()
1903
-	_, err = buildImageFromContext(name, ctx, false)
1904
-	if err != nil {
1905
-		c.Fatal(err)
1906
-	}
1892
+			`),
1893
+		withFile("wc1", "hellowc1"),
1894
+		withFile("wc2", "worldwc2"),
1895
+		withFile("wd1", "hellowd1"),
1896
+		withFile("wd2", "worldwd2"),
1897
+	))
1907 1898
 }
1908 1899
 
1909 1900
 func (s *DockerSuite) TestBuildWorkdirWithEnvVariables(c *check.C) {
... ...
@@ -1916,16 +1472,11 @@ func (s *DockerSuite) TestBuildWorkdirWithEnvVariables(c *check.C) {
1916 1916
 		expected = `/test1/test2`
1917 1917
 	}
1918 1918
 
1919
-	_, err := buildImage(name,
1920
-		`FROM busybox
1919
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
1921 1920
 		ENV DIRPATH /test1
1922 1921
 		ENV SUBDIRNAME test2
1923 1922
 		WORKDIR $DIRPATH
1924
-		WORKDIR $SUBDIRNAME/$MISSING_VAR`,
1925
-		true)
1926
-	if err != nil {
1927
-		c.Fatal(err)
1928
-	}
1923
+		WORKDIR $SUBDIRNAME/$MISSING_VAR`))
1929 1924
 	res := inspectField(c, name, "Config.WorkingDir")
1930 1925
 	if res != expected {
1931 1926
 		c.Fatalf("Workdir %s, expected %s", res, expected)
... ...
@@ -1943,12 +1494,11 @@ func (s *DockerSuite) TestBuildRelativeCopy(c *check.C) {
1943 1943
 		expected = `/test1/test2`
1944 1944
 	}
1945 1945
 
1946
-	name := "testbuildrelativecopy"
1947
-	dockerfile := `
1948
-		FROM busybox
1946
+	buildImageSuccessfully(c, "testbuildrelativecopy", withBuildContext(c,
1947
+		withFile("Dockerfile", `FROM busybox
1949 1948
 			WORKDIR /test1
1950 1949
 			WORKDIR test2
1951
-			RUN sh -c "[ "$PWD" = '` + expected + `' ]"
1950
+			RUN sh -c "[ "$PWD" = '`+expected+`' ]"
1952 1951
 			COPY foo ./
1953 1952
 			RUN sh -c "[ $(cat /test1/test2/foo) = 'hello' ]"
1954 1953
 			ADD foo ./bar/baz
... ...
@@ -1966,53 +1516,37 @@ func (s *DockerSuite) TestBuildRelativeCopy(c *check.C) {
1966 1966
 			WORKDIR /test5/test6
1967 1967
 			COPY foo ../
1968 1968
 			RUN sh -c "[ $(cat /test5/foo) = 'hello' ]"
1969
-			`
1970
-	ctx, err := fakeContext(dockerfile, map[string]string{
1971
-		"foo": "hello",
1972
-	})
1973
-	if err != nil {
1974
-		c.Fatal(err)
1975
-	}
1976
-	defer ctx.Close()
1977
-	_, err = buildImageFromContext(name, ctx, false)
1978
-	if err != nil {
1979
-		c.Fatal(err)
1980
-	}
1969
+			`),
1970
+		withFile("foo", "hello"),
1971
+	))
1981 1972
 }
1982 1973
 
1983 1974
 func (s *DockerSuite) TestBuildBlankName(c *check.C) {
1984 1975
 	name := "testbuildblankname"
1985
-	_, _, stderr, err := buildImageWithStdoutStderr(name,
1986
-		`FROM busybox
1987
-		ENV =`,
1988
-		true)
1989
-	if err == nil {
1990
-		c.Fatal("Build was supposed to fail but didn't")
1991
-	}
1992
-	if !strings.Contains(stderr, "ENV names can not be blank") {
1993
-		c.Fatalf("Missing error message, got: %s", stderr)
1994
-	}
1995
-
1996
-	_, _, stderr, err = buildImageWithStdoutStderr(name,
1997
-		`FROM busybox
1998
-		LABEL =`,
1999
-		true)
2000
-	if err == nil {
2001
-		c.Fatal("Build was supposed to fail but didn't")
2002
-	}
2003
-	if !strings.Contains(stderr, "LABEL names can not be blank") {
2004
-		c.Fatalf("Missing error message, got: %s", stderr)
1976
+	testCases := []struct {
1977
+		expression     string
1978
+		expectedStderr string
1979
+	}{
1980
+		{
1981
+			expression:     "ENV =",
1982
+			expectedStderr: "ENV names can not be blank",
1983
+		},
1984
+		{
1985
+			expression:     "LABEL =",
1986
+			expectedStderr: "LABEL names can not be blank",
1987
+		},
1988
+		{
1989
+			expression:     "ARG =foo",
1990
+			expectedStderr: "ARG names can not be blank",
1991
+		},
2005 1992
 	}
2006 1993
 
2007
-	_, _, stderr, err = buildImageWithStdoutStderr(name,
2008
-		`FROM busybox
2009
-		ARG =foo`,
2010
-		true)
2011
-	if err == nil {
2012
-		c.Fatal("Build was supposed to fail but didn't")
2013
-	}
2014
-	if !strings.Contains(stderr, "ARG names can not be blank") {
2015
-		c.Fatalf("Missing error message, got: %s", stderr)
1994
+	for _, tc := range testCases {
1995
+		buildImageNew(name, withDockerfile(fmt.Sprintf(`FROM busybox
1996
+		%s`, tc.expression))).Assert(c, icmd.Expected{
1997
+			ExitCode: 1,
1998
+			Err:      tc.expectedStderr,
1999
+		})
2016 2000
 	}
2017 2001
 }
2018 2002
 
... ...
@@ -2020,15 +1554,10 @@ func (s *DockerSuite) TestBuildEnv(c *check.C) {
2020 2020
 	testRequires(c, DaemonIsLinux) // ENV expansion is different in Windows
2021 2021
 	name := "testbuildenv"
2022 2022
 	expected := "[PATH=/test:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PORT=2375]"
2023
-	_, err := buildImage(name,
2024
-		`FROM busybox
2023
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
2025 2024
 		ENV PATH /test:$PATH
2026
-        ENV PORT 2375
2027
-		RUN [ $(env | grep PORT) = 'PORT=2375' ]`,
2028
-		true)
2029
-	if err != nil {
2030
-		c.Fatal(err)
2031
-	}
2025
+		ENV PORT 2375
2026
+		RUN [ $(env | grep PORT) = 'PORT=2375' ]`))
2032 2027
 	res := inspectField(c, name, "Config.Env")
2033 2028
 	if res != expected {
2034 2029
 		c.Fatalf("Env %s, expected %s", res, expected)
... ...
@@ -2040,14 +1569,11 @@ func (s *DockerSuite) TestBuildPATH(c *check.C) {
2040 2040
 
2041 2041
 	defPath := "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
2042 2042
 
2043
-	fn := func(dockerfile string, exp string) {
2044
-		_, err := buildImage("testbldpath", dockerfile, true)
2045
-		c.Assert(err, check.IsNil)
2046
-
2043
+	fn := func(dockerfile string, expected string) {
2044
+		buildImageSuccessfully(c, "testbldpath", withDockerfile(dockerfile))
2047 2045
 		res := inspectField(c, "testbldpath", "Config.Env")
2048
-
2049
-		if res != exp {
2050
-			c.Fatalf("Env %q, expected %q for dockerfile:%q", res, exp, dockerfile)
2046
+		if res != expected {
2047
+			c.Fatalf("Env %q, expected %q for dockerfile:%q", res, expected, dockerfile)
2051 2048
 		}
2052 2049
 	}
2053 2050
 
... ...
@@ -2075,13 +1601,10 @@ func (s *DockerSuite) TestBuildContextCleanup(c *check.C) {
2075 2075
 	if err != nil {
2076 2076
 		c.Fatalf("failed to list contents of tmp dir: %s", err)
2077 2077
 	}
2078
-	_, err = buildImage(name,
2079
-		`FROM `+minimalBaseImage()+`
2080
-        ENTRYPOINT ["/bin/echo"]`,
2081
-		true)
2082
-	if err != nil {
2083
-		c.Fatal(err)
2084
-	}
2078
+
2079
+	buildImageSuccessfully(c, name, withDockerfile(`FROM `+minimalBaseImage()+`
2080
+        ENTRYPOINT ["/bin/echo"]`))
2081
+
2085 2082
 	entriesFinal, err := ioutil.ReadDir(filepath.Join(dockerBasePath, "tmp"))
2086 2083
 	if err != nil {
2087 2084
 		c.Fatalf("failed to list contents of tmp dir: %s", err)
... ...
@@ -2100,13 +1623,12 @@ func (s *DockerSuite) TestBuildContextCleanupFailedBuild(c *check.C) {
2100 2100
 	if err != nil {
2101 2101
 		c.Fatalf("failed to list contents of tmp dir: %s", err)
2102 2102
 	}
2103
-	_, err = buildImage(name,
2104
-		`FROM `+minimalBaseImage()+`
2105
-	RUN /non/existing/command`,
2106
-		true)
2107
-	if err == nil {
2108
-		c.Fatalf("expected build to fail, but it didn't")
2109
-	}
2103
+
2104
+	buildImageNew(name, withDockerfile(`FROM `+minimalBaseImage()+`
2105
+	RUN /non/existing/command`)).Assert(c, icmd.Expected{
2106
+		ExitCode: 1,
2107
+	})
2108
+
2110 2109
 	entriesFinal, err := ioutil.ReadDir(filepath.Join(dockerBasePath, "tmp"))
2111 2110
 	if err != nil {
2112 2111
 		c.Fatalf("failed to list contents of tmp dir: %s", err)
... ...
@@ -2119,15 +1641,11 @@ func (s *DockerSuite) TestBuildContextCleanupFailedBuild(c *check.C) {
2119 2119
 
2120 2120
 func (s *DockerSuite) TestBuildCmd(c *check.C) {
2121 2121
 	name := "testbuildcmd"
2122
-
2123 2122
 	expected := "[/bin/echo Hello World]"
2124
-	_, err := buildImage(name,
2125
-		`FROM `+minimalBaseImage()+`
2126
-        CMD ["/bin/echo", "Hello World"]`,
2127
-		true)
2128
-	if err != nil {
2129
-		c.Fatal(err)
2130
-	}
2123
+
2124
+	buildImageSuccessfully(c, name, withDockerfile(`FROM `+minimalBaseImage()+`
2125
+        CMD ["/bin/echo", "Hello World"]`))
2126
+
2131 2127
 	res := inspectField(c, name, "Config.Cmd")
2132 2128
 	if res != expected {
2133 2129
 		c.Fatalf("Cmd %s, expected %s", res, expected)
... ...
@@ -2138,13 +1656,10 @@ func (s *DockerSuite) TestBuildExpose(c *check.C) {
2138 2138
 	testRequires(c, DaemonIsLinux) // Expose not implemented on Windows
2139 2139
 	name := "testbuildexpose"
2140 2140
 	expected := "map[2375/tcp:{}]"
2141
-	_, err := buildImage(name,
2142
-		`FROM scratch
2143
-        EXPOSE 2375`,
2144
-		true)
2145
-	if err != nil {
2146
-		c.Fatal(err)
2147
-	}
2141
+
2142
+	buildImageSuccessfully(c, name, withDockerfile(`FROM scratch
2143
+        EXPOSE 2375`))
2144
+
2148 2145
 	res := inspectField(c, name, "Config.ExposedPorts")
2149 2146
 	if res != expected {
2150 2147
 		c.Fatalf("Exposed ports %s, expected %s", res, expected)
... ...
@@ -2178,10 +1693,7 @@ func (s *DockerSuite) TestBuildExposeMorePorts(c *check.C) {
2178 2178
 	tmpl.Execute(buf, portList)
2179 2179
 
2180 2180
 	name := "testbuildexpose"
2181
-	_, err := buildImage(name, buf.String(), true)
2182
-	if err != nil {
2183
-		c.Fatal(err)
2184
-	}
2181
+	buildImageSuccessfully(c, name, withDockerfile(buf.String()))
2185 2182
 
2186 2183
 	// check if all the ports are saved inside Config.ExposedPorts
2187 2184
 	res := inspectFieldJSON(c, name, "Config.ExposedPorts")
... ...
@@ -2206,11 +1718,8 @@ func (s *DockerSuite) TestBuildExposeMorePorts(c *check.C) {
2206 2206
 func (s *DockerSuite) TestBuildExposeOrder(c *check.C) {
2207 2207
 	testRequires(c, DaemonIsLinux) // Expose not implemented on Windows
2208 2208
 	buildID := func(name, exposed string) string {
2209
-		_, err := buildImage(name, fmt.Sprintf(`FROM scratch
2210
-		EXPOSE %s`, exposed), true)
2211
-		if err != nil {
2212
-			c.Fatal(err)
2213
-		}
2209
+		buildImageSuccessfully(c, name, withDockerfile(fmt.Sprintf(`FROM scratch
2210
+		EXPOSE %s`, exposed)))
2214 2211
 		id := inspectField(c, name, "Id")
2215 2212
 		return id
2216 2213
 	}
... ...
@@ -2226,13 +1735,8 @@ func (s *DockerSuite) TestBuildExposeUpperCaseProto(c *check.C) {
2226 2226
 	testRequires(c, DaemonIsLinux) // Expose not implemented on Windows
2227 2227
 	name := "testbuildexposeuppercaseproto"
2228 2228
 	expected := "map[5678/udp:{}]"
2229
-	_, err := buildImage(name,
2230
-		`FROM scratch
2231
-        EXPOSE 5678/UDP`,
2232
-		true)
2233
-	if err != nil {
2234
-		c.Fatal(err)
2235
-	}
2229
+	buildImageSuccessfully(c, name, withDockerfile(`FROM scratch
2230
+        EXPOSE 5678/UDP`))
2236 2231
 	res := inspectField(c, name, "Config.ExposedPorts")
2237 2232
 	if res != expected {
2238 2233
 		c.Fatalf("Exposed ports %s, expected %s", res, expected)
... ...
@@ -2243,13 +1747,8 @@ func (s *DockerSuite) TestBuildEmptyEntrypointInheritance(c *check.C) {
2243 2243
 	name := "testbuildentrypointinheritance"
2244 2244
 	name2 := "testbuildentrypointinheritance2"
2245 2245
 
2246
-	_, err := buildImage(name,
2247
-		`FROM busybox
2248
-        ENTRYPOINT ["/bin/echo"]`,
2249
-		true)
2250
-	if err != nil {
2251
-		c.Fatal(err)
2252
-	}
2246
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
2247
+        ENTRYPOINT ["/bin/echo"]`))
2253 2248
 	res := inspectField(c, name, "Config.Entrypoint")
2254 2249
 
2255 2250
 	expected := "[/bin/echo]"
... ...
@@ -2257,34 +1756,23 @@ func (s *DockerSuite) TestBuildEmptyEntrypointInheritance(c *check.C) {
2257 2257
 		c.Fatalf("Entrypoint %s, expected %s", res, expected)
2258 2258
 	}
2259 2259
 
2260
-	_, err = buildImage(name2,
2261
-		fmt.Sprintf(`FROM %s
2262
-        ENTRYPOINT []`, name),
2263
-		true)
2264
-	if err != nil {
2265
-		c.Fatal(err)
2266
-	}
2260
+	buildImageSuccessfully(c, name2, withDockerfile(fmt.Sprintf(`FROM %s
2261
+        ENTRYPOINT []`, name)))
2267 2262
 	res = inspectField(c, name2, "Config.Entrypoint")
2268 2263
 
2269 2264
 	expected = "[]"
2270
-
2271 2265
 	if res != expected {
2272 2266
 		c.Fatalf("Entrypoint %s, expected %s", res, expected)
2273 2267
 	}
2274
-
2275 2268
 }
2276 2269
 
2277 2270
 func (s *DockerSuite) TestBuildEmptyEntrypoint(c *check.C) {
2278 2271
 	name := "testbuildentrypoint"
2279 2272
 	expected := "[]"
2280 2273
 
2281
-	_, err := buildImage(name,
2282
-		`FROM busybox
2283
-        ENTRYPOINT []`,
2284
-		true)
2285
-	if err != nil {
2286
-		c.Fatal(err)
2287
-	}
2274
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
2275
+        ENTRYPOINT []`))
2276
+
2288 2277
 	res := inspectField(c, name, "Config.Entrypoint")
2289 2278
 	if res != expected {
2290 2279
 		c.Fatalf("Entrypoint %s, expected %s", res, expected)
... ...
@@ -2296,13 +1784,9 @@ func (s *DockerSuite) TestBuildEntrypoint(c *check.C) {
2296 2296
 	name := "testbuildentrypoint"
2297 2297
 
2298 2298
 	expected := "[/bin/echo]"
2299
-	_, err := buildImage(name,
2300
-		`FROM `+minimalBaseImage()+`
2301
-        ENTRYPOINT ["/bin/echo"]`,
2302
-		true)
2303
-	if err != nil {
2304
-		c.Fatal(err)
2305
-	}
2299
+	buildImageSuccessfully(c, name, withDockerfile(`FROM `+minimalBaseImage()+`
2300
+        ENTRYPOINT ["/bin/echo"]`))
2301
+
2306 2302
 	res := inspectField(c, name, "Config.Entrypoint")
2307 2303
 	if res != expected {
2308 2304
 		c.Fatalf("Entrypoint %s, expected %s", res, expected)
... ...
@@ -2312,127 +1796,48 @@ func (s *DockerSuite) TestBuildEntrypoint(c *check.C) {
2312 2312
 
2313 2313
 // #6445 ensure ONBUILD triggers aren't committed to grandchildren
2314 2314
 func (s *DockerSuite) TestBuildOnBuildLimitedInheritence(c *check.C) {
2315
-	var (
2316
-		out2, out3 string
2317
-	)
2318
-	{
2319
-		name1 := "testonbuildtrigger1"
2320
-		dockerfile1 := `
2315
+	buildImageSuccessfully(c, "testonbuildtrigger1", withDockerfile(`
2321 2316
 		FROM busybox
2322 2317
 		RUN echo "GRANDPARENT"
2323 2318
 		ONBUILD RUN echo "ONBUILD PARENT"
2324
-		`
2325
-		ctx, err := fakeContext(dockerfile1, nil)
2326
-		if err != nil {
2327
-			c.Fatal(err)
2328
-		}
2329
-		defer ctx.Close()
2330
-
2331
-		out1, _, err := dockerCmdInDir(c, ctx.Dir, "build", "-t", name1, ".")
2332
-		if err != nil {
2333
-			c.Fatalf("build failed to complete: %s, %v", out1, err)
2334
-		}
2335
-	}
2336
-	{
2337
-		name2 := "testonbuildtrigger2"
2338
-		dockerfile2 := `
2339
-		FROM testonbuildtrigger1
2340
-		`
2341
-		ctx, err := fakeContext(dockerfile2, nil)
2342
-		if err != nil {
2343
-			c.Fatal(err)
2344
-		}
2345
-		defer ctx.Close()
2346
-
2347
-		out2, _, err = dockerCmdInDir(c, ctx.Dir, "build", "-t", name2, ".")
2348
-		if err != nil {
2349
-			c.Fatalf("build failed to complete: %s, %v", out2, err)
2350
-		}
2351
-	}
2352
-	{
2353
-		name3 := "testonbuildtrigger3"
2354
-		dockerfile3 := `
2355
-		FROM testonbuildtrigger2
2356
-		`
2357
-		ctx, err := fakeContext(dockerfile3, nil)
2358
-		if err != nil {
2359
-			c.Fatal(err)
2360
-		}
2361
-		defer ctx.Close()
2362
-
2363
-		out3, _, err = dockerCmdInDir(c, ctx.Dir, "build", "-t", name3, ".")
2364
-		if err != nil {
2365
-			c.Fatalf("build failed to complete: %s, %v", out3, err)
2366
-		}
2367
-
2368
-	}
2369
-
2319
+		`))
2370 2320
 	// ONBUILD should be run in second build.
2371
-	if !strings.Contains(out2, "ONBUILD PARENT") {
2372
-		c.Fatalf("ONBUILD instruction did not run in child of ONBUILD parent")
2373
-	}
2374
-
2321
+	buildImageNew("testonbuildtrigger2", withDockerfile("FROM testonbuildtrigger1")).Assert(c, icmd.Expected{
2322
+		Out: "ONBUILD PARENT",
2323
+	})
2375 2324
 	// ONBUILD should *not* be run in third build.
2376
-	if strings.Contains(out3, "ONBUILD PARENT") {
2325
+	result := buildImageNew("testonbuildtrigger3", withDockerfile("FROM testonbuildtrigger2"))
2326
+	result.Assert(c, icmd.Success)
2327
+	if strings.Contains(result.Combined(), "ONBUILD PARENT") {
2377 2328
 		c.Fatalf("ONBUILD instruction ran in grandchild of ONBUILD parent")
2378 2329
 	}
2379
-
2380 2330
 }
2381 2331
 
2382
-func (s *DockerSuite) TestBuildWithCache(c *check.C) {
2332
+func (s *DockerSuite) TestBuildSameDockerfileWithAndWithoutCache(c *check.C) {
2383 2333
 	testRequires(c, DaemonIsLinux) // Expose not implemented on Windows
2384 2334
 	name := "testbuildwithcache"
2385
-	id1, err := buildImage(name,
2386
-		`FROM scratch
2387
-		MAINTAINER dockerio
2388
-		EXPOSE 5432
2389
-        ENTRYPOINT ["/bin/echo"]`,
2390
-		true)
2391
-	if err != nil {
2392
-		c.Fatal(err)
2393
-	}
2394
-	id2, err := buildImage(name,
2395
-		`FROM scratch
2335
+	dockerfile := `FROM scratch
2396 2336
 		MAINTAINER dockerio
2397 2337
 		EXPOSE 5432
2398
-        ENTRYPOINT ["/bin/echo"]`,
2399
-		true)
2400
-	if err != nil {
2401
-		c.Fatal(err)
2402
-	}
2338
+        ENTRYPOINT ["/bin/echo"]`
2339
+	buildImageSuccessfully(c, name, withDockerfile(dockerfile))
2340
+	id1, err := getIDByName(name)
2341
+	c.Assert(err, checker.IsNil)
2342
+	buildImageSuccessfully(c, name, withDockerfile(dockerfile))
2343
+	id2, err := getIDByName(name)
2344
+	c.Assert(err, checker.IsNil)
2345
+	buildImageSuccessfully(c, name, withoutCache, withDockerfile(dockerfile))
2346
+	id3, err := getIDByName(name)
2347
+	c.Assert(err, checker.IsNil)
2403 2348
 	if id1 != id2 {
2404 2349
 		c.Fatal("The cache should have been used but hasn't.")
2405 2350
 	}
2406
-}
2407
-
2408
-func (s *DockerSuite) TestBuildWithoutCache(c *check.C) {
2409
-	testRequires(c, DaemonIsLinux) // Expose not implemented on Windows
2410
-	name := "testbuildwithoutcache"
2411
-	name2 := "testbuildwithoutcache2"
2412
-	id1, err := buildImage(name,
2413
-		`FROM scratch
2414
-		MAINTAINER dockerio
2415
-		EXPOSE 5432
2416
-        ENTRYPOINT ["/bin/echo"]`,
2417
-		true)
2418
-	if err != nil {
2419
-		c.Fatal(err)
2420
-	}
2421
-
2422
-	id2, err := buildImage(name2,
2423
-		`FROM scratch
2424
-		MAINTAINER dockerio
2425
-		EXPOSE 5432
2426
-        ENTRYPOINT ["/bin/echo"]`,
2427
-		false)
2428
-	if err != nil {
2429
-		c.Fatal(err)
2430
-	}
2431
-	if id1 == id2 {
2351
+	if id1 == id3 {
2432 2352
 		c.Fatal("The cache should have been invalided but hasn't.")
2433 2353
 	}
2434 2354
 }
2435 2355
 
2356
+// Make sure that ADD/COPY still populate the cache even if they don't use it
2436 2357
 func (s *DockerSuite) TestBuildConditionalCache(c *check.C) {
2437 2358
 	name := "testbuildconditionalcache"
2438 2359
 
... ...
@@ -2456,7 +1861,8 @@ func (s *DockerSuite) TestBuildConditionalCache(c *check.C) {
2456 2456
 		c.Fatalf("Error modifying foo: %s", err)
2457 2457
 	}
2458 2458
 
2459
-	id2, err := buildImageFromContext(name, ctx, false)
2459
+	// Updating a file should invalidate the cache
2460
+	id2, err := buildImageFromContext(name, ctx, true)
2460 2461
 	if err != nil {
2461 2462
 		c.Fatalf("Error building #2: %s", err)
2462 2463
 	}
... ...
@@ -2473,39 +1879,9 @@ func (s *DockerSuite) TestBuildConditionalCache(c *check.C) {
2473 2473
 	}
2474 2474
 }
2475 2475
 
2476
-func (s *DockerSuite) TestBuildAddLocalFileWithCache(c *check.C) {
2477
-	// local files are not owned by the correct user
2478
-	testRequires(c, NotUserNamespace)
2479
-	name := "testbuildaddlocalfilewithcache"
2480
-	name2 := "testbuildaddlocalfilewithcache2"
2481
-	dockerfile := `
2482
-		FROM busybox
2483
-        MAINTAINER dockerio
2484
-        ADD foo /usr/lib/bla/bar
2485
-		RUN sh -c "[ $(cat /usr/lib/bla/bar) = "hello" ]"`
2486
-	ctx, err := fakeContext(dockerfile, map[string]string{
2487
-		"foo": "hello",
2488
-	})
2489
-	if err != nil {
2490
-		c.Fatal(err)
2491
-	}
2492
-	defer ctx.Close()
2493
-	id1, err := buildImageFromContext(name, ctx, true)
2494
-	if err != nil {
2495
-		c.Fatal(err)
2496
-	}
2497
-	id2, err := buildImageFromContext(name2, ctx, true)
2498
-	if err != nil {
2499
-		c.Fatal(err)
2500
-	}
2501
-	if id1 != id2 {
2502
-		c.Fatal("The cache should have been used but hasn't.")
2503
-	}
2504
-}
2505
-
2506
-func (s *DockerSuite) TestBuildAddMultipleLocalFileWithCache(c *check.C) {
2476
+// FIXME(vdemeester) this really seems to test the same thing as before
2477
+func (s *DockerSuite) TestBuildAddMultipleLocalFileWithAndWithoutCache(c *check.C) {
2507 2478
 	name := "testbuildaddmultiplelocalfilewithcache"
2508
-	name2 := "testbuildaddmultiplelocalfilewithcache2"
2509 2479
 	dockerfile := `
2510 2480
 		FROM busybox
2511 2481
         MAINTAINER dockerio
... ...
@@ -2522,41 +1898,18 @@ func (s *DockerSuite) TestBuildAddMultipleLocalFileWithCache(c *check.C) {
2522 2522
 	if err != nil {
2523 2523
 		c.Fatal(err)
2524 2524
 	}
2525
-	id2, err := buildImageFromContext(name2, ctx, true)
2526
-	if err != nil {
2527
-		c.Fatal(err)
2528
-	}
2529
-	if id1 != id2 {
2530
-		c.Fatal("The cache should have been used but hasn't.")
2531
-	}
2532
-}
2533
-
2534
-func (s *DockerSuite) TestBuildAddLocalFileWithoutCache(c *check.C) {
2535
-	// local files are not owned by the correct user
2536
-	testRequires(c, NotUserNamespace)
2537
-	name := "testbuildaddlocalfilewithoutcache"
2538
-	name2 := "testbuildaddlocalfilewithoutcache2"
2539
-	dockerfile := `
2540
-		FROM busybox
2541
-        MAINTAINER dockerio
2542
-        ADD foo /usr/lib/bla/bar
2543
-		RUN sh -c "[ $(cat /usr/lib/bla/bar) = "hello" ]"`
2544
-	ctx, err := fakeContext(dockerfile, map[string]string{
2545
-		"foo": "hello",
2546
-	})
2525
+	id2, err := buildImageFromContext(name, ctx, true)
2547 2526
 	if err != nil {
2548 2527
 		c.Fatal(err)
2549 2528
 	}
2550
-	defer ctx.Close()
2551
-	id1, err := buildImageFromContext(name, ctx, true)
2529
+	id3, err := buildImageFromContext(name, ctx, false)
2552 2530
 	if err != nil {
2553 2531
 		c.Fatal(err)
2554 2532
 	}
2555
-	id2, err := buildImageFromContext(name2, ctx, false)
2556
-	if err != nil {
2557
-		c.Fatal(err)
2533
+	if id1 != id2 {
2534
+		c.Fatal("The cache should have been used but hasn't.")
2558 2535
 	}
2559
-	if id1 == id2 {
2536
+	if id1 == id3 {
2560 2537
 		c.Fatal("The cache should have been invalided but hasn't.")
2561 2538
 	}
2562 2539
 }
... ...
@@ -2649,9 +2002,9 @@ func (s *DockerSuite) TestBuildAddCurrentDirWithCache(c *check.C) {
2649 2649
 	}
2650 2650
 }
2651 2651
 
2652
+// FIXME(vdemeester) this really seems to test the same thing as before (TestBuildAddMultipleLocalFileWithAndWithoutCache)
2652 2653
 func (s *DockerSuite) TestBuildAddCurrentDirWithoutCache(c *check.C) {
2653 2654
 	name := "testbuildaddcurrentdirwithoutcache"
2654
-	name2 := "testbuildaddcurrentdirwithoutcache2"
2655 2655
 	dockerfile := `
2656 2656
         FROM ` + minimalBaseImage() + `
2657 2657
         MAINTAINER dockerio
... ...
@@ -2667,7 +2020,7 @@ func (s *DockerSuite) TestBuildAddCurrentDirWithoutCache(c *check.C) {
2667 2667
 	if err != nil {
2668 2668
 		c.Fatal(err)
2669 2669
 	}
2670
-	id2, err := buildImageFromContext(name2, ctx, false)
2670
+	id2, err := buildImageFromContext(name, ctx, false)
2671 2671
 	if err != nil {
2672 2672
 		c.Fatal(err)
2673 2673
 	}
... ...
@@ -2676,7 +2029,7 @@ func (s *DockerSuite) TestBuildAddCurrentDirWithoutCache(c *check.C) {
2676 2676
 	}
2677 2677
 }
2678 2678
 
2679
-func (s *DockerSuite) TestBuildAddRemoteFileWithCache(c *check.C) {
2679
+func (s *DockerSuite) TestBuildAddRemoteFileWithAndWithoutCache(c *check.C) {
2680 2680
 	name := "testbuildaddremotefilewithcache"
2681 2681
 	server, err := fakeStorage(map[string]string{
2682 2682
 		"baz": "hello",
... ...
@@ -2686,55 +2039,23 @@ func (s *DockerSuite) TestBuildAddRemoteFileWithCache(c *check.C) {
2686 2686
 	}
2687 2687
 	defer server.Close()
2688 2688
 
2689
-	id1, err := buildImage(name,
2690
-		fmt.Sprintf(`FROM `+minimalBaseImage()+`
2691
-        MAINTAINER dockerio
2692
-        ADD %s/baz /usr/lib/baz/quux`, server.URL()),
2693
-		true)
2694
-	if err != nil {
2695
-		c.Fatal(err)
2696
-	}
2697
-	id2, err := buildImage(name,
2698
-		fmt.Sprintf(`FROM `+minimalBaseImage()+`
2689
+	dockerfile := fmt.Sprintf(`FROM `+minimalBaseImage()+`
2699 2690
         MAINTAINER dockerio
2700
-        ADD %s/baz /usr/lib/baz/quux`, server.URL()),
2701
-		true)
2702
-	if err != nil {
2703
-		c.Fatal(err)
2704
-	}
2691
+        ADD %s/baz /usr/lib/baz/quux`, server.URL())
2692
+	buildImageSuccessfully(c, name, withDockerfile(dockerfile))
2693
+	id1, err := getIDByName(name)
2694
+	c.Assert(err, checker.IsNil)
2695
+	buildImageSuccessfully(c, name, withDockerfile(dockerfile))
2696
+	id2, err := getIDByName(name)
2697
+	c.Assert(err, checker.IsNil)
2698
+	buildImageSuccessfully(c, name, withoutCache, withDockerfile(dockerfile))
2699
+	id3, err := getIDByName(name)
2700
+	c.Assert(err, checker.IsNil)
2701
+
2705 2702
 	if id1 != id2 {
2706 2703
 		c.Fatal("The cache should have been used but hasn't.")
2707 2704
 	}
2708
-}
2709
-
2710
-func (s *DockerSuite) TestBuildAddRemoteFileWithoutCache(c *check.C) {
2711
-	name := "testbuildaddremotefilewithoutcache"
2712
-	name2 := "testbuildaddremotefilewithoutcache2"
2713
-	server, err := fakeStorage(map[string]string{
2714
-		"baz": "hello",
2715
-	})
2716
-	if err != nil {
2717
-		c.Fatal(err)
2718
-	}
2719
-	defer server.Close()
2720
-
2721
-	id1, err := buildImage(name,
2722
-		fmt.Sprintf(`FROM `+minimalBaseImage()+`
2723
-        MAINTAINER dockerio
2724
-        ADD %s/baz /usr/lib/baz/quux`, server.URL()),
2725
-		true)
2726
-	if err != nil {
2727
-		c.Fatal(err)
2728
-	}
2729
-	id2, err := buildImage(name2,
2730
-		fmt.Sprintf(`FROM `+minimalBaseImage()+`
2731
-        MAINTAINER dockerio
2732
-        ADD %s/baz /usr/lib/baz/quux`, server.URL()),
2733
-		false)
2734
-	if err != nil {
2735
-		c.Fatal(err)
2736
-	}
2737
-	if id1 == id2 {
2705
+	if id1 == id3 {
2738 2706
 		c.Fatal("The cache should have been invalided but hasn't.")
2739 2707
 	}
2740 2708
 }
... ...
@@ -2800,7 +2121,8 @@ func (s *DockerSuite) TestBuildAddRemoteFileMTime(c *check.C) {
2800 2800
 	}
2801 2801
 }
2802 2802
 
2803
-func (s *DockerSuite) TestBuildAddLocalAndRemoteFilesWithCache(c *check.C) {
2803
+// FIXME(vdemeester) this really seems to test the same thing as before (combined)
2804
+func (s *DockerSuite) TestBuildAddLocalAndRemoteFilesWithAndWithoutCache(c *check.C) {
2804 2805
 	name := "testbuildaddlocalandremotefilewithcache"
2805 2806
 	server, err := fakeStorage(map[string]string{
2806 2807
 		"baz": "hello",
... ...
@@ -2829,9 +2151,16 @@ func (s *DockerSuite) TestBuildAddLocalAndRemoteFilesWithCache(c *check.C) {
2829 2829
 	if err != nil {
2830 2830
 		c.Fatal(err)
2831 2831
 	}
2832
+	id3, err := buildImageFromContext(name, ctx, false)
2833
+	if err != nil {
2834
+		c.Fatal(err)
2835
+	}
2832 2836
 	if id1 != id2 {
2833 2837
 		c.Fatal("The cache should have been used but hasn't.")
2834 2838
 	}
2839
+	if id1 == id3 {
2840
+		c.Fatal("The cache should have been invalidated but hasn't.")
2841
+	}
2835 2842
 }
2836 2843
 
2837 2844
 func testContextTar(c *check.C, compression archive.Compression) {
... ...
@@ -2852,12 +2181,11 @@ CMD ["cat", "/foo"]`,
2852 2852
 		c.Fatalf("failed to build context tar: %v", err)
2853 2853
 	}
2854 2854
 	name := "contexttar"
2855
-	buildCmd := exec.Command(dockerBinary, "build", "-t", name, "-")
2856
-	buildCmd.Stdin = context
2857 2855
 
2858
-	if out, _, err := runCommandWithOutput(buildCmd); err != nil {
2859
-		c.Fatalf("build failed to complete: %v %v", out, err)
2860
-	}
2856
+	icmd.RunCmd(icmd.Cmd{
2857
+		Command: []string{dockerBinary, "build", "-t", name, "-"},
2858
+		Stdin:   context,
2859
+	}).Assert(c, icmd.Success)
2861 2860
 }
2862 2861
 
2863 2862
 func (s *DockerSuite) TestBuildContextTarGzip(c *check.C) {
... ...
@@ -2869,76 +2197,31 @@ func (s *DockerSuite) TestBuildContextTarNoCompression(c *check.C) {
2869 2869
 }
2870 2870
 
2871 2871
 func (s *DockerSuite) TestBuildNoContext(c *check.C) {
2872
-	buildCmd := exec.Command(dockerBinary, "build", "-t", "nocontext", "-")
2873
-	buildCmd.Stdin = strings.NewReader(
2874
-		`FROM busybox
2875
-		CMD ["echo", "ok"]`)
2876
-
2877
-	if out, _, err := runCommandWithOutput(buildCmd); err != nil {
2878
-		c.Fatalf("build failed to complete: %v %v", out, err)
2879
-	}
2872
+	name := "nocontext"
2873
+	icmd.RunCmd(icmd.Cmd{
2874
+		Command: []string{dockerBinary, "build", "-t", name, "-"},
2875
+		Stdin: strings.NewReader(
2876
+			`FROM busybox
2877
+			CMD ["echo", "ok"]`),
2878
+	}).Assert(c, icmd.Success)
2880 2879
 
2881 2880
 	if out, _ := dockerCmd(c, "run", "--rm", "nocontext"); out != "ok\n" {
2882 2881
 		c.Fatalf("run produced invalid output: %q, expected %q", out, "ok")
2883 2882
 	}
2884 2883
 }
2885 2884
 
2886
-// TODO: TestCaching
2887
-func (s *DockerSuite) TestBuildAddLocalAndRemoteFilesWithoutCache(c *check.C) {
2888
-	name := "testbuildaddlocalandremotefilewithoutcache"
2889
-	name2 := "testbuildaddlocalandremotefilewithoutcache2"
2890
-	server, err := fakeStorage(map[string]string{
2891
-		"baz": "hello",
2892
-	})
2893
-	if err != nil {
2894
-		c.Fatal(err)
2895
-	}
2896
-	defer server.Close()
2897
-
2898
-	ctx, err := fakeContext(fmt.Sprintf(`FROM `+minimalBaseImage()+`
2899
-        MAINTAINER dockerio
2900
-        ADD foo /usr/lib/bla/bar
2901
-        ADD %s/baz /usr/lib/baz/quux`, server.URL()),
2902
-		map[string]string{
2903
-			"foo": "hello world",
2904
-		})
2905
-	if err != nil {
2906
-		c.Fatal(err)
2907
-	}
2908
-	defer ctx.Close()
2909
-	id1, err := buildImageFromContext(name, ctx, true)
2910
-	if err != nil {
2911
-		c.Fatal(err)
2912
-	}
2913
-	id2, err := buildImageFromContext(name2, ctx, false)
2914
-	if err != nil {
2915
-		c.Fatal(err)
2916
-	}
2917
-	if id1 == id2 {
2918
-		c.Fatal("The cache should have been invalidated but hasn't.")
2919
-	}
2920
-}
2921
-
2922 2885
 func (s *DockerSuite) TestBuildWithVolumeOwnership(c *check.C) {
2923 2886
 	testRequires(c, DaemonIsLinux)
2924 2887
 	name := "testbuildimg"
2925 2888
 
2926
-	_, err := buildImage(name,
2927
-		`FROM busybox:latest
2889
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox:latest
2928 2890
         RUN mkdir /test && chown daemon:daemon /test && chmod 0600 /test
2929
-        VOLUME /test`,
2930
-		true)
2931
-
2932
-	if err != nil {
2933
-		c.Fatal(err)
2934
-	}
2891
+        VOLUME /test`))
2935 2892
 
2936 2893
 	out, _ := dockerCmd(c, "run", "--rm", "testbuildimg", "ls", "-la", "/test")
2937
-
2938 2894
 	if expected := "drw-------"; !strings.Contains(out, expected) {
2939 2895
 		c.Fatalf("expected %s received %s", expected, out)
2940 2896
 	}
2941
-
2942 2897
 	if expected := "daemon   daemon"; !strings.Contains(out, expected) {
2943 2898
 		c.Fatalf("expected %s received %s", expected, out)
2944 2899
 	}
... ...
@@ -2949,27 +2232,16 @@ func (s *DockerSuite) TestBuildWithVolumeOwnership(c *check.C) {
2949 2949
 // utilizing cache
2950 2950
 func (s *DockerSuite) TestBuildEntrypointRunCleanup(c *check.C) {
2951 2951
 	name := "testbuildcmdcleanup"
2952
-	if _, err := buildImage(name,
2953
-		`FROM busybox
2954
-        RUN echo "hello"`,
2955
-		true); err != nil {
2956
-		c.Fatal(err)
2957
-	}
2952
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
2953
+        RUN echo "hello"`))
2958 2954
 
2959
-	ctx, err := fakeContext(`FROM busybox
2955
+	buildImageSuccessfully(c, name, withBuildContext(c,
2956
+		withFile("Dockerfile", `FROM busybox
2960 2957
         RUN echo "hello"
2961 2958
         ADD foo /foo
2962
-        ENTRYPOINT ["/bin/echo"]`,
2963
-		map[string]string{
2964
-			"foo": "hello",
2965
-		})
2966
-	if err != nil {
2967
-		c.Fatal(err)
2968
-	}
2969
-	defer ctx.Close()
2970
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
2971
-		c.Fatal(err)
2972
-	}
2959
+        ENTRYPOINT ["/bin/echo"]`),
2960
+		withFile("foo", "hello")))
2961
+
2973 2962
 	res := inspectField(c, name, "Config.Cmd")
2974 2963
 	// Cmd must be cleaned up
2975 2964
 	if res != "[]" {
... ...
@@ -2985,42 +2257,25 @@ func (s *DockerSuite) TestBuildAddFileNotFound(c *check.C) {
2985 2985
 		expected = "foo: The system cannot find the file specified"
2986 2986
 	}
2987 2987
 
2988
-	ctx, err := fakeContext(`FROM `+minimalBaseImage()+`
2989
-        ADD foo /usr/local/bar`,
2990
-		map[string]string{"bar": "hello"})
2991
-	if err != nil {
2992
-		c.Fatal(err)
2993
-	}
2994
-	defer ctx.Close()
2995
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
2996
-		if !strings.Contains(err.Error(), expected) {
2997
-			c.Fatalf("Wrong error %v, must be about missing foo file or directory", err)
2998
-		}
2999
-	} else {
3000
-		c.Fatal("Error must not be nil")
3001
-	}
2988
+	buildImageNew(name, withBuildContext(c,
2989
+		withFile("Dockerfile", `FROM `+minimalBaseImage()+`
2990
+        ADD foo /usr/local/bar`),
2991
+		withFile("bar", "hello"))).Assert(c, icmd.Expected{
2992
+		ExitCode: 1,
2993
+		Err:      expected,
2994
+	})
3002 2995
 }
3003 2996
 
3004 2997
 func (s *DockerSuite) TestBuildInheritance(c *check.C) {
3005 2998
 	testRequires(c, DaemonIsLinux)
3006 2999
 	name := "testbuildinheritance"
3007 3000
 
3008
-	_, err := buildImage(name,
3009
-		`FROM scratch
3010
-		EXPOSE 2375`,
3011
-		true)
3012
-	if err != nil {
3013
-		c.Fatal(err)
3014
-	}
3001
+	buildImageSuccessfully(c, name, withDockerfile(`FROM scratch
3002
+		EXPOSE 2375`))
3015 3003
 	ports1 := inspectField(c, name, "Config.ExposedPorts")
3016 3004
 
3017
-	_, err = buildImage(name,
3018
-		fmt.Sprintf(`FROM %s
3019
-		ENTRYPOINT ["/bin/echo"]`, name),
3020
-		true)
3021
-	if err != nil {
3022
-		c.Fatal(err)
3023
-	}
3005
+	buildImageSuccessfully(c, name, withDockerfile(fmt.Sprintf(`FROM %s
3006
+		ENTRYPOINT ["/bin/echo"]`, name)))
3024 3007
 
3025 3008
 	res := inspectField(c, name, "Config.Entrypoint")
3026 3009
 	if expected := "[/bin/echo]"; res != expected {
... ...
@@ -3034,35 +2289,19 @@ func (s *DockerSuite) TestBuildInheritance(c *check.C) {
3034 3034
 
3035 3035
 func (s *DockerSuite) TestBuildFails(c *check.C) {
3036 3036
 	name := "testbuildfails"
3037
-	_, err := buildImage(name,
3038
-		`FROM busybox
3039
-		RUN sh -c "exit 23"`,
3040
-		true)
3041
-	if err != nil {
3042
-		if !strings.Contains(err.Error(), "returned a non-zero code: 23") {
3043
-			c.Fatalf("Wrong error %v, must be about non-zero code 23", err)
3044
-		}
3045
-	} else {
3046
-		c.Fatal("Error must not be nil")
3047
-	}
3037
+	buildImageNew(name, withDockerfile(`FROM busybox
3038
+		RUN sh -c "exit 23"`)).Assert(c, icmd.Expected{
3039
+		ExitCode: 23,
3040
+		Err:      "returned a non-zero code: 23",
3041
+	})
3048 3042
 }
3049 3043
 
3050 3044
 func (s *DockerSuite) TestBuildOnBuild(c *check.C) {
3051 3045
 	name := "testbuildonbuild"
3052
-	_, err := buildImage(name,
3053
-		`FROM busybox
3054
-		ONBUILD RUN touch foobar`,
3055
-		true)
3056
-	if err != nil {
3057
-		c.Fatal(err)
3058
-	}
3059
-	_, err = buildImage(name,
3060
-		fmt.Sprintf(`FROM %s
3061
-		RUN [ -f foobar ]`, name),
3062
-		true)
3063
-	if err != nil {
3064
-		c.Fatal(err)
3065
-	}
3046
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
3047
+		ONBUILD RUN touch foobar`))
3048
+	buildImageSuccessfully(c, name, withDockerfile(fmt.Sprintf(`FROM %s
3049
+		RUN [ -f foobar ]`, name)))
3066 3050
 }
3067 3051
 
3068 3052
 // gh #2446
... ...
@@ -3072,40 +2311,30 @@ func (s *DockerSuite) TestBuildAddToSymlinkDest(c *check.C) {
3072 3072
 		makeLink = `mklink /D C:\bar C:\foo`
3073 3073
 	}
3074 3074
 	name := "testbuildaddtosymlinkdest"
3075
-	ctx, err := fakeContext(`FROM busybox
3076
-        RUN sh -c "mkdir /foo"
3077
-        RUN `+makeLink+`
3078
-        ADD foo /bar/
3079
-        RUN sh -c "[ -f /bar/foo ]"
3080
-        RUN sh -c "[ -f /foo/foo ]"`,
3081
-		map[string]string{
3082
-			"foo": "hello",
3083
-		})
3084
-	if err != nil {
3085
-		c.Fatal(err)
3086
-	}
3087
-	defer ctx.Close()
3088
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
3089
-		c.Fatal(err)
3090
-	}
3075
+	buildImageSuccessfully(c, name, withBuildContext(c,
3076
+		withFile("Dockerfile", `
3077
+		FROM busybox
3078
+		RUN sh -c "mkdir /foo"
3079
+		RUN `+makeLink+`
3080
+		ADD foo /bar/
3081
+		RUN sh -c "[ -f /bar/foo ]"
3082
+		RUN sh -c "[ -f /foo/foo ]"`),
3083
+		withFile("foo", "hello"),
3084
+	))
3091 3085
 }
3092 3086
 
3093 3087
 func (s *DockerSuite) TestBuildEscapeWhitespace(c *check.C) {
3094 3088
 	name := "testbuildescapewhitespace"
3095 3089
 
3096
-	_, err := buildImage(name, `
3090
+	buildImageSuccessfully(c, name, withDockerfile(`
3097 3091
   # ESCAPE=\
3098 3092
   FROM busybox
3099 3093
   MAINTAINER "Docker \
3100 3094
 IO <io@\
3101 3095
 docker.com>"
3102
-  `, true)
3103
-	if err != nil {
3104
-		c.Fatal(err)
3105
-	}
3096
+  `))
3106 3097
 
3107 3098
 	res := inspectField(c, name, "Author")
3108
-
3109 3099
 	if res != "\"Docker IO <io@docker.com>\"" {
3110 3100
 		c.Fatalf("Parsed string did not match the escaped string. Got: %q", res)
3111 3101
 	}
... ...
@@ -3116,17 +2345,11 @@ func (s *DockerSuite) TestBuildVerifyIntString(c *check.C) {
3116 3116
 	// Verify that strings that look like ints are still passed as strings
3117 3117
 	name := "testbuildstringing"
3118 3118
 
3119
-	_, err := buildImage(name, `
3120
-  FROM busybox
3121
-  MAINTAINER 123
3122
-  `, true)
3123
-
3124
-	if err != nil {
3125
-		c.Fatal(err)
3126
-	}
3119
+	buildImageSuccessfully(c, name, withDockerfile(`
3120
+	FROM busybox
3121
+	MAINTAINER 123`))
3127 3122
 
3128 3123
 	out, _ := dockerCmd(c, "inspect", name)
3129
-
3130 3124
 	if !strings.Contains(out, "\"123\"") {
3131 3125
 		c.Fatalf("Output does not contain the int as a string:\n%s", out)
3132 3126
 	}
... ...
@@ -3135,9 +2358,10 @@ func (s *DockerSuite) TestBuildVerifyIntString(c *check.C) {
3135 3135
 
3136 3136
 func (s *DockerSuite) TestBuildDockerignore(c *check.C) {
3137 3137
 	name := "testbuilddockerignore"
3138
-	dockerfile := `
3139
-        FROM busybox
3140
-        ADD . /bla
3138
+	buildImageSuccessfully(c, name, withBuildContext(c,
3139
+		withFile("Dockerfile", `
3140
+		FROM busybox
3141
+		 ADD . /bla
3141 3142
 		RUN sh -c "[[ -f /bla/src/x.go ]]"
3142 3143
 		RUN sh -c "[[ -f /bla/Makefile ]]"
3143 3144
 		RUN sh -c "[[ ! -e /bla/src/_vendor ]]"
... ...
@@ -3148,62 +2372,48 @@ func (s *DockerSuite) TestBuildDockerignore(c *check.C) {
3148 3148
 		RUN sh -c "[[ ! -e /bla/.git ]]"
3149 3149
 		RUN sh -c "[[ ! -e v.cc ]]"
3150 3150
 		RUN sh -c "[[ ! -e src/v.cc ]]"
3151
-		RUN sh -c "[[ ! -e src/_vendor/v.cc ]]"`
3152
-	ctx, err := fakeContext(dockerfile, map[string]string{
3153
-		"Makefile":         "all:",
3154
-		".git/HEAD":        "ref: foo",
3155
-		"src/x.go":         "package main",
3156
-		"src/_vendor/v.go": "package main",
3157
-		"src/_vendor/v.cc": "package main",
3158
-		"src/v.cc":         "package main",
3159
-		"v.cc":             "package main",
3160
-		"dir/foo":          "",
3161
-		".gitignore":       "",
3162
-		"README.md":        "readme",
3163
-		".dockerignore": `
3151
+		RUN sh -c "[[ ! -e src/_vendor/v.cc ]]"`),
3152
+		withFile("Makefile", "all:"),
3153
+		withFile(".git/HEAD", "ref: foo"),
3154
+		withFile("src/x.go", "package main"),
3155
+		withFile("src/_vendor/v.go", "package main"),
3156
+		withFile("src/_vendor/v.cc", "package main"),
3157
+		withFile("src/v.cc", "package main"),
3158
+		withFile("v.cc", "package main"),
3159
+		withFile("dir/foo", ""),
3160
+		withFile(".gitignore", ""),
3161
+		withFile("README.md", "readme"),
3162
+		withFile(".dockerignore", `
3164 3163
 .git
3165 3164
 pkg
3166 3165
 .gitignore
3167 3166
 src/_vendor
3168 3167
 *.md
3169 3168
 **/*.cc
3170
-dir`,
3171
-	})
3172
-	if err != nil {
3173
-		c.Fatal(err)
3174
-	}
3175
-	defer ctx.Close()
3176
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
3177
-		c.Fatal(err)
3178
-	}
3169
+dir`),
3170
+	))
3179 3171
 }
3180 3172
 
3181 3173
 func (s *DockerSuite) TestBuildDockerignoreCleanPaths(c *check.C) {
3182 3174
 	name := "testbuilddockerignorecleanpaths"
3183
-	dockerfile := `
3175
+	buildImageSuccessfully(c, name, withBuildContext(c,
3176
+		withFile("Dockerfile", `
3184 3177
         FROM busybox
3185 3178
         ADD . /tmp/
3186
-        RUN sh -c "(! ls /tmp/foo) && (! ls /tmp/foo2) && (! ls /tmp/dir1/foo)"`
3187
-	ctx, err := fakeContext(dockerfile, map[string]string{
3188
-		"foo":           "foo",
3189
-		"foo2":          "foo2",
3190
-		"dir1/foo":      "foo in dir1",
3191
-		".dockerignore": "./foo\ndir1//foo\n./dir1/../foo2",
3192
-	})
3193
-	if err != nil {
3194
-		c.Fatal(err)
3195
-	}
3196
-	defer ctx.Close()
3197
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
3198
-		c.Fatal(err)
3199
-	}
3179
+        RUN sh -c "(! ls /tmp/foo) && (! ls /tmp/foo2) && (! ls /tmp/dir1/foo)"`),
3180
+		withFile("foo", "foo"),
3181
+		withFile("foo2", "foo2"),
3182
+		withFile("dir1/foo", "foo in dir1"),
3183
+		withFile(".dockerignore", "./foo\ndir1//foo\n./dir1/../foo2"),
3184
+	))
3200 3185
 }
3201 3186
 
3202 3187
 func (s *DockerSuite) TestBuildDockerignoreExceptions(c *check.C) {
3203 3188
 	name := "testbuilddockerignoreexceptions"
3204
-	dockerfile := `
3205
-        FROM busybox
3206
-        ADD . /bla
3189
+	buildImageSuccessfully(c, name, withBuildContext(c,
3190
+		withFile("Dockerfile", `
3191
+		FROM busybox
3192
+		ADD . /bla
3207 3193
 		RUN sh -c "[[ -f /bla/src/x.go ]]"
3208 3194
 		RUN sh -c "[[ -f /bla/Makefile ]]"
3209 3195
 		RUN sh -c "[[ ! -e /bla/src/_vendor ]]"
... ...
@@ -3215,22 +2425,21 @@ func (s *DockerSuite) TestBuildDockerignoreExceptions(c *check.C) {
3215 3215
 		RUN sh -c "[[ -f /bla/dir/e-dir/foo ]]"
3216 3216
 		RUN sh -c "[[ ! -e /bla/foo ]]"
3217 3217
 		RUN sh -c "[[ ! -e /bla/.git ]]"
3218
-		RUN sh -c "[[ -e /bla/dir/a.cc ]]"`
3219
-	ctx, err := fakeContext(dockerfile, map[string]string{
3220
-		"Makefile":         "all:",
3221
-		".git/HEAD":        "ref: foo",
3222
-		"src/x.go":         "package main",
3223
-		"src/_vendor/v.go": "package main",
3224
-		"dir/foo":          "",
3225
-		"dir/foo1":         "",
3226
-		"dir/dir/f1":       "",
3227
-		"dir/dir/foo":      "",
3228
-		"dir/e":            "",
3229
-		"dir/e-dir/foo":    "",
3230
-		".gitignore":       "",
3231
-		"README.md":        "readme",
3232
-		"dir/a.cc":         "hello",
3233
-		".dockerignore": `
3218
+		RUN sh -c "[[ -e /bla/dir/a.cc ]]"`),
3219
+		withFile("Makefile", "all:"),
3220
+		withFile(".git/HEAD", "ref: foo"),
3221
+		withFile("src/x.go", "package main"),
3222
+		withFile("src/_vendor/v.go", "package main"),
3223
+		withFile("dir/foo", ""),
3224
+		withFile("dir/foo1", ""),
3225
+		withFile("dir/dir/f1", ""),
3226
+		withFile("dir/dir/foo", ""),
3227
+		withFile("dir/e", ""),
3228
+		withFile("dir/e-dir/foo", ""),
3229
+		withFile(".gitignore", ""),
3230
+		withFile("README.md", "readme"),
3231
+		withFile("dir/a.cc", "hello"),
3232
+		withFile(".dockerignore", `
3234 3233
 .git
3235 3234
 pkg
3236 3235
 .gitignore
... ...
@@ -3240,93 +2449,58 @@ dir
3240 3240
 !dir/e*
3241 3241
 !dir/dir/foo
3242 3242
 **/*.cc
3243
-!**/*.cc`,
3244
-	})
3245
-	if err != nil {
3246
-		c.Fatal(err)
3247
-	}
3248
-	defer ctx.Close()
3249
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
3250
-		c.Fatal(err)
3251
-	}
3243
+!**/*.cc`),
3244
+	))
3252 3245
 }
3253 3246
 
3254 3247
 func (s *DockerSuite) TestBuildDockerignoringDockerfile(c *check.C) {
3255 3248
 	name := "testbuilddockerignoredockerfile"
3256 3249
 	dockerfile := `
3257
-        FROM busybox
3250
+		FROM busybox
3258 3251
 		ADD . /tmp/
3259 3252
 		RUN sh -c "! ls /tmp/Dockerfile"
3260 3253
 		RUN ls /tmp/.dockerignore`
3261
-	ctx, err := fakeContext(dockerfile, map[string]string{
3262
-		"Dockerfile":    dockerfile,
3263
-		".dockerignore": "Dockerfile\n",
3264
-	})
3265
-	if err != nil {
3266
-		c.Fatal(err)
3267
-	}
3268
-	defer ctx.Close()
3269
-
3270
-	if _, err = buildImageFromContext(name, ctx, true); err != nil {
3271
-		c.Fatalf("Didn't ignore Dockerfile correctly:%s", err)
3272
-	}
3273
-
3274
-	// now try it with ./Dockerfile
3275
-	ctx.Add(".dockerignore", "./Dockerfile\n")
3276
-	if _, err = buildImageFromContext(name, ctx, true); err != nil {
3277
-		c.Fatalf("Didn't ignore ./Dockerfile correctly:%s", err)
3278
-	}
3279
-
3254
+	buildImageSuccessfully(c, name, withBuildContext(c,
3255
+		withFile("Dockerfile", dockerfile),
3256
+		withFile(".dockerignore", "Dockerfile\n"),
3257
+	))
3258
+	buildImageSuccessfully(c, name, withBuildContext(c,
3259
+		withFile("Dockerfile", dockerfile),
3260
+		withFile(".dockerignore", "./Dockerfile\n"),
3261
+	))
3280 3262
 }
3281 3263
 
3282 3264
 func (s *DockerSuite) TestBuildDockerignoringRenamedDockerfile(c *check.C) {
3283 3265
 	name := "testbuilddockerignoredockerfile"
3284 3266
 	dockerfile := `
3285
-        FROM busybox
3267
+		FROM busybox
3286 3268
 		ADD . /tmp/
3287 3269
 		RUN ls /tmp/Dockerfile
3288 3270
 		RUN sh -c "! ls /tmp/MyDockerfile"
3289 3271
 		RUN ls /tmp/.dockerignore`
3290
-	ctx, err := fakeContext(dockerfile, map[string]string{
3291
-		"Dockerfile":    "Should not use me",
3292
-		"MyDockerfile":  dockerfile,
3293
-		".dockerignore": "MyDockerfile\n",
3294
-	})
3295
-	if err != nil {
3296
-		c.Fatal(err)
3297
-	}
3298
-	defer ctx.Close()
3299
-
3300
-	if _, err = buildImageFromContext(name, ctx, true); err != nil {
3301
-		c.Fatalf("Didn't ignore MyDockerfile correctly:%s", err)
3302
-	}
3303
-
3304
-	// now try it with ./MyDockerfile
3305
-	ctx.Add(".dockerignore", "./MyDockerfile\n")
3306
-	if _, err = buildImageFromContext(name, ctx, true); err != nil {
3307
-		c.Fatalf("Didn't ignore ./MyDockerfile correctly:%s", err)
3308
-	}
3309
-
3272
+	buildImageSuccessfully(c, name, withBuildFlags("-f", "MyDockerfile"), withBuildContext(c,
3273
+		withFile("Dockerfile", "Should not use me"),
3274
+		withFile("MyDockerfile", dockerfile),
3275
+		withFile(".dockerignore", "MyDockerfile\n"),
3276
+	))
3277
+	buildImageSuccessfully(c, name, withBuildFlags("-f", "MyDockerfile"), withBuildContext(c,
3278
+		withFile("Dockerfile", "Should not use me"),
3279
+		withFile("MyDockerfile", dockerfile),
3280
+		withFile(".dockerignore", "./MyDockerfile\n"),
3281
+	))
3310 3282
 }
3311 3283
 
3312 3284
 func (s *DockerSuite) TestBuildDockerignoringDockerignore(c *check.C) {
3313 3285
 	name := "testbuilddockerignoredockerignore"
3314 3286
 	dockerfile := `
3315
-        FROM busybox
3287
+		FROM busybox
3316 3288
 		ADD . /tmp/
3317 3289
 		RUN sh -c "! ls /tmp/.dockerignore"
3318 3290
 		RUN ls /tmp/Dockerfile`
3319
-	ctx, err := fakeContext(dockerfile, map[string]string{
3320
-		"Dockerfile":    dockerfile,
3321
-		".dockerignore": ".dockerignore\n",
3322
-	})
3323
-	if err != nil {
3324
-		c.Fatal(err)
3325
-	}
3326
-	defer ctx.Close()
3327
-	if _, err = buildImageFromContext(name, ctx, true); err != nil {
3328
-		c.Fatalf("Didn't ignore .dockerignore correctly:%s", err)
3329
-	}
3291
+	buildImageSuccessfully(c, name, withBuildContext(c,
3292
+		withFile("Dockerfile", dockerfile),
3293
+		withFile(".dockerignore", ".dockerignore\n"),
3294
+	))
3330 3295
 }
3331 3296
 
3332 3297
 func (s *DockerSuite) TestBuildDockerignoreTouchDockerfile(c *check.C) {
... ...
@@ -3378,90 +2552,78 @@ func (s *DockerSuite) TestBuildDockerignoreTouchDockerfile(c *check.C) {
3378 3378
 	if id1 != id2 {
3379 3379
 		c.Fatalf("Didn't use the cache - 3")
3380 3380
 	}
3381
-
3382 3381
 }
3383 3382
 
3384 3383
 func (s *DockerSuite) TestBuildDockerignoringWholeDir(c *check.C) {
3385 3384
 	name := "testbuilddockerignorewholedir"
3385
+
3386 3386
 	dockerfile := `
3387
-        FROM busybox
3387
+		FROM busybox
3388 3388
 		COPY . /
3389 3389
 		RUN sh -c "[[ ! -e /.gitignore ]]"
3390
-		RUN sh -c "[[ -f /Makefile ]]"`
3391
-	ctx, err := fakeContext(dockerfile, map[string]string{
3392
-		"Dockerfile":    "FROM scratch",
3393
-		"Makefile":      "all:",
3394
-		".gitignore":    "",
3395
-		".dockerignore": ".*\n",
3396
-	})
3397
-	c.Assert(err, check.IsNil)
3398
-	defer ctx.Close()
3399
-	if _, err = buildImageFromContext(name, ctx, true); err != nil {
3400
-		c.Fatal(err)
3401
-	}
3390
+		RUN sh -c "[[ ! -e /Makefile ]]"`
3402 3391
 
3403
-	c.Assert(ctx.Add(".dockerfile", "*"), check.IsNil)
3404
-	if _, err = buildImageFromContext(name, ctx, true); err != nil {
3405
-		c.Fatal(err)
3406
-	}
3392
+	buildImageSuccessfully(c, name, withBuildContext(c,
3393
+		withFile("Dockerfile", dockerfile),
3394
+		withFile(".dockerignore", "*\n"),
3395
+		withFile("Makefile", "all:"),
3396
+		withFile(".gitignore", ""),
3397
+	))
3398
+}
3407 3399
 
3408
-	c.Assert(ctx.Add(".dockerfile", "."), check.IsNil)
3409
-	if _, err = buildImageFromContext(name, ctx, true); err != nil {
3410
-		c.Fatal(err)
3411
-	}
3400
+func (s *DockerSuite) TestBuildDockerignoringOnlyDotfiles(c *check.C) {
3401
+	name := "testbuilddockerignorewholedir"
3412 3402
 
3413
-	c.Assert(ctx.Add(".dockerfile", "?"), check.IsNil)
3414
-	if _, err = buildImageFromContext(name, ctx, true); err != nil {
3415
-		c.Fatal(err)
3416
-	}
3403
+	dockerfile := `
3404
+		FROM busybox
3405
+		COPY . /
3406
+		RUN sh -c "[[ ! -e /.gitignore ]]"
3407
+		RUN sh -c "[[ -f /Makefile ]]"`
3408
+
3409
+	buildImageSuccessfully(c, name, withBuildContext(c,
3410
+		withFile("Dockerfile", dockerfile),
3411
+		withFile(".dockerignore", ".*"),
3412
+		withFile("Makefile", "all:"),
3413
+		withFile(".gitignore", ""),
3414
+	))
3417 3415
 }
3418 3416
 
3419 3417
 func (s *DockerSuite) TestBuildDockerignoringBadExclusion(c *check.C) {
3420 3418
 	name := "testbuilddockerignorebadexclusion"
3421
-	dockerfile := `
3422
-        FROM busybox
3419
+	buildImageNew(name, withBuildContext(c,
3420
+		withFile("Dockerfile", `
3421
+		FROM busybox
3423 3422
 		COPY . /
3424 3423
 		RUN sh -c "[[ ! -e /.gitignore ]]"
3425
-		RUN sh -c "[[ -f /Makefile ]]"`
3426
-	ctx, err := fakeContext(dockerfile, map[string]string{
3427
-		"Dockerfile":    "FROM scratch",
3428
-		"Makefile":      "all:",
3429
-		".gitignore":    "",
3430
-		".dockerignore": "!\n",
3424
+		RUN sh -c "[[ -f /Makefile ]]"`),
3425
+		withFile("Makefile", "all:"),
3426
+		withFile(".gitignore", ""),
3427
+		withFile(".dockerignore", "!\n"),
3428
+	)).Assert(c, icmd.Expected{
3429
+		ExitCode: 1,
3430
+		Err:      "Error checking context: 'Illegal exclusion pattern: !",
3431 3431
 	})
3432
-	c.Assert(err, check.IsNil)
3433
-	defer ctx.Close()
3434
-	if _, err = buildImageFromContext(name, ctx, true); err == nil {
3435
-		c.Fatalf("Build was supposed to fail but didn't")
3436
-	}
3437
-
3438
-	if err.Error() != "failed to build the image: Error checking context: 'Illegal exclusion pattern: !'.\n" {
3439
-		c.Fatalf("Incorrect output, got:%q", err.Error())
3440
-	}
3441 3432
 }
3442 3433
 
3443 3434
 func (s *DockerSuite) TestBuildDockerignoringWildTopDir(c *check.C) {
3444 3435
 	dockerfile := `
3445
-        FROM busybox
3436
+		FROM busybox
3446 3437
 		COPY . /
3447 3438
 		RUN sh -c "[[ ! -e /.dockerignore ]]"
3448 3439
 		RUN sh -c "[[ ! -e /Dockerfile ]]"
3449 3440
 		RUN sh -c "[[ ! -e /file1 ]]"
3450 3441
 		RUN sh -c "[[ ! -e /dir ]]"`
3451 3442
 
3452
-	ctx, err := fakeContext(dockerfile, map[string]string{
3453
-		"Dockerfile": "FROM scratch",
3454
-		"file1":      "",
3455
-		"dir/dfile1": "",
3456
-	})
3457
-	c.Assert(err, check.IsNil)
3458
-	defer ctx.Close()
3459
-
3460 3443
 	// All of these should result in ignoring all files
3461 3444
 	for _, variant := range []string{"**", "**/", "**/**", "*"} {
3462
-		ctx.Add(".dockerignore", variant)
3463
-		_, err = buildImageFromContext("noname", ctx, true)
3464
-		c.Assert(err, check.IsNil, check.Commentf("variant: %s", variant))
3445
+		buildImageSuccessfully(c, "noname", withBuildContext(c,
3446
+			withFile("Dockerfile", dockerfile),
3447
+			withFile("file1", ""),
3448
+			withFile("dir/file1", ""),
3449
+			withFile(".dockerignore", variant),
3450
+		))
3451
+
3452
+		dockerCmd(c, "rmi", "noname")
3465 3453
 	}
3466 3454
 }
3467 3455
 
... ...
@@ -3492,31 +2654,7 @@ func (s *DockerSuite) TestBuildDockerignoringWildDirs(c *check.C) {
3492 3492
 
3493 3493
 		RUN echo all done!`
3494 3494
 
3495
-	ctx, err := fakeContext(dockerfile, map[string]string{
3496
-		"Dockerfile":      "FROM scratch",
3497
-		"file0":           "",
3498
-		"dir1/file0":      "",
3499
-		"dir1/dir2/file0": "",
3500
-
3501
-		"file1":           "",
3502
-		"dir1/file1":      "",
3503
-		"dir1/dir2/file1": "",
3504
-
3505
-		"dir1/file2":      "",
3506
-		"dir1/dir2/file2": "", // remains
3507
-
3508
-		"dir1/dir2/file4": "",
3509
-		"dir1/dir2/file5": "",
3510
-		"dir1/dir2/file6": "",
3511
-		"dir1/dir3/file7": "",
3512
-		"dir1/dir3/file8": "",
3513
-		"dir1/dir4/file9": "",
3514
-
3515
-		"dir1/dir5/fileAA": "",
3516
-		"dir1/dir5/fileAB": "",
3517
-		"dir1/dir5/fileB":  "",
3518
-
3519
-		".dockerignore": `
3495
+	dockerignore := `
3520 3496
 **/file0
3521 3497
 **/*file1
3522 3498
 **/dir1/file2
... ...
@@ -3528,52 +2666,56 @@ dir1/dir3/**
3528 3528
 **/file?A
3529 3529
 **/file\?B
3530 3530
 **/dir5/file.
3531
-`,
3532
-	})
3533
-	c.Assert(err, check.IsNil)
3534
-	defer ctx.Close()
3531
+`
3535 3532
 
3536
-	_, err = buildImageFromContext("noname", ctx, true)
3537
-	c.Assert(err, check.IsNil)
3533
+	buildImageSuccessfully(c, "noname", withBuildContext(c,
3534
+		withFile("Dockerfile", dockerfile),
3535
+		withFile(".dockerignore", dockerignore),
3536
+		withFile("dir1/file0", ""),
3537
+		withFile("dir1/dir2/file0", ""),
3538
+		withFile("file1", ""),
3539
+		withFile("dir1/file1", ""),
3540
+		withFile("dir1/dir2/file1", ""),
3541
+		withFile("dir1/file2", ""),
3542
+		withFile("dir1/dir2/file2", ""), // remains
3543
+		withFile("dir1/dir2/file4", ""),
3544
+		withFile("dir1/dir2/file5", ""),
3545
+		withFile("dir1/dir2/file6", ""),
3546
+		withFile("dir1/dir3/file7", ""),
3547
+		withFile("dir1/dir3/file8", ""),
3548
+		withFile("dir1/dir4/file9", ""),
3549
+		withFile("dir1/dir5/fileAA", ""),
3550
+		withFile("dir1/dir5/fileAB", ""),
3551
+		withFile("dir1/dir5/fileB", ""),
3552
+	))
3538 3553
 }
3539 3554
 
3540 3555
 func (s *DockerSuite) TestBuildLineBreak(c *check.C) {
3541 3556
 	testRequires(c, DaemonIsLinux)
3542 3557
 	name := "testbuildlinebreak"
3543
-	_, err := buildImage(name,
3544
-		`FROM  busybox
3558
+	buildImageSuccessfully(c, name, withDockerfile(`FROM  busybox
3545 3559
 RUN    sh -c 'echo root:testpass \
3546 3560
 	> /tmp/passwd'
3547 3561
 RUN    mkdir -p /var/run/sshd
3548 3562
 RUN    sh -c "[ "$(cat /tmp/passwd)" = "root:testpass" ]"
3549
-RUN    sh -c "[ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]"`,
3550
-		true)
3551
-	if err != nil {
3552
-		c.Fatal(err)
3553
-	}
3563
+RUN    sh -c "[ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]"`))
3554 3564
 }
3555 3565
 
3556 3566
 func (s *DockerSuite) TestBuildEOLInLine(c *check.C) {
3557 3567
 	testRequires(c, DaemonIsLinux)
3558 3568
 	name := "testbuildeolinline"
3559
-	_, err := buildImage(name,
3560
-		`FROM   busybox
3569
+	buildImageSuccessfully(c, name, withDockerfile(`FROM   busybox
3561 3570
 RUN    sh -c 'echo root:testpass > /tmp/passwd'
3562 3571
 RUN    echo "foo \n bar"; echo "baz"
3563 3572
 RUN    mkdir -p /var/run/sshd
3564 3573
 RUN    sh -c "[ "$(cat /tmp/passwd)" = "root:testpass" ]"
3565
-RUN    sh -c "[ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]"`,
3566
-		true)
3567
-	if err != nil {
3568
-		c.Fatal(err)
3569
-	}
3574
+RUN    sh -c "[ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]"`))
3570 3575
 }
3571 3576
 
3572 3577
 func (s *DockerSuite) TestBuildCommentsShebangs(c *check.C) {
3573 3578
 	testRequires(c, DaemonIsLinux)
3574 3579
 	name := "testbuildcomments"
3575
-	_, err := buildImage(name,
3576
-		`FROM busybox
3580
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
3577 3581
 # This is an ordinary comment.
3578 3582
 RUN { echo '#!/bin/sh'; echo 'echo hello world'; } > /hello.sh
3579 3583
 RUN [ ! -x /hello.sh ]
... ...
@@ -3581,18 +2723,13 @@ RUN [ ! -x /hello.sh ]
3581 3581
 RUN chmod +x /hello.sh
3582 3582
 RUN [ -x /hello.sh ]
3583 3583
 RUN [ "$(cat /hello.sh)" = $'#!/bin/sh\necho hello world' ]
3584
-RUN [ "$(/hello.sh)" = "hello world" ]`,
3585
-		true)
3586
-	if err != nil {
3587
-		c.Fatal(err)
3588
-	}
3584
+RUN [ "$(/hello.sh)" = "hello world" ]`))
3589 3585
 }
3590 3586
 
3591 3587
 func (s *DockerSuite) TestBuildUsersAndGroups(c *check.C) {
3592 3588
 	testRequires(c, DaemonIsLinux)
3593 3589
 	name := "testbuildusers"
3594
-	_, err := buildImage(name,
3595
-		`FROM busybox
3590
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
3596 3591
 
3597 3592
 # Make sure our defaults work
3598 3593
 RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)" = '0:0/root:root' ]
... ...
@@ -3641,13 +2778,10 @@ RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/do
3641 3641
 
3642 3642
 # make sure unknown uid/gid still works properly
3643 3643
 USER 1042:1043
3644
-RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1042:1043/1042:1043/1043:1043' ]`,
3645
-		true)
3646
-	if err != nil {
3647
-		c.Fatal(err)
3648
-	}
3644
+RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1042:1043/1042:1043/1043:1043' ]`))
3649 3645
 }
3650 3646
 
3647
+// FIXME(vdemeester) rename this test (and probably "merge" it with the one below TestBuildEnvUsage2)
3651 3648
 func (s *DockerSuite) TestBuildEnvUsage(c *check.C) {
3652 3649
 	// /docker/world/hello is not owned by the correct user
3653 3650
 	testRequires(c, NotUserNamespace)
... ...
@@ -3664,7 +2798,7 @@ ENV    BAZ $BAR
3664 3664
 ENV    FOOPATH $PATH:$FOO
3665 3665
 RUN    [ "$BAR" = "$BAZ" ]
3666 3666
 RUN    [ "$FOOPATH" = "$PATH:/foo/baz" ]
3667
-ENV	   FROM hello/docker/world
3667
+ENV    FROM hello/docker/world
3668 3668
 ENV    TO /docker/world/hello
3669 3669
 ADD    $FROM $TO
3670 3670
 RUN    [ "$(cat $TO)" = "hello" ]
... ...
@@ -3672,20 +2806,13 @@ ENV    abc=def
3672 3672
 ENV    ghi=$abc
3673 3673
 RUN    [ "$ghi" = "def" ]
3674 3674
 `
3675
-	ctx, err := fakeContext(dockerfile, map[string]string{
3676
-		"hello/docker/world": "hello",
3677
-	})
3678
-	if err != nil {
3679
-		c.Fatal(err)
3680
-	}
3681
-	defer ctx.Close()
3682
-
3683
-	_, err = buildImageFromContext(name, ctx, true)
3684
-	if err != nil {
3685
-		c.Fatal(err)
3686
-	}
3675
+	buildImageSuccessfully(c, name, withBuildContext(c,
3676
+		withFile("Dockerfile", dockerfile),
3677
+		withFile("hello/docker/world", "hello"),
3678
+	))
3687 3679
 }
3688 3680
 
3681
+// FIXME(vdemeester) rename this test (and probably "merge" it with the one above TestBuildEnvUsage)
3689 3682
 func (s *DockerSuite) TestBuildEnvUsage2(c *check.C) {
3690 3683
 	// /docker/world/hello is not owned by the correct user
3691 3684
 	testRequires(c, NotUserNamespace)
... ...
@@ -3749,18 +2876,10 @@ ENV    eee4 'foo'
3749 3749
 RUN    [ "$eee1,$eee2,$eee3,$eee4" = 'foo,foo,foo,foo' ]
3750 3750
 
3751 3751
 `
3752
-	ctx, err := fakeContext(dockerfile, map[string]string{
3753
-		"hello/docker/world": "hello",
3754
-	})
3755
-	if err != nil {
3756
-		c.Fatal(err)
3757
-	}
3758
-	defer ctx.Close()
3759
-
3760
-	_, err = buildImageFromContext(name, ctx, true)
3761
-	if err != nil {
3762
-		c.Fatal(err)
3763
-	}
3752
+	buildImageSuccessfully(c, name, withBuildContext(c,
3753
+		withFile("Dockerfile", dockerfile),
3754
+		withFile("hello/docker/world", "hello"),
3755
+	))
3764 3756
 }
3765 3757
 
3766 3758
 func (s *DockerSuite) TestBuildAddScript(c *check.C) {
... ...
@@ -3772,18 +2891,11 @@ ADD test /test
3772 3772
 RUN ["chmod","+x","/test"]
3773 3773
 RUN ["/test"]
3774 3774
 RUN [ "$(cat /testfile)" = 'test!' ]`
3775
-	ctx, err := fakeContext(dockerfile, map[string]string{
3776
-		"test": "#!/bin/sh\necho 'test!' > /testfile",
3777
-	})
3778
-	if err != nil {
3779
-		c.Fatal(err)
3780
-	}
3781
-	defer ctx.Close()
3782 3775
 
3783
-	_, err = buildImageFromContext(name, ctx, true)
3784
-	if err != nil {
3785
-		c.Fatal(err)
3786
-	}
3776
+	buildImageSuccessfully(c, name, withBuildContext(c,
3777
+		withFile("Dockerfile", dockerfile),
3778
+		withFile("test", "#!/bin/sh\necho 'test!' > /testfile"),
3779
+	))
3787 3780
 }
3788 3781
 
3789 3782
 func (s *DockerSuite) TestBuildAddTar(c *check.C) {
... ...
@@ -3898,20 +3010,13 @@ func (s *DockerSuite) TestBuildAddNonTar(c *check.C) {
3898 3898
 	name := "testbuildaddnontar"
3899 3899
 
3900 3900
 	// Should not try to extract test.tar
3901
-	ctx, err := fakeContext(`
3901
+	buildImageSuccessfully(c, name, withBuildContext(c,
3902
+		withFile("Dockerfile", `
3902 3903
 		FROM busybox
3903 3904
 		ADD test.tar /
3904
-		RUN test -f /test.tar`,
3905
-		map[string]string{"test.tar": "not_a_tar_file"})
3906
-
3907
-	if err != nil {
3908
-		c.Fatal(err)
3909
-	}
3910
-	defer ctx.Close()
3911
-
3912
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
3913
-		c.Fatalf("build failed for TestBuildAddNonTar")
3914
-	}
3905
+		RUN test -f /test.tar`),
3906
+		withFile("test.tar", "not_a_tar_file"),
3907
+	))
3915 3908
 }
3916 3909
 
3917 3910
 func (s *DockerSuite) TestBuildAddTarXz(c *check.C) {
... ...
@@ -4033,9 +3138,9 @@ func (s *DockerSuite) TestBuildFromGit(c *check.C) {
4033 4033
 	name := "testbuildfromgit"
4034 4034
 	git, err := newFakeGit("repo", map[string]string{
4035 4035
 		"Dockerfile": `FROM busybox
4036
-					ADD first /first
4037
-					RUN [ -f /first ]
4038
-					MAINTAINER docker`,
4036
+		ADD first /first
4037
+		RUN [ -f /first ]
4038
+		MAINTAINER docker`,
4039 4039
 		"first": "test git data",
4040 4040
 	}, true)
4041 4041
 	if err != nil {
... ...
@@ -4043,10 +3148,8 @@ func (s *DockerSuite) TestBuildFromGit(c *check.C) {
4043 4043
 	}
4044 4044
 	defer git.Close()
4045 4045
 
4046
-	_, err = buildImageFromPath(name, git.RepoURL, true)
4047
-	if err != nil {
4048
-		c.Fatal(err)
4049
-	}
4046
+	buildImageSuccessfully(c, name, withBuildContextPath(git.RepoURL))
4047
+
4050 4048
 	res := inspectField(c, name, "Author")
4051 4049
 	if res != "docker" {
4052 4050
 		c.Fatalf("Maintainer should be docker, got %s", res)
... ...
@@ -4067,11 +3170,8 @@ func (s *DockerSuite) TestBuildFromGitWithContext(c *check.C) {
4067 4067
 	}
4068 4068
 	defer git.Close()
4069 4069
 
4070
-	u := fmt.Sprintf("%s#master:docker", git.RepoURL)
4071
-	_, err = buildImageFromPath(name, u, true)
4072
-	if err != nil {
4073
-		c.Fatal(err)
4074
-	}
4070
+	buildImageSuccessfully(c, name, withBuildContextPath(fmt.Sprintf("%s#master:docker", git.RepoURL)))
4071
+
4075 4072
 	res := inspectField(c, name, "Author")
4076 4073
 	if res != "docker" {
4077 4074
 		c.Fatalf("Maintainer should be docker, got %s", res)
... ...
@@ -4089,14 +3189,9 @@ func (s *DockerSuite) TestBuildFromGitwithF(c *check.C) {
4089 4089
 	}
4090 4090
 	defer git.Close()
4091 4091
 
4092
-	out, _, err := dockerCmdWithError("build", "-t", name, "--no-cache", "-f", "myApp/myDockerfile", git.RepoURL)
4093
-	if err != nil {
4094
-		c.Fatalf("Error on build. Out: %s\nErr: %v", out, err)
4095
-	}
4096
-
4097
-	if !strings.Contains(out, "hi from Dockerfile") {
4098
-		c.Fatalf("Missing expected output, got:\n%s", out)
4099
-	}
4092
+	buildImageNew(name, withBuildFlags("-f", "myApp/myDockerfile"), withBuildContextPath(git.RepoURL)).Assert(c, icmd.Expected{
4093
+		Out: "hi from Dockerfile",
4094
+	})
4100 4095
 }
4101 4096
 
4102 4097
 func (s *DockerSuite) TestBuildFromRemoteTarball(c *check.C) {
... ...
@@ -4128,11 +3223,9 @@ func (s *DockerSuite) TestBuildFromRemoteTarball(c *check.C) {
4128 4128
 
4129 4129
 	defer server.Close()
4130 4130
 
4131
-	_, err = buildImageFromPath(name, server.URL()+"/testT.tar", true)
4132
-	c.Assert(err, check.IsNil)
4131
+	buildImageSuccessfully(c, name, withBuildContextPath(server.URL()+"/testT.tar"))
4133 4132
 
4134 4133
 	res := inspectField(c, name, "Author")
4135
-
4136 4134
 	if res != "docker" {
4137 4135
 		c.Fatalf("Maintainer should be docker, got %s", res)
4138 4136
 	}
... ...
@@ -4140,24 +3233,17 @@ func (s *DockerSuite) TestBuildFromRemoteTarball(c *check.C) {
4140 4140
 
4141 4141
 func (s *DockerSuite) TestBuildCleanupCmdOnEntrypoint(c *check.C) {
4142 4142
 	name := "testbuildcmdcleanuponentrypoint"
4143
-	if _, err := buildImage(name,
4144
-		`FROM `+minimalBaseImage()+`
4145
-        CMD ["test"]
4146
-		ENTRYPOINT ["echo"]`,
4147
-		true); err != nil {
4148
-		c.Fatal(err)
4149
-	}
4150
-	if _, err := buildImage(name,
4151
-		fmt.Sprintf(`FROM %s
4152
-		ENTRYPOINT ["cat"]`, name),
4153
-		true); err != nil {
4154
-		c.Fatal(err)
4155
-	}
4143
+
4144
+	buildImageSuccessfully(c, name, withDockerfile(`FROM `+minimalBaseImage()+`
4145
+		CMD ["test"]
4146
+		ENTRYPOINT ["echo"]`))
4147
+	buildImageSuccessfully(c, name, withDockerfile(fmt.Sprintf(`FROM %s
4148
+		ENTRYPOINT ["cat"]`, name)))
4149
+
4156 4150
 	res := inspectField(c, name, "Config.Cmd")
4157 4151
 	if res != "[]" {
4158 4152
 		c.Fatalf("Cmd %s, expected nil", res)
4159 4153
 	}
4160
-
4161 4154
 	res = inspectField(c, name, "Config.Entrypoint")
4162 4155
 	if expected := "[cat]"; res != expected {
4163 4156
 		c.Fatalf("Entrypoint %s, expected %s", res, expected)
... ...
@@ -4166,14 +3252,10 @@ func (s *DockerSuite) TestBuildCleanupCmdOnEntrypoint(c *check.C) {
4166 4166
 
4167 4167
 func (s *DockerSuite) TestBuildClearCmd(c *check.C) {
4168 4168
 	name := "testbuildclearcmd"
4169
-	_, err := buildImage(name,
4170
-		`From `+minimalBaseImage()+`
4169
+	buildImageSuccessfully(c, name, withDockerfile(`FROM `+minimalBaseImage()+`
4171 4170
    ENTRYPOINT ["/bin/bash"]
4172
-   CMD []`,
4173
-		true)
4174
-	if err != nil {
4175
-		c.Fatal(err)
4176
-	}
4171
+   CMD []`))
4172
+
4177 4173
 	res := inspectFieldJSON(c, name, "Config.Cmd")
4178 4174
 	if res != "[]" {
4179 4175
 		c.Fatalf("Cmd %s, expected %s", res, "[]")
... ...
@@ -4185,9 +3267,8 @@ func (s *DockerSuite) TestBuildEmptyCmd(c *check.C) {
4185 4185
 	testRequires(c, DaemonIsLinux)
4186 4186
 
4187 4187
 	name := "testbuildemptycmd"
4188
-	if _, err := buildImage(name, "FROM "+minimalBaseImage()+"\nMAINTAINER quux\n", true); err != nil {
4189
-		c.Fatal(err)
4190
-	}
4188
+	buildImageSuccessfully(c, name, withDockerfile("FROM "+minimalBaseImage()+"\nMAINTAINER quux\n"))
4189
+
4191 4190
 	res := inspectFieldJSON(c, name, "Config.Cmd")
4192 4191
 	if res != "null" {
4193 4192
 		c.Fatalf("Cmd %s, expected %s", res, "null")
... ...
@@ -4196,43 +3277,31 @@ func (s *DockerSuite) TestBuildEmptyCmd(c *check.C) {
4196 4196
 
4197 4197
 func (s *DockerSuite) TestBuildOnBuildOutput(c *check.C) {
4198 4198
 	name := "testbuildonbuildparent"
4199
-	if _, err := buildImage(name, "FROM busybox\nONBUILD RUN echo foo\n", true); err != nil {
4200
-		c.Fatal(err)
4201
-	}
4199
+	buildImageSuccessfully(c, name, withDockerfile("FROM busybox\nONBUILD RUN echo foo\n"))
4202 4200
 
4203
-	_, out, err := buildImageWithOut(name, "FROM "+name+"\nMAINTAINER quux\n", true)
4204
-	if err != nil {
4205
-		c.Fatal(err)
4206
-	}
4207
-
4208
-	if !strings.Contains(out, "# Executing 1 build trigger") {
4209
-		c.Fatal("failed to find the build trigger output", out)
4210
-	}
4201
+	buildImageNew(name, withDockerfile("FROM "+name+"\nMAINTAINER quux\n")).Assert(c, icmd.Expected{
4202
+		Out: "# Executing 1 build trigger",
4203
+	})
4211 4204
 }
4212 4205
 
4206
+// FIXME(vdemeester) should be a unit test
4213 4207
 func (s *DockerSuite) TestBuildInvalidTag(c *check.C) {
4214 4208
 	name := "abcd:" + stringutils.GenerateRandomAlphaOnlyString(200)
4215
-	_, out, err := buildImageWithOut(name, "FROM "+minimalBaseImage()+"\nMAINTAINER quux\n", true)
4216
-	// if the error doesn't check for illegal tag name, or the image is built
4217
-	// then this should fail
4218
-	if !strings.Contains(out, "Error parsing reference") || strings.Contains(out, "Sending build context to Docker daemon") {
4219
-		c.Fatalf("failed to stop before building. Error: %s, Output: %s", err, out)
4220
-	}
4209
+	buildImageNew(name, withDockerfile("FROM "+minimalBaseImage()+"\nMAINTAINER quux\n")).Assert(c, icmd.Expected{
4210
+		ExitCode: 125,
4211
+		Err:      "Error parsing reference",
4212
+	})
4221 4213
 }
4222 4214
 
4223 4215
 func (s *DockerSuite) TestBuildCmdShDashC(c *check.C) {
4224 4216
 	name := "testbuildcmdshc"
4225
-	if _, err := buildImage(name, "FROM busybox\nCMD echo cmd\n", true); err != nil {
4226
-		c.Fatal(err)
4227
-	}
4217
+	buildImageSuccessfully(c, name, withDockerfile("FROM busybox\nCMD echo cmd\n"))
4228 4218
 
4229 4219
 	res := inspectFieldJSON(c, name, "Config.Cmd")
4230
-
4231 4220
 	expected := `["/bin/sh","-c","echo cmd"]`
4232 4221
 	if daemonPlatform == "windows" {
4233 4222
 		expected = `["cmd","/S","/C","echo cmd"]`
4234 4223
 	}
4235
-
4236 4224
 	if res != expected {
4237 4225
 		c.Fatalf("Expected value %s not in Config.Cmd: %s", expected, res)
4238 4226
 	}
... ...
@@ -4244,80 +3313,61 @@ func (s *DockerSuite) TestBuildCmdSpaces(c *check.C) {
4244 4244
 	// the arg separator to make sure ["echo","hi"] and ["echo hi"] don't
4245 4245
 	// look the same
4246 4246
 	name := "testbuildcmdspaces"
4247
-	var id1 string
4248
-	var id2 string
4249
-	var err error
4250 4247
 
4251
-	if id1, err = buildImage(name, "FROM busybox\nCMD [\"echo hi\"]\n", true); err != nil {
4252
-		c.Fatal(err)
4253
-	}
4254
-
4255
-	if id2, err = buildImage(name, "FROM busybox\nCMD [\"echo\", \"hi\"]\n", true); err != nil {
4256
-		c.Fatal(err)
4257
-	}
4248
+	buildImageSuccessfully(c, name, withDockerfile("FROM busybox\nCMD [\"echo hi\"]\n"))
4249
+	id1, err := getIDByName(name)
4250
+	c.Assert(err, checker.IsNil)
4251
+	buildImageSuccessfully(c, name, withDockerfile("FROM busybox\nCMD [\"echo\", \"hi\"]\n"))
4252
+	id2, err := getIDByName(name)
4258 4253
 
4259 4254
 	if id1 == id2 {
4260 4255
 		c.Fatal("Should not have resulted in the same CMD")
4261 4256
 	}
4262 4257
 
4263 4258
 	// Now do the same with ENTRYPOINT
4264
-	if id1, err = buildImage(name, "FROM busybox\nENTRYPOINT [\"echo hi\"]\n", true); err != nil {
4265
-		c.Fatal(err)
4266
-	}
4267
-
4268
-	if id2, err = buildImage(name, "FROM busybox\nENTRYPOINT [\"echo\", \"hi\"]\n", true); err != nil {
4269
-		c.Fatal(err)
4270
-	}
4259
+	buildImageSuccessfully(c, name, withDockerfile("FROM busybox\nENTRYPOINT [\"echo hi\"]\n"))
4260
+	id1, err = getIDByName(name)
4261
+	c.Assert(err, checker.IsNil)
4262
+	buildImageSuccessfully(c, name, withDockerfile("FROM busybox\nENTRYPOINT [\"echo\", \"hi\"]\n"))
4263
+	id2, err = getIDByName(name)
4271 4264
 
4272 4265
 	if id1 == id2 {
4273 4266
 		c.Fatal("Should not have resulted in the same ENTRYPOINT")
4274 4267
 	}
4275
-
4276 4268
 }
4277 4269
 
4278 4270
 func (s *DockerSuite) TestBuildCmdJSONNoShDashC(c *check.C) {
4279 4271
 	name := "testbuildcmdjson"
4280
-	if _, err := buildImage(name, "FROM busybox\nCMD [\"echo\", \"cmd\"]", true); err != nil {
4281
-		c.Fatal(err)
4282
-	}
4272
+	buildImageSuccessfully(c, name, withDockerfile("FROM busybox\nCMD [\"echo\", \"cmd\"]"))
4283 4273
 
4284 4274
 	res := inspectFieldJSON(c, name, "Config.Cmd")
4285
-
4286 4275
 	expected := `["echo","cmd"]`
4287
-
4288 4276
 	if res != expected {
4289 4277
 		c.Fatalf("Expected value %s not in Config.Cmd: %s", expected, res)
4290 4278
 	}
4291
-
4292 4279
 }
4293 4280
 
4294
-func (s *DockerSuite) TestBuildEntrypointInheritance(c *check.C) {
4295
-
4296
-	if _, err := buildImage("parent", `
4281
+func (s *DockerSuite) TestBuildEntrypointCanBeOverridenByChild(c *check.C) {
4282
+	buildImageSuccessfully(c, "parent", withDockerfile(`
4297 4283
     FROM busybox
4298 4284
     ENTRYPOINT exit 130
4299
-    `, true); err != nil {
4300
-		c.Fatal(err)
4301
-	}
4285
+    `))
4302 4286
 
4303
-	if _, status, _ := dockerCmdWithError("run", "parent"); status != 130 {
4304
-		c.Fatalf("expected exit code 130 but received %d", status)
4305
-	}
4287
+	icmd.RunCommand(dockerBinary, "run", "parent").Assert(c, icmd.Expected{
4288
+		ExitCode: 130,
4289
+	})
4306 4290
 
4307
-	if _, err := buildImage("child", `
4291
+	buildImageSuccessfully(c, "child", withDockerfile(`
4308 4292
     FROM parent
4309 4293
     ENTRYPOINT exit 5
4310
-    `, true); err != nil {
4311
-		c.Fatal(err)
4312
-	}
4313
-
4314
-	if _, status, _ := dockerCmdWithError("run", "child"); status != 5 {
4315
-		c.Fatalf("expected exit code 5 but received %d", status)
4316
-	}
4294
+    `))
4317 4295
 
4296
+	icmd.RunCommand(dockerBinary, "run", "child").Assert(c, icmd.Expected{
4297
+		ExitCode: 5,
4298
+	})
4318 4299
 }
4319 4300
 
4320
-func (s *DockerSuite) TestBuildEntrypointInheritanceInspect(c *check.C) {
4301
+func (s *DockerSuite) TestBuildEntrypointCanBeOverridenByChildInspect(c *check.C) {
4321 4302
 	var (
4322 4303
 		name     = "testbuildepinherit"
4323 4304
 		name2    = "testbuildepinherit2"
... ...
@@ -4328,40 +3378,23 @@ func (s *DockerSuite) TestBuildEntrypointInheritanceInspect(c *check.C) {
4328 4328
 		expected = `["cmd","/S","/C","echo quux"]`
4329 4329
 	}
4330 4330
 
4331
-	if _, err := buildImage(name, "FROM busybox\nENTRYPOINT /foo/bar", true); err != nil {
4332
-		c.Fatal(err)
4333
-	}
4334
-
4335
-	if _, err := buildImage(name2, fmt.Sprintf("FROM %s\nENTRYPOINT echo quux", name), true); err != nil {
4336
-		c.Fatal(err)
4337
-	}
4331
+	buildImageSuccessfully(c, name, withDockerfile("FROM busybox\nENTRYPOINT /foo/bar"))
4332
+	buildImageSuccessfully(c, name2, withDockerfile(fmt.Sprintf("FROM %s\nENTRYPOINT echo quux", name)))
4338 4333
 
4339 4334
 	res := inspectFieldJSON(c, name2, "Config.Entrypoint")
4340
-
4341 4335
 	if res != expected {
4342 4336
 		c.Fatalf("Expected value %s not in Config.Entrypoint: %s", expected, res)
4343 4337
 	}
4344 4338
 
4345
-	out, _ := dockerCmd(c, "run", name2)
4346
-
4347
-	expected = "quux"
4348
-
4349
-	if strings.TrimSpace(out) != expected {
4350
-		c.Fatalf("Expected output is %s, got %s", expected, out)
4351
-	}
4352
-
4339
+	icmd.RunCommand(dockerBinary, "run", name2).Assert(c, icmd.Expected{
4340
+		Out: "quux",
4341
+	})
4353 4342
 }
4354 4343
 
4355 4344
 func (s *DockerSuite) TestBuildRunShEntrypoint(c *check.C) {
4356 4345
 	name := "testbuildentrypoint"
4357
-	_, err := buildImage(name,
4358
-		`FROM busybox
4359
-                                ENTRYPOINT echo`,
4360
-		true)
4361
-	if err != nil {
4362
-		c.Fatal(err)
4363
-	}
4364
-
4346
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
4347
+                                ENTRYPOINT echo`))
4365 4348
 	dockerCmd(c, "run", "--rm", name)
4366 4349
 }
4367 4350
 
... ...
@@ -4369,7 +3402,7 @@ func (s *DockerSuite) TestBuildExoticShellInterpolation(c *check.C) {
4369 4369
 	testRequires(c, DaemonIsLinux)
4370 4370
 	name := "testbuildexoticshellinterpolation"
4371 4371
 
4372
-	_, err := buildImage(name, `
4372
+	buildImageSuccessfully(c, name, withDockerfile(`
4373 4373
 		FROM busybox
4374 4374
 
4375 4375
 		ENV SOME_VAR a.b.c
... ...
@@ -4387,11 +3420,7 @@ func (s *DockerSuite) TestBuildExoticShellInterpolation(c *check.C) {
4387 4387
 		RUN [ "${SOME_VAR:+Version: ${SOME_VAR}}" = 'Version: a.b.c' ]
4388 4388
 		RUN [ "${SOME_UNSET_VAR:+${SOME_VAR}}" = '' ]
4389 4389
 		RUN [ "${SOME_UNSET_VAR:-${SOME_VAR:-d.e.f}}" = 'a.b.c' ]
4390
-	`, false)
4391
-	if err != nil {
4392
-		c.Fatal(err)
4393
-	}
4394
-
4390
+	`))
4395 4391
 }
4396 4392
 
4397 4393
 func (s *DockerSuite) TestBuildVerifySingleQuoteFails(c *check.C) {
... ...
@@ -4401,18 +3430,17 @@ func (s *DockerSuite) TestBuildVerifySingleQuoteFails(c *check.C) {
4401 4401
 	// as a "string" instead of "JSON array" and pass it on to "sh -c" and
4402 4402
 	// it should barf on it.
4403 4403
 	name := "testbuildsinglequotefails"
4404
-
4405
-	if _, err := buildImage(name,
4406
-		`FROM busybox
4407
-		CMD [ '/bin/sh', '-c', 'echo hi' ]`,
4408
-		true); err != nil {
4409
-		c.Fatal(err)
4404
+	expectedExitCode := 2
4405
+	if daemonPlatform == "windows" {
4406
+		expectedExitCode = 127
4410 4407
 	}
4411 4408
 
4412
-	if _, _, err := dockerCmdWithError("run", "--rm", name); err == nil {
4413
-		c.Fatal("The image was not supposed to be able to run")
4414
-	}
4409
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
4410
+		CMD [ '/bin/sh', '-c', 'echo hi' ]`))
4415 4411
 
4412
+	icmd.RunCommand(dockerBinary, "run", "--rm", name).Assert(c, icmd.Expected{
4413
+		ExitCode: expectedExitCode,
4414
+	})
4416 4415
 }
4417 4416
 
4418 4417
 func (s *DockerSuite) TestBuildVerboseOut(c *check.C) {
... ...
@@ -4423,27 +3451,15 @@ func (s *DockerSuite) TestBuildVerboseOut(c *check.C) {
4423 4423
 		expected = "\n123\r\n"
4424 4424
 	}
4425 4425
 
4426
-	_, out, err := buildImageWithOut(name,
4427
-		`FROM busybox
4428
-RUN echo 123`,
4429
-		false)
4430
-
4431
-	if err != nil {
4432
-		c.Fatal(err)
4433
-	}
4434
-	if !strings.Contains(out, expected) {
4435
-		c.Fatalf("Output should contain %q: %q", "123", out)
4436
-	}
4437
-
4426
+	buildImageNew(name, withDockerfile(`FROM busybox
4427
+RUN echo 123`)).Assert(c, icmd.Expected{
4428
+		Out: expected,
4429
+	})
4438 4430
 }
4439 4431
 
4440 4432
 func (s *DockerSuite) TestBuildWithTabs(c *check.C) {
4441 4433
 	name := "testbuildwithtabs"
4442
-	_, err := buildImage(name,
4443
-		"FROM busybox\nRUN echo\tone\t\ttwo", true)
4444
-	if err != nil {
4445
-		c.Fatal(err)
4446
-	}
4434
+	buildImageSuccessfully(c, name, withDockerfile("FROM busybox\nRUN echo\tone\t\ttwo"))
4447 4435
 	res := inspectFieldJSON(c, name, "ContainerConfig.Cmd")
4448 4436
 	expected1 := `["/bin/sh","-c","echo\tone\t\ttwo"]`
4449 4437
 	expected2 := `["/bin/sh","-c","echo\u0009one\u0009\u0009two"]` // syntactically equivalent, and what Go 1.3 generates
... ...
@@ -4459,14 +3475,9 @@ func (s *DockerSuite) TestBuildWithTabs(c *check.C) {
4459 4459
 func (s *DockerSuite) TestBuildLabels(c *check.C) {
4460 4460
 	name := "testbuildlabel"
4461 4461
 	expected := `{"License":"GPL","Vendor":"Acme"}`
4462
-	_, err := buildImage(name,
4463
-		`FROM busybox
4462
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
4464 4463
 		LABEL Vendor=Acme
4465
-                LABEL License GPL`,
4466
-		true)
4467
-	if err != nil {
4468
-		c.Fatal(err)
4469
-	}
4464
+                LABEL License GPL`))
4470 4465
 	res := inspectFieldJSON(c, name, "Config.Labels")
4471 4466
 	if res != expected {
4472 4467
 		c.Fatalf("Labels %s, expected %s", res, expected)
... ...
@@ -4476,46 +3487,45 @@ func (s *DockerSuite) TestBuildLabels(c *check.C) {
4476 4476
 func (s *DockerSuite) TestBuildLabelsCache(c *check.C) {
4477 4477
 	name := "testbuildlabelcache"
4478 4478
 
4479
-	id1, err := buildImage(name,
4480
-		`FROM busybox
4481
-		LABEL Vendor=Acme`, false)
4482
-	if err != nil {
4483
-		c.Fatalf("Build 1 should have worked: %v", err)
4484
-	}
4485
-
4486
-	id2, err := buildImage(name,
4487
-		`FROM busybox
4488
-		LABEL Vendor=Acme`, true)
4489
-	if err != nil || id1 != id2 {
4479
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
4480
+		LABEL Vendor=Acme`))
4481
+	id1, err := getIDByName(name)
4482
+	c.Assert(err, checker.IsNil)
4483
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
4484
+		LABEL Vendor=Acme`))
4485
+	id2, err := getIDByName(name)
4486
+	c.Assert(err, checker.IsNil)
4487
+	if id1 != id2 {
4490 4488
 		c.Fatalf("Build 2 should have worked & used cache(%s,%s): %v", id1, id2, err)
4491 4489
 	}
4492 4490
 
4493
-	id2, err = buildImage(name,
4494
-		`FROM busybox
4495
-		LABEL Vendor=Acme1`, true)
4496
-	if err != nil || id1 == id2 {
4491
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
4492
+		LABEL Vendor=Acme1`))
4493
+	id2, err = getIDByName(name)
4494
+	c.Assert(err, checker.IsNil)
4495
+	if id1 == id2 {
4497 4496
 		c.Fatalf("Build 3 should have worked & NOT used cache(%s,%s): %v", id1, id2, err)
4498 4497
 	}
4499 4498
 
4500
-	id2, err = buildImage(name,
4501
-		`FROM busybox
4502
-		LABEL Vendor Acme`, true) // Note: " " and "=" should be same
4503
-	if err != nil || id1 != id2 {
4499
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
4500
+		LABEL Vendor Acme`))
4501
+	id2, err = getIDByName(name)
4502
+	c.Assert(err, checker.IsNil)
4503
+	if id1 != id2 {
4504 4504
 		c.Fatalf("Build 4 should have worked & used cache(%s,%s): %v", id1, id2, err)
4505 4505
 	}
4506 4506
 
4507 4507
 	// Now make sure the cache isn't used by mistake
4508
-	id1, err = buildImage(name,
4509
-		`FROM busybox
4510
-       LABEL f1=b1 f2=b2`, false)
4511
-	if err != nil {
4512
-		c.Fatalf("Build 5 should have worked: %q", err)
4513
-	}
4508
+	buildImageSuccessfully(c, name, withoutCache, withDockerfile(`FROM busybox
4509
+       LABEL f1=b1 f2=b2`))
4510
+	_, err = getIDByName(name)
4511
+	c.Assert(err, checker.IsNil)
4514 4512
 
4515
-	id2, err = buildImage(name,
4516
-		`FROM busybox
4517
-       LABEL f1="b1 f2=b2"`, true)
4518
-	if err != nil || id1 == id2 {
4513
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
4514
+       LABEL f1=b1 f2=b2`))
4515
+	id2, err = getIDByName(name)
4516
+	c.Assert(err, checker.IsNil)
4517
+	if id1 == id2 {
4519 4518
 		c.Fatalf("Build 6 should have worked & NOT used the cache(%s,%s): %q", id1, id2, err)
4520 4519
 	}
4521 4520
 
... ...
@@ -4524,60 +3534,49 @@ func (s *DockerSuite) TestBuildLabelsCache(c *check.C) {
4524 4524
 func (s *DockerSuite) TestBuildNotVerboseSuccess(c *check.C) {
4525 4525
 	// This test makes sure that -q works correctly when build is successful:
4526 4526
 	// stdout has only the image ID (long image ID) and stderr is empty.
4527
-	var stdout, stderr string
4528
-	var err error
4529 4527
 	outRegexp := regexp.MustCompile("^(sha256:|)[a-z0-9]{64}\\n$")
4528
+	buildFlags := withBuildFlags("-q")
4530 4529
 
4531 4530
 	tt := []struct {
4532 4531
 		Name      string
4533
-		BuildFunc func(string)
4532
+		BuildFunc func(string) *icmd.Result
4534 4533
 	}{
4535 4534
 		{
4536 4535
 			Name: "quiet_build_stdin_success",
4537
-			BuildFunc: func(name string) {
4538
-				_, stdout, stderr, err = buildImageWithStdoutStderr(name, "FROM busybox", true, "-q", "--force-rm", "--rm")
4536
+			BuildFunc: func(name string) *icmd.Result {
4537
+				return buildImageNew(name, buildFlags, withDockerfile("FROM busybox"))
4539 4538
 			},
4540 4539
 		},
4541 4540
 		{
4542 4541
 			Name: "quiet_build_ctx_success",
4543
-			BuildFunc: func(name string) {
4544
-				ctx, err := fakeContext("FROM busybox", map[string]string{
4545
-					"quiet_build_success_fctx": "test",
4546
-				})
4547
-				if err != nil {
4548
-					c.Fatalf("Failed to create context: %s", err.Error())
4549
-				}
4550
-				defer ctx.Close()
4551
-				_, stdout, stderr, err = buildImageFromContextWithStdoutStderr(name, ctx, true, "-q", "--force-rm", "--rm")
4542
+			BuildFunc: func(name string) *icmd.Result {
4543
+				return buildImageNew(name, buildFlags, withBuildContext(c,
4544
+					withFile("Dockerfile", "FROM busybox"),
4545
+					withFile("quiet_build_success_fctx", "test"),
4546
+				))
4552 4547
 			},
4553 4548
 		},
4554 4549
 		{
4555 4550
 			Name: "quiet_build_git_success",
4556
-			BuildFunc: func(name string) {
4551
+			BuildFunc: func(name string) *icmd.Result {
4557 4552
 				git, err := newFakeGit("repo", map[string]string{
4558 4553
 					"Dockerfile": "FROM busybox",
4559 4554
 				}, true)
4560
-				if err != nil {
4561
-					c.Fatalf("Failed to create the git repo: %s", err.Error())
4562
-				}
4563
-				defer git.Close()
4564
-				_, stdout, stderr, err = buildImageFromGitWithStdoutStderr(name, git, true, "-q", "--force-rm", "--rm")
4565
-
4555
+				c.Assert(err, checker.IsNil)
4556
+				return buildImageNew(name, buildFlags, withBuildContextPath(git.RepoURL))
4566 4557
 			},
4567 4558
 		},
4568 4559
 	}
4569 4560
 
4570 4561
 	for _, te := range tt {
4571
-		te.BuildFunc(te.Name)
4572
-		if err != nil {
4573
-			c.Fatalf("Test %s shouldn't fail, but got the following error: %s", te.Name, err.Error())
4574
-		}
4575
-		if outRegexp.Find([]byte(stdout)) == nil {
4576
-			c.Fatalf("Test %s expected stdout to match the [%v] regexp, but it is [%v]", te.Name, outRegexp, stdout)
4562
+		result := te.BuildFunc(te.Name)
4563
+		result.Assert(c, icmd.Success)
4564
+		if outRegexp.Find([]byte(result.Stdout())) == nil {
4565
+			c.Fatalf("Test %s expected stdout to match the [%v] regexp, but it is [%v]", te.Name, outRegexp, result.Stdout())
4577 4566
 		}
4578 4567
 
4579
-		if stderr != "" {
4580
-			c.Fatalf("Test %s expected stderr to be empty, but it is [%#v]", te.Name, stderr)
4568
+		if result.Stderr() != "" {
4569
+			c.Fatalf("Test %s expected stderr to be empty, but it is [%#v]", te.Name, result.Stderr())
4581 4570
 		}
4582 4571
 	}
4583 4572
 
... ...
@@ -4589,14 +3588,17 @@ func (s *DockerSuite) TestBuildNotVerboseFailureWithNonExistImage(c *check.C) {
4589 4589
 	// and stderr output in verbose mode
4590 4590
 	testRequires(c, Network)
4591 4591
 	testName := "quiet_build_not_exists_image"
4592
-	buildCmd := "FROM busybox11"
4593
-	_, _, qstderr, qerr := buildImageWithStdoutStderr(testName, buildCmd, false, "-q", "--force-rm", "--rm")
4594
-	_, vstdout, vstderr, verr := buildImageWithStdoutStderr(testName, buildCmd, false, "--force-rm", "--rm")
4595
-	if verr == nil || qerr == nil {
4596
-		c.Fatal(fmt.Errorf("Test [%s] expected to fail but didn't", testName))
4597
-	}
4598
-	if qstderr != vstdout+vstderr {
4599
-		c.Fatal(fmt.Errorf("Test[%s] expected that quiet stderr and verbose stdout are equal; quiet [%v], verbose [%v]", testName, qstderr, vstdout+vstderr))
4592
+	dockerfile := "FROM busybox11"
4593
+	quietResult := buildImageNew(testName, withBuildFlags("-q"), withDockerfile(dockerfile))
4594
+	quietResult.Assert(c, icmd.Expected{
4595
+		ExitCode: 1,
4596
+	})
4597
+	result := buildImageNew(testName, withDockerfile(dockerfile))
4598
+	result.Assert(c, icmd.Expected{
4599
+		ExitCode: 1,
4600
+	})
4601
+	if quietResult.Stderr() != result.Combined() {
4602
+		c.Fatal(fmt.Errorf("Test[%s] expected that quiet stderr and verbose stdout are equal; quiet [%v], verbose [%v]", testName, quietResult.Stderr(), result.Combined()))
4600 4603
 	}
4601 4604
 }
4602 4605
 
... ...
@@ -4604,22 +3606,25 @@ func (s *DockerSuite) TestBuildNotVerboseFailure(c *check.C) {
4604 4604
 	// This test makes sure that -q works correctly when build fails by
4605 4605
 	// comparing between the stderr output in quiet mode and in stdout
4606 4606
 	// and stderr output in verbose mode
4607
-	tt := []struct {
4608
-		TestName  string
4609
-		BuildCmds string
4607
+	testCases := []struct {
4608
+		testName   string
4609
+		dockerfile string
4610 4610
 	}{
4611 4611
 		{"quiet_build_no_from_at_the_beginning", "RUN whoami"},
4612 4612
 		{"quiet_build_unknown_instr", "FROMD busybox"},
4613 4613
 	}
4614 4614
 
4615
-	for _, te := range tt {
4616
-		_, _, qstderr, qerr := buildImageWithStdoutStderr(te.TestName, te.BuildCmds, false, "-q", "--force-rm", "--rm")
4617
-		_, vstdout, vstderr, verr := buildImageWithStdoutStderr(te.TestName, te.BuildCmds, false, "--force-rm", "--rm")
4618
-		if verr == nil || qerr == nil {
4619
-			c.Fatal(fmt.Errorf("Test [%s] expected to fail but didn't", te.TestName))
4620
-		}
4621
-		if qstderr != vstdout+vstderr {
4622
-			c.Fatal(fmt.Errorf("Test[%s] expected that quiet stderr and verbose stdout are equal; quiet [%v], verbose [%v]", te.TestName, qstderr, vstdout+vstderr))
4615
+	for _, tc := range testCases {
4616
+		quietResult := buildImageNew(tc.testName, withBuildFlags("-q"), withDockerfile(tc.dockerfile))
4617
+		quietResult.Assert(c, icmd.Expected{
4618
+			ExitCode: 1,
4619
+		})
4620
+		result := buildImageNew(tc.testName, withDockerfile(tc.dockerfile))
4621
+		result.Assert(c, icmd.Expected{
4622
+			ExitCode: 1,
4623
+		})
4624
+		if quietResult.Stderr() != result.Combined() {
4625
+			c.Fatal(fmt.Errorf("Test[%s] expected that quiet stderr and verbose stdout are equal; quiet [%v], verbose [%v]", tc.testName, quietResult.Stderr(), result.Combined()))
4623 4626
 		}
4624 4627
 	}
4625 4628
 }
... ...
@@ -4629,14 +3634,17 @@ func (s *DockerSuite) TestBuildNotVerboseFailureRemote(c *check.C) {
4629 4629
 	// stderr in verbose mode are identical.
4630 4630
 	// TODO(vdemeester) with cobra, stdout has a carriage return too much so this test should not check stdout
4631 4631
 	URL := "http://something.invalid"
4632
-	Name := "quiet_build_wrong_remote"
4633
-	_, _, qstderr, qerr := buildImageWithStdoutStderr(Name, "", false, "-q", "--force-rm", "--rm", URL)
4634
-	_, _, vstderr, verr := buildImageWithStdoutStderr(Name, "", false, "--force-rm", "--rm", URL)
4635
-	if qerr == nil || verr == nil {
4636
-		c.Fatal(fmt.Errorf("Test [%s] expected to fail but didn't", Name))
4637
-	}
4638
-	if qstderr != vstderr {
4639
-		c.Fatal(fmt.Errorf("Test[%s] expected that quiet stderr and verbose stdout are equal; quiet [%v], verbose [%v]", Name, qstderr, vstderr))
4632
+	name := "quiet_build_wrong_remote"
4633
+	quietResult := buildImageNew(name, withBuildFlags("-q"), withBuildContextPath(URL))
4634
+	quietResult.Assert(c, icmd.Expected{
4635
+		ExitCode: 1,
4636
+	})
4637
+	result := buildImageNew(name, withBuildContextPath(URL))
4638
+	result.Assert(c, icmd.Expected{
4639
+		ExitCode: 1,
4640
+	})
4641
+	if strings.TrimSpace(quietResult.Stderr()) != strings.TrimSpace(result.Combined()) {
4642
+		c.Fatal(fmt.Errorf("Test[%s] expected that quiet stderr and verbose stdout are equal; quiet [%v], verbose [%v]", name, quietResult.Stderr(), result.Combined()))
4640 4643
 	}
4641 4644
 }
4642 4645
 
... ...
@@ -4644,26 +3652,22 @@ func (s *DockerSuite) TestBuildStderr(c *check.C) {
4644 4644
 	// This test just makes sure that no non-error output goes
4645 4645
 	// to stderr
4646 4646
 	name := "testbuildstderr"
4647
-	_, stdout, stderr, err := buildImageWithStdoutStderr(name,
4648
-		"FROM busybox\nRUN echo one", true)
4649
-	if err != nil {
4650
-		c.Fatal(err)
4651
-	}
4647
+	result := buildImageNew(name, withDockerfile("FROM busybox\nRUN echo one"))
4648
+	result.Assert(c, icmd.Success)
4652 4649
 
4653 4650
 	// Windows to non-Windows should have a security warning
4654
-	if runtime.GOOS == "windows" && daemonPlatform != "windows" && !strings.Contains(stdout, "SECURITY WARNING:") {
4655
-		c.Fatalf("Stdout contains unexpected output: %q", stdout)
4651
+	if runtime.GOOS == "windows" && daemonPlatform != "windows" && !strings.Contains(result.Stdout(), "SECURITY WARNING:") {
4652
+		c.Fatalf("Stdout contains unexpected output: %q", result.Stdout())
4656 4653
 	}
4657 4654
 
4658 4655
 	// Stderr should always be empty
4659
-	if stderr != "" {
4660
-		c.Fatalf("Stderr should have been empty, instead it's: %q", stderr)
4656
+	if result.Stderr() != "" {
4657
+		c.Fatalf("Stderr should have been empty, instead it's: %q", result.Stderr())
4661 4658
 	}
4662 4659
 }
4663 4660
 
4664 4661
 func (s *DockerSuite) TestBuildChownSingleFile(c *check.C) {
4665
-	testRequires(c, UnixCli) // test uses chown: not available on windows
4666
-	testRequires(c, DaemonIsLinux)
4662
+	testRequires(c, UnixCli, DaemonIsLinux) // test uses chown: not available on windows
4667 4663
 
4668 4664
 	name := "testbuildchownsinglefile"
4669 4665
 
... ...
@@ -4747,28 +3751,18 @@ func (s *DockerSuite) TestBuildXZHost(c *check.C) {
4747 4747
 	testRequires(c, DaemonIsLinux)
4748 4748
 	name := "testbuildxzhost"
4749 4749
 
4750
-	ctx, err := fakeContext(`
4750
+	buildImageSuccessfully(c, name, withBuildContext(c,
4751
+		withFile("Dockerfile", `
4751 4752
 FROM busybox
4752 4753
 ADD xz /usr/local/sbin/
4753 4754
 RUN chmod 755 /usr/local/sbin/xz
4754 4755
 ADD test.xz /
4755
-RUN [ ! -e /injected ]`,
4756
-		map[string]string{
4757
-			"test.xz": "\xfd\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x02\x00" +
4758
-				"\x21\x01\x16\x00\x00\x00\x74\x2f\xe5\xa3\x01\x00\x3f\xfd" +
4759
-				"\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x02\x00\x21",
4760
-			"xz": "#!/bin/sh\ntouch /injected",
4761
-		})
4762
-
4763
-	if err != nil {
4764
-		c.Fatal(err)
4765
-	}
4766
-	defer ctx.Close()
4767
-
4768
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
4769
-		c.Fatal(err)
4770
-	}
4771
-
4756
+RUN [ ! -e /injected ]`),
4757
+		withFile("test.xz", "\xfd\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x02\x00"+
4758
+			"\x21\x01\x16\x00\x00\x00\x74\x2f\xe5\xa3\x01\x00\x3f\xfd"+
4759
+			"\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x02\x00\x21"),
4760
+		withFile("xz", "#!/bin/sh\ntouch /injected"),
4761
+	))
4772 4762
 }
4773 4763
 
4774 4764
 func (s *DockerSuite) TestBuildVolumesRetainContents(c *check.C) {
... ...
@@ -4785,22 +3779,14 @@ func (s *DockerSuite) TestBuildVolumesRetainContents(c *check.C) {
4785 4785
 		volName = "C:/foo"
4786 4786
 	}
4787 4787
 
4788
-	ctx, err := fakeContext(`
4788
+	buildImageSuccessfully(c, name, withBuildContext(c,
4789
+		withFile("Dockerfile", `
4789 4790
 FROM busybox
4790 4791
 COPY content /foo/file
4791 4792
 VOLUME `+volName+`
4792
-CMD cat /foo/file`,
4793
-		map[string]string{
4794
-			"content": expected,
4795
-		})
4796
-	if err != nil {
4797
-		c.Fatal(err)
4798
-	}
4799
-	defer ctx.Close()
4800
-
4801
-	if _, err := buildImageFromContext(name, ctx, false); err != nil {
4802
-		c.Fatal(err)
4803
-	}
4793
+CMD cat /foo/file`),
4794
+		withFile("content", expected),
4795
+	))
4804 4796
 
4805 4797
 	out, _ := dockerCmd(c, "run", "--rm", name)
4806 4798
 	if out != expected {
... ...
@@ -4809,8 +3795,8 @@ CMD cat /foo/file`,
4809 4809
 
4810 4810
 }
4811 4811
 
4812
+// FIXME(vdemeester) part of this should be unit test, other part should be clearer
4812 4813
 func (s *DockerSuite) TestBuildRenamedDockerfile(c *check.C) {
4813
-
4814 4814
 	ctx, err := fakeContext(`FROM busybox
4815 4815
 	RUN echo from Dockerfile`,
4816 4816
 		map[string]string{
... ...
@@ -4917,50 +3903,23 @@ func (s *DockerSuite) TestBuildFromMixedcaseDockerfile(c *check.C) {
4917 4917
 	testRequires(c, UnixCli) // Dockerfile overwrites dockerfile on windows
4918 4918
 	testRequires(c, DaemonIsLinux)
4919 4919
 
4920
-	ctx, err := fakeContext(`FROM busybox
4921
-	RUN echo from dockerfile`,
4922
-		map[string]string{
4923
-			"dockerfile": "FROM busybox\nRUN echo from dockerfile",
4924
-		})
4925
-	if err != nil {
4926
-		c.Fatal(err)
4927
-	}
4928
-	defer ctx.Close()
4929
-
4930
-	out, _, err := dockerCmdInDir(c, ctx.Dir, "build", "-t", "test1", ".")
4931
-	if err != nil {
4932
-		c.Fatalf("Failed to build: %s\n%s", out, err)
4933
-	}
4934
-
4935
-	if !strings.Contains(out, "from dockerfile") {
4936
-		c.Fatalf("Missing proper output: %s", out)
4937
-	}
4938
-
4939
-}
4940
-
4941
-func (s *DockerSuite) TestBuildWithTwoDockerfiles(c *check.C) {
4942
-	testRequires(c, UnixCli) // Dockerfile overwrites dockerfile on windows
4943
-	testRequires(c, DaemonIsLinux)
4944
-
4945
-	ctx, err := fakeContext(`FROM busybox
4946
-RUN echo from Dockerfile`,
4947
-		map[string]string{
4948
-			"dockerfile": "FROM busybox\nRUN echo from dockerfile",
4949
-		})
4950
-	if err != nil {
4951
-		c.Fatal(err)
4952
-	}
4953
-	defer ctx.Close()
4954
-
4955
-	out, _, err := dockerCmdInDir(c, ctx.Dir, "build", "-t", "test1", ".")
4956
-	if err != nil {
4957
-		c.Fatalf("Failed to build: %s\n%s", out, err)
4958
-	}
4959
-
4960
-	if !strings.Contains(out, "from Dockerfile") {
4961
-		c.Fatalf("Missing proper output: %s", out)
4962
-	}
4920
+	// If Dockerfile is not present, use dockerfile
4921
+	buildImageNew("test1", withBuildContext(c,
4922
+		withFile("dockerfile", `FROM busybox
4923
+	RUN echo from dockerfile`),
4924
+	)).Assert(c, icmd.Expected{
4925
+		Out: "from dockerfile",
4926
+	})
4963 4927
 
4928
+	// Prefer Dockerfile in place of dockerfile
4929
+	buildImageNew("test1", withBuildContext(c,
4930
+		withFile("dockerfile", `FROM busybox
4931
+	RUN echo from dockerfile`),
4932
+		withFile("Dockerfile", `FROM busybox
4933
+	RUN echo from Dockerfile`),
4934
+	)).Assert(c, icmd.Expected{
4935
+		Out: "from Dockerfile",
4936
+	})
4964 4937
 }
4965 4938
 
4966 4939
 func (s *DockerSuite) TestBuildFromURLWithF(c *check.C) {
... ...
@@ -4983,15 +3942,16 @@ RUN echo from Dockerfile`,
4983 4983
 
4984 4984
 	// Make sure that -f is ignored and that we don't use the Dockerfile
4985 4985
 	// that's in the current dir
4986
-	out, _, err := dockerCmdInDir(c, ctx.Dir, "build", "-f", "baz", "-t", "test1", server.URL()+"/baz")
4987
-	if err != nil {
4988
-		c.Fatalf("Failed to build: %s\n%s", out, err)
4989
-	}
4986
+	result := buildImageNew("test1", withBuildFlags("-f", "baz", server.URL()+"/baz"), func(cmd *icmd.Cmd) func() {
4987
+		cmd.Dir = ctx.Dir
4988
+		return nil
4989
+	})
4990
+	result.Assert(c, icmd.Success)
4990 4991
 
4991
-	if !strings.Contains(out, "from baz") ||
4992
-		strings.Contains(out, "/tmp/baz") ||
4993
-		!strings.Contains(out, "/tmp/Dockerfile") {
4994
-		c.Fatalf("Missing proper output: %s", out)
4992
+	if !strings.Contains(result.Combined(), "from baz") ||
4993
+		strings.Contains(result.Combined(), "/tmp/baz") ||
4994
+		!strings.Contains(result.Combined(), "/tmp/Dockerfile") {
4995
+		c.Fatalf("Missing proper output: %s", result.Combined())
4995 4996
 	}
4996 4997
 
4997 4998
 }
... ...
@@ -5008,21 +3968,20 @@ RUN echo "from Dockerfile"`,
5008 5008
 
5009 5009
 	// Make sure that -f is ignored and that we don't use the Dockerfile
5010 5010
 	// that's in the current dir
5011
-	dockerCommand := exec.Command(dockerBinary, "build", "-f", "baz", "-t", "test1", "-")
5012
-	dockerCommand.Dir = ctx.Dir
5013
-	dockerCommand.Stdin = strings.NewReader(`FROM busybox
5011
+	result := buildImageNew("test1", withBuildFlags("-f", "baz", "-"), func(cmd *icmd.Cmd) func() {
5012
+		cmd.Dir = ctx.Dir
5013
+		cmd.Stdin = strings.NewReader(`FROM busybox
5014 5014
 RUN echo "from baz"
5015 5015
 COPY * /tmp/
5016 5016
 RUN sh -c "find /tmp/" # sh -c is needed on Windows to use the correct find`)
5017
-	out, status, err := runCommandWithOutput(dockerCommand)
5018
-	if err != nil || status != 0 {
5019
-		c.Fatalf("Error building: %s", err)
5020
-	}
5017
+		return nil
5018
+	})
5019
+	result.Assert(c, icmd.Success)
5021 5020
 
5022
-	if !strings.Contains(out, "from baz") ||
5023
-		strings.Contains(out, "/tmp/baz") ||
5024
-		!strings.Contains(out, "/tmp/Dockerfile") {
5025
-		c.Fatalf("Missing proper output: %s", out)
5021
+	if !strings.Contains(result.Combined(), "from baz") ||
5022
+		strings.Contains(result.Combined(), "/tmp/baz") ||
5023
+		!strings.Contains(result.Combined(), "/tmp/Dockerfile") {
5024
+		c.Fatalf("Missing proper output: %s", result.Combined())
5026 5025
 	}
5027 5026
 
5028 5027
 }
... ...
@@ -5039,17 +3998,13 @@ func (s *DockerSuite) TestBuildFromOfficialNames(c *check.C) {
5039 5039
 	}
5040 5040
 	for idx, fromName := range fromNames {
5041 5041
 		imgName := fmt.Sprintf("%s%d", name, idx)
5042
-		_, err := buildImage(imgName, "FROM "+fromName, true)
5043
-		if err != nil {
5044
-			c.Errorf("Build failed using FROM %s: %s", fromName, err)
5045
-		}
5046
-		deleteImages(imgName)
5042
+		buildImageSuccessfully(c, imgName, withDockerfile("FROM "+fromName))
5043
+		dockerCmd(c, "rmi", imgName)
5047 5044
 	}
5048 5045
 }
5049 5046
 
5050 5047
 func (s *DockerSuite) TestBuildDockerfileOutsideContext(c *check.C) {
5051
-	testRequires(c, UnixCli) // uses os.Symlink: not implemented in windows at the time of writing (go-1.4.2)
5052
-	testRequires(c, DaemonIsLinux)
5048
+	testRequires(c, UnixCli, DaemonIsLinux) // uses os.Symlink: not implemented in windows at the time of writing (go-1.4.2)
5053 5049
 
5054 5050
 	name := "testbuilddockerfileoutsidecontext"
5055 5051
 	tmpdir, err := ioutil.TempDir("", name)
... ...
@@ -5103,6 +4058,7 @@ func (s *DockerSuite) TestBuildDockerfileOutsideContext(c *check.C) {
5103 5103
 	}
5104 5104
 }
5105 5105
 
5106
+// FIXME(vdemeester) should be a unit test
5106 5107
 func (s *DockerSuite) TestBuildSpaces(c *check.C) {
5107 5108
 	// Test to make sure that leading/trailing spaces on a command
5108 5109
 	// doesn't change the error msg we get
... ...
@@ -5181,39 +4137,31 @@ func (s *DockerSuite) TestBuildSpacesWithQuotes(c *check.C) {
5181 5181
 RUN echo "  \
5182 5182
   foo  "`
5183 5183
 
5184
-	_, out, err := buildImageWithOut(name, dockerfile, false)
5185
-	if err != nil {
5186
-		c.Fatal("Build failed:", err)
5187
-	}
5188
-
5189
-	expecting := "\n    foo  \n"
5184
+	expected := "\n    foo  \n"
5190 5185
 	// Windows uses the builtin echo, which preserves quotes
5191 5186
 	if daemonPlatform == "windows" {
5192
-		expecting = "\"    foo  \""
5193
-	}
5194
-	if !strings.Contains(out, expecting) {
5195
-		c.Fatalf("Bad output: %q expecting to contain %q", out, expecting)
5187
+		expected = "\"    foo  \""
5196 5188
 	}
5197 5189
 
5190
+	buildImageNew(name, withDockerfile(dockerfile)).Assert(c, icmd.Expected{
5191
+		Out: expected,
5192
+	})
5198 5193
 }
5199 5194
 
5200 5195
 // #4393
5201 5196
 func (s *DockerSuite) TestBuildVolumeFileExistsinContainer(c *check.C) {
5202 5197
 	testRequires(c, DaemonIsLinux) // TODO Windows: This should error out
5203
-	buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-errcreatevolumewithfile", "-")
5204
-	buildCmd.Stdin = strings.NewReader(`
5198
+	buildImageNew("docker-test-errcreatevolumewithfile", withDockerfile(`
5205 5199
 	FROM busybox
5206 5200
 	RUN touch /foo
5207 5201
 	VOLUME /foo
5208
-	`)
5209
-
5210
-	out, _, err := runCommandWithOutput(buildCmd)
5211
-	if err == nil || !strings.Contains(out, "file exists") {
5212
-		c.Fatalf("expected build to fail when file exists in container at requested volume path")
5213
-	}
5214
-
5202
+	`)).Assert(c, icmd.Expected{
5203
+		ExitCode: 1,
5204
+		Err:      "file exists",
5205
+	})
5215 5206
 }
5216 5207
 
5208
+// FIXME(vdemeester) should be a unit test
5217 5209
 func (s *DockerSuite) TestBuildMissingArgs(c *check.C) {
5218 5210
 	// Test to make sure that all Dockerfile commands (except the ones listed
5219 5211
 	// in skipCmds) will generate an error if no args are provided.
... ...
@@ -5243,7 +4191,6 @@ func (s *DockerSuite) TestBuildMissingArgs(c *check.C) {
5243 5243
 		if _, ok := skipCmds[cmd]; ok {
5244 5244
 			continue
5245 5245
 		}
5246
-
5247 5246
 		var dockerfile string
5248 5247
 		if cmd == "FROM" {
5249 5248
 			dockerfile = cmd
... ...
@@ -5252,87 +4199,53 @@ func (s *DockerSuite) TestBuildMissingArgs(c *check.C) {
5252 5252
 			dockerfile = "FROM busybox\n" + cmd
5253 5253
 		}
5254 5254
 
5255
-		ctx, err := fakeContext(dockerfile, map[string]string{})
5256
-		if err != nil {
5257
-			c.Fatal(err)
5258
-		}
5259
-		defer ctx.Close()
5260
-		var out string
5261
-		if out, err = buildImageFromContext("args", ctx, true); err == nil {
5262
-			c.Fatalf("%s was supposed to fail. Out:%s", cmd, out)
5263
-		}
5264
-		if !strings.Contains(err.Error(), cmd+" requires") {
5265
-			c.Fatalf("%s returned the wrong type of error:%s", cmd, err)
5266
-		}
5255
+		buildImageNew("args", withDockerfile(dockerfile)).Assert(c, icmd.Expected{
5256
+			ExitCode: 1,
5257
+			Err:      cmd + " requires",
5258
+		})
5267 5259
 	}
5268 5260
 
5269 5261
 }
5270 5262
 
5271 5263
 func (s *DockerSuite) TestBuildEmptyScratch(c *check.C) {
5272 5264
 	testRequires(c, DaemonIsLinux)
5273
-	_, out, err := buildImageWithOut("sc", "FROM scratch", true)
5274
-	if err == nil {
5275
-		c.Fatalf("Build was supposed to fail")
5276
-	}
5277
-	if !strings.Contains(out, "No image was generated") {
5278
-		c.Fatalf("Wrong error message: %v", out)
5279
-	}
5265
+	buildImageNew("sc", withDockerfile("FROM scratch")).Assert(c, icmd.Expected{
5266
+		ExitCode: 1,
5267
+		Err:      "No image was generated",
5268
+	})
5280 5269
 }
5281 5270
 
5282 5271
 func (s *DockerSuite) TestBuildDotDotFile(c *check.C) {
5283
-	ctx, err := fakeContext("FROM busybox\n",
5284
-		map[string]string{
5285
-			"..gitme": "",
5286
-		})
5287
-	if err != nil {
5288
-		c.Fatal(err)
5289
-	}
5290
-	defer ctx.Close()
5291
-
5292
-	if _, err = buildImageFromContext("sc", ctx, false); err != nil {
5293
-		c.Fatalf("Build was supposed to work: %s", err)
5294
-	}
5272
+	buildImageSuccessfully(c, "sc", withBuildContext(c,
5273
+		withFile("Dockerfile", "FROM busybox\n"),
5274
+		withFile("..gitme", ""),
5275
+	))
5295 5276
 }
5296 5277
 
5297 5278
 func (s *DockerSuite) TestBuildRUNoneJSON(c *check.C) {
5298 5279
 	testRequires(c, DaemonIsLinux) // No hello-world Windows image
5299 5280
 	name := "testbuildrunonejson"
5300 5281
 
5301
-	ctx, err := fakeContext(`FROM hello-world:frozen
5302
-RUN [ "/hello" ]`, map[string]string{})
5303
-	if err != nil {
5304
-		c.Fatal(err)
5305
-	}
5306
-	defer ctx.Close()
5307
-
5308
-	out, _, err := dockerCmdInDir(c, ctx.Dir, "build", "--no-cache", "-t", name, ".")
5309
-	if err != nil {
5310
-		c.Fatalf("failed to build the image: %s, %v", out, err)
5311
-	}
5312
-
5313
-	if !strings.Contains(out, "Hello from Docker") {
5314
-		c.Fatalf("bad output: %s", out)
5315
-	}
5316
-
5282
+	buildImageNew(name, withDockerfile(`FROM hello-world:frozen
5283
+RUN [ "/hello" ]`)).Assert(c, icmd.Expected{
5284
+		Out: "Hello from Docker",
5285
+	})
5317 5286
 }
5318 5287
 
5319 5288
 func (s *DockerSuite) TestBuildEmptyStringVolume(c *check.C) {
5320 5289
 	name := "testbuildemptystringvolume"
5321 5290
 
5322
-	_, err := buildImage(name, `
5291
+	buildImageNew(name, withDockerfile(`
5323 5292
   FROM busybox
5324 5293
   ENV foo=""
5325 5294
   VOLUME $foo
5326
-  `, false)
5327
-	if err == nil {
5328
-		c.Fatal("Should have failed to build")
5329
-	}
5330
-
5295
+  `)).Assert(c, icmd.Expected{
5296
+		ExitCode: 1,
5297
+	})
5331 5298
 }
5332 5299
 
5333 5300
 func (s *DockerSuite) TestBuildContainerWithCgroupParent(c *check.C) {
5334
-	testRequires(c, SameHostDaemon)
5335
-	testRequires(c, DaemonIsLinux)
5301
+	testRequires(c, SameHostDaemon, DaemonIsLinux)
5336 5302
 
5337 5303
 	cgroupParent := "test"
5338 5304
 	data, err := ioutil.ReadFile("/proc/self/cgroup")
... ...
@@ -5344,56 +4257,46 @@ func (s *DockerSuite) TestBuildContainerWithCgroupParent(c *check.C) {
5344 5344
 	if !found {
5345 5345
 		c.Fatalf("unable to find self memory cgroup path. CgroupsPath: %v", selfCgroupPaths)
5346 5346
 	}
5347
-	cmd := exec.Command(dockerBinary, "build", "--cgroup-parent", cgroupParent, "-")
5348
-	cmd.Stdin = strings.NewReader(`
5347
+	result := buildImageNew("buildcgroupparent",
5348
+		withBuildFlags("--cgroup-parent", cgroupParent),
5349
+		withDockerfile(`
5349 5350
 FROM busybox
5350 5351
 RUN cat /proc/self/cgroup
5351
-`)
5352
-
5353
-	out, _, err := runCommandWithOutput(cmd)
5354
-	if err != nil {
5355
-		c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
5356
-	}
5357
-	m, err := regexp.MatchString(fmt.Sprintf("memory:.*/%s/.*", cgroupParent), out)
5352
+`))
5353
+	result.Assert(c, icmd.Success)
5354
+	m, err := regexp.MatchString(fmt.Sprintf("memory:.*/%s/.*", cgroupParent), result.Combined())
5358 5355
 	c.Assert(err, check.IsNil)
5359 5356
 	if !m {
5360
-		c.Fatalf("There is no expected memory cgroup with parent /%s/: %s", cgroupParent, out)
5357
+		c.Fatalf("There is no expected memory cgroup with parent /%s/: %s", cgroupParent, result.Combined())
5361 5358
 	}
5362 5359
 }
5363 5360
 
5361
+// FIXME(vdemeester) could be a unit test
5364 5362
 func (s *DockerSuite) TestBuildNoDupOutput(c *check.C) {
5365 5363
 	// Check to make sure our build output prints the Dockerfile cmd
5366 5364
 	// property - there was a bug that caused it to be duplicated on the
5367 5365
 	// Step X  line
5368 5366
 	name := "testbuildnodupoutput"
5369
-
5370
-	_, out, err := buildImageWithOut(name, `
5367
+	result := buildImageNew(name, withDockerfile(`
5371 5368
   FROM busybox
5372
-  RUN env`, false)
5373
-	if err != nil {
5374
-		c.Fatalf("Build should have worked: %q", err)
5375
-	}
5376
-
5369
+  RUN env`))
5370
+	result.Assert(c, icmd.Success)
5377 5371
 	exp := "\nStep 2/2 : RUN env\n"
5378
-	if !strings.Contains(out, exp) {
5379
-		c.Fatalf("Bad output\nGot:%s\n\nExpected to contain:%s\n", out, exp)
5372
+	if !strings.Contains(result.Combined(), exp) {
5373
+		c.Fatalf("Bad output\nGot:%s\n\nExpected to contain:%s\n", result.Combined(), exp)
5380 5374
 	}
5381 5375
 }
5382 5376
 
5383 5377
 // GH15826
5378
+// FIXME(vdemeester) could be a unit test
5384 5379
 func (s *DockerSuite) TestBuildStartsFromOne(c *check.C) {
5385 5380
 	// Explicit check to ensure that build starts from step 1 rather than 0
5386 5381
 	name := "testbuildstartsfromone"
5387
-
5388
-	_, out, err := buildImageWithOut(name, `
5389
-  FROM busybox`, false)
5390
-	if err != nil {
5391
-		c.Fatalf("Build should have worked: %q", err)
5392
-	}
5393
-
5382
+	result := buildImageNew(name, withDockerfile(`FROM busybox`))
5383
+	result.Assert(c, icmd.Success)
5394 5384
 	exp := "\nStep 1/1 : FROM busybox\n"
5395
-	if !strings.Contains(out, exp) {
5396
-		c.Fatalf("Bad output\nGot:%s\n\nExpected to contain:%s\n", out, exp)
5385
+	if !strings.Contains(result.Combined(), exp) {
5386
+		c.Fatalf("Bad output\nGot:%s\n\nExpected to contain:%s\n", result.Combined(), exp)
5397 5387
 	}
5398 5388
 }
5399 5389
 
... ...
@@ -5401,23 +4304,21 @@ func (s *DockerSuite) TestBuildRUNErrMsg(c *check.C) {
5401 5401
 	// Test to make sure the bad command is quoted with just "s and
5402 5402
 	// not as a Go []string
5403 5403
 	name := "testbuildbadrunerrmsg"
5404
-	_, out, err := buildImageWithOut(name, `
5405
-  FROM busybox
5406
-  RUN badEXE a1 \& a2	a3`, false) // tab between a2 and a3
5407
-	if err == nil {
5408
-		c.Fatal("Should have failed to build")
5409
-	}
5410 5404
 	shell := "/bin/sh -c"
5411
-	exitCode := "127"
5405
+	exitCode := 127
5412 5406
 	if daemonPlatform == "windows" {
5413 5407
 		shell = "cmd /S /C"
5414 5408
 		// architectural - Windows has to start the container to determine the exe is bad, Linux does not
5415
-		exitCode = "1"
5416
-	}
5417
-	exp := `The command '` + shell + ` badEXE a1 \& a2	a3' returned a non-zero code: ` + exitCode
5418
-	if !strings.Contains(out, exp) {
5419
-		c.Fatalf("RUN doesn't have the correct output:\nGot:%s\nExpected:%s", out, exp)
5409
+		exitCode = 1
5420 5410
 	}
5411
+	exp := fmt.Sprintf(`The command '%s badEXE a1 \& a2	a3' returned a non-zero code: %d`, shell, exitCode)
5412
+
5413
+	buildImageNew(name, withDockerfile(`
5414
+  FROM busybox
5415
+  RUN badEXE a1 \& a2	a3`)).Assert(c, icmd.Expected{
5416
+		ExitCode: exitCode,
5417
+		Err:      exp,
5418
+	})
5421 5419
 }
5422 5420
 
5423 5421
 func (s *DockerTrustSuite) TestTrustedBuild(c *check.C) {
... ...
@@ -5429,26 +4330,15 @@ func (s *DockerTrustSuite) TestTrustedBuild(c *check.C) {
5429 5429
 
5430 5430
 	name := "testtrustedbuild"
5431 5431
 
5432
-	buildCmd := buildImageCmd(name, dockerFile, true)
5433
-	trustedExecCmd(buildCmd)
5434
-	out, _, err := runCommandWithOutput(buildCmd)
5435
-	if err != nil {
5436
-		c.Fatalf("Error running trusted build: %s\n%s", err, out)
5437
-	}
5438
-
5439
-	if !strings.Contains(out, fmt.Sprintf("FROM %s@sha", repoName[:len(repoName)-7])) {
5440
-		c.Fatalf("Unexpected output on trusted build:\n%s", out)
5441
-	}
5432
+	buildImageNew(name, trustedBuild, withDockerfile(dockerFile)).Assert(c, icmd.Expected{
5433
+		Out: fmt.Sprintf("FROM %s@sha", repoName[:len(repoName)-7]),
5434
+	})
5442 5435
 
5443 5436
 	// We should also have a tag reference for the image.
5444
-	if out, exitCode := dockerCmd(c, "inspect", repoName); exitCode != 0 {
5445
-		c.Fatalf("unexpected exit code inspecting image %q: %d: %s", repoName, exitCode, out)
5446
-	}
5437
+	dockerCmd(c, "inspect", repoName)
5447 5438
 
5448 5439
 	// We should now be able to remove the tag reference.
5449
-	if out, exitCode := dockerCmd(c, "rmi", repoName); exitCode != 0 {
5450
-		c.Fatalf("unexpected exit code inspecting image %q: %d: %s", repoName, exitCode, out)
5451
-	}
5440
+	dockerCmd(c, "rmi", repoName)
5452 5441
 }
5453 5442
 
5454 5443
 func (s *DockerTrustSuite) TestTrustedBuildUntrustedTag(c *check.C) {
... ...
@@ -5460,16 +4350,10 @@ func (s *DockerTrustSuite) TestTrustedBuildUntrustedTag(c *check.C) {
5460 5460
 
5461 5461
 	name := "testtrustedbuilduntrustedtag"
5462 5462
 
5463
-	buildCmd := buildImageCmd(name, dockerFile, true)
5464
-	trustedExecCmd(buildCmd)
5465
-	out, _, err := runCommandWithOutput(buildCmd)
5466
-	if err == nil {
5467
-		c.Fatalf("Expected error on trusted build with untrusted tag: %s\n%s", err, out)
5468
-	}
5469
-
5470
-	if !strings.Contains(out, "does not have trust data for") {
5471
-		c.Fatalf("Unexpected output on trusted build with untrusted tag:\n%s", out)
5472
-	}
5463
+	buildImageNew(name, trustedBuild, withDockerfile(dockerFile)).Assert(c, icmd.Expected{
5464
+		ExitCode: 1,
5465
+		Err:      "does not have trust data for",
5466
+	})
5473 5467
 }
5474 5468
 
5475 5469
 func (s *DockerTrustSuite) TestBuildContextDirIsSymlink(c *check.C) {
... ...
@@ -5504,9 +4388,7 @@ func (s *DockerTrustSuite) TestBuildContextDirIsSymlink(c *check.C) {
5504 5504
 
5505 5505
 	// Executing the build with the symlink as the specified context should
5506 5506
 	// *not* fail.
5507
-	if out, exitStatus := dockerCmd(c, "build", contextSymlinkName); exitStatus != 0 {
5508
-		c.Fatalf("build failed with exit status %d: %s", exitStatus, out)
5509
-	}
5507
+	dockerCmd(c, "build", contextSymlinkName)
5510 5508
 }
5511 5509
 
5512 5510
 func (s *DockerTrustSuite) TestTrustedBuildTagFromReleasesRole(c *check.C) {
... ...
@@ -5535,14 +4417,10 @@ func (s *DockerTrustSuite) TestTrustedBuildTagFromReleasesRole(c *check.C) {
5535 5535
   FROM %s
5536 5536
   RUN []
5537 5537
     `, otherTag)
5538
-
5539 5538
 	name := "testtrustedbuildreleasesrole"
5540
-
5541
-	buildCmd := buildImageCmd(name, dockerFile, true)
5542
-	trustedExecCmd(buildCmd)
5543
-	out, _, err := runCommandWithOutput(buildCmd)
5544
-	c.Assert(err, check.IsNil, check.Commentf("Trusted build failed: %s", out))
5545
-	c.Assert(out, checker.Contains, fmt.Sprintf("FROM %s@sha", repoName))
5539
+	buildImageNew(name, trustedBuild, withDockerfile(dockerFile)).Assert(c, icmd.Expected{
5540
+		Out: fmt.Sprintf("FROM %s@sha", repoName),
5541
+	})
5546 5542
 }
5547 5543
 
5548 5544
 func (s *DockerTrustSuite) TestTrustedBuildTagIgnoresOtherDelegationRoles(c *check.C) {
... ...
@@ -5573,50 +4451,37 @@ func (s *DockerTrustSuite) TestTrustedBuildTagIgnoresOtherDelegationRoles(c *che
5573 5573
     `, otherTag)
5574 5574
 
5575 5575
 	name := "testtrustedbuildotherrole"
5576
-
5577
-	buildCmd := buildImageCmd(name, dockerFile, true)
5578
-	trustedExecCmd(buildCmd)
5579
-	out, _, err := runCommandWithOutput(buildCmd)
5580
-	c.Assert(err, check.NotNil, check.Commentf("Trusted build expected to fail: %s", out))
5576
+	buildImageNew(name, trustedBuild, withDockerfile(dockerFile)).Assert(c, icmd.Expected{
5577
+		ExitCode: 1,
5578
+	})
5581 5579
 }
5582 5580
 
5583 5581
 // Issue #15634: COPY fails when path starts with "null"
5584 5582
 func (s *DockerSuite) TestBuildNullStringInAddCopyVolume(c *check.C) {
5585 5583
 	name := "testbuildnullstringinaddcopyvolume"
5586
-
5587 5584
 	volName := "nullvolume"
5588
-
5589 5585
 	if daemonPlatform == "windows" {
5590 5586
 		volName = `C:\\nullvolume`
5591 5587
 	}
5592 5588
 
5593
-	ctx, err := fakeContext(`
5589
+	buildImageSuccessfully(c, name, withBuildContext(c,
5590
+		withFile("Dockerfile", `
5594 5591
 		FROM busybox
5595 5592
 
5596 5593
 		ADD null /
5597 5594
 		COPY nullfile /
5598 5595
 		VOLUME `+volName+`
5599
-		`,
5600
-		map[string]string{
5601
-			"null":     "test1",
5602
-			"nullfile": "test2",
5603
-		},
5604
-	)
5605
-	c.Assert(err, check.IsNil)
5606
-	defer ctx.Close()
5607
-
5608
-	_, err = buildImageFromContext(name, ctx, true)
5609
-	c.Assert(err, check.IsNil)
5596
+		`),
5597
+		withFile("null", "test1"),
5598
+		withFile("nullfile", "test2"),
5599
+	))
5610 5600
 }
5611 5601
 
5612 5602
 func (s *DockerSuite) TestBuildStopSignal(c *check.C) {
5613 5603
 	testRequires(c, DaemonIsLinux) // Windows does not support STOPSIGNAL yet
5614 5604
 	imgName := "test_build_stop_signal"
5615
-	_, err := buildImage(imgName,
5616
-		`FROM busybox
5617
-		 STOPSIGNAL SIGKILL`,
5618
-		true)
5619
-	c.Assert(err, check.IsNil)
5605
+	buildImageSuccessfully(c, imgName, withDockerfile(`FROM busybox
5606
+		 STOPSIGNAL SIGKILL`))
5620 5607
 	res := inspectFieldJSON(c, imgName, "Config.StopSignal")
5621 5608
 	if res != `"SIGKILL"` {
5622 5609
 		c.Fatalf("Signal %s, expected SIGKILL", res)
... ...
@@ -5624,7 +4489,6 @@ func (s *DockerSuite) TestBuildStopSignal(c *check.C) {
5624 5624
 
5625 5625
 	containerName := "test-container-stop-signal"
5626 5626
 	dockerCmd(c, "run", "-d", "--name", containerName, imgName, "top")
5627
-
5628 5627
 	res = inspectFieldJSON(c, containerName, "Config.StopSignal")
5629 5628
 	if res != `"SIGKILL"` {
5630 5629
 		c.Fatalf("Signal %s, expected SIGKILL", res)
... ...
@@ -5635,7 +4499,6 @@ func (s *DockerSuite) TestBuildBuildTimeArg(c *check.C) {
5635 5635
 	imgName := "bldargtest"
5636 5636
 	envKey := "foo"
5637 5637
 	envVal := "bar"
5638
-	args := []string{"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)}
5639 5638
 	var dockerfile string
5640 5639
 	if daemonPlatform == "windows" {
5641 5640
 		// Bugs in Windows busybox port - use the default base image and native cmd stuff
... ...
@@ -5650,13 +4513,12 @@ func (s *DockerSuite) TestBuildBuildTimeArg(c *check.C) {
5650 5650
 			CMD echo $%s`, envKey, envKey, envKey)
5651 5651
 
5652 5652
 	}
5653
-
5654
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); err != nil || !strings.Contains(out, envVal) {
5655
-		if err != nil {
5656
-			c.Fatalf("build failed to complete: %q %q", out, err)
5657
-		}
5658
-		c.Fatalf("failed to access environment variable in output: %q expected: %q", out, envVal)
5659
-	}
5653
+	buildImageNew(imgName,
5654
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
5655
+		withDockerfile(dockerfile),
5656
+	).Assert(c, icmd.Expected{
5657
+		Out: envVal,
5658
+	})
5660 5659
 
5661 5660
 	containerName := "bldargCont"
5662 5661
 	out, _ := dockerCmd(c, "run", "--name", containerName, imgName)
... ...
@@ -5671,18 +4533,14 @@ func (s *DockerSuite) TestBuildBuildTimeArgHistory(c *check.C) {
5671 5671
 	envKey := "foo"
5672 5672
 	envVal := "bar"
5673 5673
 	envDef := "bar1"
5674
-	args := []string{
5675
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
5676
-	}
5677 5674
 	dockerfile := fmt.Sprintf(`FROM busybox
5678 5675
 		ARG %s=%s`, envKey, envDef)
5679
-
5680
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); err != nil || !strings.Contains(out, envVal) {
5681
-		if err != nil {
5682
-			c.Fatalf("build failed to complete: %q %q", out, err)
5683
-		}
5684
-		c.Fatalf("failed to access environment variable in output: %q expected: %q", out, envVal)
5685
-	}
5676
+	buildImageNew(imgName,
5677
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
5678
+		withDockerfile(dockerfile),
5679
+	).Assert(c, icmd.Expected{
5680
+		Out: envVal,
5681
+	})
5686 5682
 
5687 5683
 	out, _ := dockerCmd(c, "history", "--no-trunc", imgName)
5688 5684
 	outputTabs := strings.Split(out, "\n")[1]
... ...
@@ -5695,24 +4553,24 @@ func (s *DockerSuite) TestBuildBuildTimeArgCacheHit(c *check.C) {
5695 5695
 	imgName := "bldargtest"
5696 5696
 	envKey := "foo"
5697 5697
 	envVal := "bar"
5698
-	args := []string{
5699
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
5700
-	}
5701 5698
 	dockerfile := fmt.Sprintf(`FROM busybox
5702 5699
 		ARG %s
5703 5700
 		RUN echo $%s`, envKey, envKey)
5704
-
5705
-	origImgID := ""
5706
-	var err error
5707
-	if origImgID, err = buildImage(imgName, dockerfile, true, args...); err != nil {
5708
-		c.Fatal(err)
5709
-	}
5701
+	buildImageSuccessfully(c, imgName,
5702
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
5703
+		withDockerfile(dockerfile),
5704
+	)
5705
+	origImgID, err := getIDByName(imgName)
5706
+	c.Assert(err, checker.IsNil)
5710 5707
 
5711 5708
 	imgNameCache := "bldargtestcachehit"
5712
-	if newImgID, err := buildImage(imgNameCache, dockerfile, true, args...); err != nil || newImgID != origImgID {
5713
-		if err != nil {
5714
-			c.Fatal(err)
5715
-		}
5709
+	buildImageSuccessfully(c, imgNameCache,
5710
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
5711
+		withDockerfile(dockerfile),
5712
+	)
5713
+	newImgID, err := getIDByName(imgName)
5714
+	c.Assert(err, checker.IsNil)
5715
+	if newImgID != origImgID {
5716 5716
 		c.Fatalf("build didn't use cache! expected image id: %q built image id: %q", origImgID, newImgID)
5717 5717
 	}
5718 5718
 }
... ...
@@ -5723,27 +4581,29 @@ func (s *DockerSuite) TestBuildBuildTimeArgCacheMissExtraArg(c *check.C) {
5723 5723
 	envVal := "bar"
5724 5724
 	extraEnvKey := "foo1"
5725 5725
 	extraEnvVal := "bar1"
5726
-	args := []string{
5727
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
5728
-	}
5729
-
5730 5726
 	dockerfile := fmt.Sprintf(`FROM busybox
5731 5727
 		ARG %s
5732 5728
 		ARG %s
5733 5729
 		RUN echo $%s`, envKey, extraEnvKey, envKey)
5734
-
5735
-	origImgID := ""
5736
-	var err error
5737
-	if origImgID, err = buildImage(imgName, dockerfile, true, args...); err != nil {
5738
-		c.Fatal(err)
5739
-	}
5730
+	buildImageSuccessfully(c, imgName,
5731
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
5732
+		withDockerfile(dockerfile),
5733
+	)
5734
+	origImgID, err := getIDByName(imgName)
5735
+	c.Assert(err, checker.IsNil)
5740 5736
 
5741 5737
 	imgNameCache := "bldargtestcachemiss"
5742
-	args = append(args, "--build-arg", fmt.Sprintf("%s=%s", extraEnvKey, extraEnvVal))
5743
-	if newImgID, err := buildImage(imgNameCache, dockerfile, true, args...); err != nil || newImgID == origImgID {
5744
-		if err != nil {
5745
-			c.Fatal(err)
5746
-		}
5738
+	buildImageSuccessfully(c, imgNameCache,
5739
+		withBuildFlags(
5740
+			"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
5741
+			"--build-arg", fmt.Sprintf("%s=%s", extraEnvKey, extraEnvVal),
5742
+		),
5743
+		withDockerfile(dockerfile),
5744
+	)
5745
+	newImgID, err := getIDByName(imgNameCache)
5746
+	c.Assert(err, checker.IsNil)
5747
+
5748
+	if newImgID == origImgID {
5747 5749
 		c.Fatalf("build used cache, expected a miss!")
5748 5750
 	}
5749 5751
 }
... ...
@@ -5753,28 +4613,24 @@ func (s *DockerSuite) TestBuildBuildTimeArgCacheMissSameArgDiffVal(c *check.C) {
5753 5753
 	envKey := "foo"
5754 5754
 	envVal := "bar"
5755 5755
 	newEnvVal := "bar1"
5756
-	args := []string{
5757
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
5758
-	}
5759
-
5760 5756
 	dockerfile := fmt.Sprintf(`FROM busybox
5761 5757
 		ARG %s
5762 5758
 		RUN echo $%s`, envKey, envKey)
5763
-
5764
-	origImgID := ""
5765
-	var err error
5766
-	if origImgID, err = buildImage(imgName, dockerfile, true, args...); err != nil {
5767
-		c.Fatal(err)
5768
-	}
5759
+	buildImageSuccessfully(c, imgName,
5760
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
5761
+		withDockerfile(dockerfile),
5762
+	)
5763
+	origImgID, err := getIDByName(imgName)
5764
+	c.Assert(err, checker.IsNil)
5769 5765
 
5770 5766
 	imgNameCache := "bldargtestcachemiss"
5771
-	args = []string{
5772
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, newEnvVal),
5773
-	}
5774
-	if newImgID, err := buildImage(imgNameCache, dockerfile, true, args...); err != nil || newImgID == origImgID {
5775
-		if err != nil {
5776
-			c.Fatal(err)
5777
-		}
5767
+	buildImageSuccessfully(c, imgNameCache,
5768
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, newEnvVal)),
5769
+		withDockerfile(dockerfile),
5770
+	)
5771
+	newImgID, err := getIDByName(imgNameCache)
5772
+	c.Assert(err, checker.IsNil)
5773
+	if newImgID == origImgID {
5778 5774
 		c.Fatalf("build used cache, expected a miss!")
5779 5775
 	}
5780 5776
 }
... ...
@@ -5785,9 +4641,6 @@ func (s *DockerSuite) TestBuildBuildTimeArgOverrideArgDefinedBeforeEnv(c *check.
5785 5785
 	envKey := "foo"
5786 5786
 	envVal := "bar"
5787 5787
 	envValOveride := "barOverride"
5788
-	args := []string{
5789
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
5790
-	}
5791 5788
 	dockerfile := fmt.Sprintf(`FROM busybox
5792 5789
 		ARG %s
5793 5790
 		ENV %s %s
... ...
@@ -5795,11 +4648,13 @@ func (s *DockerSuite) TestBuildBuildTimeArgOverrideArgDefinedBeforeEnv(c *check.
5795 5795
 		CMD echo $%s
5796 5796
         `, envKey, envKey, envValOveride, envKey, envKey)
5797 5797
 
5798
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); err != nil || strings.Count(out, envValOveride) != 2 {
5799
-		if err != nil {
5800
-			c.Fatalf("build failed to complete: %q %q", out, err)
5801
-		}
5802
-		c.Fatalf("failed to access environment variable in output: %q expected: %q", out, envValOveride)
5798
+	result := buildImageNew(imgName,
5799
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
5800
+		withDockerfile(dockerfile),
5801
+	)
5802
+	result.Assert(c, icmd.Success)
5803
+	if strings.Count(result.Combined(), envValOveride) != 2 {
5804
+		c.Fatalf("failed to access environment variable in output: %q expected: %q", result.Combined(), envValOveride)
5803 5805
 	}
5804 5806
 
5805 5807
 	containerName := "bldargCont"
... ...
@@ -5808,27 +4663,26 @@ func (s *DockerSuite) TestBuildBuildTimeArgOverrideArgDefinedBeforeEnv(c *check.
5808 5808
 	}
5809 5809
 }
5810 5810
 
5811
+// FIXME(vdemeester) might be useful to merge with the one above ?
5811 5812
 func (s *DockerSuite) TestBuildBuildTimeArgOverrideEnvDefinedBeforeArg(c *check.C) {
5812 5813
 	testRequires(c, DaemonIsLinux) // Windows does not support ARG
5813 5814
 	imgName := "bldargtest"
5814 5815
 	envKey := "foo"
5815 5816
 	envVal := "bar"
5816 5817
 	envValOveride := "barOverride"
5817
-	args := []string{
5818
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
5819
-	}
5820 5818
 	dockerfile := fmt.Sprintf(`FROM busybox
5821 5819
 		ENV %s %s
5822 5820
 		ARG %s
5823 5821
 		RUN echo $%s
5824 5822
 		CMD echo $%s
5825 5823
         `, envKey, envValOveride, envKey, envKey, envKey)
5826
-
5827
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); err != nil || strings.Count(out, envValOveride) != 2 {
5828
-		if err != nil {
5829
-			c.Fatalf("build failed to complete: %q %q", out, err)
5830
-		}
5831
-		c.Fatalf("failed to access environment variable in output: %q expected: %q", out, envValOveride)
5824
+	result := buildImageNew(imgName,
5825
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
5826
+		withDockerfile(dockerfile),
5827
+	)
5828
+	result.Assert(c, icmd.Success)
5829
+	if strings.Count(result.Combined(), envValOveride) != 2 {
5830
+		c.Fatalf("failed to access environment variable in output: %q expected: %q", result.Combined(), envValOveride)
5832 5831
 	}
5833 5832
 
5834 5833
 	containerName := "bldargCont"
... ...
@@ -5855,16 +4709,19 @@ func (s *DockerSuite) TestBuildBuildTimeArgExpansion(c *check.C) {
5855 5855
 	userVal := "testUser"
5856 5856
 	volVar := "VOL"
5857 5857
 	volVal := "/testVol/"
5858
-	args := []string{
5859
-		"--build-arg", fmt.Sprintf("%s=%s", wdVar, wdVal),
5860
-		"--build-arg", fmt.Sprintf("%s=%s", addVar, addVal),
5861
-		"--build-arg", fmt.Sprintf("%s=%s", copyVar, copyVal),
5862
-		"--build-arg", fmt.Sprintf("%s=%s", envVar, envVal),
5863
-		"--build-arg", fmt.Sprintf("%s=%s", exposeVar, exposeVal),
5864
-		"--build-arg", fmt.Sprintf("%s=%s", userVar, userVal),
5865
-		"--build-arg", fmt.Sprintf("%s=%s", volVar, volVal),
5866
-	}
5867
-	ctx, err := fakeContext(fmt.Sprintf(`FROM busybox
5858
+
5859
+	buildImageSuccessfully(c, imgName,
5860
+		withBuildFlags(
5861
+			"--build-arg", fmt.Sprintf("%s=%s", wdVar, wdVal),
5862
+			"--build-arg", fmt.Sprintf("%s=%s", addVar, addVal),
5863
+			"--build-arg", fmt.Sprintf("%s=%s", copyVar, copyVal),
5864
+			"--build-arg", fmt.Sprintf("%s=%s", envVar, envVal),
5865
+			"--build-arg", fmt.Sprintf("%s=%s", exposeVar, exposeVal),
5866
+			"--build-arg", fmt.Sprintf("%s=%s", userVar, userVal),
5867
+			"--build-arg", fmt.Sprintf("%s=%s", volVar, volVal),
5868
+		),
5869
+		withBuildContext(c,
5870
+			withFile("Dockerfile", fmt.Sprintf(`FROM busybox
5868 5871
 		ARG %s
5869 5872
 		WORKDIR ${%s}
5870 5873
 		ARG %s
... ...
@@ -5879,30 +4736,20 @@ func (s *DockerSuite) TestBuildBuildTimeArgExpansion(c *check.C) {
5879 5879
 		USER $%s
5880 5880
 		ARG %s
5881 5881
 		VOLUME ${%s}`,
5882
-		wdVar, wdVar, addVar, addVar, copyVar, copyVar, envVar, envVar,
5883
-		envVar, exposeVar, exposeVar, userVar, userVar, volVar, volVar),
5884
-		map[string]string{
5885
-			addVal:  "some stuff",
5886
-			copyVal: "some stuff",
5887
-		})
5888
-	if err != nil {
5889
-		c.Fatal(err)
5890
-	}
5891
-	defer ctx.Close()
5892
-
5893
-	if _, err := buildImageFromContext(imgName, ctx, true, args...); err != nil {
5894
-		c.Fatal(err)
5895
-	}
5882
+				wdVar, wdVar, addVar, addVar, copyVar, copyVar, envVar, envVar,
5883
+				envVar, exposeVar, exposeVar, userVar, userVar, volVar, volVar)),
5884
+			withFile(addVal, "some stuff"),
5885
+			withFile(copyVal, "some stuff"),
5886
+		),
5887
+	)
5896 5888
 
5897
-	var resMap map[string]interface{}
5898
-	var resArr []string
5899
-	res := ""
5900
-	res = inspectField(c, imgName, "Config.WorkingDir")
5889
+	res := inspectField(c, imgName, "Config.WorkingDir")
5901 5890
 	if res != filepath.ToSlash(filepath.Clean(wdVal)) {
5902 5891
 		c.Fatalf("Config.WorkingDir value mismatch. Expected: %s, got: %s", filepath.ToSlash(filepath.Clean(wdVal)), res)
5903 5892
 	}
5904 5893
 
5905
-	inspectFieldAndMarshall(c, imgName, "Config.Env", &resArr)
5894
+	var resArr []string
5895
+	inspectFieldAndUnmarshall(c, imgName, "Config.Env", &resArr)
5906 5896
 
5907 5897
 	found := false
5908 5898
 	for _, v := range resArr {
... ...
@@ -5916,7 +4763,8 @@ func (s *DockerSuite) TestBuildBuildTimeArgExpansion(c *check.C) {
5916 5916
 			envVar, envVal, resArr)
5917 5917
 	}
5918 5918
 
5919
-	inspectFieldAndMarshall(c, imgName, "Config.ExposedPorts", &resMap)
5919
+	var resMap map[string]interface{}
5920
+	inspectFieldAndUnmarshall(c, imgName, "Config.ExposedPorts", &resMap)
5920 5921
 	if _, ok := resMap[fmt.Sprintf("%s/tcp", exposeVal)]; !ok {
5921 5922
 		c.Fatalf("Config.ExposedPorts value mismatch. Expected exposed port: %s/tcp, got: %v", exposeVal, resMap)
5922 5923
 	}
... ...
@@ -5926,7 +4774,7 @@ func (s *DockerSuite) TestBuildBuildTimeArgExpansion(c *check.C) {
5926 5926
 		c.Fatalf("Config.User value mismatch. Expected: %s, got: %s", userVal, res)
5927 5927
 	}
5928 5928
 
5929
-	inspectFieldAndMarshall(c, imgName, "Config.Volumes", &resMap)
5929
+	inspectFieldAndUnmarshall(c, imgName, "Config.Volumes", &resMap)
5930 5930
 	if _, ok := resMap[volVal]; !ok {
5931 5931
 		c.Fatalf("Config.Volumes value mismatch. Expected volume: %s, got: %v", volVal, resMap)
5932 5932
 	}
... ...
@@ -5939,21 +4787,19 @@ func (s *DockerSuite) TestBuildBuildTimeArgExpansionOverride(c *check.C) {
5939 5939
 	envVal := "bar"
5940 5940
 	envKey1 := "foo1"
5941 5941
 	envValOveride := "barOverride"
5942
-	args := []string{
5943
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
5944
-	}
5945 5942
 	dockerfile := fmt.Sprintf(`FROM busybox
5946 5943
 		ARG %s
5947 5944
 		ENV %s %s
5948 5945
 		ENV %s ${%s}
5949 5946
 		RUN echo $%s
5950 5947
 		CMD echo $%s`, envKey, envKey, envValOveride, envKey1, envKey, envKey1, envKey1)
5951
-
5952
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); err != nil || strings.Count(out, envValOveride) != 2 {
5953
-		if err != nil {
5954
-			c.Fatalf("build failed to complete: %q %q", out, err)
5955
-		}
5956
-		c.Fatalf("failed to access environment variable in output: %q expected: %q", out, envValOveride)
5948
+	result := buildImageNew(imgName,
5949
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
5950
+		withDockerfile(dockerfile),
5951
+	)
5952
+	result.Assert(c, icmd.Success)
5953
+	if strings.Count(result.Combined(), envValOveride) != 2 {
5954
+		c.Fatalf("failed to access environment variable in output: %q expected: %q", result.Combined(), envValOveride)
5957 5955
 	}
5958 5956
 
5959 5957
 	containerName := "bldargCont"
... ...
@@ -5967,19 +4813,17 @@ func (s *DockerSuite) TestBuildBuildTimeArgUntrustedDefinedAfterUse(c *check.C)
5967 5967
 	imgName := "bldargtest"
5968 5968
 	envKey := "foo"
5969 5969
 	envVal := "bar"
5970
-	args := []string{
5971
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
5972
-	}
5973 5970
 	dockerfile := fmt.Sprintf(`FROM busybox
5974 5971
 		RUN echo $%s
5975 5972
 		ARG %s
5976 5973
 		CMD echo $%s`, envKey, envKey, envKey)
5977
-
5978
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); err != nil || strings.Contains(out, envVal) {
5979
-		if err != nil {
5980
-			c.Fatalf("build failed to complete: %q %q", out, err)
5981
-		}
5982
-		c.Fatalf("able to access environment variable in output: %q expected to be missing", out)
5974
+	result := buildImageNew(imgName,
5975
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
5976
+		withDockerfile(dockerfile),
5977
+	)
5978
+	result.Assert(c, icmd.Success)
5979
+	if strings.Contains(result.Combined(), envVal) {
5980
+		c.Fatalf("able to access environment variable in output: %q expected to be missing", result.Combined())
5983 5981
 	}
5984 5982
 
5985 5983
 	containerName := "bldargCont"
... ...
@@ -5993,20 +4837,18 @@ func (s *DockerSuite) TestBuildBuildTimeArgBuiltinArg(c *check.C) {
5993 5993
 	imgName := "bldargtest"
5994 5994
 	envKey := "HTTP_PROXY"
5995 5995
 	envVal := "bar"
5996
-	args := []string{
5997
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
5998
-	}
5999 5996
 	dockerfile := fmt.Sprintf(`FROM busybox
6000 5997
 		RUN echo $%s
6001 5998
 		CMD echo $%s`, envKey, envKey)
6002 5999
 
6003
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); err != nil || !strings.Contains(out, envVal) {
6004
-		if err != nil {
6005
-			c.Fatalf("build failed to complete: %q %q", out, err)
6006
-		}
6007
-		c.Fatalf("failed to access environment variable in output: %q expected: %q", out, envVal)
6000
+	result := buildImageNew(imgName,
6001
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
6002
+		withDockerfile(dockerfile),
6003
+	)
6004
+	result.Assert(c, icmd.Success)
6005
+	if !strings.Contains(result.Combined(), envVal) {
6006
+		c.Fatalf("failed to access environment variable in output: %q expected: %q", result.Combined(), envVal)
6008 6007
 	}
6009
-
6010 6008
 	containerName := "bldargCont"
6011 6009
 	if out, _ := dockerCmd(c, "run", "--name", containerName, imgName); out != "\n" {
6012 6010
 		c.Fatalf("run produced invalid output: %q, expected empty string", out)
... ...
@@ -6019,20 +4861,18 @@ func (s *DockerSuite) TestBuildBuildTimeArgDefaultOverride(c *check.C) {
6019 6019
 	envKey := "foo"
6020 6020
 	envVal := "bar"
6021 6021
 	envValOveride := "barOverride"
6022
-	args := []string{
6023
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, envValOveride),
6024
-	}
6025 6022
 	dockerfile := fmt.Sprintf(`FROM busybox
6026 6023
 		ARG %s=%s
6027 6024
 		ENV %s $%s
6028 6025
 		RUN echo $%s
6029 6026
 		CMD echo $%s`, envKey, envVal, envKey, envKey, envKey, envKey)
6030
-
6031
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); err != nil || strings.Count(out, envValOveride) != 1 {
6032
-		if err != nil {
6033
-			c.Fatalf("build failed to complete: %q %q", out, err)
6034
-		}
6035
-		c.Fatalf("failed to access environment variable in output: %q expected: %q", out, envValOveride)
6027
+	result := buildImageNew(imgName,
6028
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envValOveride)),
6029
+		withDockerfile(dockerfile),
6030
+	)
6031
+	result.Assert(c, icmd.Success)
6032
+	if strings.Count(result.Combined(), envValOveride) != 1 {
6033
+		c.Fatalf("failed to access environment variable in output: %q expected: %q", result.Combined(), envValOveride)
6036 6034
 	}
6037 6035
 
6038 6036
 	containerName := "bldargCont"
... ...
@@ -6045,40 +4885,21 @@ func (s *DockerSuite) TestBuildBuildTimeArgUnconsumedArg(c *check.C) {
6045 6045
 	imgName := "bldargtest"
6046 6046
 	envKey := "foo"
6047 6047
 	envVal := "bar"
6048
-	args := []string{
6049
-		"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
6050
-	}
6051 6048
 	dockerfile := fmt.Sprintf(`FROM busybox
6052 6049
 		RUN echo $%s
6053 6050
 		CMD echo $%s`, envKey, envKey)
6054
-
6055 6051
 	warnStr := "[Warning] One or more build-args"
6056
-
6057
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); !strings.Contains(out, warnStr) {
6058
-		c.Fatalf("build completed without warning: %q %q", out, err)
6059
-	} else if err != nil {
6060
-		c.Fatalf("build failed to complete: %q %q", out, err)
6061
-	}
6062
-
6052
+	buildImageNew(imgName,
6053
+		withBuildFlags("--build-arg", fmt.Sprintf("%s=%s", envKey, envVal)),
6054
+		withDockerfile(dockerfile),
6055
+	).Assert(c, icmd.Expected{
6056
+		Out: warnStr,
6057
+	})
6063 6058
 }
6064 6059
 
6065 6060
 func (s *DockerSuite) TestBuildBuildTimeArgEnv(c *check.C) {
6066 6061
 	testRequires(c, DaemonIsLinux) // Windows does not support ARG
6067
-	args := []string{
6068
-		"build",
6069
-		"--build-arg", fmt.Sprintf("FOO1=fromcmd"),
6070
-		"--build-arg", fmt.Sprintf("FOO2="),
6071
-		"--build-arg", fmt.Sprintf("FOO3"), // set in env
6072
-		"--build-arg", fmt.Sprintf("FOO4"), // not set in env
6073
-		"--build-arg", fmt.Sprintf("FOO5=fromcmd"),
6074
-		// FOO6 is not set at all
6075
-		"--build-arg", fmt.Sprintf("FOO7=fromcmd"), // should produce a warning
6076
-		"--build-arg", fmt.Sprintf("FOO8="), // should produce a warning
6077
-		"--build-arg", fmt.Sprintf("FOO9"), // should produce a warning
6078
-		".",
6079
-	}
6080
-
6081
-	dockerfile := fmt.Sprintf(`FROM busybox
6062
+	dockerfile := `FROM busybox
6082 6063
 		ARG FOO1=fromfile
6083 6064
 		ARG FOO2=fromfile
6084 6065
 		ARG FOO3=fromfile
... ...
@@ -6096,30 +4917,36 @@ func (s *DockerSuite) TestBuildBuildTimeArgEnv(c *check.C) {
6096 6096
 		RUN [ "$(env | grep FOO7)" == "" ]
6097 6097
 		RUN [ "$(env | grep FOO8)" == "" ]
6098 6098
 		RUN [ "$(env | grep FOO9)" == "" ]
6099
-	    `)
6100
-
6101
-	ctx, err := fakeContext(dockerfile, nil)
6102
-	c.Assert(err, check.IsNil)
6103
-	defer ctx.Close()
6104
-
6105
-	cmd := exec.Command(dockerBinary, args...)
6106
-	cmd.Dir = ctx.Dir
6107
-	cmd.Env = append(os.Environ(),
6108
-		"FOO1=fromenv",
6109
-		"FOO2=fromenv",
6110
-		"FOO3=fromenv")
6111
-	out, _, err := runCommandWithOutput(cmd)
6112
-	if err != nil {
6113
-		c.Fatal(err, out)
6114
-	}
6099
+	    `
6100
+	result := buildImageNew("testbuildtimeargenv",
6101
+		withBuildFlags(
6102
+			"--build-arg", fmt.Sprintf("FOO1=fromcmd"),
6103
+			"--build-arg", fmt.Sprintf("FOO2="),
6104
+			"--build-arg", fmt.Sprintf("FOO3"), // set in env
6105
+			"--build-arg", fmt.Sprintf("FOO4"), // not set in env
6106
+			"--build-arg", fmt.Sprintf("FOO5=fromcmd"),
6107
+			// FOO6 is not set at all
6108
+			"--build-arg", fmt.Sprintf("FOO7=fromcmd"), // should produce a warning
6109
+			"--build-arg", fmt.Sprintf("FOO8="), // should produce a warning
6110
+			"--build-arg", fmt.Sprintf("FOO9"), // should produce a warning
6111
+		),
6112
+		withEnvironmentVariales(append(os.Environ(),
6113
+			"FOO1=fromenv",
6114
+			"FOO2=fromenv",
6115
+			"FOO3=fromenv")...),
6116
+		withBuildContext(c,
6117
+			withFile("Dockerfile", dockerfile),
6118
+		),
6119
+	)
6120
+	result.Assert(c, icmd.Success)
6115 6121
 
6116 6122
 	// Now check to make sure we got a warning msg about unused build-args
6117
-	i := strings.Index(out, "[Warning]")
6123
+	i := strings.Index(result.Combined(), "[Warning]")
6118 6124
 	if i < 0 {
6119
-		c.Fatalf("Missing the build-arg warning in %q", out)
6125
+		c.Fatalf("Missing the build-arg warning in %q", result.Combined())
6120 6126
 	}
6121 6127
 
6122
-	out = out[i:] // "out" should contain just the warning message now
6128
+	out := result.Combined()[i:] // "out" should contain just the warning message now
6123 6129
 
6124 6130
 	// These were specified on a --build-arg but no ARG was in the Dockerfile
6125 6131
 	c.Assert(out, checker.Contains, "FOO7")
... ...
@@ -6133,7 +4960,6 @@ func (s *DockerSuite) TestBuildBuildTimeArgQuotedValVariants(c *check.C) {
6133 6133
 	envKey1 := "foo1"
6134 6134
 	envKey2 := "foo2"
6135 6135
 	envKey3 := "foo3"
6136
-	args := []string{}
6137 6136
 	dockerfile := fmt.Sprintf(`FROM busybox
6138 6137
 		ARG %s=""
6139 6138
 		ARG %s=''
... ...
@@ -6146,10 +4972,7 @@ func (s *DockerSuite) TestBuildBuildTimeArgQuotedValVariants(c *check.C) {
6146 6146
 		RUN [ "$%s" != "$%s" ]`, envKey, envKey1, envKey2, envKey3,
6147 6147
 		envKey, envKey2, envKey, envKey3, envKey1, envKey2, envKey1, envKey3,
6148 6148
 		envKey2, envKey3)
6149
-
6150
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); err != nil {
6151
-		c.Fatalf("build failed to complete: %q %q", out, err)
6152
-	}
6149
+	buildImageSuccessfully(c, imgName, withDockerfile(dockerfile))
6153 6150
 }
6154 6151
 
6155 6152
 func (s *DockerSuite) TestBuildBuildTimeArgEmptyValVariants(c *check.C) {
... ...
@@ -6158,7 +4981,6 @@ func (s *DockerSuite) TestBuildBuildTimeArgEmptyValVariants(c *check.C) {
6158 6158
 	envKey := "foo"
6159 6159
 	envKey1 := "foo1"
6160 6160
 	envKey2 := "foo2"
6161
-	args := []string{}
6162 6161
 	dockerfile := fmt.Sprintf(`FROM busybox
6163 6162
 		ARG %s=
6164 6163
 		ARG %s=""
... ...
@@ -6166,25 +4988,20 @@ func (s *DockerSuite) TestBuildBuildTimeArgEmptyValVariants(c *check.C) {
6166 6166
 		RUN [ "$%s" == "$%s" ]
6167 6167
 		RUN [ "$%s" == "$%s" ]
6168 6168
 		RUN [ "$%s" == "$%s" ]`, envKey, envKey1, envKey2, envKey, envKey1, envKey1, envKey2, envKey, envKey2)
6169
-
6170
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); err != nil {
6171
-		c.Fatalf("build failed to complete: %q %q", out, err)
6172
-	}
6169
+	buildImageSuccessfully(c, imgName, withDockerfile(dockerfile))
6173 6170
 }
6174 6171
 
6175 6172
 func (s *DockerSuite) TestBuildBuildTimeArgDefintionWithNoEnvInjection(c *check.C) {
6176 6173
 	imgName := "bldargtest"
6177 6174
 	envKey := "foo"
6178
-	args := []string{}
6179 6175
 	dockerfile := fmt.Sprintf(`FROM busybox
6180 6176
 		ARG %s
6181 6177
 		RUN env`, envKey)
6182 6178
 
6183
-	if _, out, err := buildImageWithOut(imgName, dockerfile, true, args...); err != nil || strings.Count(out, envKey) != 1 {
6184
-		if err != nil {
6185
-			c.Fatalf("build failed to complete: %q %q", out, err)
6186
-		}
6187
-		c.Fatalf("unexpected number of occurrences of the arg in output: %q expected: 1", out)
6179
+	result := buildImageNew(imgName, withDockerfile(dockerfile))
6180
+	result.Assert(c, icmd.Success)
6181
+	if strings.Count(result.Combined(), envKey) != 1 {
6182
+		c.Fatalf("unexpected number of occurrences of the arg in output: %q expected: 1", result.Combined())
6188 6183
 	}
6189 6184
 }
6190 6185
 
... ...
@@ -6200,8 +5017,9 @@ func (s *DockerSuite) TestBuildNoNamedVolume(c *check.C) {
6200 6200
 	VOLUME ` + volName + `
6201 6201
 	RUN ls /foo/oops
6202 6202
 	`
6203
-	_, err := buildImage("test", dockerFile, false)
6204
-	c.Assert(err, check.NotNil, check.Commentf("image build should have failed"))
6203
+	buildImageNew("test", withDockerfile(dockerFile)).Assert(c, icmd.Expected{
6204
+		ExitCode: 1,
6205
+	})
6205 6206
 }
6206 6207
 
6207 6208
 func (s *DockerSuite) TestBuildTagEvent(c *check.C) {
... ...
@@ -6210,8 +5028,7 @@ func (s *DockerSuite) TestBuildTagEvent(c *check.C) {
6210 6210
 	dockerFile := `FROM busybox
6211 6211
 	RUN echo events
6212 6212
 	`
6213
-	_, err := buildImage("test", dockerFile, false)
6214
-	c.Assert(err, check.IsNil)
6213
+	buildImageSuccessfully(c, "test", withDockerfile(dockerFile))
6215 6214
 
6216 6215
 	until := daemonUnixTime(c)
6217 6216
 	out, _ := dockerCmd(c, "events", "--since", since, "--until", until, "--filter", "type=image")
... ...
@@ -6234,10 +5051,7 @@ func (s *DockerSuite) TestBuildMultipleTags(c *check.C) {
6234 6234
 	FROM busybox
6235 6235
 	MAINTAINER test-15780
6236 6236
 	`
6237
-	icmd.RunCmd(icmd.Cmd{
6238
-		Command: []string{dockerBinary, "build", "-t", "tag1", "-t", "tag2:v2", "-t", "tag1:latest", "-t", "tag1", "--no-cache", "-"},
6239
-		Stdin:   strings.NewReader(dockerfile),
6240
-	}).Assert(c, icmd.Success)
6237
+	buildImageSuccessfully(c, "tag1", withBuildFlags("-t", "tag2:v2", "-t", "tag1:latest", "-t", "tag1"), withDockerfile(dockerfile))
6241 6238
 
6242 6239
 	id1, err := getIDByName("tag1")
6243 6240
 	c.Assert(err, check.IsNil)
... ...
@@ -6394,55 +5208,43 @@ func (s *DockerSuite) TestBuildCacheRootSource(c *check.C) {
6394 6394
 
6395 6395
 // #19375
6396 6396
 func (s *DockerSuite) TestBuildFailsGitNotCallable(c *check.C) {
6397
-	cmd := exec.Command(dockerBinary, "build", "github.com/docker/v1.10-migrator.git")
6398
-	cmd.Env = append(cmd.Env, "PATH=")
6399
-	out, _, err := runCommandWithOutput(cmd)
6400
-	c.Assert(err, checker.NotNil)
6401
-	c.Assert(out, checker.Contains, "unable to prepare context: unable to find 'git': ")
6397
+	buildImageNew("gitnotcallable", withEnvironmentVariales("PATH="),
6398
+		withBuildContextPath("github.com/docker/v1.10-migrator.git")).Assert(c, icmd.Expected{
6399
+		ExitCode: 1,
6400
+		Err:      "unable to prepare context: unable to find 'git': ",
6401
+	})
6402 6402
 
6403
-	cmd = exec.Command(dockerBinary, "build", "https://github.com/docker/v1.10-migrator.git")
6404
-	cmd.Env = append(cmd.Env, "PATH=")
6405
-	out, _, err = runCommandWithOutput(cmd)
6406
-	c.Assert(err, checker.NotNil)
6407
-	c.Assert(out, checker.Contains, "unable to prepare context: unable to find 'git': ")
6403
+	buildImageNew("gitnotcallable", withEnvironmentVariales("PATH="),
6404
+		withBuildContextPath("https://github.com/docker/v1.10-migrator.git")).Assert(c, icmd.Expected{
6405
+		ExitCode: 1,
6406
+		Err:      "unable to prepare context: unable to find 'git': ",
6407
+	})
6408 6408
 }
6409 6409
 
6410 6410
 // TestBuildWorkdirWindowsPath tests that a Windows style path works as a workdir
6411 6411
 func (s *DockerSuite) TestBuildWorkdirWindowsPath(c *check.C) {
6412 6412
 	testRequires(c, DaemonIsWindows)
6413 6413
 	name := "testbuildworkdirwindowspath"
6414
-
6415
-	_, err := buildImage(name, `
6414
+	buildImageSuccessfully(c, name, withDockerfile(`
6416 6415
 	FROM `+WindowsBaseImage+`
6417 6416
 	RUN mkdir C:\\work
6418 6417
 	WORKDIR C:\\work
6419 6418
 	RUN if "%CD%" NEQ "C:\work" exit -1
6420
-	`, true)
6421
-
6422
-	if err != nil {
6423
-		c.Fatal(err)
6424
-	}
6419
+	`))
6425 6420
 }
6426 6421
 
6427 6422
 func (s *DockerSuite) TestBuildLabel(c *check.C) {
6428 6423
 	name := "testbuildlabel"
6429 6424
 	testLabel := "foo"
6430 6425
 
6431
-	_, err := buildImage(name, `
6426
+	buildImageSuccessfully(c, name, withBuildFlags("--label", testLabel),
6427
+		withDockerfile(`
6432 6428
   FROM `+minimalBaseImage()+`
6433 6429
   LABEL default foo
6434
-`, false, "--label", testLabel)
6435
-
6436
-	c.Assert(err, checker.IsNil)
6437
-
6438
-	res := inspectFieldJSON(c, name, "Config.Labels")
6430
+`))
6439 6431
 
6440 6432
 	var labels map[string]string
6441
-
6442
-	if err := json.Unmarshal([]byte(res), &labels); err != nil {
6443
-		c.Fatal(err)
6444
-	}
6445
-
6433
+	inspectFieldAndUnmarshall(c, name, "Config.Labels", &labels)
6446 6434
 	if _, ok := labels[testLabel]; !ok {
6447 6435
 		c.Fatal("label not found in image")
6448 6436
 	}
... ...
@@ -6450,19 +5252,11 @@ func (s *DockerSuite) TestBuildLabel(c *check.C) {
6450 6450
 
6451 6451
 func (s *DockerSuite) TestBuildLabelOneNode(c *check.C) {
6452 6452
 	name := "testbuildlabel"
6453
+	buildImageSuccessfully(c, name, withBuildFlags("--label", "foo=bar"),
6454
+		withDockerfile("FROM busybox"))
6453 6455
 
6454
-	_, err := buildImage(name, "FROM busybox", false, "--label", "foo=bar")
6455
-
6456
-	c.Assert(err, checker.IsNil)
6457
-
6458
-	res, err := inspectImage(name, "json .Config.Labels")
6459
-	c.Assert(err, checker.IsNil)
6460 6456
 	var labels map[string]string
6461
-
6462
-	if err := json.Unmarshal([]byte(res), &labels); err != nil {
6463
-		c.Fatal(err)
6464
-	}
6465
-
6457
+	inspectFieldAndUnmarshall(c, name, "Config.Labels", &labels)
6466 6458
 	v, ok := labels["foo"]
6467 6459
 	if !ok {
6468 6460
 		c.Fatal("label `foo` not found in image")
... ...
@@ -6474,28 +5268,18 @@ func (s *DockerSuite) TestBuildLabelCacheCommit(c *check.C) {
6474 6474
 	name := "testbuildlabelcachecommit"
6475 6475
 	testLabel := "foo"
6476 6476
 
6477
-	if _, err := buildImage(name, `
6477
+	buildImageSuccessfully(c, name, withDockerfile(`
6478 6478
   FROM `+minimalBaseImage()+`
6479 6479
   LABEL default foo
6480
-  `, false); err != nil {
6481
-		c.Fatal(err)
6482
-	}
6483
-
6484
-	_, err := buildImage(name, `
6480
+  `))
6481
+	buildImageSuccessfully(c, name, withBuildFlags("--label", testLabel),
6482
+		withDockerfile(`
6485 6483
   FROM `+minimalBaseImage()+`
6486 6484
   LABEL default foo
6487
-`, true, "--label", testLabel)
6488
-
6489
-	c.Assert(err, checker.IsNil)
6490
-
6491
-	res := inspectFieldJSON(c, name, "Config.Labels")
6485
+  `))
6492 6486
 
6493 6487
 	var labels map[string]string
6494
-
6495
-	if err := json.Unmarshal([]byte(res), &labels); err != nil {
6496
-		c.Fatal(err)
6497
-	}
6498
-
6488
+	inspectFieldAndUnmarshall(c, name, "Config.Labels", &labels)
6499 6489
 	if _, ok := labels[testLabel]; !ok {
6500 6490
 		c.Fatal("label not found in image")
6501 6491
 	}
... ...
@@ -6507,30 +5291,19 @@ func (s *DockerSuite) TestBuildLabelMultiple(c *check.C) {
6507 6507
 		"foo": "bar",
6508 6508
 		"123": "456",
6509 6509
 	}
6510
-
6511 6510
 	labelArgs := []string{}
6512
-
6513 6511
 	for k, v := range testLabels {
6514 6512
 		labelArgs = append(labelArgs, "--label", k+"="+v)
6515 6513
 	}
6516 6514
 
6517
-	_, err := buildImage(name, `
6515
+	buildImageSuccessfully(c, name, withBuildFlags(labelArgs...),
6516
+		withDockerfile(`
6518 6517
   FROM `+minimalBaseImage()+`
6519 6518
   LABEL default foo
6520
-`, false, labelArgs...)
6521
-
6522
-	if err != nil {
6523
-		c.Fatal("error building image with labels", err)
6524
-	}
6525
-
6526
-	res := inspectFieldJSON(c, name, "Config.Labels")
6519
+`))
6527 6520
 
6528 6521
 	var labels map[string]string
6529
-
6530
-	if err := json.Unmarshal([]byte(res), &labels); err != nil {
6531
-		c.Fatal(err)
6532
-	}
6533
-
6522
+	inspectFieldAndUnmarshall(c, name, "Config.Labels", &labels)
6534 6523
 	for k, v := range testLabels {
6535 6524
 		if x, ok := labels[k]; !ok || x != v {
6536 6525
 			c.Fatalf("label %s=%s not found in image", k, v)
... ...
@@ -6538,59 +5311,22 @@ func (s *DockerSuite) TestBuildLabelMultiple(c *check.C) {
6538 6538
 	}
6539 6539
 }
6540 6540
 
6541
-func (s *DockerSuite) TestBuildLabelOverwrite(c *check.C) {
6542
-	name := "testbuildlabeloverwrite"
6543
-	testLabel := "foo"
6544
-	testValue := "bar"
6545
-
6546
-	_, err := buildImage(name, `
6547
-  FROM `+minimalBaseImage()+`
6548
-  LABEL `+testLabel+`+ foo
6549
-`, false, []string{"--label", testLabel + "=" + testValue}...)
6550
-
6551
-	if err != nil {
6552
-		c.Fatal("error building image with labels", err)
6553
-	}
6554
-
6555
-	res := inspectFieldJSON(c, name, "Config.Labels")
6556
-
6557
-	var labels map[string]string
6558
-
6559
-	if err := json.Unmarshal([]byte(res), &labels); err != nil {
6560
-		c.Fatal(err)
6561
-	}
6562
-
6563
-	v, ok := labels[testLabel]
6564
-	if !ok {
6565
-		c.Fatal("label not found in image")
6566
-	}
6567
-
6568
-	if v != testValue {
6569
-		c.Fatal("label not overwritten")
6570
-	}
6571
-}
6572
-
6573 6541
 func (s *DockerRegistryAuthHtpasswdSuite) TestBuildFromAuthenticatedRegistry(c *check.C) {
6574 6542
 	dockerCmd(c, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), privateRegistryURL)
6575
-
6576 6543
 	baseImage := privateRegistryURL + "/baseimage"
6577 6544
 
6578
-	_, err := buildImage(baseImage, `
6545
+	buildImageSuccessfully(c, baseImage, withDockerfile(`
6579 6546
 	FROM busybox
6580 6547
 	ENV env1 val1
6581
-	`, true)
6582
-
6583
-	c.Assert(err, checker.IsNil)
6548
+	`))
6584 6549
 
6585 6550
 	dockerCmd(c, "push", baseImage)
6586 6551
 	dockerCmd(c, "rmi", baseImage)
6587 6552
 
6588
-	_, err = buildImage(baseImage, fmt.Sprintf(`
6553
+	buildImageSuccessfully(c, baseImage, withDockerfile(fmt.Sprintf(`
6589 6554
 	FROM %s
6590 6555
 	ENV env2 val2
6591
-	`, baseImage), true)
6592
-
6593
-	c.Assert(err, checker.IsNil)
6556
+	`, baseImage)))
6594 6557
 }
6595 6558
 
6596 6559
 func (s *DockerRegistryAuthHtpasswdSuite) TestBuildWithExternalAuth(c *check.C) {
... ...
@@ -6628,11 +5364,10 @@ func (s *DockerRegistryAuthHtpasswdSuite) TestBuildWithExternalAuth(c *check.C)
6628 6628
 	// make sure the image is pulled when building
6629 6629
 	dockerCmd(c, "rmi", repoName)
6630 6630
 
6631
-	buildCmd := exec.Command(dockerBinary, "--config", tmp, "build", "-")
6632
-	buildCmd.Stdin = strings.NewReader(fmt.Sprintf("FROM %s", repoName))
6633
-
6634
-	out, _, err := runCommandWithOutput(buildCmd)
6635
-	c.Assert(err, check.IsNil, check.Commentf(out))
6631
+	icmd.RunCmd(icmd.Cmd{
6632
+		Command: []string{dockerBinary, "--config", tmp, "build", "-"},
6633
+		Stdin:   strings.NewReader(fmt.Sprintf("FROM %s", repoName)),
6634
+	}).Assert(c, icmd.Success)
6636 6635
 }
6637 6636
 
6638 6637
 // Test cases in #22036
... ...
@@ -6640,12 +5375,9 @@ func (s *DockerSuite) TestBuildLabelsOverride(c *check.C) {
6640 6640
 	// Command line option labels will always override
6641 6641
 	name := "scratchy"
6642 6642
 	expected := `{"bar":"from-flag","foo":"from-flag"}`
6643
-	_, err := buildImage(name,
6644
-		`FROM `+minimalBaseImage()+`
6645
-                LABEL foo=from-dockerfile`,
6646
-		true, "--label", "foo=from-flag", "--label", "bar=from-flag")
6647
-	c.Assert(err, check.IsNil)
6648
-
6643
+	buildImageSuccessfully(c, name, withBuildFlags("--label", "foo=from-flag", "--label", "bar=from-flag"),
6644
+		withDockerfile(`FROM `+minimalBaseImage()+`
6645
+                LABEL foo=from-dockerfile`))
6649 6646
 	res := inspectFieldJSON(c, name, "Config.Labels")
6650 6647
 	if res != expected {
6651 6648
 		c.Fatalf("Labels %s, expected %s", res, expected)
... ...
@@ -6653,12 +5385,8 @@ func (s *DockerSuite) TestBuildLabelsOverride(c *check.C) {
6653 6653
 
6654 6654
 	name = "from"
6655 6655
 	expected = `{"foo":"from-dockerfile"}`
6656
-	_, err = buildImage(name,
6657
-		`FROM `+minimalBaseImage()+`
6658
-                LABEL foo from-dockerfile`,
6659
-		true)
6660
-	c.Assert(err, check.IsNil)
6661
-
6656
+	buildImageSuccessfully(c, name, withDockerfile(`FROM `+minimalBaseImage()+`
6657
+                LABEL foo from-dockerfile`))
6662 6658
 	res = inspectFieldJSON(c, name, "Config.Labels")
6663 6659
 	if res != expected {
6664 6660
 		c.Fatalf("Labels %s, expected %s", res, expected)
... ...
@@ -6667,12 +5395,9 @@ func (s *DockerSuite) TestBuildLabelsOverride(c *check.C) {
6667 6667
 	// Command line option label will override even via `FROM`
6668 6668
 	name = "new"
6669 6669
 	expected = `{"bar":"from-dockerfile2","foo":"new"}`
6670
-	_, err = buildImage(name,
6671
-		`FROM from
6672
-                LABEL bar from-dockerfile2`,
6673
-		true, "--label", "foo=new")
6674
-	c.Assert(err, check.IsNil)
6675
-
6670
+	buildImageSuccessfully(c, name, withBuildFlags("--label", "foo=new"),
6671
+		withDockerfile(`FROM from
6672
+                LABEL bar from-dockerfile2`))
6676 6673
 	res = inspectFieldJSON(c, name, "Config.Labels")
6677 6674
 	if res != expected {
6678 6675
 		c.Fatalf("Labels %s, expected %s", res, expected)
... ...
@@ -6682,12 +5407,9 @@ func (s *DockerSuite) TestBuildLabelsOverride(c *check.C) {
6682 6682
 	// will be treated as --label foo="", --label bar=""
6683 6683
 	name = "scratchy2"
6684 6684
 	expected = `{"bar":"","foo":""}`
6685
-	_, err = buildImage(name,
6686
-		`FROM `+minimalBaseImage()+`
6687
-                LABEL foo=from-dockerfile`,
6688
-		true, "--label", "foo", "--label", "bar=")
6689
-	c.Assert(err, check.IsNil)
6690
-
6685
+	buildImageSuccessfully(c, name, withBuildFlags("--label", "foo", "--label", "bar="),
6686
+		withDockerfile(`FROM `+minimalBaseImage()+`
6687
+                LABEL foo=from-dockerfile`))
6691 6688
 	res = inspectFieldJSON(c, name, "Config.Labels")
6692 6689
 	if res != expected {
6693 6690
 		c.Fatalf("Labels %s, expected %s", res, expected)
... ...
@@ -6698,12 +5420,9 @@ func (s *DockerSuite) TestBuildLabelsOverride(c *check.C) {
6698 6698
 	// This time is for inherited images
6699 6699
 	name = "new2"
6700 6700
 	expected = `{"bar":"","foo":""}`
6701
-	_, err = buildImage(name,
6702
-		`FROM from
6703
-                LABEL bar from-dockerfile2`,
6704
-		true, "--label", "foo=", "--label", "bar")
6705
-	c.Assert(err, check.IsNil)
6706
-
6701
+	buildImageSuccessfully(c, name, withBuildFlags("--label", "foo=", "--label", "bar"),
6702
+		withDockerfile(`FROM from
6703
+                LABEL bar from-dockerfile2`))
6707 6704
 	res = inspectFieldJSON(c, name, "Config.Labels")
6708 6705
 	if res != expected {
6709 6706
 		c.Fatalf("Labels %s, expected %s", res, expected)
... ...
@@ -6712,11 +5431,8 @@ func (s *DockerSuite) TestBuildLabelsOverride(c *check.C) {
6712 6712
 	// Command line option labels with only `FROM`
6713 6713
 	name = "scratchy"
6714 6714
 	expected = `{"bar":"from-flag","foo":"from-flag"}`
6715
-	_, err = buildImage(name,
6716
-		`FROM `+minimalBaseImage(),
6717
-		true, "--label", "foo=from-flag", "--label", "bar=from-flag")
6718
-	c.Assert(err, check.IsNil)
6719
-
6715
+	buildImageSuccessfully(c, name, withBuildFlags("--label", "foo=from-flag", "--label", "bar=from-flag"),
6716
+		withDockerfile(`FROM `+minimalBaseImage()))
6720 6717
 	res = inspectFieldJSON(c, name, "Config.Labels")
6721 6718
 	if res != expected {
6722 6719
 		c.Fatalf("Labels %s, expected %s", res, expected)
... ...
@@ -6725,31 +5441,22 @@ func (s *DockerSuite) TestBuildLabelsOverride(c *check.C) {
6725 6725
 	// Command line option labels with env var
6726 6726
 	name = "scratchz"
6727 6727
 	expected = `{"bar":"$PATH"}`
6728
-	_, err = buildImage(name,
6729
-		`FROM `+minimalBaseImage(),
6730
-		true, "--label", "bar=$PATH")
6731
-	c.Assert(err, check.IsNil)
6732
-
6728
+	buildImageSuccessfully(c, name, withBuildFlags("--label", "bar=$PATH"),
6729
+		withDockerfile(`FROM `+minimalBaseImage()))
6733 6730
 	res = inspectFieldJSON(c, name, "Config.Labels")
6734 6731
 	if res != expected {
6735 6732
 		c.Fatalf("Labels %s, expected %s", res, expected)
6736 6733
 	}
6737
-
6738 6734
 }
6739 6735
 
6740 6736
 // Test case for #22855
6741 6737
 func (s *DockerSuite) TestBuildDeleteCommittedFile(c *check.C) {
6742 6738
 	name := "test-delete-committed-file"
6743
-
6744
-	_, err := buildImage(name,
6745
-		`FROM busybox
6739
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
6746 6740
 		RUN echo test > file
6747 6741
 		RUN test -e file
6748 6742
 		RUN rm file
6749
-		RUN sh -c "! test -e file"`, false)
6750
-	if err != nil {
6751
-		c.Fatal(err)
6752
-	}
6743
+		RUN sh -c "! test -e file"`))
6753 6744
 }
6754 6745
 
6755 6746
 // #20083
... ...
@@ -6766,13 +5473,14 @@ func (s *DockerSuite) TestBuildDockerignoreComment(c *check.C) {
6766 6766
         RUN sh -c "(ls -la /tmp/#1)"
6767 6767
         RUN sh -c "(! ls -la /tmp/#2)"
6768 6768
         RUN sh -c "(! ls /tmp/foo) && (! ls /tmp/foo2) && (ls /tmp/dir1/foo)"`
6769
-	ctx, err := fakeContext(dockerfile, map[string]string{
6770
-		"foo":      "foo",
6771
-		"foo2":     "foo2",
6772
-		"dir1/foo": "foo in dir1",
6773
-		"#1":       "# file 1",
6774
-		"#2":       "# file 2",
6775
-		".dockerignore": `# Visual C++ cache files
6769
+	buildImageSuccessfully(c, name, withBuildContext(c,
6770
+		withFile("Dockerfile", dockerfile),
6771
+		withFile("foo", "foo"),
6772
+		withFile("foo2", "foo2"),
6773
+		withFile("dir1/foo", "foo in dir1"),
6774
+		withFile("#1", "# file 1"),
6775
+		withFile("#2", "# file 2"),
6776
+		withFile(".dockerignore", `# Visual C++ cache files
6776 6777
 # because we have git ;-)
6777 6778
 # The above comment is from #20083
6778 6779
 foo
... ...
@@ -6782,15 +5490,7 @@ foo2
6782 6782
 #1
6783 6783
 # The following is not considered as comment as # is not at the beginning
6784 6784
   #2
6785
-`,
6786
-	})
6787
-	if err != nil {
6788
-		c.Fatal(err)
6789
-	}
6790
-	defer ctx.Close()
6791
-	if _, err := buildImageFromContext(name, ctx, true); err != nil {
6792
-		c.Fatal(err)
6793
-	}
6785
+`)))
6794 6786
 }
6795 6787
 
6796 6788
 // Test case for #23221
... ...
@@ -6798,13 +5498,9 @@ func (s *DockerSuite) TestBuildWithUTF8BOM(c *check.C) {
6798 6798
 	name := "test-with-utf8-bom"
6799 6799
 	dockerfile := []byte(`FROM busybox`)
6800 6800
 	bomDockerfile := append([]byte{0xEF, 0xBB, 0xBF}, dockerfile...)
6801
-	ctx, err := fakeContextFromNewTempDir()
6802
-	c.Assert(err, check.IsNil)
6803
-	defer ctx.Close()
6804
-	err = ctx.addFile("Dockerfile", bomDockerfile)
6805
-	c.Assert(err, check.IsNil)
6806
-	_, err = buildImageFromContext(name, ctx, true)
6807
-	c.Assert(err, check.IsNil)
6801
+	buildImageSuccessfully(c, name, withBuildContext(c,
6802
+		withFile("Dockerfile", string(bomDockerfile)),
6803
+	))
6808 6804
 }
6809 6805
 
6810 6806
 // Test case for UTF-8 BOM in .dockerignore, related to #23221
... ...
@@ -6818,31 +5514,19 @@ func (s *DockerSuite) TestBuildWithUTF8BOMDockerignore(c *check.C) {
6818 6818
 		RUN ls /tmp/.dockerignore`
6819 6819
 	dockerignore := []byte("./Dockerfile\n")
6820 6820
 	bomDockerignore := append([]byte{0xEF, 0xBB, 0xBF}, dockerignore...)
6821
-	ctx, err := fakeContext(dockerfile, map[string]string{
6822
-		"Dockerfile": dockerfile,
6823
-	})
6824
-	c.Assert(err, check.IsNil)
6825
-	defer ctx.Close()
6826
-	err = ctx.addFile(".dockerignore", bomDockerignore)
6827
-	c.Assert(err, check.IsNil)
6828
-	_, err = buildImageFromContext(name, ctx, true)
6829
-	if err != nil {
6830
-		c.Fatal(err)
6831
-	}
6821
+	buildImageSuccessfully(c, name, withBuildContext(c,
6822
+		withFile("Dockerfile", dockerfile),
6823
+		withFile(".dockerignore", string(bomDockerignore)),
6824
+	))
6832 6825
 }
6833 6826
 
6834 6827
 // #22489 Shell test to confirm config gets updated correctly
6835 6828
 func (s *DockerSuite) TestBuildShellUpdatesConfig(c *check.C) {
6836 6829
 	name := "testbuildshellupdatesconfig"
6837 6830
 
6831
+	buildImageSuccessfully(c, name, withDockerfile(`FROM `+minimalBaseImage()+`
6832
+        SHELL ["foo", "-bar"]`))
6838 6833
 	expected := `["foo","-bar","#(nop) ","SHELL [foo -bar]"]`
6839
-	_, err := buildImage(name,
6840
-		`FROM `+minimalBaseImage()+`
6841
-        SHELL ["foo", "-bar"]`,
6842
-		true)
6843
-	if err != nil {
6844
-		c.Fatal(err)
6845
-	}
6846 6834
 	res := inspectFieldJSON(c, name, "ContainerConfig.Cmd")
6847 6835
 	if res != expected {
6848 6836
 		c.Fatalf("%s, expected %s", res, expected)
... ...
@@ -6857,32 +5541,28 @@ func (s *DockerSuite) TestBuildShellUpdatesConfig(c *check.C) {
6857 6857
 func (s *DockerSuite) TestBuildShellMultiple(c *check.C) {
6858 6858
 	name := "testbuildshellmultiple"
6859 6859
 
6860
-	_, out, _, err := buildImageWithStdoutStderr(name,
6861
-		`FROM busybox
6860
+	result := buildImageNew(name, withDockerfile(`FROM busybox
6862 6861
 		RUN echo defaultshell
6863 6862
 		SHELL ["echo"]
6864 6863
 		RUN echoshell
6865 6864
 		SHELL ["ls"]
6866 6865
 		RUN -l
6867
-		CMD -l`,
6868
-		true)
6869
-	if err != nil {
6870
-		c.Fatal(err)
6871
-	}
6866
+		CMD -l`))
6867
+	result.Assert(c, icmd.Success)
6872 6868
 
6873 6869
 	// Must contain 'defaultshell' twice
6874
-	if len(strings.Split(out, "defaultshell")) != 3 {
6875
-		c.Fatalf("defaultshell should have appeared twice in %s", out)
6870
+	if len(strings.Split(result.Combined(), "defaultshell")) != 3 {
6871
+		c.Fatalf("defaultshell should have appeared twice in %s", result.Combined())
6876 6872
 	}
6877 6873
 
6878 6874
 	// Must contain 'echoshell' twice
6879
-	if len(strings.Split(out, "echoshell")) != 3 {
6880
-		c.Fatalf("echoshell should have appeared twice in %s", out)
6875
+	if len(strings.Split(result.Combined(), "echoshell")) != 3 {
6876
+		c.Fatalf("echoshell should have appeared twice in %s", result.Combined())
6881 6877
 	}
6882 6878
 
6883 6879
 	// Must contain "total " (part of ls -l)
6884
-	if !strings.Contains(out, "total ") {
6885
-		c.Fatalf("%s should have contained 'total '", out)
6880
+	if !strings.Contains(result.Combined(), "total ") {
6881
+		c.Fatalf("%s should have contained 'total '", result.Combined())
6886 6882
 	}
6887 6883
 
6888 6884
 	// A container started from the image uses the shell-form CMD.
... ...
@@ -6897,15 +5577,9 @@ func (s *DockerSuite) TestBuildShellMultiple(c *check.C) {
6897 6897
 func (s *DockerSuite) TestBuildShellEntrypoint(c *check.C) {
6898 6898
 	name := "testbuildshellentrypoint"
6899 6899
 
6900
-	_, err := buildImage(name,
6901
-		`FROM busybox
6900
+	buildImageSuccessfully(c, name, withDockerfile(`FROM busybox
6902 6901
 		SHELL ["ls"]
6903
-		ENTRYPOINT -l`,
6904
-		true)
6905
-	if err != nil {
6906
-		c.Fatal(err)
6907
-	}
6908
-
6902
+		ENTRYPOINT -l`))
6909 6903
 	// A container started from the image uses the shell-form ENTRYPOINT.
6910 6904
 	// Shell is ls. ENTRYPOINT is -l. So should contain 'total '.
6911 6905
 	outrun, _ := dockerCmd(c, "run", "--rm", name)
... ...
@@ -6917,43 +5591,26 @@ func (s *DockerSuite) TestBuildShellEntrypoint(c *check.C) {
6917 6917
 // #22489 Shell test to confirm shell is inherited in a subsequent build
6918 6918
 func (s *DockerSuite) TestBuildShellInherited(c *check.C) {
6919 6919
 	name1 := "testbuildshellinherited1"
6920
-	_, err := buildImage(name1,
6921
-		`FROM busybox
6922
-        SHELL ["ls"]`,
6923
-		true)
6924
-	if err != nil {
6925
-		c.Fatal(err)
6926
-	}
6927
-
6920
+	buildImageSuccessfully(c, name1, withDockerfile(`FROM busybox
6921
+        SHELL ["ls"]`))
6928 6922
 	name2 := "testbuildshellinherited2"
6929
-	_, out, _, err := buildImageWithStdoutStderr(name2,
6930
-		`FROM `+name1+`
6931
-        RUN -l`,
6932
-		true)
6933
-	if err != nil {
6934
-		c.Fatal(err)
6935
-	}
6936
-
6937
-	// ls -l has "total " followed by some number in it, ls without -l does not.
6938
-	if !strings.Contains(out, "total ") {
6939
-		c.Fatalf("Should have seen total in 'ls -l'.\n%s", out)
6940
-	}
6923
+	buildImageNew(name2, withDockerfile(`FROM `+name1+`
6924
+        RUN -l`)).Assert(c, icmd.Expected{
6925
+		// ls -l has "total " followed by some number in it, ls without -l does not.
6926
+		Out: "total ",
6927
+	})
6941 6928
 }
6942 6929
 
6943 6930
 // #22489 Shell test to confirm non-JSON doesn't work
6944 6931
 func (s *DockerSuite) TestBuildShellNotJSON(c *check.C) {
6945 6932
 	name := "testbuildshellnotjson"
6946 6933
 
6947
-	_, err := buildImage(name,
6948
-		`FROM `+minimalBaseImage()+`
6934
+	buildImageNew(name, withDockerfile(`FROM `+minimalBaseImage()+`
6949 6935
         sHeLl exec -form`, // Casing explicit to ensure error is upper-cased.
6950
-		true)
6951
-	if err == nil {
6952
-		c.Fatal("Image build should have failed")
6953
-	}
6954
-	if !strings.Contains(err.Error(), "SHELL requires the arguments to be in JSON form") {
6955
-		c.Fatal("Error didn't indicate that arguments must be in JSON form")
6956
-	}
6936
+	)).Assert(c, icmd.Expected{
6937
+		ExitCode: 1,
6938
+		Err:      "SHELL requires the arguments to be in JSON form",
6939
+	})
6957 6940
 }
6958 6941
 
6959 6942
 // #22489 Windows shell test to confirm native is powershell if executing a PS command
... ...
@@ -6961,17 +5618,11 @@ func (s *DockerSuite) TestBuildShellNotJSON(c *check.C) {
6961 6961
 func (s *DockerSuite) TestBuildShellWindowsPowershell(c *check.C) {
6962 6962
 	testRequires(c, DaemonIsWindows)
6963 6963
 	name := "testbuildshellpowershell"
6964
-	_, out, err := buildImageWithOut(name,
6965
-		`FROM `+minimalBaseImage()+`
6964
+	buildImageNew(name, withDockerfile(`FROM `+minimalBaseImage()+`
6966 6965
         SHELL ["powershell", "-command"]
6967
-		RUN Write-Host John`,
6968
-		true)
6969
-	if err != nil {
6970
-		c.Fatal(err)
6971
-	}
6972
-	if !strings.Contains(out, "\nJohn\n") {
6973
-		c.Fatalf("Line with 'John' not found in output %q", out)
6974
-	}
6966
+		RUN Write-Host John`)).Assert(c, icmd.Expected{
6967
+		Out: "\nJohn\n",
6968
+	})
6975 6969
 }
6976 6970
 
6977 6971
 // Verify that escape is being correctly applied to words when escape directive is not \.
... ...
@@ -6979,48 +5630,32 @@ func (s *DockerSuite) TestBuildShellWindowsPowershell(c *check.C) {
6979 6979
 func (s *DockerSuite) TestBuildEscapeNotBackslashWordTest(c *check.C) {
6980 6980
 	testRequires(c, DaemonIsWindows)
6981 6981
 	name := "testbuildescapenotbackslashwordtesta"
6982
-	_, out, err := buildImageWithOut(name,
6983
-		`# escape= `+"`"+`
6982
+	buildImageNew(name, withDockerfile(`# escape= `+"`"+`
6984 6983
 		FROM `+minimalBaseImage()+`
6985 6984
         WORKDIR c:\windows
6986
-		RUN dir /w`,
6987
-		true)
6988
-	if err != nil {
6989
-		c.Fatal(err)
6990
-	}
6991
-	if !strings.Contains(strings.ToLower(out), "[system32]") {
6992
-		c.Fatalf("Line with '[windows]' not found in output %q", out)
6993
-	}
6985
+		RUN dir /w`)).Assert(c, icmd.Expected{
6986
+		Out: "[System32]",
6987
+	})
6994 6988
 
6995 6989
 	name = "testbuildescapenotbackslashwordtestb"
6996
-	_, out, err = buildImageWithOut(name,
6997
-		`# escape= `+"`"+`
6990
+	buildImageNew(name, withDockerfile(`# escape= `+"`"+`
6998 6991
 		FROM `+minimalBaseImage()+`
6999 6992
 		SHELL ["powershell.exe"]
7000 6993
         WORKDIR c:\foo
7001 6994
 		ADD Dockerfile c:\foo\
7002
-		RUN dir Dockerfile`,
7003
-		true)
7004
-	if err != nil {
7005
-		c.Fatal(err)
7006
-	}
7007
-	if !strings.Contains(strings.ToLower(out), "-a----") {
7008
-		c.Fatalf("Line with '-a----' not found in output %q", out)
7009
-	}
7010
-
6995
+		RUN dir Dockerfile`)).Assert(c, icmd.Expected{
6996
+		Out: "-a----",
6997
+	})
7011 6998
 }
7012 6999
 
7013 7000
 // #22868. Make sure shell-form CMD is marked as escaped in the config of the image
7014 7001
 func (s *DockerSuite) TestBuildCmdShellArgsEscaped(c *check.C) {
7015 7002
 	testRequires(c, DaemonIsWindows)
7016 7003
 	name := "testbuildcmdshellescaped"
7017
-	_, err := buildImage(name, `
7004
+	buildImageSuccessfully(c, name, withDockerfile(`
7018 7005
   FROM `+minimalBaseImage()+`
7019 7006
   CMD "ipconfig"
7020
-  `, true)
7021
-	if err != nil {
7022
-		c.Fatal(err)
7023
-	}
7007
+  `))
7024 7008
 	res := inspectFieldJSON(c, name, "Config.ArgsEscaped")
7025 7009
 	if res != "true" {
7026 7010
 		c.Fatalf("CMD did not update Config.ArgsEscaped on image: %v", res)
... ...
@@ -7037,13 +5672,12 @@ func (s *DockerSuite) TestBuildCmdShellArgsEscaped(c *check.C) {
7037 7037
 // Test case for #24912.
7038 7038
 func (s *DockerSuite) TestBuildStepsWithProgress(c *check.C) {
7039 7039
 	name := "testbuildstepswithprogress"
7040
-
7041 7040
 	totalRun := 5
7042
-	_, out, err := buildImageWithOut(name, "FROM busybox\n"+strings.Repeat("RUN echo foo\n", totalRun), true)
7043
-	c.Assert(err, checker.IsNil)
7044
-	c.Assert(out, checker.Contains, fmt.Sprintf("Step 1/%d : FROM busybox", 1+totalRun))
7041
+	result := buildImageNew(name, withDockerfile("FROM busybox\n"+strings.Repeat("RUN echo foo\n", totalRun)))
7042
+	result.Assert(c, icmd.Success)
7043
+	c.Assert(result.Combined(), checker.Contains, fmt.Sprintf("Step 1/%d : FROM busybox", 1+totalRun))
7045 7044
 	for i := 2; i <= 1+totalRun; i++ {
7046
-		c.Assert(out, checker.Contains, fmt.Sprintf("Step %d/%d : RUN echo foo", i, 1+totalRun))
7045
+		c.Assert(result.Combined(), checker.Contains, fmt.Sprintf("Step %d/%d : RUN echo foo", i, 1+totalRun))
7047 7046
 	}
7048 7047
 }
7049 7048
 
... ...
@@ -7051,18 +5685,18 @@ func (s *DockerSuite) TestBuildWithFailure(c *check.C) {
7051 7051
 	name := "testbuildwithfailure"
7052 7052
 
7053 7053
 	// First test case can only detect `nobody` in runtime so all steps will show up
7054
-	buildCmd := "FROM busybox\nRUN nobody"
7055
-	_, stdout, _, err := buildImageWithStdoutStderr(name, buildCmd, false, "--force-rm", "--rm")
7056
-	c.Assert(err, checker.NotNil)
7057
-	c.Assert(stdout, checker.Contains, "Step 1/2 : FROM busybox")
7058
-	c.Assert(stdout, checker.Contains, "Step 2/2 : RUN nobody")
7054
+	dockerfile := "FROM busybox\nRUN nobody"
7055
+	result := buildImageNew(name, withDockerfile(dockerfile))
7056
+	c.Assert(result.Error, checker.NotNil)
7057
+	c.Assert(result.Stdout(), checker.Contains, "Step 1/2 : FROM busybox")
7058
+	c.Assert(result.Stdout(), checker.Contains, "Step 2/2 : RUN nobody")
7059 7059
 
7060 7060
 	// Second test case `FFOM` should have been detected before build runs so no steps
7061
-	buildCmd = "FFOM nobody\nRUN nobody"
7062
-	_, stdout, _, err = buildImageWithStdoutStderr(name, buildCmd, false, "--force-rm", "--rm")
7063
-	c.Assert(err, checker.NotNil)
7064
-	c.Assert(stdout, checker.Not(checker.Contains), "Step 1/2 : FROM busybox")
7065
-	c.Assert(stdout, checker.Not(checker.Contains), "Step 2/2 : RUN nobody")
7061
+	dockerfile = "FFOM nobody\nRUN nobody"
7062
+	result = buildImageNew(name, withDockerfile(dockerfile))
7063
+	c.Assert(result.Error, checker.NotNil)
7064
+	c.Assert(result.Stdout(), checker.Not(checker.Contains), "Step 1/2 : FROM busybox")
7065
+	c.Assert(result.Stdout(), checker.Not(checker.Contains), "Step 2/2 : RUN nobody")
7066 7066
 }
7067 7067
 
7068 7068
 func (s *DockerSuite) TestBuildCacheFrom(c *check.C) {
... ...
@@ -7160,14 +5794,14 @@ func (s *DockerSuite) TestBuildCacheFrom(c *check.C) {
7160 7160
 
7161 7161
 func (s *DockerSuite) TestBuildNetNone(c *check.C) {
7162 7162
 	testRequires(c, DaemonIsLinux)
7163
-
7164 7163
 	name := "testbuildnetnone"
7165
-	_, out, err := buildImageWithOut(name, `
7164
+	buildImageNew(name, withBuildFlags("--network=none"), withDockerfile(`
7166 7165
   FROM busybox
7167 7166
   RUN ping -c 1 8.8.8.8
7168
-  `, true, "--network=none")
7169
-	c.Assert(err, checker.NotNil)
7170
-	c.Assert(out, checker.Contains, "unreachable")
7167
+  `)).Assert(c, icmd.Expected{
7168
+		ExitCode: 1,
7169
+		Out:      "unreachable",
7170
+	})
7171 7171
 }
7172 7172
 
7173 7173
 func (s *DockerSuite) TestBuildNetContainer(c *check.C) {
... ...
@@ -7176,11 +5810,11 @@ func (s *DockerSuite) TestBuildNetContainer(c *check.C) {
7176 7176
 	id, _ := dockerCmd(c, "run", "--hostname", "foobar", "-d", "busybox", "nc", "-ll", "-p", "1234", "-e", "hostname")
7177 7177
 
7178 7178
 	name := "testbuildnetcontainer"
7179
-	out, err := buildImage(name, `
7179
+	buildImageSuccessfully(c, name, withBuildFlags("--network=container:"+strings.TrimSpace(id)),
7180
+		withDockerfile(`
7180 7181
   FROM busybox
7181 7182
   RUN nc localhost 1234 > /otherhost
7182
-  `, true, "--network=container:"+strings.TrimSpace(id))
7183
-	c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
7183
+  `))
7184 7184
 
7185 7185
 	host, _ := dockerCmd(c, "run", "testbuildnetcontainer", "cat", "/otherhost")
7186 7186
 	c.Assert(strings.TrimSpace(host), check.Equals, "foobar")
... ...
@@ -7197,11 +5831,14 @@ func (s *DockerSuite) TestBuildSquashParent(c *check.C) {
7197 7197
 		RUN rm /remove_me
7198 7198
 		`
7199 7199
 	// build and get the ID that we can use later for history comparison
7200
-	origID, err := buildImage("test", dockerFile, false)
7200
+	name := "test"
7201
+	buildImageSuccessfully(c, name, withDockerfile(dockerFile))
7202
+	origID, err := getIDByName(name)
7201 7203
 	c.Assert(err, checker.IsNil)
7202 7204
 
7203 7205
 	// build with squash
7204
-	id, err := buildImage("test", dockerFile, true, "--squash")
7206
+	buildImageSuccessfully(c, name, withBuildFlags("--squash"), withDockerfile(dockerFile))
7207
+	id, err := getIDByName(name)
7205 7208
 	c.Assert(err, checker.IsNil)
7206 7209
 
7207 7210
 	out, _ := dockerCmd(c, "run", "--rm", id, "/bin/sh", "-c", "cat /hello")
... ...
@@ -7230,31 +5867,27 @@ func (s *DockerSuite) TestBuildSquashParent(c *check.C) {
7230 7230
 func (s *DockerSuite) TestBuildContChar(c *check.C) {
7231 7231
 	name := "testbuildcontchar"
7232 7232
 
7233
-	_, out, err := buildImageWithOut(name,
7234
-		`FROM busybox\`, true)
7235
-	c.Assert(err, checker.IsNil)
7236
-	c.Assert(out, checker.Contains, "Step 1/1 : FROM busybox")
7233
+	buildImageNew(name, withDockerfile(`FROM busybox\`)).Assert(c, icmd.Expected{
7234
+		Out: "Step 1/1 : FROM busybox",
7235
+	})
7237 7236
 
7238
-	_, out, err = buildImageWithOut(name,
7239
-		`FROM busybox
7240
-		 RUN echo hi \`, true)
7241
-	c.Assert(err, checker.IsNil)
7242
-	c.Assert(out, checker.Contains, "Step 1/2 : FROM busybox")
7243
-	c.Assert(out, checker.Contains, "Step 2/2 : RUN echo hi\n")
7237
+	result := buildImageNew(name, withDockerfile(`FROM busybox
7238
+		 RUN echo hi \`))
7239
+	result.Assert(c, icmd.Success)
7240
+	c.Assert(result.Combined(), checker.Contains, "Step 1/2 : FROM busybox")
7241
+	c.Assert(result.Combined(), checker.Contains, "Step 2/2 : RUN echo hi\n")
7244 7242
 
7245
-	_, out, err = buildImageWithOut(name,
7246
-		`FROM busybox
7247
-		 RUN echo hi \\`, true)
7248
-	c.Assert(err, checker.IsNil)
7249
-	c.Assert(out, checker.Contains, "Step 1/2 : FROM busybox")
7250
-	c.Assert(out, checker.Contains, "Step 2/2 : RUN echo hi \\\n")
7243
+	result = buildImageNew(name, withDockerfile(`FROM busybox
7244
+		 RUN echo hi \\`))
7245
+	result.Assert(c, icmd.Success)
7246
+	c.Assert(result.Combined(), checker.Contains, "Step 1/2 : FROM busybox")
7247
+	c.Assert(result.Combined(), checker.Contains, "Step 2/2 : RUN echo hi \\\n")
7251 7248
 
7252
-	_, out, err = buildImageWithOut(name,
7253
-		`FROM busybox
7254
-		 RUN echo hi \\\`, true)
7255
-	c.Assert(err, checker.IsNil)
7256
-	c.Assert(out, checker.Contains, "Step 1/2 : FROM busybox")
7257
-	c.Assert(out, checker.Contains, "Step 2/2 : RUN echo hi \\\\\n")
7249
+	result = buildImageNew(name, withDockerfile(`FROM busybox
7250
+		 RUN echo hi \\\`))
7251
+	result.Assert(c, icmd.Success)
7252
+	c.Assert(result.Combined(), checker.Contains, "Step 1/2 : FROM busybox")
7253
+	c.Assert(result.Combined(), checker.Contains, "Step 2/2 : RUN echo hi \\\\\n")
7258 7254
 }
7259 7255
 
7260 7256
 // TestBuildOpaqueDirectory tests that a build succeeds which
... ...
@@ -7262,7 +5895,6 @@ func (s *DockerSuite) TestBuildContChar(c *check.C) {
7262 7262
 // See https://github.com/docker/docker/issues/25244
7263 7263
 func (s *DockerSuite) TestBuildOpaqueDirectory(c *check.C) {
7264 7264
 	testRequires(c, DaemonIsLinux)
7265
-
7266 7265
 	dockerFile := `
7267 7266
 		FROM busybox
7268 7267
 		RUN mkdir /dir1 && touch /dir1/f1
... ...
@@ -7270,28 +5902,22 @@ func (s *DockerSuite) TestBuildOpaqueDirectory(c *check.C) {
7270 7270
 		RUN touch /dir1/f3
7271 7271
 		RUN [ -f /dir1/f2 ]
7272 7272
 		`
7273
-
7274 7273
 	// Test that build succeeds, last command fails if opaque directory
7275 7274
 	// was not handled correctly
7276
-	_, err := buildImage("testopaquedirectory", dockerFile, false)
7277
-	c.Assert(err, checker.IsNil)
7275
+	buildImageSuccessfully(c, "testopaquedirectory", withDockerfile(dockerFile))
7278 7276
 }
7279 7277
 
7280 7278
 // Windows test for USER in dockerfile
7281 7279
 func (s *DockerSuite) TestBuildWindowsUser(c *check.C) {
7282 7280
 	testRequires(c, DaemonIsWindows)
7283 7281
 	name := "testbuildwindowsuser"
7284
-	_, out, err := buildImageWithOut(name,
7285
-		`FROM `+WindowsBaseImage+`
7282
+	buildImageNew(name, withDockerfile(`FROM `+WindowsBaseImage+`
7286 7283
 		RUN net user user /add
7287 7284
 		USER user
7288 7285
 		RUN set username
7289
-		`,
7290
-		true)
7291
-	if err != nil {
7292
-		c.Fatal(err)
7293
-	}
7294
-	c.Assert(strings.ToLower(out), checker.Contains, "username=user")
7286
+		`)).Assert(c, icmd.Expected{
7287
+		Out: "USERNAME=user",
7288
+	})
7295 7289
 }
7296 7290
 
7297 7291
 // Verifies if COPY file . when WORKDIR is set to a non-existing directory,
... ...
@@ -7301,39 +5927,26 @@ func (s *DockerSuite) TestBuildWindowsUser(c *check.C) {
7301 7301
 // Note 27545 was reverted in 28505, but a new fix was added subsequently in 28514.
7302 7302
 func (s *DockerSuite) TestBuildCopyFileDotWithWorkdir(c *check.C) {
7303 7303
 	name := "testbuildcopyfiledotwithworkdir"
7304
-	ctx, err := fakeContext(`FROM busybox 
7305
-WORKDIR /foo 
7306
-COPY file . 
7307
-RUN ["cat", "/foo/file"] 
7308
-`,
7309
-		map[string]string{})
7310
-
7311
-	if err != nil {
7312
-		c.Fatal(err)
7313
-	}
7314
-	defer ctx.Close()
7315
-
7316
-	if err := ctx.Add("file", "content"); err != nil {
7317
-		c.Fatal(err)
7318
-	}
7319
-
7320
-	if _, err = buildImageFromContext(name, ctx, true); err != nil {
7321
-		c.Fatal(err)
7322
-	}
7304
+	buildImageSuccessfully(c, name, withBuildContext(c,
7305
+		withFile("Dockerfile", `FROM busybox
7306
+WORKDIR /foo
7307
+COPY file .
7308
+RUN ["cat", "/foo/file"]
7309
+`),
7310
+		withFile("file", "content"),
7311
+	))
7323 7312
 }
7324 7313
 
7325 7314
 // Case-insensitive environment variables on Windows
7326 7315
 func (s *DockerSuite) TestBuildWindowsEnvCaseInsensitive(c *check.C) {
7327 7316
 	testRequires(c, DaemonIsWindows)
7328 7317
 	name := "testbuildwindowsenvcaseinsensitive"
7329
-	if _, err := buildImage(name, `
7318
+	buildImageSuccessfully(c, name, withDockerfile(`
7330 7319
 		FROM `+WindowsBaseImage+`
7331
-		ENV FOO=bar foo=bar
7332
-  `, true); err != nil {
7333
-		c.Fatal(err)
7334
-	}
7320
+		ENV FOO=bar foo=baz
7321
+  `))
7335 7322
 	res := inspectFieldJSON(c, name, "Config.Env")
7336
-	if res != `["foo=bar"]` { // Should not have FOO=bar in it - takes the last one processed. And only one entry as deduped.
7323
+	if res != `["foo=baz"]` { // Should not have FOO=bar in it - takes the last one processed. And only one entry as deduped.
7337 7324
 		c.Fatalf("Case insensitive environment variables on Windows failed. Got %s", res)
7338 7325
 	}
7339 7326
 }
... ...
@@ -7341,14 +5954,11 @@ func (s *DockerSuite) TestBuildWindowsEnvCaseInsensitive(c *check.C) {
7341 7341
 // Test case for 29667
7342 7342
 func (s *DockerSuite) TestBuildWorkdirImageCmd(c *check.C) {
7343 7343
 	image := "testworkdirimagecmd"
7344
-	dockerfile := `
7344
+	buildImageSuccessfully(c, image, withDockerfile(`
7345 7345
 FROM busybox
7346 7346
 WORKDIR /foo/bar
7347
-`
7348
-	out, err := buildImage(image, dockerfile, true)
7349
-	c.Assert(err, checker.IsNil, check.Commentf("Output: %s", out))
7350
-
7351
-	out, _ = dockerCmd(c, "inspect", "--format", "{{ json .Config.Cmd }}", image)
7347
+`))
7348
+	out, _ := dockerCmd(c, "inspect", "--format", "{{ json .Config.Cmd }}", image)
7352 7349
 
7353 7350
 	// The Windows busybox image has a blank `cmd`
7354 7351
 	lookingFor := `["sh"]`
... ...
@@ -7358,13 +5968,11 @@ WORKDIR /foo/bar
7358 7358
 	c.Assert(strings.TrimSpace(out), checker.Equals, lookingFor)
7359 7359
 
7360 7360
 	image = "testworkdirlabelimagecmd"
7361
-	dockerfile = `
7361
+	buildImageSuccessfully(c, image, withDockerfile(`
7362 7362
 FROM busybox
7363 7363
 WORKDIR /foo/bar
7364 7364
 LABEL a=b
7365
-`
7366
-	out, err = buildImage(image, dockerfile, true)
7367
-	c.Assert(err, checker.IsNil, check.Commentf("Output: %s", out))
7365
+`))
7368 7366
 
7369 7367
 	out, _ = dockerCmd(c, "inspect", "--format", "{{ json .Config.Cmd }}", image)
7370 7368
 	c.Assert(strings.TrimSpace(out), checker.Equals, lookingFor)
... ...
@@ -7373,47 +5981,46 @@ LABEL a=b
7373 7373
 // Test case for 28902/28909
7374 7374
 func (s *DockerSuite) TestBuildWorkdirCmd(c *check.C) {
7375 7375
 	testRequires(c, DaemonIsLinux)
7376
-
7376
+	name := "testbuildworkdircmd"
7377 7377
 	dockerFile := `
7378 7378
                 FROM busybox
7379 7379
                 WORKDIR /
7380 7380
                 `
7381
-	_, err := buildImage("testbuildworkdircmd", dockerFile, true)
7382
-	c.Assert(err, checker.IsNil)
7383
-
7384
-	_, out, err := buildImageWithOut("testbuildworkdircmd", dockerFile, true)
7385
-	c.Assert(err, checker.IsNil)
7386
-	c.Assert(strings.Count(out, "Using cache"), checker.Equals, 1)
7381
+	buildImageSuccessfully(c, name, withDockerfile(dockerFile))
7382
+	result := buildImageNew(name, withDockerfile(dockerFile))
7383
+	result.Assert(c, icmd.Success)
7384
+	c.Assert(strings.Count(result.Combined(), "Using cache"), checker.Equals, 1)
7387 7385
 }
7388 7386
 
7387
+// FIXME(vdemeester) should be a unit test
7389 7388
 func (s *DockerSuite) TestBuildLineErrorOnBuild(c *check.C) {
7390 7389
 	name := "test_build_line_error_onbuild"
7391
-
7392
-	_, err := buildImage(name,
7393
-		`FROM busybox
7390
+	buildImageNew(name, withDockerfile(`FROM busybox
7394 7391
   ONBUILD
7395
-  `, true)
7396
-	c.Assert(err.Error(), checker.Contains, "Dockerfile parse error line 2: ONBUILD requires at least one argument")
7392
+  `)).Assert(c, icmd.Expected{
7393
+		ExitCode: 1,
7394
+		Err:      "Dockerfile parse error line 2: ONBUILD requires at least one argument",
7395
+	})
7397 7396
 }
7398 7397
 
7398
+// FIXME(vdemeester) should be a unit test
7399 7399
 func (s *DockerSuite) TestBuildLineErrorUknownInstruction(c *check.C) {
7400 7400
 	name := "test_build_line_error_unknown_instruction"
7401
-
7402
-	_, err := buildImage(name,
7403
-		`FROM busybox
7401
+	buildImageNew(name, withDockerfile(`FROM busybox
7404 7402
   RUN echo hello world
7405 7403
   NOINSTRUCTION echo ba
7406 7404
   RUN echo hello
7407 7405
   ERROR
7408
-  `, true)
7409
-	c.Assert(err.Error(), checker.Contains, "Dockerfile parse error line 3: Unknown instruction: NOINSTRUCTION")
7406
+  `)).Assert(c, icmd.Expected{
7407
+		ExitCode: 1,
7408
+		Err:      "Dockerfile parse error line 3: Unknown instruction: NOINSTRUCTION",
7409
+	})
7410 7410
 }
7411 7411
 
7412
+// FIXME(vdemeester) should be a unit test
7412 7413
 func (s *DockerSuite) TestBuildLineErrorWithEmptyLines(c *check.C) {
7413 7414
 	name := "test_build_line_error_with_empty_lines"
7414
-
7415
-	_, err := buildImage(name,
7416
-		`
7415
+	buildImageNew(name, withDockerfile(`
7417 7416
   FROM busybox
7418 7417
 
7419 7418
   RUN echo hello world
... ...
@@ -7421,19 +6028,22 @@ func (s *DockerSuite) TestBuildLineErrorWithEmptyLines(c *check.C) {
7421 7421
   NOINSTRUCTION echo ba
7422 7422
 
7423 7423
   CMD ["/bin/init"]
7424
-  `, true)
7425
-	c.Assert(err.Error(), checker.Contains, "Dockerfile parse error line 6: Unknown instruction: NOINSTRUCTION")
7424
+  `)).Assert(c, icmd.Expected{
7425
+		ExitCode: 1,
7426
+		Err:      "Dockerfile parse error line 6: Unknown instruction: NOINSTRUCTION",
7427
+	})
7426 7428
 }
7427 7429
 
7430
+// FIXME(vdemeester) should be a unit test
7428 7431
 func (s *DockerSuite) TestBuildLineErrorWithComments(c *check.C) {
7429 7432
 	name := "test_build_line_error_with_comments"
7430
-
7431
-	_, err := buildImage(name,
7432
-		`FROM busybox
7433
+	buildImageNew(name, withDockerfile(`FROM busybox
7433 7434
   # This will print hello world
7434 7435
   # and then ba
7435 7436
   RUN echo hello world
7436
-  RUM echo ba
7437
-  `, true)
7438
-	c.Assert(err.Error(), checker.Contains, "Dockerfile parse error line 5: Unknown instruction: RUM")
7437
+  NOINSTRUCTION echo ba
7438
+  `)).Assert(c, icmd.Expected{
7439
+		ExitCode: 1,
7440
+		Err:      "Dockerfile parse error line 5: Unknown instruction: NOINSTRUCTION",
7441
+	})
7439 7442
 }
... ...
@@ -195,7 +195,7 @@ func (s *DockerSuite) TestCreateLabels(c *check.C) {
195 195
 	dockerCmd(c, "create", "--name", name, "-l", "k1=v1", "--label", "k2=v2", "busybox")
196 196
 
197 197
 	actual := make(map[string]string)
198
-	inspectFieldAndMarshall(c, name, "Config.Labels", &actual)
198
+	inspectFieldAndUnmarshall(c, name, "Config.Labels", &actual)
199 199
 
200 200
 	if !reflect.DeepEqual(expected, actual) {
201 201
 		c.Fatalf("Expected %s got %s", expected, actual)
... ...
@@ -216,7 +216,7 @@ func (s *DockerSuite) TestCreateLabelFromImage(c *check.C) {
216 216
 	dockerCmd(c, "create", "--name", name, "-l", "k2=x", "--label", "k3=v3", imageName)
217 217
 
218 218
 	actual := make(map[string]string)
219
-	inspectFieldAndMarshall(c, name, "Config.Labels", &actual)
219
+	inspectFieldAndUnmarshall(c, name, "Config.Labels", &actual)
220 220
 
221 221
 	if !reflect.DeepEqual(expected, actual) {
222 222
 		c.Fatalf("Expected %s got %s", expected, actual)
... ...
@@ -503,7 +503,7 @@ func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverWithDaemonRestart(c
503 503
 
504 504
 	dockerCmd(c, "run", "--name=test", "-v", "abc1:/foo", "busybox", "true")
505 505
 	var mounts []types.MountPoint
506
-	inspectFieldAndMarshall(c, "test", "Mounts", &mounts)
506
+	inspectFieldAndUnmarshall(c, "test", "Mounts", &mounts)
507 507
 	c.Assert(mounts, checker.HasLen, 1)
508 508
 	c.Assert(mounts[0].Driver, checker.Equals, volumePluginName)
509 509
 }
... ...
@@ -305,17 +305,6 @@ func validateArgs(args ...string) error {
305 305
 	return nil
306 306
 }
307 307
 
308
-// find the State.ExitCode in container metadata
309
-func findContainerExitCode(c *check.C, name string, vargs ...string) string {
310
-	args := append(vargs, "inspect", "--format='{{ .State.ExitCode }} {{ .State.Error }}'", name)
311
-	cmd := exec.Command(dockerBinary, args...)
312
-	out, _, err := runCommandWithOutput(cmd)
313
-	if err != nil {
314
-		c.Fatal(err, out)
315
-	}
316
-	return out
317
-}
318
-
319 308
 func findContainerIP(c *check.C, id string, network string) string {
320 309
 	out, _ := dockerCmd(c, "inspect", fmt.Sprintf("--format='{{ .NetworkSettings.Networks.%s.IPAddress }}'", network), id)
321 310
 	return strings.Trim(out, " \r\n'")
... ...
@@ -582,7 +571,7 @@ COPY . /static`); err != nil {
582 582
 		ctx:       ctx}, nil
583 583
 }
584 584
 
585
-func inspectFieldAndMarshall(c *check.C, name, field string, output interface{}) {
585
+func inspectFieldAndUnmarshall(c *check.C, name, field string, output interface{}) {
586 586
 	str := inspectFieldJSON(c, name, field)
587 587
 	err := json.Unmarshal([]byte(str), output)
588 588
 	if c != nil {
... ...
@@ -592,12 +581,11 @@ func inspectFieldAndMarshall(c *check.C, name, field string, output interface{})
592 592
 
593 593
 func inspectFilter(name, filter string) (string, error) {
594 594
 	format := fmt.Sprintf("{{%s}}", filter)
595
-	inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
596
-	out, exitCode, err := runCommandWithOutput(inspectCmd)
597
-	if err != nil || exitCode != 0 {
598
-		return "", fmt.Errorf("failed to inspect %s: %s", name, out)
595
+	result := icmd.RunCommand(dockerBinary, "inspect", "-f", format, name)
596
+	if result.Error != nil || result.ExitCode != 0 {
597
+		return "", fmt.Errorf("failed to inspect %s: %s", name, result.Combined())
599 598
 	}
600
-	return strings.TrimSpace(out), nil
599
+	return strings.TrimSpace(result.Combined()), nil
601 600
 }
602 601
 
603 602
 func inspectFieldWithError(name, field string) (string, error) {
... ...
@@ -687,10 +675,12 @@ func getIDByName(name string) (string, error) {
687 687
 	return inspectFieldWithError(name, "Id")
688 688
 }
689 689
 
690
+// Deprecated
690 691
 func buildImageCmd(name, dockerfile string, useCache bool, buildFlags ...string) *exec.Cmd {
691 692
 	return daemon.BuildImageCmdWithHost(dockerBinary, name, dockerfile, "", useCache, buildFlags...)
692 693
 }
693 694
 
695
+// Deprecated
694 696
 func buildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, string, error) {
695 697
 	buildCmd := buildImageCmd(name, dockerfile, useCache, buildFlags...)
696 698
 	out, exitCode, err := runCommandWithOutput(buildCmd)
... ...
@@ -704,6 +694,7 @@ func buildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...str
704 704
 	return id, out, nil
705 705
 }
706 706
 
707
+// Deprecated
707 708
 func buildImageWithStdoutStderr(name, dockerfile string, useCache bool, buildFlags ...string) (string, string, string, error) {
708 709
 	buildCmd := buildImageCmd(name, dockerfile, useCache, buildFlags...)
709 710
 	result := icmd.RunCmd(transformCmd(buildCmd))
... ...
@@ -719,11 +710,99 @@ func buildImageWithStdoutStderr(name, dockerfile string, useCache bool, buildFla
719 719
 	return id, result.Stdout(), result.Stderr(), nil
720 720
 }
721 721
 
722
+func buildImageSuccessfully(c *check.C, name string, cmdOperators ...func(*icmd.Cmd) func()) {
723
+	buildImageNew(name, cmdOperators...).Assert(c, icmd.Success)
724
+}
725
+
726
+// FIXME(vdemeester) rename this buildImage once deprecated buildImage is no more
727
+func buildImageNew(name string, cmdOperators ...func(*icmd.Cmd) func()) *icmd.Result {
728
+	cmd := icmd.Command(dockerBinary, "build", "-t", name)
729
+	for _, op := range cmdOperators {
730
+		deferFn := op(&cmd)
731
+		if deferFn != nil {
732
+			defer deferFn()
733
+		}
734
+	}
735
+	return icmd.RunCmd(cmd)
736
+}
737
+
738
+func withBuildContextPath(path string) func(*icmd.Cmd) func() {
739
+	return func(cmd *icmd.Cmd) func() {
740
+		cmd.Command = append(cmd.Command, path)
741
+		return nil
742
+	}
743
+}
744
+
745
+func withBuildContext(c *check.C, contextOperators ...func(*FakeContext) error) func(*icmd.Cmd) func() {
746
+	ctx, err := fakeContextFromNewTempDir()
747
+	if err != nil {
748
+		c.Fatalf("error creating build context : %v", err)
749
+	}
750
+	for _, op := range contextOperators {
751
+		if err := op(ctx); err != nil {
752
+			c.Fatal(err)
753
+		}
754
+	}
755
+	return func(cmd *icmd.Cmd) func() {
756
+		cmd.Dir = ctx.Dir
757
+		cmd.Command = append(cmd.Command, ".")
758
+		return closeBuildContext(c, ctx)
759
+	}
760
+}
761
+
762
+func withBuildFlags(flags ...string) func(*icmd.Cmd) func() {
763
+	return func(cmd *icmd.Cmd) func() {
764
+		cmd.Command = append(cmd.Command, flags...)
765
+		return nil
766
+	}
767
+}
768
+
769
+func withoutCache(cmd *icmd.Cmd) func() {
770
+	cmd.Command = append(cmd.Command, "--no-cache")
771
+	return nil
772
+}
773
+
774
+func withFile(name, content string) func(*FakeContext) error {
775
+	return func(ctx *FakeContext) error {
776
+		return ctx.Add(name, content)
777
+	}
778
+}
779
+
780
+func closeBuildContext(c *check.C, ctx *FakeContext) func() {
781
+	return func() {
782
+		if err := ctx.Close(); err != nil {
783
+			c.Fatal(err)
784
+		}
785
+	}
786
+}
787
+
788
+func withDockerfile(dockerfile string) func(*icmd.Cmd) func() {
789
+	return func(cmd *icmd.Cmd) func() {
790
+		cmd.Command = append(cmd.Command, "-")
791
+		cmd.Stdin = strings.NewReader(dockerfile)
792
+		return nil
793
+	}
794
+}
795
+
796
+func trustedBuild(cmd *icmd.Cmd) func() {
797
+	trustedCmd(cmd)
798
+	return nil
799
+}
800
+
801
+func withEnvironmentVariales(envs ...string) func(cmd *icmd.Cmd) func() {
802
+	return func(cmd *icmd.Cmd) func() {
803
+		cmd.Env = envs
804
+		return nil
805
+	}
806
+}
807
+
808
+// Deprecated
722 809
 func buildImage(name, dockerfile string, useCache bool, buildFlags ...string) (string, error) {
723 810
 	id, _, err := buildImageWithOut(name, dockerfile, useCache, buildFlags...)
724 811
 	return id, err
725 812
 }
726 813
 
814
+// Deprecated
727 815
 func buildImageFromContext(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, error) {
728 816
 	id, _, err := buildImageFromContextWithOut(name, ctx, useCache, buildFlags...)
729 817
 	if err != nil {
... ...
@@ -732,6 +811,7 @@ func buildImageFromContext(name string, ctx *FakeContext, useCache bool, buildFl
732 732
 	return id, nil
733 733
 }
734 734
 
735
+// Deprecated
735 736
 func buildImageFromContextWithOut(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, string, error) {
736 737
 	args := []string{"build", "-t", name}
737 738
 	if !useCache {
... ...
@@ -754,6 +834,7 @@ func buildImageFromContextWithOut(name string, ctx *FakeContext, useCache bool,
754 754
 	return id, out, nil
755 755
 }
756 756
 
757
+// Deprecated
757 758
 func buildImageFromContextWithStdoutStderr(name string, ctx *FakeContext, useCache bool, buildFlags ...string) (string, string, string, error) {
758 759
 	args := []string{"build", "-t", name}
759 760
 	if !useCache {
... ...
@@ -778,6 +859,7 @@ func buildImageFromContextWithStdoutStderr(name string, ctx *FakeContext, useCac
778 778
 	return id, result.Stdout(), result.Stderr(), nil
779 779
 }
780 780
 
781
+// Deprecated
781 782
 func buildImageFromGitWithStdoutStderr(name string, ctx *fakeGit, useCache bool, buildFlags ...string) (string, string, string, error) {
782 783
 	args := []string{"build", "-t", name}
783 784
 	if !useCache {
... ...
@@ -800,6 +882,7 @@ func buildImageFromGitWithStdoutStderr(name string, ctx *fakeGit, useCache bool,
800 800
 	return id, result.Stdout(), result.Stderr(), nil
801 801
 }
802 802
 
803
+// Deprecated
803 804
 func buildImageFromPath(name, path string, useCache bool, buildFlags ...string) (string, error) {
804 805
 	args := []string{"build", "-t", name}
805 806
 	if !useCache {