From c34360cc8ebb6c4661a497b543e384685241089e Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 1 Sep 2017 17:52:41 -0400 Subject: [PATCH] Add an end-to-end test for container run for testing attach, remove, and pull image when missing. Signed-off-by: Daniel Nephin --- e2e/container/main_test.go | 17 ++++++++ e2e/container/run_test.go | 42 +++++++++++++++++++ ...run-attached-from-remote-and-remove.golden | 4 ++ scripts/test/e2e/run | 3 +- scripts/test/e2e/wrapper | 1 + 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 e2e/container/main_test.go create mode 100644 e2e/container/run_test.go create mode 100644 e2e/container/testdata/run-attached-from-remote-and-remove.golden diff --git a/e2e/container/main_test.go b/e2e/container/main_test.go new file mode 100644 index 0000000000..4363c43888 --- /dev/null +++ b/e2e/container/main_test.go @@ -0,0 +1,17 @@ +package container + +import ( + "fmt" + "os" + "testing" + + "github.com/docker/cli/internal/test/environment" +) + +func TestMain(m *testing.M) { + if err := environment.Setup(); err != nil { + fmt.Println(err.Error()) + os.Exit(3) + } + os.Exit(m.Run()) +} diff --git a/e2e/container/run_test.go b/e2e/container/run_test.go new file mode 100644 index 0000000000..fa5f28b4fa --- /dev/null +++ b/e2e/container/run_test.go @@ -0,0 +1,42 @@ +package container + +import ( + "fmt" + "testing" + + shlex "github.com/flynn-archive/go-shlex" + "github.com/gotestyourself/gotestyourself/golden" + "github.com/gotestyourself/gotestyourself/icmd" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const alpineImage = "registry:5000/alpine:3.6" + +func TestRunAttachedFromRemoteImageAndRemove(t *testing.T) { + image := createRemoteImage(t) + + result := icmd.RunCmd(shell(t, + "docker run --rm %s echo this is output", image)) + + result.Assert(t, icmd.Success) + assert.Equal(t, "this is output\n", result.Stdout()) + golden.Assert(t, result.Stderr(), "run-attached-from-remote-and-remove.golden") +} + +// TODO: create this with registry API instead of engine API +func createRemoteImage(t *testing.T) string { + image := "registry:5000/alpine:test-run-pulls" + icmd.RunCommand("docker", "pull", alpineImage).Assert(t, icmd.Success) + icmd.RunCommand("docker", "tag", alpineImage, image).Assert(t, icmd.Success) + icmd.RunCommand("docker", "push", image).Assert(t, icmd.Success) + icmd.RunCommand("docker", "rmi", image).Assert(t, icmd.Success) + return image +} + +// TODO: move to gotestyourself +func shell(t *testing.T, format string, args ...interface{}) icmd.Cmd { + cmd, err := shlex.Split(fmt.Sprintf(format, args...)) + require.NoError(t, err) + return icmd.Cmd{Command: cmd} +} diff --git a/e2e/container/testdata/run-attached-from-remote-and-remove.golden b/e2e/container/testdata/run-attached-from-remote-and-remove.golden new file mode 100644 index 0000000000..26377199bf --- /dev/null +++ b/e2e/container/testdata/run-attached-from-remote-and-remove.golden @@ -0,0 +1,4 @@ +Unable to find image 'registry:5000/alpine:test-run-pulls' locally +test-run-pulls: Pulling from alpine +Digest: sha256:0930dd4cc97ed5771ebe9be9caf3e8dc5341e0b5e32e8fb143394d7dfdfa100e +Status: Downloaded newer image for registry:5000/alpine:test-run-pulls diff --git a/scripts/test/e2e/run b/scripts/test/e2e/run index 936b5898fd..4010e756c4 100755 --- a/scripts/test/e2e/run +++ b/scripts/test/e2e/run @@ -39,11 +39,12 @@ function cleanup { function runtests { local engine_host=$1 + # shellcheck disable=SC2086 env -i \ TEST_DOCKER_HOST="$engine_host" \ GOPATH="$GOPATH" \ PATH="$PWD/build/" \ - "$(which go)" test -v ./e2e/... + "$(which go)" test -v ./e2e/... ${TESTFLAGS-} } export unique_id="${E2E_UNIQUE_ID:-cliendtoendsuite}" diff --git a/scripts/test/e2e/wrapper b/scripts/test/e2e/wrapper index a3a4f00b67..0f85f43283 100755 --- a/scripts/test/e2e/wrapper +++ b/scripts/test/e2e/wrapper @@ -27,6 +27,7 @@ testexit=0 docker run -i --rm \ -v "$PWD:/go/src/github.com/docker/cli" \ --network "${unique_id}_default" \ + -e TESTFLAGS \ "$dev_image" \ ./scripts/test/e2e/run test "$engine_host" || testexit="$?" run_in_env cleanup