Remove daemon.BuildImageWithOut and use cli helpers function

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2018-04-18 16:45:55 +02:00
parent 3b01e92c9a
commit 5d2afe4f51
No known key found for this signature in database
GPG Key ID: 083CC6FD6EB699A3
8 changed files with 77 additions and 86 deletions

View File

@ -2,7 +2,6 @@ package daemon // import "github.com/docker/docker/integration-cli/daemon"
import ( import (
"fmt" "fmt"
"os/exec"
"strings" "strings"
"time" "time"
@ -88,19 +87,6 @@ func (d *Daemon) inspectFieldWithError(name, field string) (string, error) {
return d.inspectFilter(name, fmt.Sprintf(".%s", field)) 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 // CheckActiveContainerCount returns the number of active containers
// FIXME(vdemeester) should re-use ActivateContainers in some way // FIXME(vdemeester) should re-use ActivateContainers in some way
func (d *Daemon) CheckActiveContainerCount(c *check.C) (interface{}, check.CommentInterface) { 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 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
}

View File

@ -13,11 +13,14 @@ import (
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/swarm/runtime" "github.com/docker/docker/api/types/swarm/runtime"
"github.com/docker/docker/integration-cli/checker" "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/docker/docker/integration-cli/daemon"
testdaemon "github.com/docker/docker/internal/test/daemon" testdaemon "github.com/docker/docker/internal/test/daemon"
"github.com/docker/docker/internal/test/fixtures/plugin" "github.com/docker/docker/internal/test/fixtures/plugin"
"github.com/docker/docker/internal/test/registry" "github.com/docker/docker/internal/test/registry"
"github.com/go-check/check" "github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -209,12 +212,12 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesUpdateStartFirst(c *check.C) {
image2 := "testhealth:latest" image2 := "testhealth:latest"
// service started from this image won't pass health check // service started from this image won't pass health check
_, _, err := d.BuildImageWithOut(image2, result := cli.BuildCmd(c, image2, cli.Daemon(d),
`FROM busybox build.WithDockerfile(`FROM busybox
HEALTHCHECK --interval=1s --timeout=30s --retries=1024 \ HEALTHCHECK --interval=1s --timeout=30s --retries=1024 \
CMD cat /status`, CMD cat /status`),
true) )
c.Check(err, check.IsNil) result.Assert(c, icmd.Success)
// create service // create service
instances := 5 instances := 5

View File

@ -31,6 +31,7 @@ import (
moby_daemon "github.com/docker/docker/daemon" moby_daemon "github.com/docker/docker/daemon"
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/daemon" "github.com/docker/docker/integration-cli/daemon"
testdaemon "github.com/docker/docker/internal/test/daemon" testdaemon "github.com/docker/docker/internal/test/daemon"
"github.com/docker/docker/opts" "github.com/docker/docker/opts"
@ -1156,14 +1157,16 @@ func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) {
func (s *DockerDaemonSuite) TestDaemonLoggingDriverShouldBeIgnoredForBuild(c *check.C) { func (s *DockerDaemonSuite) TestDaemonLoggingDriverShouldBeIgnoredForBuild(c *check.C) {
s.d.StartWithBusybox(c, "--log-driver=splunk") s.d.StartWithBusybox(c, "--log-driver=splunk")
out, err := s.d.Cmd("build") result := cli.BuildCmd(c, "busyboxs", cli.Daemon(s.d),
out, code, err := s.d.BuildImageWithOut("busyboxs", ` build.WithDockerfile(`
FROM busybox FROM busybox
RUN echo foo`, false) RUN echo foo`),
comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", out, code, err) build.WithoutCache,
c.Assert(err, check.IsNil, comment) )
c.Assert(code, check.Equals, 0, comment) comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", result.Combined(), result.ExitCode, result.Error)
c.Assert(out, checker.Contains, "foo", comment) 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) { func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) {
@ -2404,12 +2407,16 @@ func (s *DockerDaemonSuite) TestDaemonMaxConcurrencyWithConfigFileReload(c *chec
func (s *DockerDaemonSuite) TestBuildOnDisabledBridgeNetworkDaemon(c *check.C) { func (s *DockerDaemonSuite) TestBuildOnDisabledBridgeNetworkDaemon(c *check.C) {
s.d.StartWithBusybox(c, "-b=none", "--iptables=false") s.d.StartWithBusybox(c, "-b=none", "--iptables=false")
out, code, err := s.d.BuildImageWithOut("busyboxs",
`FROM busybox result := cli.BuildCmd(c, "busyboxs", cli.Daemon(s.d),
RUN cat /etc/hosts`, false) build.WithDockerfile(`
comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", out, code, err) FROM busybox
c.Assert(err, check.IsNil, comment) RUN cat /etc/hosts`),
c.Assert(code, check.Equals, 0, comment) 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 // Test case for #21976

View File

@ -12,8 +12,10 @@ import (
"github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/daemon" "github.com/docker/docker/integration-cli/daemon"
"github.com/go-check/check" "github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
) )
func pruneNetworkAndVerify(c *check.C, d *daemon.Daemon, kept, pruned []string) { 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) { func (s *DockerDaemonSuite) TestPruneImageDangling(c *check.C) {
s.d.StartWithBusybox(c) s.d.StartWithBusybox(c)
out, _, err := s.d.BuildImageWithOut("test", result := cli.BuildCmd(c, "test", cli.Daemon(s.d),
`FROM busybox build.WithDockerfile(`FROM busybox
LABEL foo=bar`, true, "-q") LABEL foo=bar`),
c.Assert(err, checker.IsNil) cli.WithFlags("-q"),
id := strings.TrimSpace(out) )
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(err, checker.IsNil)
c.Assert(strings.TrimSpace(out), checker.Contains, id) 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) { func (s *DockerDaemonSuite) TestPruneImageLabel(c *check.C) {
s.d.StartWithBusybox(c) s.d.StartWithBusybox(c)
out, _, err := s.d.BuildImageWithOut("test1", result := cli.BuildCmd(c, "test1", cli.Daemon(s.d),
`FROM busybox build.WithDockerfile(`FROM busybox
LABEL foo=bar`, true, "-q") LABEL foo=bar`),
c.Assert(err, checker.IsNil) cli.WithFlags("-q"),
id1 := strings.TrimSpace(out) )
out, err = s.d.Cmd("images", "-q", "--no-trunc") 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(err, checker.IsNil)
c.Assert(strings.TrimSpace(out), checker.Contains, id1) c.Assert(strings.TrimSpace(out), checker.Contains, id1)
out, _, err = s.d.BuildImageWithOut("test2", result = cli.BuildCmd(c, "test2", cli.Daemon(s.d),
`FROM busybox build.WithDockerfile(`FROM busybox
LABEL bar=foo`, true, "-q") LABEL bar=foo`),
c.Assert(err, checker.IsNil) cli.WithFlags("-q"),
id2 := strings.TrimSpace(out) )
result.Assert(c, icmd.Success)
id2 := 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(err, checker.IsNil)
c.Assert(strings.TrimSpace(out), checker.Contains, id2) c.Assert(strings.TrimSpace(out), checker.Contains, id2)

View File

@ -9,7 +9,10 @@ import (
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/daemon/cluster/executor/container" "github.com/docker/docker/daemon/cluster/executor/container"
"github.com/docker/docker/integration-cli/checker" "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/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
) )
// start a service, and then make its task unhealthy during running // 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) d := s.AddDaemon(c, true, true)
// build image with health-check // build image with health-check
// note: use `daemon.buildImageWithOut` to build, do not use `buildImage` to build
imageName := "testhealth" imageName := "testhealth"
_, _, err := d.BuildImageWithOut(imageName, result := cli.BuildCmd(c, imageName, cli.Daemon(d),
`FROM busybox build.WithDockerfile(`FROM busybox
RUN touch /status RUN touch /status
HEALTHCHECK --interval=1s --timeout=1s --retries=1\ HEALTHCHECK --interval=1s --timeout=1s --retries=1\
CMD cat /status`, CMD cat /status`),
true) )
c.Check(err, check.IsNil) result.Assert(c, icmd.Success)
serviceName := "healthServiceRun" serviceName := "healthServiceRun"
out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--name", serviceName, imageName, "top") 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 // service started from this image won't pass health check
imageName := "testhealth" imageName := "testhealth"
_, _, err := d.BuildImageWithOut(imageName, result := cli.BuildCmd(c, imageName, cli.Daemon(d),
`FROM busybox build.WithDockerfile(`FROM busybox
HEALTHCHECK --interval=1s --timeout=1s --retries=1024\ HEALTHCHECK --interval=1s --timeout=1s --retries=1024\
CMD cat /status`, CMD cat /status`),
true) )
c.Check(err, check.IsNil) result.Assert(c, icmd.Success)
serviceName := "healthServiceStart" serviceName := "healthServiceStart"
out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--name", serviceName, imageName, "top") out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--name", serviceName, imageName, "top")

View File

@ -179,11 +179,8 @@ func (d *Daemon) NewClientT(t assert.TestingT) *client.Client {
// Cleanup cleans the daemon files : exec root (network namespaces, ...), swarmkit files // Cleanup cleans the daemon files : exec root (network namespaces, ...), swarmkit files
func (d *Daemon) Cleanup(t testingT) { func (d *Daemon) Cleanup(t testingT) {
// Cleanup swarmkit wal files if present // Cleanup swarmkit wal files if present
walDir := filepath.Join(d.Root, "swarm/raft/wal") cleanupRaftDir(t, d.Root)
if err := os.RemoveAll(walDir); err != nil { cleanupNetworkNamespace(t, d.execRoot)
t.Logf("error removing %v: %v", walDir, err)
}
cleanupExecRoot(t, d.execRoot)
} }
// Start starts the daemon and return once it is ready to receive requests. // 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) assert.NilError(t, err)
return info 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)
}
}

View File

@ -9,7 +9,7 @@ import (
"golang.org/x/sys/unix" "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 // Cleanup network namespaces in the exec root of this
// daemon because this exec root is specific to this // daemon because this exec root is specific to this
// daemon instance and has no chance of getting // daemon instance and has no chance of getting

View File

@ -21,5 +21,5 @@ func signalDaemonReload(pid int) error {
return fmt.Errorf("daemon reload not supported") return fmt.Errorf("daemon reload not supported")
} }
func cleanupExecRoot(t testingT, execRoot string) { func cleanupNetworkNamespace(t testingT, execRoot string) {
} }