From 5fa134b9061d0b794e9e5b887a8ea4ee15fecb61 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 12 Jul 2017 17:51:46 -0400 Subject: [PATCH] Protect images in the integration/ suite Signed-off-by: Daniel Nephin --- hack/make/.ensure-emptyfs | 21 ++++---- hack/make/test-integration-shell | 2 + integration-cli/check_test.go | 14 +---- integration-cli/environment/protect.go | 40 ++++++++++++++- integration-cli/fixtures_linux_daemon_test.go | 10 ---- integration/container/create_test.go | 51 +------------------ integration/container/main_test.go | 45 ++++++++++++++++ 7 files changed, 96 insertions(+), 87 deletions(-) create mode 100644 integration/container/main_test.go diff --git a/hack/make/.ensure-emptyfs b/hack/make/.ensure-emptyfs index f237905dce..898cc22834 100644 --- a/hack/make/.ensure-emptyfs +++ b/hack/make/.ensure-emptyfs @@ -1,22 +1,19 @@ #!/usr/bin/env bash set -e -if ! docker inspect -t image emptyfs &> /dev/null; then - # let's build a "docker save" tarball for "emptyfs" +if ! docker image inspect emptyfs > /dev/null; then + # build a "docker save" tarball for "emptyfs" # see https://github.com/docker/docker/pull/5262 # and also https://github.com/docker/docker/issues/4242 dir="$DEST/emptyfs" - mkdir -p "$dir" + uuid=511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158 + mkdir -p "$dir/$uuid" ( - cd "$dir" - echo '{"emptyfs":{"latest":"511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158"}}' > repositories - mkdir -p 511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158 - ( - cd 511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158 - echo '{"id":"511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158","comment":"Imported from -","created":"2013-06-13T14:03:50.821769-07:00","container_config":{"Hostname":"","Domainname":"","User":"","Memory":0,"MemorySwap":0,"CpuShares":0,"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"PortSpecs":null,"ExposedPorts":null,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":null,"Image":"","Volumes":null,"WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"OnBuild":null},"docker_version":"0.4.0","architecture":"x86_64","Size":0}' > json - echo '1.0' > VERSION - tar -cf layer.tar --files-from /dev/null - ) + echo '{"emptyfs":{"latest":"511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158"}}' > "$dir/repositories" + cd "$dir/$uuid" + echo '{"id":"511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158","comment":"Imported from -","created":"2013-06-13T14:03:50.821769-07:00","container_config":{"Hostname":"","Domainname":"","User":"","Memory":0,"MemorySwap":0,"CpuShares":0,"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"PortSpecs":null,"ExposedPorts":null,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":null,"Image":"","Volumes":null,"WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"OnBuild":null},"docker_version":"0.4.0","architecture":"x86_64","Size":0}' > json + echo '1.0' > VERSION + tar -cf layer.tar --files-from /dev/null ) ( [ -n "$TESTDEBUG" ] && set -x diff --git a/hack/make/test-integration-shell b/hack/make/test-integration-shell index 2201f5eb37..bcfa4682eb 100644 --- a/hack/make/test-integration-shell +++ b/hack/make/test-integration-shell @@ -5,3 +5,5 @@ bundle .integration-daemon-setup export ABS_DEST bash +e + +bundle .integration-daemon-stop diff --git a/integration-cli/check_test.go b/integration-cli/check_test.go index f05b6504e9..6af5229f04 100644 --- a/integration-cli/check_test.go +++ b/integration-cli/check_test.go @@ -4,10 +4,8 @@ import ( "fmt" "net/http/httptest" "os" - "os/exec" "path" "path/filepath" - "strings" "sync" "syscall" "testing" @@ -72,17 +70,7 @@ func TestMain(m *testing.M) { func Test(t *testing.T) { cli.EnsureTestEnvIsLoaded(t) fakestorage.EnsureTestEnvIsLoaded(t) - cmd := exec.Command(dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}") - cmd.Env = appendBaseEnv(true) - out, err := cmd.CombinedOutput() - if err != nil { - panic(fmt.Errorf("err=%v\nout=%s\n", err, out)) - } - images := strings.Split(strings.TrimSpace(string(out)), "\n") - testEnv.ProtectImage(t, images...) - if testEnv.DaemonPlatform() == "linux" { - ensureFrozenImagesLinux(t) - } + environment.ProtectImages(t, testEnv) check.TestingT(t) } diff --git a/integration-cli/environment/protect.go b/integration-cli/environment/protect.go index 2b0dd6df2f..05c86b7e4a 100644 --- a/integration-cli/environment/protect.go +++ b/integration-cli/environment/protect.go @@ -1,5 +1,16 @@ package environment +import ( + "strings" + + "github.com/docker/docker/integration-cli/fixtures/load" + icmd "github.com/docker/docker/pkg/testutil/cmd" +) + +type protectedElements struct { + images map[string]struct{} +} + // ProtectImage adds the specified image(s) to be protected in case of clean func (e *Execution) ProtectImage(t testingT, images ...string) { for _, image := range images { @@ -7,6 +18,31 @@ func (e *Execution) ProtectImage(t testingT, images ...string) { } } -type protectedElements struct { - images map[string]struct{} +// ProtectImages protects existing images and on linux frozen images from being +// cleaned up at the end of test runs +func ProtectImages(t testingT, testEnv *Execution) { + images := getExistingImages(t, testEnv) + + if testEnv.DaemonPlatform() == "linux" { + images = append(images, ensureFrozenImagesLinux(t, testEnv)...) + } + testEnv.ProtectImage(t, images...) +} + +func getExistingImages(t testingT, testEnv *Execution) []string { + // TODO: use API instead of cli + result := icmd.RunCommand(testEnv.dockerBinary, "images", "-f", "dangling=false", "--format", "{{.Repository}}:{{.Tag}}") + result.Assert(t, icmd.Success) + return strings.Split(strings.TrimSpace(result.Stdout()), "\n") +} + +func ensureFrozenImagesLinux(t testingT, testEnv *Execution) []string { + images := []string{"busybox:latest", "hello-world:frozen", "debian:jessie"} + err := load.FrozenImagesLinux(testEnv.DockerBinary(), images...) + if err != nil { + result := icmd.RunCommand(testEnv.DockerBinary(), "image", "ls") + t.Logf(result.String()) + t.Fatalf("%+v", err) + } + return images } diff --git a/integration-cli/fixtures_linux_daemon_test.go b/integration-cli/fixtures_linux_daemon_test.go index 895f976a18..0011797c0e 100644 --- a/integration-cli/fixtures_linux_daemon_test.go +++ b/integration-cli/fixtures_linux_daemon_test.go @@ -24,16 +24,6 @@ type logT interface { Logf(string, ...interface{}) } -func ensureFrozenImagesLinux(t testingT) { - images := []string{"busybox:latest", "hello-world:frozen", "debian:jessie"} - err := load.FrozenImagesLinux(dockerBinary, images...) - if err != nil { - t.Logf(dockerCmdWithError("images")) - t.Fatalf("%+v", err) - } - defer testEnv.ProtectImage(t, images...) -} - var ensureSyscallTestOnce sync.Once func ensureSyscallTest(c *check.C) { diff --git a/integration/container/create_test.go b/integration/container/create_test.go index 05cd5ccef4..c3aed0d225 100644 --- a/integration/container/create_test.go +++ b/integration/container/create_test.go @@ -2,43 +2,14 @@ package container import ( "context" - "fmt" - "os" - "testing" - "strconv" + "testing" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/network" - "github.com/docker/docker/client" - "github.com/docker/docker/integration-cli/environment" - "github.com/docker/docker/integration-cli/fixtures/load" "github.com/stretchr/testify/require" ) -var ( - testEnv *environment.Execution -) - -func TestMain(m *testing.M) { - var err error - testEnv, err = environment.New() - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - if testEnv.LocalDaemon() { - fmt.Println("INFO: Testing against a local daemon") - } else { - fmt.Println("INFO: Testing against a remote daemon") - } - - // TODO: ensure and protect images - res := m.Run() - os.Exit(res) -} - func TestAPICreateWithNotExistImage(t *testing.T) { defer setupTest(t)() clt := createClient(t) @@ -119,23 +90,3 @@ func TestAPICreateEmptyEnv(t *testing.T) { }) } } - -func createClient(t *testing.T) client.APIClient { - clt, err := client.NewEnvClient() - require.NoError(t, err) - return clt -} - -func setupTest(t *testing.T) func() { - if testEnv.DaemonPlatform() == "linux" { - images := []string{"busybox:latest", "hello-world:frozen", "debian:jessie"} - err := load.FrozenImagesLinux(testEnv.DockerBinary(), images...) - if err != nil { - t.Fatalf("%+v", err) - } - defer testEnv.ProtectImage(t, images...) - } - return func() { - testEnv.Clean(t, testEnv.DockerBinary()) - } -} diff --git a/integration/container/main_test.go b/integration/container/main_test.go new file mode 100644 index 0000000000..9977acb001 --- /dev/null +++ b/integration/container/main_test.go @@ -0,0 +1,45 @@ +package container + +import ( + "fmt" + "os" + "testing" + + "github.com/docker/docker/client" + "github.com/docker/docker/integration-cli/environment" + "github.com/stretchr/testify/require" +) + +var ( + testEnv *environment.Execution +) + +func TestMain(m *testing.M) { + var err error + testEnv, err = environment.New() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // TODO: replace this with `testEnv.Print()` to print the full env + if testEnv.LocalDaemon() { + fmt.Println("INFO: Testing against a local daemon") + } else { + fmt.Println("INFO: Testing against a remote daemon") + } + + res := m.Run() + os.Exit(res) +} + +func createClient(t *testing.T) client.APIClient { + clt, err := client.NewEnvClient() + require.NoError(t, err) + return clt +} + +func setupTest(t *testing.T) func() { + environment.ProtectImages(t, testEnv) + return func() { testEnv.Clean(t, testEnv.DockerBinary()) } +}