Browse code

Remove daemon.BuildImageWithOut and use cli helpers function

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

Vincent Demeester authored on 2018/04/18 23:45:55
Showing 8 changed files
... ...
@@ -2,7 +2,6 @@ package daemon // import "github.com/docker/docker/integration-cli/daemon"
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"os/exec"
6 5
 	"strings"
7 6
 	"time"
8 7
 
... ...
@@ -88,19 +87,6 @@ func (d *Daemon) inspectFieldWithError(name, field string) (string, error) {
88 88
 	return d.inspectFilter(name, fmt.Sprintf(".%s", field))
89 89
 }
90 90
 
91
-// BuildImageWithOut builds an image with the specified dockerfile and options and returns the output
92
-func (d *Daemon) BuildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, int, error) {
93
-	buildCmd := BuildImageCmdWithHost(d.dockerBinary, name, dockerfile, d.Sock(), useCache, buildFlags...)
94
-	result := icmd.RunCmd(icmd.Cmd{
95
-		Command: buildCmd.Args,
96
-		Env:     buildCmd.Env,
97
-		Dir:     buildCmd.Dir,
98
-		Stdin:   buildCmd.Stdin,
99
-		Stdout:  buildCmd.Stdout,
100
-	})
101
-	return result.Combined(), result.ExitCode, result.Error
102
-}
103
-
104 91
 // CheckActiveContainerCount returns the number of active containers
105 92
 // FIXME(vdemeester) should re-use ActivateContainers in some way
106 93
 func (d *Daemon) CheckActiveContainerCount(c *check.C) (interface{}, check.CommentInterface) {
... ...
@@ -155,22 +141,3 @@ func WaitInspectWithArgs(dockerBinary, name, expr, expected string, timeout time
155 155
 	}
156 156
 	return nil
157 157
 }
158
-
159
-// BuildImageCmdWithHost create a build command with the specified arguments.
160
-// Deprecated
161
-// FIXME(vdemeester) move this away
162
-func BuildImageCmdWithHost(dockerBinary, name, dockerfile, host string, useCache bool, buildFlags ...string) *exec.Cmd {
163
-	args := []string{}
164
-	if host != "" {
165
-		args = append(args, "--host", host)
166
-	}
167
-	args = append(args, "build", "-t", name)
168
-	if !useCache {
169
-		args = append(args, "--no-cache")
170
-	}
171
-	args = append(args, buildFlags...)
172
-	args = append(args, "-")
173
-	buildCmd := exec.Command(dockerBinary, args...)
174
-	buildCmd.Stdin = strings.NewReader(dockerfile)
175
-	return buildCmd
176
-}
... ...
@@ -13,11 +13,14 @@ import (
13 13
 	"github.com/docker/docker/api/types/swarm"
14 14
 	"github.com/docker/docker/api/types/swarm/runtime"
15 15
 	"github.com/docker/docker/integration-cli/checker"
16
+	"github.com/docker/docker/integration-cli/cli"
17
+	"github.com/docker/docker/integration-cli/cli/build"
16 18
 	"github.com/docker/docker/integration-cli/daemon"
17 19
 	testdaemon "github.com/docker/docker/internal/test/daemon"
18 20
 	"github.com/docker/docker/internal/test/fixtures/plugin"
19 21
 	"github.com/docker/docker/internal/test/registry"
20 22
 	"github.com/go-check/check"
23
+	"github.com/gotestyourself/gotestyourself/icmd"
21 24
 	"golang.org/x/net/context"
22 25
 	"golang.org/x/sys/unix"
23 26
 )
... ...
@@ -209,12 +212,12 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesUpdateStartFirst(c *check.C) {
209 209
 	image2 := "testhealth:latest"
210 210
 
211 211
 	// service started from this image won't pass health check
212
-	_, _, err := d.BuildImageWithOut(image2,
213
-		`FROM busybox
212
+	result := cli.BuildCmd(c, image2, cli.Daemon(d),
213
+		build.WithDockerfile(`FROM busybox
214 214
 		HEALTHCHECK --interval=1s --timeout=30s --retries=1024 \
215
-		  CMD cat /status`,
216
-		true)
217
-	c.Check(err, check.IsNil)
215
+		  CMD cat /status`),
216
+	)
217
+	result.Assert(c, icmd.Success)
218 218
 
219 219
 	// create service
220 220
 	instances := 5
... ...
@@ -31,6 +31,7 @@ import (
31 31
 	moby_daemon "github.com/docker/docker/daemon"
32 32
 	"github.com/docker/docker/integration-cli/checker"
33 33
 	"github.com/docker/docker/integration-cli/cli"
34
+	"github.com/docker/docker/integration-cli/cli/build"
34 35
 	"github.com/docker/docker/integration-cli/daemon"
35 36
 	testdaemon "github.com/docker/docker/internal/test/daemon"
36 37
 	"github.com/docker/docker/opts"
... ...
@@ -1156,14 +1157,16 @@ func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) {
1156 1156
 func (s *DockerDaemonSuite) TestDaemonLoggingDriverShouldBeIgnoredForBuild(c *check.C) {
1157 1157
 	s.d.StartWithBusybox(c, "--log-driver=splunk")
1158 1158
 
1159
-	out, err := s.d.Cmd("build")
1160
-	out, code, err := s.d.BuildImageWithOut("busyboxs", `
1159
+	result := cli.BuildCmd(c, "busyboxs", cli.Daemon(s.d),
1160
+		build.WithDockerfile(`
1161 1161
         FROM busybox
1162
-        RUN echo foo`, false)
1163
-	comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", out, code, err)
1164
-	c.Assert(err, check.IsNil, comment)
1165
-	c.Assert(code, check.Equals, 0, comment)
1166
-	c.Assert(out, checker.Contains, "foo", comment)
1162
+        RUN echo foo`),
1163
+		build.WithoutCache,
1164
+	)
1165
+	comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", result.Combined(), result.ExitCode, result.Error)
1166
+	c.Assert(result.Error, check.IsNil, comment)
1167
+	c.Assert(result.ExitCode, check.Equals, 0, comment)
1168
+	c.Assert(result.Combined(), checker.Contains, "foo", comment)
1167 1169
 }
1168 1170
 
1169 1171
 func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) {
... ...
@@ -2404,12 +2407,16 @@ func (s *DockerDaemonSuite) TestDaemonMaxConcurrencyWithConfigFileReload(c *chec
2404 2404
 
2405 2405
 func (s *DockerDaemonSuite) TestBuildOnDisabledBridgeNetworkDaemon(c *check.C) {
2406 2406
 	s.d.StartWithBusybox(c, "-b=none", "--iptables=false")
2407
-	out, code, err := s.d.BuildImageWithOut("busyboxs",
2408
-		`FROM busybox
2409
-                RUN cat /etc/hosts`, false)
2410
-	comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", out, code, err)
2411
-	c.Assert(err, check.IsNil, comment)
2412
-	c.Assert(code, check.Equals, 0, comment)
2407
+
2408
+	result := cli.BuildCmd(c, "busyboxs", cli.Daemon(s.d),
2409
+		build.WithDockerfile(`
2410
+        FROM busybox
2411
+        RUN cat /etc/hosts`),
2412
+		build.WithoutCache,
2413
+	)
2414
+	comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", result.Combined(), result.ExitCode, result.Error)
2415
+	c.Assert(result.Error, check.IsNil, comment)
2416
+	c.Assert(result.ExitCode, check.Equals, 0, comment)
2413 2417
 }
2414 2418
 
2415 2419
 // Test case for #21976
... ...
@@ -12,8 +12,10 @@ import (
12 12
 
13 13
 	"github.com/docker/docker/integration-cli/checker"
14 14
 	"github.com/docker/docker/integration-cli/cli"
15
+	"github.com/docker/docker/integration-cli/cli/build"
15 16
 	"github.com/docker/docker/integration-cli/daemon"
16 17
 	"github.com/go-check/check"
18
+	"github.com/gotestyourself/gotestyourself/icmd"
17 19
 )
18 20
 
19 21
 func pruneNetworkAndVerify(c *check.C, d *daemon.Daemon, kept, pruned []string) {
... ...
@@ -79,13 +81,15 @@ func (s *DockerSwarmSuite) TestPruneNetwork(c *check.C) {
79 79
 func (s *DockerDaemonSuite) TestPruneImageDangling(c *check.C) {
80 80
 	s.d.StartWithBusybox(c)
81 81
 
82
-	out, _, err := s.d.BuildImageWithOut("test",
83
-		`FROM busybox
84
-                 LABEL foo=bar`, true, "-q")
85
-	c.Assert(err, checker.IsNil)
86
-	id := strings.TrimSpace(out)
82
+	result := cli.BuildCmd(c, "test", cli.Daemon(s.d),
83
+		build.WithDockerfile(`FROM busybox
84
+                 LABEL foo=bar`),
85
+		cli.WithFlags("-q"),
86
+	)
87
+	result.Assert(c, icmd.Success)
88
+	id := strings.TrimSpace(result.Combined())
87 89
 
88
-	out, err = s.d.Cmd("images", "-q", "--no-trunc")
90
+	out, err := s.d.Cmd("images", "-q", "--no-trunc")
89 91
 	c.Assert(err, checker.IsNil)
90 92
 	c.Assert(strings.TrimSpace(out), checker.Contains, id)
91 93
 
... ...
@@ -266,20 +270,24 @@ func (s *DockerSuite) TestPruneNetworkLabel(c *check.C) {
266 266
 func (s *DockerDaemonSuite) TestPruneImageLabel(c *check.C) {
267 267
 	s.d.StartWithBusybox(c)
268 268
 
269
-	out, _, err := s.d.BuildImageWithOut("test1",
270
-		`FROM busybox
271
-                 LABEL foo=bar`, true, "-q")
272
-	c.Assert(err, checker.IsNil)
273
-	id1 := strings.TrimSpace(out)
274
-	out, err = s.d.Cmd("images", "-q", "--no-trunc")
269
+	result := cli.BuildCmd(c, "test1", cli.Daemon(s.d),
270
+		build.WithDockerfile(`FROM busybox
271
+                 LABEL foo=bar`),
272
+		cli.WithFlags("-q"),
273
+	)
274
+	result.Assert(c, icmd.Success)
275
+	id1 := strings.TrimSpace(result.Combined())
276
+	out, err := s.d.Cmd("images", "-q", "--no-trunc")
275 277
 	c.Assert(err, checker.IsNil)
276 278
 	c.Assert(strings.TrimSpace(out), checker.Contains, id1)
277 279
 
278
-	out, _, err = s.d.BuildImageWithOut("test2",
279
-		`FROM busybox
280
-                 LABEL bar=foo`, true, "-q")
281
-	c.Assert(err, checker.IsNil)
282
-	id2 := strings.TrimSpace(out)
280
+	result = cli.BuildCmd(c, "test2", cli.Daemon(s.d),
281
+		build.WithDockerfile(`FROM busybox
282
+                 LABEL bar=foo`),
283
+		cli.WithFlags("-q"),
284
+	)
285
+	result.Assert(c, icmd.Success)
286
+	id2 := strings.TrimSpace(result.Combined())
283 287
 	out, err = s.d.Cmd("images", "-q", "--no-trunc")
284 288
 	c.Assert(err, checker.IsNil)
285 289
 	c.Assert(strings.TrimSpace(out), checker.Contains, id2)
... ...
@@ -9,7 +9,10 @@ import (
9 9
 	"github.com/docker/docker/api/types/swarm"
10 10
 	"github.com/docker/docker/daemon/cluster/executor/container"
11 11
 	"github.com/docker/docker/integration-cli/checker"
12
+	"github.com/docker/docker/integration-cli/cli"
13
+	"github.com/docker/docker/integration-cli/cli/build"
12 14
 	"github.com/go-check/check"
15
+	"github.com/gotestyourself/gotestyourself/icmd"
13 16
 )
14 17
 
15 18
 // start a service, and then make its task unhealthy during running
... ...
@@ -20,15 +23,14 @@ func (s *DockerSwarmSuite) TestServiceHealthRun(c *check.C) {
20 20
 	d := s.AddDaemon(c, true, true)
21 21
 
22 22
 	// build image with health-check
23
-	// note: use `daemon.buildImageWithOut` to build, do not use `buildImage` to build
24 23
 	imageName := "testhealth"
25
-	_, _, err := d.BuildImageWithOut(imageName,
26
-		`FROM busybox
24
+	result := cli.BuildCmd(c, imageName, cli.Daemon(d),
25
+		build.WithDockerfile(`FROM busybox
27 26
 		RUN touch /status
28 27
 		HEALTHCHECK --interval=1s --timeout=1s --retries=1\
29
-		  CMD cat /status`,
30
-		true)
31
-	c.Check(err, check.IsNil)
28
+		  CMD cat /status`),
29
+	)
30
+	result.Assert(c, icmd.Success)
32 31
 
33 32
 	serviceName := "healthServiceRun"
34 33
 	out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--name", serviceName, imageName, "top")
... ...
@@ -84,12 +86,12 @@ func (s *DockerSwarmSuite) TestServiceHealthStart(c *check.C) {
84 84
 
85 85
 	// service started from this image won't pass health check
86 86
 	imageName := "testhealth"
87
-	_, _, err := d.BuildImageWithOut(imageName,
88
-		`FROM busybox
87
+	result := cli.BuildCmd(c, imageName, cli.Daemon(d),
88
+		build.WithDockerfile(`FROM busybox
89 89
 		HEALTHCHECK --interval=1s --timeout=1s --retries=1024\
90
-		  CMD cat /status`,
91
-		true)
92
-	c.Check(err, check.IsNil)
90
+		  CMD cat /status`),
91
+	)
92
+	result.Assert(c, icmd.Success)
93 93
 
94 94
 	serviceName := "healthServiceStart"
95 95
 	out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--name", serviceName, imageName, "top")
... ...
@@ -179,11 +179,8 @@ func (d *Daemon) NewClientT(t assert.TestingT) *client.Client {
179 179
 // Cleanup cleans the daemon files : exec root (network namespaces, ...), swarmkit files
180 180
 func (d *Daemon) Cleanup(t testingT) {
181 181
 	// Cleanup swarmkit wal files if present
182
-	walDir := filepath.Join(d.Root, "swarm/raft/wal")
183
-	if err := os.RemoveAll(walDir); err != nil {
184
-		t.Logf("error removing %v: %v", walDir, err)
185
-	}
186
-	cleanupExecRoot(t, d.execRoot)
182
+	cleanupRaftDir(t, d.Root)
183
+	cleanupNetworkNamespace(t, d.execRoot)
187 184
 }
188 185
 
189 186
 // Start starts the daemon and return once it is ready to receive requests.
... ...
@@ -640,3 +637,10 @@ func (d *Daemon) Info(t assert.TestingT) types.Info {
640 640
 	assert.NilError(t, err)
641 641
 	return info
642 642
 }
643
+
644
+func cleanupRaftDir(t testingT, rootPath string) {
645
+	walDir := filepath.Join(rootPath, "swarm/raft/wal")
646
+	if err := os.RemoveAll(walDir); err != nil {
647
+		t.Logf("error removing %v: %v", walDir, err)
648
+	}
649
+}
... ...
@@ -9,7 +9,7 @@ import (
9 9
 	"golang.org/x/sys/unix"
10 10
 )
11 11
 
12
-func cleanupExecRoot(t testingT, execRoot string) {
12
+func cleanupNetworkNamespace(t testingT, execRoot string) {
13 13
 	// Cleanup network namespaces in the exec root of this
14 14
 	// daemon because this exec root is specific to this
15 15
 	// daemon instance and has no chance of getting
... ...
@@ -21,5 +21,5 @@ func signalDaemonReload(pid int) error {
21 21
 	return fmt.Errorf("daemon reload not supported")
22 22
 }
23 23
 
24
-func cleanupExecRoot(t testingT, execRoot string) {
24
+func cleanupNetworkNamespace(t testingT, execRoot string) {
25 25
 }