From be2aa448d07de2b2a3057b451d04f4cb8fffd3d5 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 23 Aug 2017 16:57:27 -0400 Subject: [PATCH 1/3] Add gotestyourself to vendor Signed-off-by: Daniel Nephin Upstream-commit: 0a7ff351b76f369b1cb1bc741e54b0d9018e7410 Component: engine --- .../gotestyourself/icmd/command.go | 274 ++++++++++++++++++ .../gotestyourself/icmd/exitcode.go | 32 ++ .../gotestyourself/gotestyourself/icmd/ops.go | 4 + 3 files changed, 310 insertions(+) create mode 100644 components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/command.go create mode 100644 components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/exitcode.go create mode 100644 components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/ops.go diff --git a/components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/command.go b/components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/command.go new file mode 100644 index 0000000000..8729457b4f --- /dev/null +++ b/components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/command.go @@ -0,0 +1,274 @@ +/*Package icmd executes binaries and provides convenient assertions for testing the results. + */ +package icmd + +import ( + "bytes" + "fmt" + "io" + "os/exec" + "path/filepath" + "runtime" + "strings" + "sync" + "time" +) + +type testingT interface { + Fatalf(string, ...interface{}) +} + +// None is a token to inform Result.Assert that the output should be empty +const None string = "[NOTHING]" + +type lockedBuffer struct { + m sync.RWMutex + buf bytes.Buffer +} + +func (buf *lockedBuffer) Write(b []byte) (int, error) { + buf.m.Lock() + defer buf.m.Unlock() + return buf.buf.Write(b) +} + +func (buf *lockedBuffer) String() string { + buf.m.RLock() + defer buf.m.RUnlock() + return buf.buf.String() +} + +// Result stores the result of running a command +type Result struct { + Cmd *exec.Cmd + ExitCode int + Error error + // Timeout is true if the command was killed because it ran for too long + Timeout bool + outBuffer *lockedBuffer + errBuffer *lockedBuffer +} + +// Assert compares the Result against the Expected struct, and fails the test if +// any of the expectations are not met. +func (r *Result) Assert(t testingT, exp Expected) *Result { + err := r.Compare(exp) + if err == nil { + return r + } + _, file, line, ok := runtime.Caller(1) + if ok { + t.Fatalf("at %s:%d - %s\n", filepath.Base(file), line, err.Error()) + } else { + t.Fatalf("(no file/line info) - %s", err.Error()) + } + return nil +} + +// Compare returns a formatted error with the command, stdout, stderr, exit +// code, and any failed expectations +// nolint: gocyclo +func (r *Result) Compare(exp Expected) error { + errors := []string{} + add := func(format string, args ...interface{}) { + errors = append(errors, fmt.Sprintf(format, args...)) + } + + if exp.ExitCode != r.ExitCode { + add("ExitCode was %d expected %d", r.ExitCode, exp.ExitCode) + } + if exp.Timeout != r.Timeout { + if exp.Timeout { + add("Expected command to timeout") + } else { + add("Expected command to finish, but it hit the timeout") + } + } + if !matchOutput(exp.Out, r.Stdout()) { + add("Expected stdout to contain %q", exp.Out) + } + if !matchOutput(exp.Err, r.Stderr()) { + add("Expected stderr to contain %q", exp.Err) + } + switch { + // If a non-zero exit code is expected there is going to be an error. + // Don't require an error message as well as an exit code because the + // error message is going to be "exit status which is not useful + case exp.Error == "" && exp.ExitCode != 0: + case exp.Error == "" && r.Error != nil: + add("Expected no error") + case exp.Error != "" && r.Error == nil: + add("Expected error to contain %q, but there was no error", exp.Error) + case exp.Error != "" && !strings.Contains(r.Error.Error(), exp.Error): + add("Expected error to contain %q", exp.Error) + } + + if len(errors) == 0 { + return nil + } + return fmt.Errorf("%s\nFailures:\n%s", r, strings.Join(errors, "\n")) +} + +func matchOutput(expected string, actual string) bool { + switch expected { + case None: + return actual == "" + default: + return strings.Contains(actual, expected) + } +} + +func (r *Result) String() string { + var timeout string + if r.Timeout { + timeout = " (timeout)" + } + + return fmt.Sprintf(` +Command: %s +ExitCode: %d%s +Error: %v +Stdout: %v +Stderr: %v +`, + strings.Join(r.Cmd.Args, " "), + r.ExitCode, + timeout, + r.Error, + r.Stdout(), + r.Stderr()) +} + +// Expected is the expected output from a Command. This struct is compared to a +// Result struct by Result.Assert(). +type Expected struct { + ExitCode int + Timeout bool + Error string + Out string + Err string +} + +// Success is the default expected result. A Success result is one with a 0 +// ExitCode. +var Success = Expected{} + +// Stdout returns the stdout of the process as a string +func (r *Result) Stdout() string { + return r.outBuffer.String() +} + +// Stderr returns the stderr of the process as a string +func (r *Result) Stderr() string { + return r.errBuffer.String() +} + +// Combined returns the stdout and stderr combined into a single string +func (r *Result) Combined() string { + return r.outBuffer.String() + r.errBuffer.String() +} + +func (r *Result) setExitError(err error) { + if err == nil { + return + } + r.Error = err + r.ExitCode = processExitCode(err) +} + +// Cmd contains the arguments and options for a process to run as part of a test +// suite. +type Cmd struct { + Command []string + Timeout time.Duration + Stdin io.Reader + Stdout io.Writer + Dir string + Env []string +} + +// Command create a simple Cmd with the specified command and arguments +func Command(command string, args ...string) Cmd { + return Cmd{Command: append([]string{command}, args...)} +} + +// RunCmd runs a command and returns a Result +func RunCmd(cmd Cmd, cmdOperators ...CmdOp) *Result { + for _, op := range cmdOperators { + op(&cmd) + } + result := StartCmd(cmd) + if result.Error != nil { + return result + } + return WaitOnCmd(cmd.Timeout, result) +} + +// RunCommand runs a command with default options, and returns a result +func RunCommand(command string, args ...string) *Result { + return RunCmd(Command(command, args...)) +} + +// StartCmd starts a command, but doesn't wait for it to finish +func StartCmd(cmd Cmd) *Result { + result := buildCmd(cmd) + if result.Error != nil { + return result + } + result.setExitError(result.Cmd.Start()) + return result +} + +func buildCmd(cmd Cmd) *Result { + var execCmd *exec.Cmd + switch len(cmd.Command) { + case 1: + execCmd = exec.Command(cmd.Command[0]) + default: + execCmd = exec.Command(cmd.Command[0], cmd.Command[1:]...) + } + outBuffer := new(lockedBuffer) + errBuffer := new(lockedBuffer) + + execCmd.Stdin = cmd.Stdin + execCmd.Dir = cmd.Dir + execCmd.Env = cmd.Env + if cmd.Stdout != nil { + execCmd.Stdout = io.MultiWriter(outBuffer, cmd.Stdout) + } else { + execCmd.Stdout = outBuffer + } + execCmd.Stderr = errBuffer + return &Result{ + Cmd: execCmd, + outBuffer: outBuffer, + errBuffer: errBuffer, + } +} + +// WaitOnCmd waits for a command to complete. If timeout is non-nil then +// only wait until the timeout. +func WaitOnCmd(timeout time.Duration, result *Result) *Result { + if timeout == time.Duration(0) { + result.setExitError(result.Cmd.Wait()) + return result + } + + done := make(chan error, 1) + // Wait for command to exit in a goroutine + go func() { + done <- result.Cmd.Wait() + }() + + select { + case <-time.After(timeout): + killErr := result.Cmd.Process.Kill() + if killErr != nil { + fmt.Printf("failed to kill (pid=%d): %v\n", result.Cmd.Process.Pid, killErr) + } + result.Timeout = true + case err := <-done: + result.setExitError(err) + } + return result +} diff --git a/components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/exitcode.go b/components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/exitcode.go new file mode 100644 index 0000000000..9356dbcdef --- /dev/null +++ b/components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/exitcode.go @@ -0,0 +1,32 @@ +package icmd + +import ( + "os/exec" + "syscall" + + "github.com/pkg/errors" +) + +// getExitCode returns the ExitStatus of a process from the error returned by +// exec.Run(). If the exit status could not be parsed an error is returned. +func getExitCode(err error) (int, error) { + if exiterr, ok := err.(*exec.ExitError); ok { + if procExit, ok := exiterr.Sys().(syscall.WaitStatus); ok { + return procExit.ExitStatus(), nil + } + } + return 0, errors.Wrap(err, "failed to get exit code") +} + +func processExitCode(err error) (exitCode int) { + if err == nil { + return 0 + } + exitCode, exiterr := getExitCode(err) + if exiterr != nil { + // TODO: Fix this so we check the error's text. + // we've failed to retrieve exit code, so we set it to 127 + return 127 + } + return exitCode +} diff --git a/components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/ops.go b/components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/ops.go new file mode 100644 index 0000000000..02b1d84023 --- /dev/null +++ b/components/engine/vendor/github.com/gotestyourself/gotestyourself/icmd/ops.go @@ -0,0 +1,4 @@ +package icmd + +// CmdOp is an operation which modified a Cmd structure used to execute commands +type CmdOp func(*Cmd) From e8bff97a6641bd70e835a081d5e262e20cd44b36 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 23 Aug 2017 17:01:29 -0400 Subject: [PATCH 2/3] Update tests to use icmd Signed-off-by: Daniel Nephin Upstream-commit: 92427b3a8146e048c4b85e5ece1530da97d8472d Component: engine --- .../engine/integration-cli/cli/build/build.go | 2 +- components/engine/integration-cli/cli/cli.go | 2 +- .../engine/integration-cli/daemon/daemon.go | 2 +- .../integration-cli/docker_cli_attach_test.go | 4 ++-- .../integration-cli/docker_cli_build_test.go | 4 ++-- .../docker_cli_build_unix_test.go | 2 +- .../integration-cli/docker_cli_cp_test.go | 2 +- .../integration-cli/docker_cli_create_test.go | 2 +- .../docker_cli_daemon_plugins_test.go | 2 +- .../integration-cli/docker_cli_daemon_test.go | 6 +++--- .../integration-cli/docker_cli_events_test.go | 11 +++++------ .../integration-cli/docker_cli_exec_test.go | 4 ++-- .../docker_cli_export_import_test.go | 2 +- .../integration-cli/docker_cli_images_test.go | 2 +- .../integration-cli/docker_cli_import_test.go | 4 ++-- .../integration-cli/docker_cli_inspect_test.go | 2 +- .../integration-cli/docker_cli_kill_test.go | 2 +- .../integration-cli/docker_cli_logs_test.go | 2 +- .../docker_cli_network_unix_test.go | 10 +++++----- .../integration-cli/docker_cli_plugins_test.go | 2 +- .../integration-cli/docker_cli_proxy_test.go | 2 +- .../integration-cli/docker_cli_ps_test.go | 4 ++-- .../docker_cli_pull_local_test.go | 2 +- .../docker_cli_pull_trusted_test.go | 2 +- .../integration-cli/docker_cli_push_test.go | 2 +- .../integration-cli/docker_cli_rename_test.go | 4 ++-- .../integration-cli/docker_cli_rmi_test.go | 2 +- .../integration-cli/docker_cli_run_test.go | 4 ++-- .../docker_cli_run_unix_test.go | 2 +- .../docker_cli_save_load_test.go | 2 +- .../docker_cli_save_load_unix_test.go | 2 +- .../docker_cli_service_logs_test.go | 18 +++++++++--------- .../integration-cli/docker_cli_start_test.go | 2 +- .../integration-cli/docker_cli_swarm_test.go | 2 +- .../integration-cli/docker_cli_top_test.go | 4 ++-- .../integration-cli/docker_cli_update_test.go | 2 +- .../integration-cli/docker_cli_volume_test.go | 4 ++-- .../integration-cli/docker_cli_wait_test.go | 2 +- .../docker_experimental_network_test.go | 8 ++++---- .../integration-cli/docker_utils_test.go | 2 +- .../integration-cli/environment/clean.go | 2 +- .../integration-cli/environment/protect.go | 2 +- .../integration-cli/trust_server_test.go | 2 +- .../engine/integration-cli/utils_test.go | 10 +++++----- 44 files changed, 78 insertions(+), 79 deletions(-) diff --git a/components/engine/integration-cli/cli/build/build.go b/components/engine/integration-cli/cli/build/build.go index 8ffaa35b4b..da55df35c6 100644 --- a/components/engine/integration-cli/cli/build/build.go +++ b/components/engine/integration-cli/cli/build/build.go @@ -5,7 +5,7 @@ import ( "strings" "github.com/docker/docker/integration-cli/cli/build/fakecontext" - icmd "github.com/docker/docker/pkg/testutil/cmd" + "github.com/gotestyourself/gotestyourself/icmd" ) type testingT interface { diff --git a/components/engine/integration-cli/cli/cli.go b/components/engine/integration-cli/cli/cli.go index 55ac0913fc..d7fadee47d 100644 --- a/components/engine/integration-cli/cli/cli.go +++ b/components/engine/integration-cli/cli/cli.go @@ -9,7 +9,7 @@ import ( "github.com/docker/docker/integration-cli/daemon" "github.com/docker/docker/integration-cli/environment" - icmd "github.com/docker/docker/pkg/testutil/cmd" + "github.com/gotestyourself/gotestyourself/icmd" "github.com/pkg/errors" ) diff --git a/components/engine/integration-cli/daemon/daemon.go b/components/engine/integration-cli/daemon/daemon.go index 05b5255661..54f3a2f388 100644 --- a/components/engine/integration-cli/daemon/daemon.go +++ b/components/engine/integration-cli/daemon/daemon.go @@ -23,10 +23,10 @@ import ( "github.com/docker/docker/opts" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/stringid" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/docker/go-connections/sockets" "github.com/docker/go-connections/tlsconfig" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "github.com/pkg/errors" "github.com/stretchr/testify/require" "golang.org/x/net/context" diff --git a/components/engine/integration-cli/docker_cli_attach_test.go b/components/engine/integration-cli/docker_cli_attach_test.go index ff319c0d8c..db43beb7d2 100644 --- a/components/engine/integration-cli/docker_cli_attach_test.go +++ b/components/engine/integration-cli/docker_cli_attach_test.go @@ -11,8 +11,8 @@ import ( "time" "github.com/docker/docker/integration-cli/cli" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) const attachWait = 5 * time.Second @@ -168,7 +168,7 @@ func (s *DockerSuite) TestAttachPausedContainer(c *check.C) { dockerCmd(c, "pause", "test") result := dockerCmdWithResult("attach", "test") - c.Assert(result, icmd.Matches, icmd.Expected{ + result.Assert(c, icmd.Expected{ Error: "exit status 1", ExitCode: 1, Err: "You cannot attach to a paused container, unpause it first", diff --git a/components/engine/integration-cli/docker_cli_build_test.go b/components/engine/integration-cli/docker_cli_build_test.go index 5f94b35daa..59213e5404 100644 --- a/components/engine/integration-cli/docker_cli_build_test.go +++ b/components/engine/integration-cli/docker_cli_build_test.go @@ -25,8 +25,8 @@ import ( "github.com/docker/docker/integration-cli/cli/build/fakestorage" "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/stringutils" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" digest "github.com/opencontainers/go-digest" ) @@ -3866,7 +3866,7 @@ func (s *DockerSuite) TestBuildDockerfileOutsideContext(c *check.C) { filepath.Join(ctx, "dockerfile2"), } { result := dockerCmdWithResult("build", "-t", name, "--no-cache", "-f", dockerfilePath, ".") - c.Assert(result, icmd.Matches, icmd.Expected{ + result.Assert(c, icmd.Expected{ Err: "must be within the build context", ExitCode: 1, }) diff --git a/components/engine/integration-cli/docker_cli_build_unix_test.go b/components/engine/integration-cli/docker_cli_build_unix_test.go index 7083fccb17..dbcf00b5d4 100644 --- a/components/engine/integration-cli/docker_cli_build_unix_test.go +++ b/components/engine/integration-cli/docker_cli_build_unix_test.go @@ -19,9 +19,9 @@ import ( "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/cli/build/fakecontext" - icmd "github.com/docker/docker/pkg/testutil/cmd" units "github.com/docker/go-units" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) { diff --git a/components/engine/integration-cli/docker_cli_cp_test.go b/components/engine/integration-cli/docker_cli_cp_test.go index 743398ca21..59248d04a2 100644 --- a/components/engine/integration-cli/docker_cli_cp_test.go +++ b/components/engine/integration-cli/docker_cli_cp_test.go @@ -11,8 +11,8 @@ import ( "strings" "github.com/docker/docker/integration-cli/checker" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) const ( diff --git a/components/engine/integration-cli/docker_cli_create_test.go b/components/engine/integration-cli/docker_cli_create_test.go index d4eb985a31..f5fe0da7f1 100644 --- a/components/engine/integration-cli/docker_cli_create_test.go +++ b/components/engine/integration-cli/docker_cli_create_test.go @@ -14,9 +14,9 @@ import ( "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/cli/build/fakecontext" "github.com/docker/docker/pkg/stringid" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/docker/go-connections/nat" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) // Make sure we can create a simple container with some args diff --git a/components/engine/integration-cli/docker_cli_daemon_plugins_test.go b/components/engine/integration-cli/docker_cli_daemon_plugins_test.go index 66c9f6ecea..10aa514fe0 100644 --- a/components/engine/integration-cli/docker_cli_daemon_plugins_test.go +++ b/components/engine/integration-cli/docker_cli_daemon_plugins_test.go @@ -9,8 +9,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/pkg/mount" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "golang.org/x/sys/unix" ) diff --git a/components/engine/integration-cli/docker_cli_daemon_test.go b/components/engine/integration-cli/docker_cli_daemon_test.go index 4851c1bcda..7e954b9b55 100644 --- a/components/engine/integration-cli/docker_cli_daemon_test.go +++ b/components/engine/integration-cli/docker_cli_daemon_test.go @@ -34,11 +34,11 @@ import ( "github.com/docker/docker/opts" "github.com/docker/docker/pkg/mount" "github.com/docker/docker/pkg/stringid" - icmd "github.com/docker/docker/pkg/testutil/cmd" units "github.com/docker/go-units" "github.com/docker/libnetwork/iptables" "github.com/docker/libtrust" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "github.com/kr/pty" "golang.org/x/sys/unix" ) @@ -834,7 +834,7 @@ func (s *DockerDaemonSuite) TestDaemonDefaultNetworkInvalidClusterConfig(c *chec // Start daemon with docker0 bridge result := icmd.RunCommand("ifconfig", defaultNetworkBridge) - c.Assert(result, icmd.Matches, icmd.Success) + result.Assert(c, icmd.Success) s.d.Restart(c, fmt.Sprintf("--cluster-store=%s", discoveryBackend)) } @@ -2105,7 +2105,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartWithUnpausedRunningContainer(t *che ctrBinary, "--address", "unix:///var/run/docker/libcontainerd/docker-containerd.sock", "containers", "resume", cid) - t.Assert(result, icmd.Matches, icmd.Success) + result.Assert(t, icmd.Success) // Give time to containerd to process the command if we don't // the resume event might be received after we do the inspect diff --git a/components/engine/integration-cli/docker_cli_events_test.go b/components/engine/integration-cli/docker_cli_events_test.go index d98c9fee94..b36f0be14e 100644 --- a/components/engine/integration-cli/docker_cli_events_test.go +++ b/components/engine/integration-cli/docker_cli_events_test.go @@ -18,9 +18,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" - - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "golang.org/x/net/context" ) @@ -70,7 +69,7 @@ func (s *DockerSuite) TestEventsUntag(c *check.C) { Command: []string{dockerBinary, "events", "--since=1"}, Timeout: time.Millisecond * 2500, }) - c.Assert(result, icmd.Matches, icmd.Expected{Timeout: true}) + result.Assert(c, icmd.Expected{Timeout: true}) events := strings.Split(result.Stdout(), "\n") nEvents := len(events) @@ -264,7 +263,7 @@ func (s *DockerSuite) TestEventsImageLoad(c *check.C) { dockerCmd(c, "load", "-i", "saveimg.tar") result := icmd.RunCommand("rm", "-rf", "saveimg.tar") - c.Assert(result, icmd.Matches, icmd.Success) + result.Assert(c, icmd.Success) out, _ = dockerCmd(c, "images", "-q", "--no-trunc", myImageName) imageID := strings.TrimSpace(out) @@ -788,7 +787,7 @@ func (s *DockerSuite) TestEventsFormat(c *check.C) { func (s *DockerSuite) TestEventsFormatBadFunc(c *check.C) { // make sure it fails immediately, without receiving any event result := dockerCmdWithResult("events", "--format", "{{badFuncString .}}") - c.Assert(result, icmd.Matches, icmd.Expected{ + result.Assert(c, icmd.Expected{ Error: "exit status 64", ExitCode: 64, Err: "Error parsing format: template: :1: function \"badFuncString\" not defined", @@ -798,7 +797,7 @@ func (s *DockerSuite) TestEventsFormatBadFunc(c *check.C) { func (s *DockerSuite) TestEventsFormatBadField(c *check.C) { // make sure it fails immediately, without receiving any event result := dockerCmdWithResult("events", "--format", "{{.badFieldString}}") - c.Assert(result, icmd.Matches, icmd.Expected{ + result.Assert(c, icmd.Expected{ Error: "exit status 64", ExitCode: 64, Err: "Error parsing format: template: :1:2: executing \"\" at <.badFieldString>: can't evaluate field badFieldString in type *events.Message", diff --git a/components/engine/integration-cli/docker_cli_exec_test.go b/components/engine/integration-cli/docker_cli_exec_test.go index dfe062b5c8..4442ca2c4c 100644 --- a/components/engine/integration-cli/docker_cli_exec_test.go +++ b/components/engine/integration-cli/docker_cli_exec_test.go @@ -18,8 +18,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "golang.org/x/net/context" ) @@ -133,7 +133,7 @@ func (s *DockerSuite) TestExecExitStatus(c *check.C) { runSleepingContainer(c, "-d", "--name", "top") result := icmd.RunCommand(dockerBinary, "exec", "top", "sh", "-c", "exit 23") - c.Assert(result, icmd.Matches, icmd.Expected{ExitCode: 23, Error: "exit status 23"}) + result.Assert(c, icmd.Expected{ExitCode: 23, Error: "exit status 23"}) } func (s *DockerSuite) TestExecPausedContainer(c *check.C) { diff --git a/components/engine/integration-cli/docker_cli_export_import_test.go b/components/engine/integration-cli/docker_cli_export_import_test.go index fe117b9ae0..45f29d54ed 100644 --- a/components/engine/integration-cli/docker_cli_export_import_test.go +++ b/components/engine/integration-cli/docker_cli_export_import_test.go @@ -5,8 +5,8 @@ import ( "strings" "github.com/docker/docker/integration-cli/checker" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) // export an image and try to import it into a new one diff --git a/components/engine/integration-cli/docker_cli_images_test.go b/components/engine/integration-cli/docker_cli_images_test.go index dccbe12626..2a1152eb50 100644 --- a/components/engine/integration-cli/docker_cli_images_test.go +++ b/components/engine/integration-cli/docker_cli_images_test.go @@ -13,8 +13,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/pkg/stringid" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func (s *DockerSuite) TestImagesEnsureImageIsListed(c *check.C) { diff --git a/components/engine/integration-cli/docker_cli_import_test.go b/components/engine/integration-cli/docker_cli_import_test.go index ac7a180461..eb0fe2cf8c 100644 --- a/components/engine/integration-cli/docker_cli_import_test.go +++ b/components/engine/integration-cli/docker_cli_import_test.go @@ -11,8 +11,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func (s *DockerSuite) TestImportDisplay(c *check.C) { @@ -138,5 +138,5 @@ func (s *DockerSuite) TestImportWithQuotedChanges(c *check.C) { image := strings.TrimSpace(result.Stdout()) result = cli.DockerCmd(c, "run", "--rm", image, "true") - c.Assert(result, icmd.Matches, icmd.Expected{Out: icmd.None}) + result.Assert(c, icmd.Expected{Out: icmd.None}) } diff --git a/components/engine/integration-cli/docker_cli_inspect_test.go b/components/engine/integration-cli/docker_cli_inspect_test.go index 7d3509a5d7..13eb2d38aa 100644 --- a/components/engine/integration-cli/docker_cli_inspect_test.go +++ b/components/engine/integration-cli/docker_cli_inspect_test.go @@ -11,8 +11,8 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/integration-cli/checker" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func checkValidGraphDriver(c *check.C, name string) { diff --git a/components/engine/integration-cli/docker_cli_kill_test.go b/components/engine/integration-cli/docker_cli_kill_test.go index b4fb91c9ea..ea1c269812 100644 --- a/components/engine/integration-cli/docker_cli_kill_test.go +++ b/components/engine/integration-cli/docker_cli_kill_test.go @@ -8,8 +8,8 @@ import ( "github.com/docker/docker/client" "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "golang.org/x/net/context" ) diff --git a/components/engine/integration-cli/docker_cli_logs_test.go b/components/engine/integration-cli/docker_cli_logs_test.go index 8304d6eedb..4f14634b82 100644 --- a/components/engine/integration-cli/docker_cli_logs_test.go +++ b/components/engine/integration-cli/docker_cli_logs_test.go @@ -11,8 +11,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/pkg/jsonlog" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) // This used to work, it test a log of PageSize-1 (gh#4851) diff --git a/components/engine/integration-cli/docker_cli_network_unix_test.go b/components/engine/integration-cli/docker_cli_network_unix_test.go index 5685c7bd0f..2fc0724171 100644 --- a/components/engine/integration-cli/docker_cli_network_unix_test.go +++ b/components/engine/integration-cli/docker_cli_network_unix_test.go @@ -20,7 +20,6 @@ import ( "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/daemon" "github.com/docker/docker/pkg/stringid" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/docker/docker/runconfig" "github.com/docker/libnetwork/driverapi" remoteapi "github.com/docker/libnetwork/drivers/remote/api" @@ -28,6 +27,7 @@ import ( remoteipam "github.com/docker/libnetwork/ipams/remote/api" "github.com/docker/libnetwork/netlabel" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "github.com/vishvananda/netlink" "golang.org/x/sys/unix" ) @@ -482,7 +482,7 @@ func (s *DockerSuite) TestDockerNetworkInspectWithID(c *check.C) { func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) { result := dockerCmdWithResult("network", "inspect", "host", "none") - c.Assert(result, icmd.Matches, icmd.Success) + result.Assert(c, icmd.Success) networkResources := []types.NetworkResource{} err := json.Unmarshal([]byte(result.Stdout()), &networkResources) @@ -494,7 +494,7 @@ func (s *DockerSuite) TestDockerInspectMultipleNetworksIncludingNonexistent(c *c // non-existent network was not at the beginning of the inspect list // This should print an error, return an exitCode 1 and print the host network result := dockerCmdWithResult("network", "inspect", "host", "nonexistent") - c.Assert(result, icmd.Matches, icmd.Expected{ + result.Assert(c, icmd.Expected{ ExitCode: 1, Err: "Error: No such network: nonexistent", Out: "host", @@ -508,7 +508,7 @@ func (s *DockerSuite) TestDockerInspectMultipleNetworksIncludingNonexistent(c *c // Only one non-existent network to inspect // Should print an error and return an exitCode, nothing else result = dockerCmdWithResult("network", "inspect", "nonexistent") - c.Assert(result, icmd.Matches, icmd.Expected{ + result.Assert(c, icmd.Expected{ ExitCode: 1, Err: "Error: No such network: nonexistent", Out: "[]", @@ -517,7 +517,7 @@ func (s *DockerSuite) TestDockerInspectMultipleNetworksIncludingNonexistent(c *c // non-existent network was at the beginning of the inspect list // Should not fail fast, and still print host network but print an error result = dockerCmdWithResult("network", "inspect", "nonexistent", "host") - c.Assert(result, icmd.Matches, icmd.Expected{ + result.Assert(c, icmd.Expected{ ExitCode: 1, Err: "Error: No such network: nonexistent", Out: "host", diff --git a/components/engine/integration-cli/docker_cli_plugins_test.go b/components/engine/integration-cli/docker_cli_plugins_test.go index bacb6c777c..13ae2b0eb6 100644 --- a/components/engine/integration-cli/docker_cli_plugins_test.go +++ b/components/engine/integration-cli/docker_cli_plugins_test.go @@ -16,8 +16,8 @@ import ( "github.com/docker/docker/integration-cli/daemon" "github.com/docker/docker/integration-cli/fixtures/plugin" "github.com/docker/docker/integration-cli/request" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "golang.org/x/net/context" ) diff --git a/components/engine/integration-cli/docker_cli_proxy_test.go b/components/engine/integration-cli/docker_cli_proxy_test.go index 3344985a0a..bdb4772592 100644 --- a/components/engine/integration-cli/docker_cli_proxy_test.go +++ b/components/engine/integration-cli/docker_cli_proxy_test.go @@ -5,8 +5,8 @@ import ( "strings" "github.com/docker/docker/integration-cli/checker" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func (s *DockerSuite) TestCLIProxyDisableProxyUnixSock(c *check.C) { diff --git a/components/engine/integration-cli/docker_cli_ps_test.go b/components/engine/integration-cli/docker_cli_ps_test.go index e44547f3bb..736103e776 100644 --- a/components/engine/integration-cli/docker_cli_ps_test.go +++ b/components/engine/integration-cli/docker_cli_ps_test.go @@ -14,8 +14,8 @@ import ( "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/pkg/stringid" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func (s *DockerSuite) TestPsListContainersBase(c *check.C) { @@ -206,7 +206,7 @@ func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) { c.Assert(containerOut, checker.Equals, secondID) result := cli.Docker(cli.Args("ps", "-a", "-q", "--filter=status=rubbish"), cli.WithTimeout(time.Second*60)) - c.Assert(result, icmd.Matches, icmd.Expected{ + result.Assert(c, icmd.Expected{ ExitCode: 1, Err: "Invalid filter 'status=rubbish'", }) diff --git a/components/engine/integration-cli/docker_cli_pull_local_test.go b/components/engine/integration-cli/docker_cli_pull_local_test.go index a45e313591..79b9390d28 100644 --- a/components/engine/integration-cli/docker_cli_pull_local_test.go +++ b/components/engine/integration-cli/docker_cli_pull_local_test.go @@ -15,8 +15,8 @@ import ( "github.com/docker/distribution/manifest/schema2" "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli/build" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "github.com/opencontainers/go-digest" ) diff --git a/components/engine/integration-cli/docker_cli_pull_trusted_test.go b/components/engine/integration-cli/docker_cli_pull_trusted_test.go index d9628d9710..60e1c3db1d 100644 --- a/components/engine/integration-cli/docker_cli_pull_trusted_test.go +++ b/components/engine/integration-cli/docker_cli_pull_trusted_test.go @@ -7,8 +7,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func (s *DockerTrustSuite) TestTrustedPull(c *check.C) { diff --git a/components/engine/integration-cli/docker_cli_push_test.go b/components/engine/integration-cli/docker_cli_push_test.go index 2ae206df7d..94efa08eab 100644 --- a/components/engine/integration-cli/docker_cli_push_test.go +++ b/components/engine/integration-cli/docker_cli_push_test.go @@ -16,8 +16,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) // Pushing an image to a private registry. diff --git a/components/engine/integration-cli/docker_cli_rename_test.go b/components/engine/integration-cli/docker_cli_rename_test.go index ea430227d9..d043620d4f 100644 --- a/components/engine/integration-cli/docker_cli_rename_test.go +++ b/components/engine/integration-cli/docker_cli_rename_test.go @@ -5,8 +5,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/pkg/stringid" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) { @@ -63,7 +63,7 @@ func (s *DockerSuite) TestRenameCheckNames(c *check.C) { c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name)) result := dockerCmdWithResult("inspect", "-f={{.Name}}", "--type=container", "first_name") - c.Assert(result, icmd.Matches, icmd.Expected{ + result.Assert(c, icmd.Expected{ ExitCode: 1, Err: "No such container: first_name", }) diff --git a/components/engine/integration-cli/docker_cli_rmi_test.go b/components/engine/integration-cli/docker_cli_rmi_test.go index afbc4c2fab..52c8837d28 100644 --- a/components/engine/integration-cli/docker_cli_rmi_test.go +++ b/components/engine/integration-cli/docker_cli_rmi_test.go @@ -9,8 +9,8 @@ import ( "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/pkg/stringid" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func (s *DockerSuite) TestRmiWithContainerFails(c *check.C) { diff --git a/components/engine/integration-cli/docker_cli_run_test.go b/components/engine/integration-cli/docker_cli_run_test.go index 8d18b7357a..504c659884 100644 --- a/components/engine/integration-cli/docker_cli_run_test.go +++ b/components/engine/integration-cli/docker_cli_run_test.go @@ -28,12 +28,12 @@ import ( "github.com/docker/docker/pkg/mount" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/stringutils" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/docker/docker/runconfig" "github.com/docker/go-connections/nat" "github.com/docker/libnetwork/resolvconf" "github.com/docker/libnetwork/types" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" libcontainerUser "github.com/opencontainers/runc/libcontainer/user" ) @@ -1798,7 +1798,7 @@ func (s *DockerSuite) TestRunInteractiveWithRestartPolicy(c *check.C) { }() result = icmd.WaitOnCmd(60*time.Second, result) - c.Assert(result, icmd.Matches, icmd.Expected{ExitCode: 11}) + result.Assert(c, icmd.Expected{ExitCode: 11}) } // Test for #2267 diff --git a/components/engine/integration-cli/docker_cli_run_unix_test.go b/components/engine/integration-cli/docker_cli_run_unix_test.go index b3d1b07218..582f929836 100644 --- a/components/engine/integration-cli/docker_cli_run_unix_test.go +++ b/components/engine/integration-cli/docker_cli_run_unix_test.go @@ -23,8 +23,8 @@ import ( "github.com/docker/docker/pkg/mount" "github.com/docker/docker/pkg/parsers" "github.com/docker/docker/pkg/sysinfo" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "github.com/kr/pty" ) diff --git a/components/engine/integration-cli/docker_cli_save_load_test.go b/components/engine/integration-cli/docker_cli_save_load_test.go index 846f84d3b4..3e6dc2dd39 100644 --- a/components/engine/integration-cli/docker_cli_save_load_test.go +++ b/components/engine/integration-cli/docker_cli_save_load_test.go @@ -17,8 +17,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli/build" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" digest "github.com/opencontainers/go-digest" ) diff --git a/components/engine/integration-cli/docker_cli_save_load_unix_test.go b/components/engine/integration-cli/docker_cli_save_load_unix_test.go index deb0616820..fcbfd7e627 100644 --- a/components/engine/integration-cli/docker_cli_save_load_unix_test.go +++ b/components/engine/integration-cli/docker_cli_save_load_unix_test.go @@ -13,8 +13,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli/build" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "github.com/kr/pty" ) diff --git a/components/engine/integration-cli/docker_cli_service_logs_test.go b/components/engine/integration-cli/docker_cli_service_logs_test.go index d2ce36def0..e95dc0942c 100644 --- a/components/engine/integration-cli/docker_cli_service_logs_test.go +++ b/components/engine/integration-cli/docker_cli_service_logs_test.go @@ -12,8 +12,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/daemon" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) type logMessage struct { @@ -287,7 +287,7 @@ func (s *DockerSwarmSuite) TestServiceLogsTTY(c *check.C) { result = icmd.RunCmd(cmd) // for some reason there is carriage return in the output. i think this is // just expected. - c.Assert(result, icmd.Matches, icmd.Expected{Out: "out\r\nerr\r\n"}) + result.Assert(c, icmd.Expected{Out: "out\r\nerr\r\n"}) } func (s *DockerSwarmSuite) TestServiceLogsNoHangDeletedContainer(c *check.C) { @@ -307,7 +307,7 @@ func (s *DockerSwarmSuite) TestServiceLogsNoHangDeletedContainer(c *check.C) { )) // confirm that the command succeeded - c.Assert(result, icmd.Matches, icmd.Expected{}) + result.Assert(c, icmd.Expected{}) // get the service id id := strings.TrimSpace(result.Stdout()) c.Assert(id, checker.Not(checker.Equals), "") @@ -322,9 +322,9 @@ func (s *DockerSwarmSuite) TestServiceLogsNoHangDeletedContainer(c *check.C) { containerID := strings.TrimSpace(result.Stdout()) c.Assert(containerID, checker.Not(checker.Equals), "") result = icmd.RunCmd(d.Command("stop", containerID)) - c.Assert(result, icmd.Matches, icmd.Expected{Out: containerID}) + result.Assert(c, icmd.Expected{Out: containerID}) result = icmd.RunCmd(d.Command("rm", containerID)) - c.Assert(result, icmd.Matches, icmd.Expected{Out: containerID}) + result.Assert(c, icmd.Expected{Out: containerID}) // run logs. use tail 2 to make sure we don't try to get a bunch of logs // somehow and slow down execution time @@ -336,7 +336,7 @@ func (s *DockerSwarmSuite) TestServiceLogsNoHangDeletedContainer(c *check.C) { // then, assert that the result matches expected. if the command timed out, // if the command is timed out, result.Timeout will be true, but the // Expected defaults to false - c.Assert(result, icmd.Matches, icmd.Expected{}) + result.Assert(c, icmd.Expected{}) } func (s *DockerSwarmSuite) TestServiceLogsDetails(c *check.C) { @@ -376,12 +376,12 @@ func (s *DockerSwarmSuite) TestServiceLogsDetails(c *check.C) { // in this case, we should get details and we should get log message, but // there will also be context as details (which will fall after the detail // we inserted in alphabetical order - c.Assert(result, icmd.Matches, icmd.Expected{Out: "asdf=test1"}) - c.Assert(result, icmd.Matches, icmd.Expected{Out: "LogLine"}) + result.Assert(c, icmd.Expected{Out: "asdf=test1"}) + result.Assert(c, icmd.Expected{Out: "LogLine"}) // call service logs with details. this time, don't pass raw result = icmd.RunCmd(d.Command("service", "logs", "--details", id)) // in this case, we should get details space logmessage as well. the context // is part of the pretty part of the logline - c.Assert(result, icmd.Matches, icmd.Expected{Out: "asdf=test1 LogLine"}) + result.Assert(c, icmd.Expected{Out: "asdf=test1 LogLine"}) } diff --git a/components/engine/integration-cli/docker_cli_start_test.go b/components/engine/integration-cli/docker_cli_start_test.go index 9f54522690..13c1d8ef2f 100644 --- a/components/engine/integration-cli/docker_cli_start_test.go +++ b/components/engine/integration-cli/docker_cli_start_test.go @@ -7,8 +7,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) // Regression test for https://github.com/docker/docker/issues/7843 diff --git a/components/engine/integration-cli/docker_cli_swarm_test.go b/components/engine/integration-cli/docker_cli_swarm_test.go index cffa7f42d4..e7dd5bfd52 100644 --- a/components/engine/integration-cli/docker_cli_swarm_test.go +++ b/components/engine/integration-cli/docker_cli_swarm_test.go @@ -22,12 +22,12 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/daemon" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/docker/libnetwork/driverapi" "github.com/docker/libnetwork/ipamapi" remoteipam "github.com/docker/libnetwork/ipams/remote/api" "github.com/go-check/check" "github.com/gotestyourself/gotestyourself/fs" + "github.com/gotestyourself/gotestyourself/icmd" "github.com/vishvananda/netlink" "golang.org/x/net/context" ) diff --git a/components/engine/integration-cli/docker_cli_top_test.go b/components/engine/integration-cli/docker_cli_top_test.go index ea32fc6722..f52f24014e 100644 --- a/components/engine/integration-cli/docker_cli_top_test.go +++ b/components/engine/integration-cli/docker_cli_top_test.go @@ -4,8 +4,8 @@ import ( "strings" "github.com/docker/docker/integration-cli/checker" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func (s *DockerSuite) TestTopMultipleArgs(c *check.C) { @@ -20,7 +20,7 @@ func (s *DockerSuite) TestTopMultipleArgs(c *check.C) { expected = icmd.Expected{Out: "PID"} } result := dockerCmdWithResult("top", cleanedContainerID, "-o", "pid") - c.Assert(result, icmd.Matches, expected) + result.Assert(c, expected) } func (s *DockerSuite) TestTopNonPrivileged(c *check.C) { diff --git a/components/engine/integration-cli/docker_cli_update_test.go b/components/engine/integration-cli/docker_cli_update_test.go index c898690c5f..5b9b7304c5 100644 --- a/components/engine/integration-cli/docker_cli_update_test.go +++ b/components/engine/integration-cli/docker_cli_update_test.go @@ -6,8 +6,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) func (s *DockerSuite) TestUpdateRestartPolicy(c *check.C) { diff --git a/components/engine/integration-cli/docker_cli_volume_test.go b/components/engine/integration-cli/docker_cli_volume_test.go index 0a2a74fc31..3ca0834806 100644 --- a/components/engine/integration-cli/docker_cli_volume_test.go +++ b/components/engine/integration-cli/docker_cli_volume_test.go @@ -14,8 +14,8 @@ import ( "github.com/docker/docker/client" "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli/build" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "golang.org/x/net/context" ) @@ -58,7 +58,7 @@ func (s *DockerSuite) TestVolumeCLIInspectMulti(c *check.C) { dockerCmd(c, "volume", "create", "test3") result := dockerCmdWithResult("volume", "inspect", "--format={{ .Name }}", "test1", "test2", "doesnotexist", "test3") - c.Assert(result, icmd.Matches, icmd.Expected{ + result.Assert(c, icmd.Expected{ ExitCode: 1, Err: "No such volume: doesnotexist", }) diff --git a/components/engine/integration-cli/docker_cli_wait_test.go b/components/engine/integration-cli/docker_cli_wait_test.go index 6f45bf07a0..e8047042d0 100644 --- a/components/engine/integration-cli/docker_cli_wait_test.go +++ b/components/engine/integration-cli/docker_cli_wait_test.go @@ -7,8 +7,8 @@ import ( "time" "github.com/docker/docker/integration-cli/checker" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) // non-blocking wait with 0 exit code diff --git a/components/engine/integration-cli/docker_experimental_network_test.go b/components/engine/integration-cli/docker_experimental_network_test.go index f352050d3b..888970a0a3 100644 --- a/components/engine/integration-cli/docker_experimental_network_test.go +++ b/components/engine/integration-cli/docker_experimental_network_test.go @@ -9,8 +9,8 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/pkg/parsers/kernel" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) // ensure Kernel version is >= v3.9 for macvlan support @@ -382,7 +382,7 @@ func (s *DockerSuite) TestDockerNetworkMacVlanBridgeInternalMode(c *check.C) { // access outside of the network should fail result := cli.Docker(cli.Args("exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8"), cli.WithTimeout(time.Second)) - c.Assert(result, icmd.Matches, icmd.Expected{Timeout: true}) + result.Assert(c, icmd.Expected{Timeout: true}) // intra-network communications should succeed cli.DockerCmd(c, "exec", "second", "ping", "-c", "1", "first") @@ -421,7 +421,7 @@ func (s *DockerSuite) TestDockerNetworkIpvlanL2InternalMode(c *check.C) { // access outside of the network should fail result := cli.Docker(cli.Args("exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8"), cli.WithTimeout(time.Second)) - c.Assert(result, icmd.Matches, icmd.Expected{Timeout: true}) + result.Assert(c, icmd.Expected{Timeout: true}) // intra-network communications should succeed cli.DockerCmd(c, "exec", "second", "ping", "-c", "1", "first") } @@ -461,7 +461,7 @@ func (s *DockerSuite) TestDockerNetworkIpvlanL3InternalMode(c *check.C) { // access outside of the network should fail result := cli.Docker(cli.Args("exec", "first", "ping", "-c", "1", "-w", "1", "8.8.8.8"), cli.WithTimeout(time.Second)) - c.Assert(result, icmd.Matches, icmd.Expected{Timeout: true}) + result.Assert(c, icmd.Expected{Timeout: true}) // intra-network communications should succeed cli.DockerCmd(c, "exec", "second", "ping", "-c", "1", "first") } diff --git a/components/engine/integration-cli/docker_utils_test.go b/components/engine/integration-cli/docker_utils_test.go index 5ccc5b9c68..79a9e009e8 100644 --- a/components/engine/integration-cli/docker_utils_test.go +++ b/components/engine/integration-cli/docker_utils_test.go @@ -21,8 +21,8 @@ import ( "github.com/docker/docker/integration-cli/daemon" "github.com/docker/docker/integration-cli/registry" "github.com/docker/docker/integration-cli/request" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" "golang.org/x/net/context" ) diff --git a/components/engine/integration-cli/environment/clean.go b/components/engine/integration-cli/environment/clean.go index 2d4f32a979..9df2470153 100644 --- a/components/engine/integration-cli/environment/clean.go +++ b/components/engine/integration-cli/environment/clean.go @@ -7,7 +7,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" - icmd "github.com/docker/docker/pkg/testutil/cmd" + "github.com/gotestyourself/gotestyourself/icmd" "golang.org/x/net/context" ) diff --git a/components/engine/integration-cli/environment/protect.go b/components/engine/integration-cli/environment/protect.go index 05c86b7e4a..173fba5425 100644 --- a/components/engine/integration-cli/environment/protect.go +++ b/components/engine/integration-cli/environment/protect.go @@ -4,7 +4,7 @@ import ( "strings" "github.com/docker/docker/integration-cli/fixtures/load" - icmd "github.com/docker/docker/pkg/testutil/cmd" + "github.com/gotestyourself/gotestyourself/icmd" ) type protectedElements struct { diff --git a/components/engine/integration-cli/trust_server_test.go b/components/engine/integration-cli/trust_server_test.go index 9a999323f8..19abe87196 100644 --- a/components/engine/integration-cli/trust_server_test.go +++ b/components/engine/integration-cli/trust_server_test.go @@ -18,9 +18,9 @@ import ( "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/fixtures/plugin" "github.com/docker/docker/integration-cli/request" - icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/docker/go-connections/tlsconfig" "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" ) var notaryBinary = "notary" diff --git a/components/engine/integration-cli/utils_test.go b/components/engine/integration-cli/utils_test.go index 9d9f83abdd..e09fb80643 100644 --- a/components/engine/integration-cli/utils_test.go +++ b/components/engine/integration-cli/utils_test.go @@ -8,7 +8,7 @@ import ( "strings" "github.com/docker/docker/pkg/stringutils" - "github.com/docker/docker/pkg/testutil/cmd" + "github.com/gotestyourself/gotestyourself/icmd" "github.com/pkg/errors" ) @@ -20,15 +20,15 @@ func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) { } // TODO: update code to call cmd.RunCmd directly, and remove this function -// Deprecated: use pkg/testutil/cmd instead +// Deprecated: use gotestyourself/gotestyourself/icmd func runCommandWithOutput(execCmd *exec.Cmd) (string, int, error) { - result := cmd.RunCmd(transformCmd(execCmd)) + result := icmd.RunCmd(transformCmd(execCmd)) return result.Combined(), result.ExitCode, result.Error } // Temporary shim for migrating commands to the new function -func transformCmd(execCmd *exec.Cmd) cmd.Cmd { - return cmd.Cmd{ +func transformCmd(execCmd *exec.Cmd) icmd.Cmd { + return icmd.Cmd{ Command: execCmd.Args, Env: execCmd.Env, Dir: execCmd.Dir, From 687fe21a977b69cad7c09cf420601cb0d0b4404e Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 23 Aug 2017 17:01:48 -0400 Subject: [PATCH 3/3] Remove pkg/testutil/cmd Signed-off-by: Daniel Nephin Upstream-commit: c2c127fa5a23599d698a71ead103b2f676fdf5c4 Component: engine --- components/engine/pkg/testutil/cmd/command.go | 307 ------------------ .../engine/pkg/testutil/cmd/command_test.go | 118 ------- 2 files changed, 425 deletions(-) delete mode 100644 components/engine/pkg/testutil/cmd/command.go delete mode 100644 components/engine/pkg/testutil/cmd/command_test.go diff --git a/components/engine/pkg/testutil/cmd/command.go b/components/engine/pkg/testutil/cmd/command.go deleted file mode 100644 index 05ca55e47e..0000000000 --- a/components/engine/pkg/testutil/cmd/command.go +++ /dev/null @@ -1,307 +0,0 @@ -package cmd - -import ( - "bytes" - "fmt" - "io" - "os/exec" - "path/filepath" - "runtime" - "strings" - "sync" - "time" - - "github.com/docker/docker/pkg/system" - "github.com/go-check/check" -) - -type testingT interface { - Fatalf(string, ...interface{}) -} - -const ( - // None is a token to inform Result.Assert that the output should be empty - None string = "" -) - -type lockedBuffer struct { - m sync.RWMutex - buf bytes.Buffer -} - -func (buf *lockedBuffer) Write(b []byte) (int, error) { - buf.m.Lock() - defer buf.m.Unlock() - return buf.buf.Write(b) -} - -func (buf *lockedBuffer) String() string { - buf.m.RLock() - defer buf.m.RUnlock() - return buf.buf.String() -} - -// Result stores the result of running a command -type Result struct { - Cmd *exec.Cmd - ExitCode int - Error error - // Timeout is true if the command was killed because it ran for too long - Timeout bool - outBuffer *lockedBuffer - errBuffer *lockedBuffer -} - -// Assert compares the Result against the Expected struct, and fails the test if -// any of the expectations are not met. -func (r *Result) Assert(t testingT, exp Expected) *Result { - err := r.Compare(exp) - if err == nil { - return r - } - _, file, line, ok := runtime.Caller(1) - if ok { - t.Fatalf("at %s:%d - %s\n", filepath.Base(file), line, err.Error()) - } else { - t.Fatalf("(no file/line info) - %s", err.Error()) - } - return nil -} - -// Compare returns a formatted error with the command, stdout, stderr, exit -// code, and any failed expectations -func (r *Result) Compare(exp Expected) error { - errors := []string{} - add := func(format string, args ...interface{}) { - errors = append(errors, fmt.Sprintf(format, args...)) - } - - if exp.ExitCode != r.ExitCode { - add("ExitCode was %d expected %d", r.ExitCode, exp.ExitCode) - } - if exp.Timeout != r.Timeout { - if exp.Timeout { - add("Expected command to timeout") - } else { - add("Expected command to finish, but it hit the timeout") - } - } - if !matchOutput(exp.Out, r.Stdout()) { - add("Expected stdout to contain %q", exp.Out) - } - if !matchOutput(exp.Err, r.Stderr()) { - add("Expected stderr to contain %q", exp.Err) - } - switch { - // If a non-zero exit code is expected there is going to be an error. - // Don't require an error message as well as an exit code because the - // error message is going to be "exit status which is not useful - case exp.Error == "" && exp.ExitCode != 0: - case exp.Error == "" && r.Error != nil: - add("Expected no error") - case exp.Error != "" && r.Error == nil: - add("Expected error to contain %q, but there was no error", exp.Error) - case exp.Error != "" && !strings.Contains(r.Error.Error(), exp.Error): - add("Expected error to contain %q", exp.Error) - } - - if len(errors) == 0 { - return nil - } - return fmt.Errorf("%s\nFailures:\n%s", r, strings.Join(errors, "\n")) -} - -func matchOutput(expected string, actual string) bool { - switch expected { - case None: - return actual == "" - default: - return strings.Contains(actual, expected) - } -} - -func (r *Result) String() string { - var timeout string - if r.Timeout { - timeout = " (timeout)" - } - - return fmt.Sprintf(` -Command: %s -ExitCode: %d%s -Error: %v -Stdout: %v -Stderr: %v -`, - strings.Join(r.Cmd.Args, " "), - r.ExitCode, - timeout, - r.Error, - r.Stdout(), - r.Stderr()) -} - -// Expected is the expected output from a Command. This struct is compared to a -// Result struct by Result.Assert(). -type Expected struct { - ExitCode int - Timeout bool - Error string - Out string - Err string -} - -// Success is the default expected result -var Success = Expected{} - -// Stdout returns the stdout of the process as a string -func (r *Result) Stdout() string { - return r.outBuffer.String() -} - -// Stderr returns the stderr of the process as a string -func (r *Result) Stderr() string { - return r.errBuffer.String() -} - -// Combined returns the stdout and stderr combined into a single string -func (r *Result) Combined() string { - return r.outBuffer.String() + r.errBuffer.String() -} - -// SetExitError sets Error and ExitCode based on Error -func (r *Result) SetExitError(err error) { - if err == nil { - return - } - r.Error = err - r.ExitCode = system.ProcessExitCode(err) -} - -type matches struct{} - -// Info returns the CheckerInfo -func (m *matches) Info() *check.CheckerInfo { - return &check.CheckerInfo{ - Name: "CommandMatches", - Params: []string{"result", "expected"}, - } -} - -// Check compares a result against the expected -func (m *matches) Check(params []interface{}, names []string) (bool, string) { - result, ok := params[0].(*Result) - if !ok { - return false, fmt.Sprintf("result must be a *Result, not %T", params[0]) - } - expected, ok := params[1].(Expected) - if !ok { - return false, fmt.Sprintf("expected must be an Expected, not %T", params[1]) - } - - err := result.Compare(expected) - if err == nil { - return true, "" - } - return false, err.Error() -} - -// Matches is a gocheck.Checker for comparing a Result against an Expected -var Matches = &matches{} - -// Cmd contains the arguments and options for a process to run as part of a test -// suite. -type Cmd struct { - Command []string - Timeout time.Duration - Stdin io.Reader - Stdout io.Writer - Dir string - Env []string -} - -// Command create a simple Cmd with the specified command and arguments -func Command(command string, args ...string) Cmd { - return Cmd{Command: append([]string{command}, args...)} -} - -// RunCmd runs a command and returns a Result -func RunCmd(cmd Cmd, cmdOperators ...func(*Cmd)) *Result { - for _, op := range cmdOperators { - op(&cmd) - } - result := StartCmd(cmd) - if result.Error != nil { - return result - } - return WaitOnCmd(cmd.Timeout, result) -} - -// RunCommand parses a command line and runs it, returning a result -func RunCommand(command string, args ...string) *Result { - return RunCmd(Command(command, args...)) -} - -// StartCmd starts a command, but doesn't wait for it to finish -func StartCmd(cmd Cmd) *Result { - result := buildCmd(cmd) - if result.Error != nil { - return result - } - result.SetExitError(result.Cmd.Start()) - return result -} - -func buildCmd(cmd Cmd) *Result { - var execCmd *exec.Cmd - switch len(cmd.Command) { - case 1: - execCmd = exec.Command(cmd.Command[0]) - default: - execCmd = exec.Command(cmd.Command[0], cmd.Command[1:]...) - } - outBuffer := new(lockedBuffer) - errBuffer := new(lockedBuffer) - - execCmd.Stdin = cmd.Stdin - execCmd.Dir = cmd.Dir - execCmd.Env = cmd.Env - if cmd.Stdout != nil { - execCmd.Stdout = io.MultiWriter(outBuffer, cmd.Stdout) - } else { - execCmd.Stdout = outBuffer - } - execCmd.Stderr = errBuffer - return &Result{ - Cmd: execCmd, - outBuffer: outBuffer, - errBuffer: errBuffer, - } -} - -// WaitOnCmd waits for a command to complete. If timeout is non-nil then -// only wait until the timeout. -func WaitOnCmd(timeout time.Duration, result *Result) *Result { - if timeout == time.Duration(0) { - result.SetExitError(result.Cmd.Wait()) - return result - } - - done := make(chan error, 1) - // Wait for command to exit in a goroutine - go func() { - done <- result.Cmd.Wait() - }() - - select { - case <-time.After(timeout): - killErr := result.Cmd.Process.Kill() - if killErr != nil { - fmt.Printf("failed to kill (pid=%d): %v\n", result.Cmd.Process.Pid, killErr) - } - result.Timeout = true - case err := <-done: - result.SetExitError(err) - } - return result -} diff --git a/components/engine/pkg/testutil/cmd/command_test.go b/components/engine/pkg/testutil/cmd/command_test.go deleted file mode 100644 index d24b42b726..0000000000 --- a/components/engine/pkg/testutil/cmd/command_test.go +++ /dev/null @@ -1,118 +0,0 @@ -package cmd - -import ( - "runtime" - "strings" - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -func TestRunCommand(t *testing.T) { - // TODO Windows: Port this test - if runtime.GOOS == "windows" { - t.Skip("Needs porting to Windows") - } - - var cmd string - if runtime.GOOS == "solaris" { - cmd = "gls" - } else { - cmd = "ls" - } - result := RunCommand(cmd) - result.Assert(t, Expected{}) - - result = RunCommand("doesnotexists") - expectedError := `exec: "doesnotexists": executable file not found` - result.Assert(t, Expected{ExitCode: 127, Error: expectedError}) - - result = RunCommand(cmd, "-z") - result.Assert(t, Expected{ - ExitCode: 2, - Error: "exit status 2", - Err: "invalid option", - }) - assert.Contains(t, result.Combined(), "invalid option") -} - -func TestRunCommandWithCombined(t *testing.T) { - // TODO Windows: Port this test - if runtime.GOOS == "windows" { - t.Skip("Needs porting to Windows") - } - - result := RunCommand("ls", "-a") - result.Assert(t, Expected{}) - - assert.Contains(t, result.Combined(), "..") - assert.Contains(t, result.Stdout(), "..") -} - -func TestRunCommandWithTimeoutFinished(t *testing.T) { - // TODO Windows: Port this test - if runtime.GOOS == "windows" { - t.Skip("Needs porting to Windows") - } - - result := RunCmd(Cmd{ - Command: []string{"ls", "-a"}, - Timeout: 50 * time.Millisecond, - }) - result.Assert(t, Expected{Out: ".."}) -} - -func TestRunCommandWithTimeoutKilled(t *testing.T) { - // TODO Windows: Port this test - if runtime.GOOS == "windows" { - t.Skip("Needs porting to Windows") - } - - command := []string{"sh", "-c", "while true ; do echo 1 ; sleep .5 ; done"} - result := RunCmd(Cmd{Command: command, Timeout: 1250 * time.Millisecond}) - result.Assert(t, Expected{Timeout: true}) - - ones := strings.Split(result.Stdout(), "\n") - assert.Len(t, ones, 4) -} - -func TestRunCommandWithErrors(t *testing.T) { - result := RunCommand("/foobar") - result.Assert(t, Expected{Error: "foobar", ExitCode: 127}) -} - -func TestRunCommandWithStdoutStderr(t *testing.T) { - result := RunCommand("echo", "hello", "world") - result.Assert(t, Expected{Out: "hello world\n", Err: None}) -} - -func TestRunCommandWithStdoutStderrError(t *testing.T) { - result := RunCommand("doesnotexists") - - expected := `exec: "doesnotexists": executable file not found` - result.Assert(t, Expected{Out: None, Err: None, ExitCode: 127, Error: expected}) - - switch runtime.GOOS { - case "windows": - expected = "ls: unknown option" - case "solaris": - expected = "gls: invalid option" - default: - expected = "ls: invalid option" - } - - var cmd string - if runtime.GOOS == "solaris" { - cmd = "gls" - } else { - cmd = "ls" - } - result = RunCommand(cmd, "-z") - result.Assert(t, Expected{ - Out: None, - Err: expected, - ExitCode: 2, - Error: "exit status 2", - }) -}