Create a unified RunCommand function with Assert()

Remove some run functions and replace them with the unified run command.
Remove DockerCmdWithStdoutStderr
Remove many duplicate runCommand functions.
Also add dockerCmdWithResult()
Allow Result.Assert() to ignore the error message if an exit status is expected.
Fix race in DockerSuite.TestDockerInspectMultipleNetwork
Fix flaky test DockerSuite.TestRunInteractiveWithRestartPolicy

Signed-off-by: Daniel Nephin <dnephin@docker.com>
Upstream-commit: d7022f2b46589cb9d860219e1d8278351ba147c3
Component: engine
This commit is contained in:
Daniel Nephin
2016-08-23 15:11:46 -04:00
parent 2b0a71c4f1
commit eaa727c6e2
22 changed files with 564 additions and 998 deletions
@@ -5,13 +5,13 @@ import (
"net/http"
"net/http/httptest"
"net/http/httputil"
"os/exec"
"strconv"
"strings"
"time"
"github.com/docker/docker/api"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/go-check/check"
)
@@ -89,15 +89,12 @@ func (s *DockerSuite) TestApiDockerApiVersion(c *check.C) {
defer server.Close()
// Test using the env var first
cmd := exec.Command(dockerBinary, "-H="+server.URL[7:], "version")
cmd.Env = appendBaseEnv(false, "DOCKER_API_VERSION=xxx")
out, _, _ := runCommandWithOutput(cmd)
result := icmd.RunCmd(icmd.Cmd{
Command: binaryWithArgs([]string{"-H", server.URL[7:], "version"}),
Env: []string{"DOCKER_API_VERSION=xxx"},
})
result.Assert(c, icmd.Expected{Out: "API version: xxx", ExitCode: 1})
c.Assert(svrVersion, check.Equals, "/vxxx/version")
if !strings.Contains(out, "API version: xxx") {
c.Fatalf("Out didn't have 'xxx' for the API version, had:\n%s", out)
}
}
func (s *DockerSuite) TestApiErrorJSON(c *check.C) {
@@ -10,7 +10,7 @@ import (
"sync"
"time"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/go-check/check"
)
@@ -161,7 +161,11 @@ func (s *DockerSuite) TestAttachPausedContainer(c *check.C) {
defer unpauseAllContainers()
dockerCmd(c, "run", "-d", "--name=test", "busybox", "top")
dockerCmd(c, "pause", "test")
out, _, err := dockerCmdWithError("attach", "test")
c.Assert(err, checker.NotNil, check.Commentf(out))
c.Assert(out, checker.Contains, "You cannot attach to a paused container, unpause it first")
result := dockerCmdWithResult("attach", "test")
result.Assert(c, icmd.Expected{
Error: "exit status 1",
ExitCode: 1,
Err: "You cannot attach to a paused container, unpause it first",
})
}
@@ -56,7 +56,6 @@ func (s *DockerSuite) TestAttachClosedOnContainerStop(c *check.C) {
}
func (s *DockerSuite) TestAttachAfterDetach(c *check.C) {
name := "detachtest"
cpty, tty, err := pty.Open()
@@ -80,7 +79,11 @@ func (s *DockerSuite) TestAttachAfterDetach(c *check.C) {
select {
case err := <-errChan:
c.Assert(err, check.IsNil)
if err != nil {
buff := make([]byte, 200)
tty.Read(buff)
c.Fatalf("%s: %s", err, buff)
}
case <-time.After(5 * time.Second):
c.Fatal("timeout while detaching")
}
@@ -20,6 +20,7 @@ import (
"github.com/docker/docker/builder/dockerfile/command"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/docker/docker/pkg/stringutils"
"github.com/go-check/check"
)
@@ -5063,13 +5064,11 @@ func (s *DockerSuite) TestBuildDockerfileOutsideContext(c *check.C) {
filepath.Join(ctx, "dockerfile1"),
filepath.Join(ctx, "dockerfile2"),
} {
out, _, err := dockerCmdWithError("build", "-t", name, "--no-cache", "-f", dockerfilePath, ".")
if err == nil {
c.Fatalf("Expected error with %s. Out: %s", dockerfilePath, out)
}
if !strings.Contains(out, "must be within the build context") && !strings.Contains(out, "Cannot locate Dockerfile") {
c.Fatalf("Unexpected error with %s. Out: %s", dockerfilePath, out)
}
result := dockerCmdWithResult("build", "-t", name, "--no-cache", "-f", dockerfilePath, ".")
result.Assert(c, icmd.Expected{
Err: "must be within the build context",
ExitCode: 1,
})
deleteImages(name)
}
@@ -14,6 +14,7 @@ import (
"strings"
"time"
"github.com/docker/docker/pkg/integration"
"github.com/docker/docker/pkg/integration/checker"
"github.com/docker/go-units"
"github.com/go-check/check"
@@ -193,7 +194,7 @@ func (s *DockerSuite) TestBuildCancellationKillsSleep(c *check.C) {
}
// Get the exit status of `docker build`, check it exited because killed.
if err := buildCmd.Wait(); err != nil && !isKilled(err) {
if err := buildCmd.Wait(); err != nil && !integration.IsKilled(err) {
c.Fatalf("wait failed during build run: %T %s", err, err)
}
@@ -21,6 +21,7 @@ import (
"time"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/docker/docker/pkg/mount"
"github.com/docker/go-units"
"github.com/docker/libnetwork/iptables"
@@ -908,9 +909,8 @@ func (s *DockerDaemonSuite) TestDaemonDefaultNetworkInvalidClusterConfig(c *chec
c.Assert(err, checker.IsNil)
// Start daemon with docker0 bridge
ifconfigCmd := exec.Command("ifconfig", defaultNetworkBridge)
_, err = runCommand(ifconfigCmd)
c.Assert(err, check.IsNil)
result := icmd.RunCommand("ifconfig", defaultNetworkBridge)
result.Assert(c, icmd.Expected{})
err = d.Restart(fmt.Sprintf("--cluster-store=%s", discoveryBackend))
c.Assert(err, checker.IsNil)
@@ -2235,7 +2235,6 @@ func (s *DockerDaemonSuite) TestDaemonRestartWithUnpausedRunningContainer(t *che
pid, err := s.d.Cmd("inspect", "-f", "{{.State.Pid}}", cid)
t.Assert(err, check.IsNil)
pid = strings.TrimSpace(pid)
// pause the container
if _, err := s.d.Cmd("pause", cid); err != nil {
@@ -2248,19 +2247,18 @@ func (s *DockerDaemonSuite) TestDaemonRestartWithUnpausedRunningContainer(t *che
}
// resume the container
runCmd := exec.Command(ctrBinary, "--address", "unix:///var/run/docker/libcontainerd/docker-containerd.sock", "containers", "resume", cid)
if out, ec, err := runCommandWithOutput(runCmd); err != nil {
t.Fatalf("Failed to run ctr, ExitCode: %d, err: '%v' output: '%s' cid: '%s'\n", ec, err, out, cid)
}
result := icmd.RunCommand(
ctrBinary,
"--address", "unix:///var/run/docker/libcontainerd/docker-containerd.sock",
"containers", "resume", cid)
result.Assert(t, icmd.Expected{})
// Give time to containerd to process the command if we don't
// the resume event might be received after we do the inspect
pidCmd := exec.Command("kill", "-0", pid)
_, ec, _ := runCommandWithOutput(pidCmd)
for ec == 0 {
time.Sleep(1 * time.Second)
_, ec, _ = runCommandWithOutput(pidCmd)
}
waitAndAssert(t, defaultReconciliationTimeout, func(*check.C) (interface{}, check.CommentInterface) {
result := icmd.RunCommand("kill", "-0", strings.TrimSpace(pid))
return result.ExitCode, nil
}, checker.Equals, 0)
// restart the daemon
if err := s.d.Start("--live-restore"); err != nil {
@@ -13,6 +13,7 @@ import (
"github.com/docker/docker/daemon/events/testutils"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/go-check/check"
)
@@ -57,11 +58,14 @@ func (s *DockerSuite) TestEventsUntag(c *check.C) {
dockerCmd(c, "tag", image, "utest:tag2")
dockerCmd(c, "rmi", "utest:tag1")
dockerCmd(c, "rmi", "utest:tag2")
eventsCmd := exec.Command(dockerBinary, "events", "--since=1")
out, exitCode, _, err := runCommandWithOutputForDuration(eventsCmd, time.Duration(time.Millisecond*2500))
c.Assert(err, checker.IsNil)
c.Assert(exitCode, checker.Equals, 0, check.Commentf("Failed to get events"))
events := strings.Split(out, "\n")
result := icmd.RunCmd(icmd.Cmd{
Command: []string{dockerBinary, "events", "--since=1"},
Timeout: time.Millisecond * 2500,
})
result.Assert(c, icmd.Expected{Timeout: true})
events := strings.Split(result.Stdout(), "\n")
nEvents := len(events)
// The last element after the split above will be an empty string, so we
// get the two elements before the last, which are the untags we're
@@ -275,8 +279,8 @@ func (s *DockerSuite) TestEventsImageLoad(c *check.C) {
c.Assert(noImageID, checker.Equals, "", check.Commentf("Should not have any image"))
dockerCmd(c, "load", "-i", "saveimg.tar")
cmd := exec.Command("rm", "-rf", "saveimg.tar")
runCommand(cmd)
result := icmd.RunCommand("rm", "-rf", "saveimg.tar")
result.Assert(c, icmd.Expected{})
out, _ = dockerCmd(c, "images", "-q", "--no-trunc", myImageName)
imageID := strings.TrimSpace(out)
@@ -16,6 +16,7 @@ import (
"time"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/go-check/check"
)
@@ -122,10 +123,8 @@ func (s *DockerSuite) TestExecEnv(c *check.C) {
func (s *DockerSuite) TestExecExitStatus(c *check.C) {
runSleepingContainer(c, "-d", "--name", "top")
// Test normal (non-detached) case first
cmd := exec.Command(dockerBinary, "exec", "top", "sh", "-c", "exit 23")
ec, _ := runCommand(cmd)
c.Assert(ec, checker.Equals, 23)
result := icmd.RunCommand(dockerBinary, "exec", "top", "sh", "-c", "exit 23")
result.Assert(c, icmd.Expected{ExitCode: 23, Error: "exit status 23"})
}
func (s *DockerSuite) TestExecPausedContainer(c *check.C) {
@@ -15,6 +15,7 @@ import (
"time"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/runconfig"
"github.com/docker/engine-api/types"
@@ -477,27 +478,33 @@ func (s *DockerSuite) TestDockerNetworkInspectWithID(c *check.C) {
}
func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
out, _ := dockerCmd(c, "network", "inspect", "host", "none")
result := dockerCmdWithResult("network", "inspect", "host", "none")
result.Assert(c, icmd.Expected{})
networkResources := []types.NetworkResource{}
err := json.Unmarshal([]byte(out), &networkResources)
err := json.Unmarshal([]byte(result.Stdout()), &networkResources)
c.Assert(err, check.IsNil)
c.Assert(networkResources, checker.HasLen, 2)
// Should print an error, return an exitCode 1 *but* should print the host network
out, exitCode, err := dockerCmdWithError("network", "inspect", "host", "nonexistent")
c.Assert(err, checker.NotNil)
c.Assert(exitCode, checker.Equals, 1)
c.Assert(out, checker.Contains, "Error: No such network: nonexistent")
result = dockerCmdWithResult("network", "inspect", "host", "nonexistent")
result.Assert(c, icmd.Expected{
ExitCode: 1,
Err: "Error: No such network: nonexistent",
Out: "host",
})
networkResources = []types.NetworkResource{}
inspectOut := strings.SplitN(out, "\nError: No such network: nonexistent\n", 2)[0]
err = json.Unmarshal([]byte(inspectOut), &networkResources)
err = json.Unmarshal([]byte(result.Stdout()), &networkResources)
c.Assert(networkResources, checker.HasLen, 1)
// Should print an error and return an exitCode, nothing else
out, exitCode, err = dockerCmdWithError("network", "inspect", "nonexistent")
c.Assert(err, checker.NotNil)
c.Assert(exitCode, checker.Equals, 1)
c.Assert(out, checker.Contains, "Error: No such network: nonexistent")
result = dockerCmdWithResult("network", "inspect", "nonexistent")
result.Assert(c, icmd.Expected{
ExitCode: 1,
Err: "Error: No such network: nonexistent",
Out: "[]",
})
}
func (s *DockerSuite) TestDockerInspectNetworkWithContainerName(c *check.C) {
@@ -12,6 +12,7 @@ import (
"time"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/docker/docker/pkg/stringid"
"github.com/go-check/check"
)
@@ -204,8 +205,11 @@ func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
containerOut = strings.TrimSpace(out)
c.Assert(containerOut, checker.Equals, secondID)
out, _, _ = dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish")
c.Assert(out, checker.Contains, "Unrecognised filter value for status", check.Commentf("Expected error response due to invalid status filter output: %q", out))
result := dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish")
result.Assert(c, icmd.Expected{
ExitCode: 1,
Err: "Unrecognised filter value for status",
})
// Windows doesn't support pausing of containers
if daemonPlatform != "windows" {
@@ -4,6 +4,7 @@ import (
"strings"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/docker/docker/pkg/stringid"
"github.com/go-check/check"
)
@@ -61,9 +62,11 @@ func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
name := inspectField(c, newName, "Name")
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
name, err := inspectFieldWithError("first_name", "Name")
c.Assert(err, checker.NotNil, check.Commentf(name))
c.Assert(err.Error(), checker.Contains, "No such container, image or task: first_name")
result := dockerCmdWithResult("inspect", "-f={{.Name}}", "first_name")
result.Assert(c, icmd.Expected{
ExitCode: 1,
Err: "No such container, image or task: first_name",
})
}
func (s *DockerSuite) TestRenameInvalidName(c *check.C) {
@@ -21,6 +21,7 @@ import (
"time"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/docker/docker/pkg/mount"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/pkg/stringutils"
@@ -1856,32 +1857,18 @@ func (s *DockerSuite) TestRunExitOnStdinClose(c *check.C) {
// Test run -i --restart xxx doesn't hang
func (s *DockerSuite) TestRunInteractiveWithRestartPolicy(c *check.C) {
name := "test-inter-restart"
runCmd := exec.Command(dockerBinary, "run", "-i", "--name", name, "--restart=always", "busybox", "sh")
stdin, err := runCmd.StdinPipe()
c.Assert(err, checker.IsNil)
err = runCmd.Start()
c.Assert(err, checker.IsNil)
c.Assert(waitRun(name), check.IsNil)
_, err = stdin.Write([]byte("exit 11\n"))
c.Assert(err, checker.IsNil)
finish := make(chan error)
go func() {
finish <- runCmd.Wait()
close(finish)
result := icmd.StartCmd(icmd.Cmd{
Command: []string{dockerBinary, "run", "-i", "--name", name, "--restart=always", "busybox", "sh"},
Stdin: bytes.NewBufferString("exit 11"),
})
c.Assert(result.Error, checker.IsNil)
defer func() {
dockerCmdWithResult("stop", name).Assert(c, icmd.Expected{})
}()
delay := 10 * time.Second
select {
case <-finish:
case <-time.After(delay):
c.Fatal("run -i --restart hangs")
}
c.Assert(waitRun(name), check.IsNil)
dockerCmd(c, "stop", name)
result = icmd.WaitOnCmd(10*time.Second, result)
result.Assert(c, icmd.Expected{ExitCode: 11})
}
// Test for #2267
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/go-check/check"
)
@@ -55,14 +56,18 @@ func (s *DockerSuite) TestVolumeCliInspectMulti(c *check.C) {
dockerCmd(c, "volume", "create", "--name", "test2")
dockerCmd(c, "volume", "create", "--name", "not-shown")
out, _, err := dockerCmdWithError("volume", "inspect", "--format='{{ .Name }}'", "test1", "test2", "doesntexist", "not-shown")
c.Assert(err, checker.NotNil)
result := dockerCmdWithResult("volume", "inspect", "--format={{ .Name }}", "test1", "test2", "doesntexist", "not-shown")
result.Assert(c, icmd.Expected{
ExitCode: 1,
Err: "No such volume: doesntexist",
})
out := result.Stdout()
outArr := strings.Split(strings.TrimSpace(out), "\n")
c.Assert(len(outArr), check.Equals, 3, check.Commentf("\n%s", out))
c.Assert(len(outArr), check.Equals, 2, check.Commentf("\n%s", out))
c.Assert(out, checker.Contains, "test1")
c.Assert(out, checker.Contains, "test2")
c.Assert(out, checker.Contains, "Error: No such volume: doesntexist")
c.Assert(out, checker.Not(checker.Contains), "not-shown")
}
@@ -8,6 +8,7 @@ import (
"time"
"github.com/docker/docker/pkg/integration/checker"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/docker/docker/pkg/parsers/kernel"
"github.com/go-check/check"
)
@@ -401,10 +402,11 @@ func (s *DockerSuite) TestDockerNetworkMacVlanBridgeInternalMode(c *check.C) {
c.Assert(waitRun("second"), check.IsNil)
// access outside of the network should fail
_, _, err := dockerCmdWithTimeout(time.Second, "exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8")
c.Assert(err, check.NotNil)
result := dockerCmdWithTimeout(time.Second, "exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8")
result.Assert(c, icmd.Expected{Timeout: true})
// intra-network communications should succeed
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
c.Assert(err, check.IsNil)
}
@@ -440,10 +442,10 @@ func (s *DockerSuite) TestDockerNetworkIpvlanL2InternalMode(c *check.C) {
c.Assert(waitRun("second"), check.IsNil)
// access outside of the network should fail
_, _, err := dockerCmdWithTimeout(time.Second, "exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8")
c.Assert(err, check.NotNil)
result := dockerCmdWithTimeout(time.Second, "exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8")
c.Assert(result.Error, check.NotNil)
// intra-network communications should succeed
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
c.Assert(err, check.IsNil)
}
@@ -481,10 +483,10 @@ func (s *DockerSuite) TestDockerNetworkIpvlanL3InternalMode(c *check.C) {
c.Assert(waitRun("second"), check.IsNil)
// access outside of the network should fail
_, _, err := dockerCmdWithTimeout(time.Second, "exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8")
c.Assert(err, check.NotNil)
result := dockerCmdWithTimeout(time.Second, "exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8")
c.Assert(result.Error, check.NotNil)
// intra-network communications should succeed
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
c.Assert(err, check.IsNil)
}
@@ -24,7 +24,7 @@ import (
"github.com/docker/docker/opts"
"github.com/docker/docker/pkg/httputils"
"github.com/docker/docker/pkg/integration"
icmd "github.com/docker/docker/pkg/integration/cmd"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/stringutils"
"github.com/docker/engine-api/types"
@@ -229,15 +229,7 @@ func readBody(b io.ReadCloser) ([]byte, error) {
}
func deleteContainer(container string) error {
container = strings.TrimSpace(strings.Replace(container, "\n", " ", -1))
rmArgs := strings.Split(fmt.Sprintf("rm -fv %v", container), " ")
exitCode, err := runCommand(exec.Command(dockerBinary, rmArgs...))
// set error manually if not set
if exitCode != 0 && err == nil {
err = fmt.Errorf("failed to remove container: `docker rm` exit is non-zero")
}
return err
return icmd.RunCommand(dockerBinary, "rm", "-fv", container).Error
}
func getAllContainers() (string, error) {
@@ -398,13 +390,7 @@ func getSliceOfPausedContainers() ([]string, error) {
}
func unpauseContainer(container string) error {
unpauseCmd := exec.Command(dockerBinary, "unpause", container)
exitCode, err := runCommand(unpauseCmd)
if exitCode != 0 && err == nil {
err = fmt.Errorf("failed to unpause container")
}
return err
return icmd.RunCommand(dockerBinary, "unpause", container).Error
}
func unpauseAllContainers() error {
@@ -428,24 +414,12 @@ func unpauseAllContainers() error {
}
func deleteImages(images ...string) error {
args := []string{"rmi", "-f"}
args = append(args, images...)
rmiCmd := exec.Command(dockerBinary, args...)
exitCode, err := runCommand(rmiCmd)
// set error manually if not set
if exitCode != 0 && err == nil {
err = fmt.Errorf("failed to remove image: `docker rmi` exit is non-zero")
}
return err
args := []string{dockerBinary, "rmi", "-f"}
return icmd.RunCmd(icmd.Cmd{Command: append(args, images...)}).Error
}
func imageExists(image string) error {
inspectCmd := exec.Command(dockerBinary, "inspect", image)
exitCode, err := runCommand(inspectCmd)
if exitCode != 0 && err == nil {
err = fmt.Errorf("couldn't find image %q", image)
}
return err
return icmd.RunCommand(dockerBinary, "inspect", image).Error
}
func pullImageIfNotExist(image string) error {
@@ -464,33 +438,49 @@ func dockerCmdWithError(args ...string) (string, int, error) {
if err := validateArgs(args...); err != nil {
return "", 0, err
}
out, code, err := integration.DockerCmdWithError(dockerBinary, args...)
if err != nil {
err = fmt.Errorf("%v: %s", err, out)
result := icmd.RunCommand(dockerBinary, args...)
if result.Error != nil {
return result.Combined(), result.ExitCode, fmt.Errorf(result.Fails(icmd.Expected{}))
}
return out, code, err
return result.Combined(), result.ExitCode, result.Error
}
func dockerCmdWithStdoutStderr(c *check.C, args ...string) (string, string, int) {
if err := validateArgs(args...); err != nil {
c.Fatalf(err.Error())
}
return integration.DockerCmdWithStdoutStderr(dockerBinary, c, args...)
result := icmd.RunCommand(dockerBinary, args...)
// TODO: why is c ever nil?
if c != nil {
result.Assert(c, icmd.Expected{})
}
return result.Stdout(), result.Stderr(), result.ExitCode
}
func dockerCmd(c *check.C, args ...string) (string, int) {
if err := validateArgs(args...); err != nil {
c.Fatalf(err.Error())
}
return integration.DockerCmd(dockerBinary, c, args...)
result := icmd.RunCommand(dockerBinary, args...)
result.Assert(c, icmd.Expected{})
return result.Combined(), result.ExitCode
}
func dockerCmdWithResult(args ...string) *icmd.Result {
return icmd.RunCommand(dockerBinary, args...)
}
func binaryWithArgs(args []string) []string {
return append([]string{dockerBinary}, args...)
}
// execute a docker command with a timeout
func dockerCmdWithTimeout(timeout time.Duration, args ...string) (string, int, error) {
func dockerCmdWithTimeout(timeout time.Duration, args ...string) *icmd.Result {
if err := validateArgs(args...); err != nil {
return "", 0, err
return &icmd.Result{Error: err}
}
return integration.DockerCmdWithTimeout(dockerBinary, timeout, args...)
return icmd.RunCmd(icmd.Cmd{Command: binaryWithArgs(args), Timeout: timeout})
}
// execute a docker command in a directory
@@ -498,15 +488,20 @@ func dockerCmdInDir(c *check.C, path string, args ...string) (string, int, error
if err := validateArgs(args...); err != nil {
c.Fatalf(err.Error())
}
return integration.DockerCmdInDir(dockerBinary, path, args...)
result := icmd.RunCmd(icmd.Cmd{Command: binaryWithArgs(args), Dir: path})
return result.Combined(), result.ExitCode, result.Error
}
// execute a docker command in a directory with a timeout
func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) (string, int, error) {
func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) *icmd.Result {
if err := validateArgs(args...); err != nil {
return "", 0, err
return &icmd.Result{Error: err}
}
return integration.DockerCmdInDirWithTimeout(dockerBinary, timeout, path, args...)
return icmd.RunCmd(icmd.Cmd{
Command: binaryWithArgs(args),
Timeout: timeout,
Dir: path,
})
}
// validateArgs is a checker to ensure tests are not running commands which are
@@ -1355,17 +1350,12 @@ func buildImageCmdArgs(args []string, name, dockerfile string, useCache bool) *e
}
func waitForContainer(contID string, args ...string) error {
args = append([]string{"run", "--name", contID}, args...)
cmd := exec.Command(dockerBinary, args...)
if _, err := runCommand(cmd); err != nil {
return err
args = append([]string{dockerBinary, "run", "--name", contID}, args...)
result := icmd.RunCmd(icmd.Cmd{Command: args})
if result.Error != nil {
return result.Error
}
if err := waitRun(contID); err != nil {
return err
}
return nil
return waitRun(contID)
}
// waitRun will wait for the specified container to be running, maximum 5 seconds.
@@ -1391,22 +1381,22 @@ func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg
args := append(arg, "inspect", "-f", expr, name)
for {
cmd := exec.Command(dockerBinary, args...)
out, _, err := runCommandWithOutput(cmd)
if err != nil {
if !strings.Contains(out, "No such") {
return fmt.Errorf("error executing docker inspect: %v\n%s", err, out)
result := icmd.RunCommand(dockerBinary, args...)
if result.Error != nil {
if !strings.Contains(result.Stderr(), "No such") {
return fmt.Errorf("error executing docker inspect: %v\n%s",
result.Stderr(), result.Stdout())
}
select {
case <-after:
return err
return result.Error
default:
time.Sleep(10 * time.Millisecond)
continue
}
}
out = strings.TrimSpace(out)
out := strings.TrimSpace(result.Stdout())
if out == expected {
break
}
+22 -24
View File
@@ -7,6 +7,7 @@ import (
"time"
"github.com/docker/docker/pkg/integration"
"github.com/docker/docker/pkg/integration/cmd"
)
func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
@@ -16,36 +17,33 @@ func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
return "", "/"
}
func getExitCode(err error) (int, error) {
return integration.GetExitCode(err)
// TODO: update code to call cmd.RunCmd directly, and remove this function
func runCommandWithOutput(execCmd *exec.Cmd) (string, int, error) {
result := cmd.RunCmd(transformCmd(execCmd))
return result.Combined(), result.ExitCode, result.Error
}
func processExitCode(err error) (exitCode int) {
return integration.ProcessExitCode(err)
// TODO: update code to call cmd.RunCmd directly, and remove this function
func runCommandWithStdoutStderr(execCmd *exec.Cmd) (string, string, int, error) {
result := cmd.RunCmd(transformCmd(execCmd))
return result.Stdout(), result.Stderr(), result.ExitCode, result.Error
}
func isKilled(err error) bool {
return integration.IsKilled(err)
// TODO: update code to call cmd.RunCmd directly, and remove this function
func runCommand(execCmd *exec.Cmd) (exitCode int, err error) {
result := cmd.RunCmd(transformCmd(execCmd))
return result.ExitCode, result.Error
}
func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) {
return integration.RunCommandWithOutput(cmd)
}
func runCommandWithStdoutStderr(cmd *exec.Cmd) (stdout string, stderr string, exitCode int, err error) {
return integration.RunCommandWithStdoutStderr(cmd)
}
func runCommandWithOutputForDuration(cmd *exec.Cmd, duration time.Duration) (output string, exitCode int, timedOut bool, err error) {
return integration.RunCommandWithOutputForDuration(cmd, duration)
}
func runCommandWithOutputAndTimeout(cmd *exec.Cmd, timeout time.Duration) (output string, exitCode int, err error) {
return integration.RunCommandWithOutputAndTimeout(cmd, timeout)
}
func runCommand(cmd *exec.Cmd) (exitCode int, err error) {
return integration.RunCommand(cmd)
// Temporary shim for migrating commands to the new function
func transformCmd(execCmd *exec.Cmd) cmd.Cmd {
return cmd.Cmd{
Command: execCmd.Args,
Env: execCmd.Env,
Dir: execCmd.Dir,
Stdin: execCmd.Stdin,
Stdout: execCmd.Stdout,
}
}
func runCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, exitCode int, err error) {