diff --git a/integration-cli/daemon/daemon.go b/integration-cli/daemon/daemon.go index 951cbe31f9..fcbbfdfb0c 100644 --- a/integration-cli/daemon/daemon.go +++ b/integration-cli/daemon/daemon.go @@ -2,7 +2,6 @@ package daemon // import "github.com/docker/docker/integration-cli/daemon" import ( "fmt" - "os/exec" "strings" "time" @@ -88,19 +87,6 @@ func (d *Daemon) inspectFieldWithError(name, field string) (string, error) { return d.inspectFilter(name, fmt.Sprintf(".%s", field)) } -// BuildImageWithOut builds an image with the specified dockerfile and options and returns the output -func (d *Daemon) BuildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, int, error) { - buildCmd := BuildImageCmdWithHost(d.dockerBinary, name, dockerfile, d.Sock(), useCache, buildFlags...) - result := icmd.RunCmd(icmd.Cmd{ - Command: buildCmd.Args, - Env: buildCmd.Env, - Dir: buildCmd.Dir, - Stdin: buildCmd.Stdin, - Stdout: buildCmd.Stdout, - }) - return result.Combined(), result.ExitCode, result.Error -} - // CheckActiveContainerCount returns the number of active containers // FIXME(vdemeester) should re-use ActivateContainers in some way func (d *Daemon) CheckActiveContainerCount(c *check.C) (interface{}, check.CommentInterface) { @@ -155,22 +141,3 @@ func WaitInspectWithArgs(dockerBinary, name, expr, expected string, timeout time } return nil } - -// BuildImageCmdWithHost create a build command with the specified arguments. -// Deprecated -// FIXME(vdemeester) move this away -func BuildImageCmdWithHost(dockerBinary, name, dockerfile, host string, useCache bool, buildFlags ...string) *exec.Cmd { - args := []string{} - if host != "" { - args = append(args, "--host", host) - } - args = append(args, "build", "-t", name) - if !useCache { - args = append(args, "--no-cache") - } - args = append(args, buildFlags...) - args = append(args, "-") - buildCmd := exec.Command(dockerBinary, args...) - buildCmd.Stdin = strings.NewReader(dockerfile) - return buildCmd -} diff --git a/integration-cli/docker_api_swarm_service_test.go b/integration-cli/docker_api_swarm_service_test.go index 25572a0ed8..8c64c754a2 100644 --- a/integration-cli/docker_api_swarm_service_test.go +++ b/integration-cli/docker_api_swarm_service_test.go @@ -13,11 +13,14 @@ import ( "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm/runtime" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/cli" + "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/daemon" testdaemon "github.com/docker/docker/internal/test/daemon" "github.com/docker/docker/internal/test/fixtures/plugin" "github.com/docker/docker/internal/test/registry" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "golang.org/x/net/context" "golang.org/x/sys/unix" ) @@ -209,12 +212,12 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesUpdateStartFirst(c *check.C) { image2 := "testhealth:latest" // service started from this image won't pass health check - _, _, err := d.BuildImageWithOut(image2, - `FROM busybox + result := cli.BuildCmd(c, image2, cli.Daemon(d), + build.WithDockerfile(`FROM busybox HEALTHCHECK --interval=1s --timeout=30s --retries=1024 \ - CMD cat /status`, - true) - c.Check(err, check.IsNil) + CMD cat /status`), + ) + result.Assert(c, icmd.Success) // create service instances := 5 diff --git a/integration-cli/docker_cli_daemon_test.go b/integration-cli/docker_cli_daemon_test.go index b9ed41ca96..22beb58a68 100644 --- a/integration-cli/docker_cli_daemon_test.go +++ b/integration-cli/docker_cli_daemon_test.go @@ -31,6 +31,7 @@ import ( moby_daemon "github.com/docker/docker/daemon" "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" + "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/daemon" testdaemon "github.com/docker/docker/internal/test/daemon" "github.com/docker/docker/opts" @@ -1156,14 +1157,16 @@ func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) { func (s *DockerDaemonSuite) TestDaemonLoggingDriverShouldBeIgnoredForBuild(c *check.C) { s.d.StartWithBusybox(c, "--log-driver=splunk") - out, err := s.d.Cmd("build") - out, code, err := s.d.BuildImageWithOut("busyboxs", ` + result := cli.BuildCmd(c, "busyboxs", cli.Daemon(s.d), + build.WithDockerfile(` FROM busybox - RUN echo foo`, false) - comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", out, code, err) - c.Assert(err, check.IsNil, comment) - c.Assert(code, check.Equals, 0, comment) - c.Assert(out, checker.Contains, "foo", comment) + RUN echo foo`), + build.WithoutCache, + ) + comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", result.Combined(), result.ExitCode, result.Error) + c.Assert(result.Error, check.IsNil, comment) + c.Assert(result.ExitCode, check.Equals, 0, comment) + c.Assert(result.Combined(), checker.Contains, "foo", comment) } func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) { @@ -2404,12 +2407,16 @@ func (s *DockerDaemonSuite) TestDaemonMaxConcurrencyWithConfigFileReload(c *chec func (s *DockerDaemonSuite) TestBuildOnDisabledBridgeNetworkDaemon(c *check.C) { s.d.StartWithBusybox(c, "-b=none", "--iptables=false") - out, code, err := s.d.BuildImageWithOut("busyboxs", - `FROM busybox - RUN cat /etc/hosts`, false) - comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", out, code, err) - c.Assert(err, check.IsNil, comment) - c.Assert(code, check.Equals, 0, comment) + + result := cli.BuildCmd(c, "busyboxs", cli.Daemon(s.d), + build.WithDockerfile(` + FROM busybox + RUN cat /etc/hosts`), + build.WithoutCache, + ) + comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", result.Combined(), result.ExitCode, result.Error) + c.Assert(result.Error, check.IsNil, comment) + c.Assert(result.ExitCode, check.Equals, 0, comment) } // Test case for #21976 diff --git a/integration-cli/docker_cli_prune_unix_test.go b/integration-cli/docker_cli_prune_unix_test.go index bc5bdc835e..259b486766 100644 --- a/integration-cli/docker_cli_prune_unix_test.go +++ b/integration-cli/docker_cli_prune_unix_test.go @@ -12,8 +12,10 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" + "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/daemon" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func pruneNetworkAndVerify(c *check.C, d *daemon.Daemon, kept, pruned []string) { @@ -79,13 +81,15 @@ func (s *DockerSwarmSuite) TestPruneNetwork(c *check.C) { func (s *DockerDaemonSuite) TestPruneImageDangling(c *check.C) { s.d.StartWithBusybox(c) - out, _, err := s.d.BuildImageWithOut("test", - `FROM busybox - LABEL foo=bar`, true, "-q") - c.Assert(err, checker.IsNil) - id := strings.TrimSpace(out) + result := cli.BuildCmd(c, "test", cli.Daemon(s.d), + build.WithDockerfile(`FROM busybox + LABEL foo=bar`), + cli.WithFlags("-q"), + ) + result.Assert(c, icmd.Success) + id := strings.TrimSpace(result.Combined()) - out, err = s.d.Cmd("images", "-q", "--no-trunc") + out, err := s.d.Cmd("images", "-q", "--no-trunc") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Contains, id) @@ -266,20 +270,24 @@ func (s *DockerSuite) TestPruneNetworkLabel(c *check.C) { func (s *DockerDaemonSuite) TestPruneImageLabel(c *check.C) { s.d.StartWithBusybox(c) - out, _, err := s.d.BuildImageWithOut("test1", - `FROM busybox - LABEL foo=bar`, true, "-q") - c.Assert(err, checker.IsNil) - id1 := strings.TrimSpace(out) - out, err = s.d.Cmd("images", "-q", "--no-trunc") + result := cli.BuildCmd(c, "test1", cli.Daemon(s.d), + build.WithDockerfile(`FROM busybox + LABEL foo=bar`), + cli.WithFlags("-q"), + ) + result.Assert(c, icmd.Success) + id1 := strings.TrimSpace(result.Combined()) + out, err := s.d.Cmd("images", "-q", "--no-trunc") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Contains, id1) - out, _, err = s.d.BuildImageWithOut("test2", - `FROM busybox - LABEL bar=foo`, true, "-q") - c.Assert(err, checker.IsNil) - id2 := strings.TrimSpace(out) + result = cli.BuildCmd(c, "test2", cli.Daemon(s.d), + build.WithDockerfile(`FROM busybox + LABEL bar=foo`), + cli.WithFlags("-q"), + ) + result.Assert(c, icmd.Success) + id2 := strings.TrimSpace(result.Combined()) out, err = s.d.Cmd("images", "-q", "--no-trunc") c.Assert(err, checker.IsNil) c.Assert(strings.TrimSpace(out), checker.Contains, id2) diff --git a/integration-cli/docker_cli_service_health_test.go b/integration-cli/docker_cli_service_health_test.go index 789838545d..ac525d08ea 100644 --- a/integration-cli/docker_cli_service_health_test.go +++ b/integration-cli/docker_cli_service_health_test.go @@ -9,7 +9,10 @@ import ( "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/daemon/cluster/executor/container" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/cli" + "github.com/docker/docker/integration-cli/cli/build" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) // start a service, and then make its task unhealthy during running @@ -20,15 +23,14 @@ func (s *DockerSwarmSuite) TestServiceHealthRun(c *check.C) { d := s.AddDaemon(c, true, true) // build image with health-check - // note: use `daemon.buildImageWithOut` to build, do not use `buildImage` to build imageName := "testhealth" - _, _, err := d.BuildImageWithOut(imageName, - `FROM busybox + result := cli.BuildCmd(c, imageName, cli.Daemon(d), + build.WithDockerfile(`FROM busybox RUN touch /status HEALTHCHECK --interval=1s --timeout=1s --retries=1\ - CMD cat /status`, - true) - c.Check(err, check.IsNil) + CMD cat /status`), + ) + result.Assert(c, icmd.Success) serviceName := "healthServiceRun" 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) { // service started from this image won't pass health check imageName := "testhealth" - _, _, err := d.BuildImageWithOut(imageName, - `FROM busybox + result := cli.BuildCmd(c, imageName, cli.Daemon(d), + build.WithDockerfile(`FROM busybox HEALTHCHECK --interval=1s --timeout=1s --retries=1024\ - CMD cat /status`, - true) - c.Check(err, check.IsNil) + CMD cat /status`), + ) + result.Assert(c, icmd.Success) serviceName := "healthServiceStart" out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--name", serviceName, imageName, "top") diff --git a/internal/test/daemon/daemon.go b/internal/test/daemon/daemon.go index 7ad576552e..9b41393be1 100644 --- a/internal/test/daemon/daemon.go +++ b/internal/test/daemon/daemon.go @@ -179,11 +179,8 @@ func (d *Daemon) NewClientT(t assert.TestingT) *client.Client { // Cleanup cleans the daemon files : exec root (network namespaces, ...), swarmkit files func (d *Daemon) Cleanup(t testingT) { // Cleanup swarmkit wal files if present - walDir := filepath.Join(d.Root, "swarm/raft/wal") - if err := os.RemoveAll(walDir); err != nil { - t.Logf("error removing %v: %v", walDir, err) - } - cleanupExecRoot(t, d.execRoot) + cleanupRaftDir(t, d.Root) + cleanupNetworkNamespace(t, d.execRoot) } // 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 { assert.NilError(t, err) return info } + +func cleanupRaftDir(t testingT, rootPath string) { + walDir := filepath.Join(rootPath, "swarm/raft/wal") + if err := os.RemoveAll(walDir); err != nil { + t.Logf("error removing %v: %v", walDir, err) + } +} diff --git a/internal/test/daemon/daemon_unix.go b/internal/test/daemon/daemon_unix.go index c0aa26c9f8..385668c7e9 100644 --- a/internal/test/daemon/daemon_unix.go +++ b/internal/test/daemon/daemon_unix.go @@ -9,7 +9,7 @@ import ( "golang.org/x/sys/unix" ) -func cleanupExecRoot(t testingT, execRoot string) { +func cleanupNetworkNamespace(t testingT, execRoot string) { // Cleanup network namespaces in the exec root of this // daemon because this exec root is specific to this // daemon instance and has no chance of getting diff --git a/internal/test/daemon/daemon_windows.go b/internal/test/daemon/daemon_windows.go index 8ec554fbc4..cb6bb6a4cb 100644 --- a/internal/test/daemon/daemon_windows.go +++ b/internal/test/daemon/daemon_windows.go @@ -21,5 +21,5 @@ func signalDaemonReload(pid int) error { return fmt.Errorf("daemon reload not supported") } -func cleanupExecRoot(t testingT, execRoot string) { +func cleanupNetworkNamespace(t testingT, execRoot string) { }