1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #10909 from ahmetalpbalkan/run_test-defer

integration-cli: Better test cleanup with defer
This commit is contained in:
Jessie Frazelle 2015-02-20 17:28:08 -08:00
commit b062ef05e5
16 changed files with 290 additions and 251 deletions

View file

@ -14,6 +14,8 @@ import (
) )
func TestContainerApiGetAll(t *testing.T) { func TestContainerApiGetAll(t *testing.T) {
defer deleteAllContainers()
startCount, err := getContainerCount() startCount, err := getContainerCount()
if err != nil { if err != nil {
t.Fatalf("Cannot query container count: %v", err) t.Fatalf("Cannot query container count: %v", err)
@ -46,12 +48,12 @@ func TestContainerApiGetAll(t *testing.T) {
t.Fatalf("Container Name mismatch. Expected: %q, received: %q\n", "/"+name, actual) t.Fatalf("Container Name mismatch. Expected: %q, received: %q\n", "/"+name, actual)
} }
deleteAllContainers()
logDone("container REST API - check GET json/all=1") logDone("container REST API - check GET json/all=1")
} }
func TestContainerApiGetExport(t *testing.T) { func TestContainerApiGetExport(t *testing.T) {
defer deleteAllContainers()
name := "exportcontainer" name := "exportcontainer"
runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "touch", "/test") runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "touch", "/test")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
@ -82,12 +84,13 @@ func TestContainerApiGetExport(t *testing.T) {
if !found { if !found {
t.Fatalf("The created test file has not been found in the exported image") t.Fatalf("The created test file has not been found in the exported image")
} }
deleteAllContainers()
logDone("container REST API - check GET containers/export") logDone("container REST API - check GET containers/export")
} }
func TestContainerApiGetChanges(t *testing.T) { func TestContainerApiGetChanges(t *testing.T) {
defer deleteAllContainers()
name := "changescontainer" name := "changescontainer"
runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "rm", "/etc/passwd") runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "rm", "/etc/passwd")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
@ -119,8 +122,6 @@ func TestContainerApiGetChanges(t *testing.T) {
t.Fatalf("/etc/passwd has been removed but is not present in the diff") t.Fatalf("/etc/passwd has been removed but is not present in the diff")
} }
deleteAllContainers()
logDone("container REST API - check GET containers/changes") logDone("container REST API - check GET containers/changes")
} }

View file

@ -7,6 +7,8 @@ import (
) )
func TestInspectApiContainerResponse(t *testing.T) { func TestInspectApiContainerResponse(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true") runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
@ -53,7 +55,5 @@ func TestInspectApiContainerResponse(t *testing.T) {
} }
} }
deleteAllContainers()
logDone("container json - check keys in container json response") logDone("container json - check keys in container json response")
} }

View file

@ -73,6 +73,8 @@ func TestCommitWithoutPause(t *testing.T) {
} }
func TestCommitNewFile(t *testing.T) { func TestCommitNewFile(t *testing.T) {
defer deleteAllContainers()
cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo") cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
if _, err := runCommand(cmd); err != nil { if _, err := runCommand(cmd); err != nil {
t.Fatal(err) t.Fatal(err)
@ -84,6 +86,7 @@ func TestCommitNewFile(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
imageID = strings.Trim(imageID, "\r\n") imageID = strings.Trim(imageID, "\r\n")
defer deleteImages(imageID)
cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo") cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
@ -95,13 +98,12 @@ func TestCommitNewFile(t *testing.T) {
t.Fatalf("expected output koye received %q", actual) t.Fatalf("expected output koye received %q", actual)
} }
deleteAllContainers()
deleteImages(imageID)
logDone("commit - commit file and read") logDone("commit - commit file and read")
} }
func TestCommitHardlink(t *testing.T) { func TestCommitHardlink(t *testing.T) {
defer deleteAllContainers()
cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2") cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
firstOuput, _, err := runCommandWithOutput(cmd) firstOuput, _, err := runCommandWithOutput(cmd)
if err != nil { if err != nil {
@ -127,6 +129,7 @@ func TestCommitHardlink(t *testing.T) {
t.Fatal(imageID, err) t.Fatal(imageID, err)
} }
imageID = strings.Trim(imageID, "\r\n") imageID = strings.Trim(imageID, "\r\n")
defer deleteImages(imageID)
cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2") cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
secondOuput, _, err := runCommandWithOutput(cmd) secondOuput, _, err := runCommandWithOutput(cmd)
@ -147,9 +150,6 @@ func TestCommitHardlink(t *testing.T) {
t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:]) t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
} }
deleteAllContainers()
deleteImages(imageID)
logDone("commit - commit hardlinks") logDone("commit - commit hardlinks")
} }
@ -178,6 +178,8 @@ func TestCommitTTY(t *testing.T) {
} }
func TestCommitWithHostBindMount(t *testing.T) { func TestCommitWithHostBindMount(t *testing.T) {
defer deleteAllContainers()
cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true") cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
if _, err := runCommand(cmd); err != nil { if _, err := runCommand(cmd); err != nil {
t.Fatal(err) t.Fatal(err)
@ -190,6 +192,7 @@ func TestCommitWithHostBindMount(t *testing.T) {
} }
imageID = strings.Trim(imageID, "\r\n") imageID = strings.Trim(imageID, "\r\n")
defer deleteImages(imageID)
cmd = exec.Command(dockerBinary, "run", "bindtest", "true") cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
@ -197,8 +200,5 @@ func TestCommitWithHostBindMount(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
deleteAllContainers()
deleteImages(imageID)
logDone("commit - commit bind mounted file") logDone("commit - commit bind mounted file")
} }

View file

@ -11,6 +11,8 @@ import (
// Make sure we can create a simple container with some args // Make sure we can create a simple container with some args
func TestCreateArgs(t *testing.T) { func TestCreateArgs(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "create", "busybox", "command", "arg1", "arg2", "arg with space") runCmd := exec.Command(dockerBinary, "create", "busybox", "command", "arg1", "arg2", "arg with space")
out, _, _, err := runCommandWithStdoutStderr(runCmd) out, _, _, err := runCommandWithStdoutStderr(runCmd)
if err != nil { if err != nil {
@ -56,13 +58,13 @@ func TestCreateArgs(t *testing.T) {
t.Fatalf("Unexpected args. Expected %v, received: %v", expected, c.Args) t.Fatalf("Unexpected args. Expected %v, received: %v", expected, c.Args)
} }
deleteAllContainers()
logDone("create - args") logDone("create - args")
} }
// Make sure we can set hostconfig options too // Make sure we can set hostconfig options too
func TestCreateHostConfig(t *testing.T) { func TestCreateHostConfig(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "create", "-P", "busybox", "echo") runCmd := exec.Command(dockerBinary, "create", "-P", "busybox", "echo")
out, _, _, err := runCommandWithStdoutStderr(runCmd) out, _, _, err := runCommandWithStdoutStderr(runCmd)
if err != nil { if err != nil {
@ -98,12 +100,12 @@ func TestCreateHostConfig(t *testing.T) {
t.Fatalf("Expected PublishAllPorts, got false") t.Fatalf("Expected PublishAllPorts, got false")
} }
deleteAllContainers()
logDone("create - hostconfig") logDone("create - hostconfig")
} }
func TestCreateWithPortRange(t *testing.T) { func TestCreateWithPortRange(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo") runCmd := exec.Command(dockerBinary, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo")
out, _, _, err := runCommandWithStdoutStderr(runCmd) out, _, _, err := runCommandWithStdoutStderr(runCmd)
if err != nil { if err != nil {
@ -147,12 +149,12 @@ func TestCreateWithPortRange(t *testing.T) {
} }
} }
deleteAllContainers()
logDone("create - port range") logDone("create - port range")
} }
func TestCreateWithiLargePortRange(t *testing.T) { func TestCreateWithiLargePortRange(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo") runCmd := exec.Command(dockerBinary, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo")
out, _, _, err := runCommandWithStdoutStderr(runCmd) out, _, _, err := runCommandWithStdoutStderr(runCmd)
if err != nil { if err != nil {
@ -196,13 +198,13 @@ func TestCreateWithiLargePortRange(t *testing.T) {
} }
} }
deleteAllContainers()
logDone("create - large port range") logDone("create - large port range")
} }
// "test123" should be printed by docker create + start // "test123" should be printed by docker create + start
func TestCreateEchoStdout(t *testing.T) { func TestCreateEchoStdout(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "create", "busybox", "echo", "test123") runCmd := exec.Command(dockerBinary, "create", "busybox", "echo", "test123")
out, _, _, err := runCommandWithStdoutStderr(runCmd) out, _, _, err := runCommandWithStdoutStderr(runCmd)
if err != nil { if err != nil {
@ -221,12 +223,12 @@ func TestCreateEchoStdout(t *testing.T) {
t.Errorf("container should've printed 'test123', got %q", out) t.Errorf("container should've printed 'test123', got %q", out)
} }
deleteAllContainers()
logDone("create - echo test123") logDone("create - echo test123")
} }
func TestCreateVolumesCreated(t *testing.T) { func TestCreateVolumesCreated(t *testing.T) {
defer deleteAllContainers()
name := "test_create_volume" name := "test_create_volume"
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-v", "/foo", "busybox")); err != nil { if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-v", "/foo", "busybox")); err != nil {
t.Fatal(out, err) t.Fatal(out, err)

View file

@ -137,6 +137,8 @@ func TestDaemonStartBridgeWithoutIPAssociation(t *testing.T) {
} }
func TestDaemonIptablesClean(t *testing.T) { func TestDaemonIptablesClean(t *testing.T) {
defer deleteAllContainers()
d := NewDaemon(t) d := NewDaemon(t)
if err := d.StartWithBusybox(); err != nil { if err := d.StartWithBusybox(); err != nil {
t.Fatalf("Could not start daemon with busybox: %v", err) t.Fatalf("Could not start daemon with busybox: %v", err)
@ -174,12 +176,12 @@ func TestDaemonIptablesClean(t *testing.T) {
t.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, out) t.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, out)
} }
deleteAllContainers()
logDone("daemon - run,iptables - iptables rules cleaned after daemon restart") logDone("daemon - run,iptables - iptables rules cleaned after daemon restart")
} }
func TestDaemonIptablesCreate(t *testing.T) { func TestDaemonIptablesCreate(t *testing.T) {
defer deleteAllContainers()
d := NewDaemon(t) d := NewDaemon(t)
if err := d.StartWithBusybox(); err != nil { if err := d.StartWithBusybox(); err != nil {
t.Fatalf("Could not start daemon with busybox: %v", err) t.Fatalf("Could not start daemon with busybox: %v", err)
@ -226,8 +228,6 @@ func TestDaemonIptablesCreate(t *testing.T) {
t.Fatalf("iptables output after restart should have contained %q, but was %q", ipTablesSearchString, out) t.Fatalf("iptables output after restart should have contained %q, but was %q", ipTablesSearchString, out)
} }
deleteAllContainers()
logDone("daemon - run,iptables - iptables rules for always restarted container created after daemon restart") logDone("daemon - run,iptables - iptables rules for always restarted container created after daemon restart")
} }

View file

@ -17,6 +17,8 @@ import (
) )
func TestExec(t *testing.T) { func TestExec(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100") runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil { if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
t.Fatal(out, err) t.Fatal(out, err)
@ -34,8 +36,6 @@ func TestExec(t *testing.T) {
t.Errorf("container exec should've printed %q but printed %q", expected, out) t.Errorf("container exec should've printed %q but printed %q", expected, out)
} }
deleteAllContainers()
logDone("exec - basic test") logDone("exec - basic test")
} }
@ -80,6 +80,8 @@ func TestExecInteractiveStdinClose(t *testing.T) {
} }
func TestExecInteractive(t *testing.T) { func TestExecInteractive(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100") runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil { if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
t.Fatal(out, err) t.Fatal(out, err)
@ -127,12 +129,12 @@ func TestExecInteractive(t *testing.T) {
t.Fatal("docker exec failed to exit on stdin close") t.Fatal("docker exec failed to exit on stdin close")
} }
deleteAllContainers()
logDone("exec - Interactive test") logDone("exec - Interactive test")
} }
func TestExecAfterContainerRestart(t *testing.T) { func TestExecAfterContainerRestart(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top") runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
@ -157,12 +159,12 @@ func TestExecAfterContainerRestart(t *testing.T) {
t.Errorf("container should've printed hello, instead printed %q", outStr) t.Errorf("container should've printed hello, instead printed %q", outStr)
} }
deleteAllContainers()
logDone("exec - exec running container after container restart") logDone("exec - exec running container after container restart")
} }
func TestExecAfterDaemonRestart(t *testing.T) { func TestExecAfterDaemonRestart(t *testing.T) {
defer deleteAllContainers()
d := NewDaemon(t) d := NewDaemon(t)
if err := d.StartWithBusybox(); err != nil { if err := d.StartWithBusybox(); err != nil {
t.Fatalf("Could not start daemon with busybox: %v", err) t.Fatalf("Could not start daemon with busybox: %v", err)
@ -222,6 +224,8 @@ func TestExecEnv(t *testing.T) {
} }
func TestExecExitStatus(t *testing.T) { func TestExecExitStatus(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top") runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil { if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
t.Fatal(out, err) t.Fatal(out, err)
@ -239,7 +243,6 @@ func TestExecExitStatus(t *testing.T) {
} }
func TestExecPausedContainer(t *testing.T) { func TestExecPausedContainer(t *testing.T) {
defer deleteAllContainers() defer deleteAllContainers()
defer unpauseAllContainers() defer unpauseAllContainers()
@ -493,6 +496,8 @@ func TestInspectExecID(t *testing.T) {
} }
func TestLinksPingLinkedContainersOnRename(t *testing.T) { func TestLinksPingLinkedContainersOnRename(t *testing.T) {
defer deleteAllContainers()
var out string var out string
out, _, _ = dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10") out, _, _ = dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
idA := stripTrailingCharacters(out) idA := stripTrailingCharacters(out)
@ -519,8 +524,6 @@ func TestLinksPingLinkedContainersOnRename(t *testing.T) {
t.Fatal(out, err) t.Fatal(out, err)
} }
deleteAllContainers()
logDone("links - ping linked container upon rename") logDone("links - ping linked container upon rename")
} }

View file

@ -15,6 +15,8 @@ import (
) )
func TestLinksEtcHostsRegularFile(t *testing.T) { func TestLinksEtcHostsRegularFile(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts") runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
out, _, _, err := runCommandWithStdoutStderr(runCmd) out, _, _, err := runCommandWithStdoutStderr(runCmd)
if err != nil { if err != nil {
@ -24,13 +26,12 @@ func TestLinksEtcHostsRegularFile(t *testing.T) {
if !strings.HasPrefix(out, "-") { if !strings.HasPrefix(out, "-") {
t.Errorf("/etc/hosts should be a regular file") t.Errorf("/etc/hosts should be a regular file")
} }
deleteAllContainers()
logDone("link - /etc/hosts is a regular file") logDone("link - /etc/hosts is a regular file")
} }
func TestLinksEtcHostsContentMatch(t *testing.T) { func TestLinksEtcHostsContentMatch(t *testing.T) {
defer deleteAllContainers()
testRequires(t, SameHostDaemon) testRequires(t, SameHostDaemon)
runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hosts") runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hosts")
@ -48,8 +49,6 @@ func TestLinksEtcHostsContentMatch(t *testing.T) {
t.Errorf("container") t.Errorf("container")
} }
deleteAllContainers()
logDone("link - /etc/hosts matches hosts copy") logDone("link - /etc/hosts matches hosts copy")
} }
@ -68,6 +67,8 @@ func TestLinksPingUnlinkedContainers(t *testing.T) {
// Test for appropriate error when calling --link with an invalid target container // Test for appropriate error when calling --link with an invalid target container
func TestLinksInvalidContainerTarget(t *testing.T) { func TestLinksInvalidContainerTarget(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "--link", "bogus:alias", "busybox", "true") runCmd := exec.Command(dockerBinary, "run", "--link", "bogus:alias", "busybox", "true")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
@ -77,12 +78,13 @@ func TestLinksInvalidContainerTarget(t *testing.T) {
if !strings.Contains(out, "Could not get container") { if !strings.Contains(out, "Could not get container") {
t.Fatal("error output expected 'Could not get container', but got %q instead; err: %v", out, err) t.Fatal("error output expected 'Could not get container', but got %q instead; err: %v", out, err)
} }
deleteAllContainers()
logDone("links - linking to non-existent container should not work") logDone("links - linking to non-existent container should not work")
} }
func TestLinksPingLinkedContainers(t *testing.T) { func TestLinksPingLinkedContainers(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top") runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
if _, err := runCommand(runCmd); err != nil { if _, err := runCommand(runCmd); err != nil {
t.Fatal(err) t.Fatal(err)
@ -103,11 +105,12 @@ func TestLinksPingLinkedContainers(t *testing.T) {
// 3. Ping by hostname // 3. Ping by hostname
dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...) dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
deleteAllContainers()
logDone("links - ping linked container") logDone("links - ping linked container")
} }
func TestLinksPingLinkedContainersAfterRename(t *testing.T) { func TestLinksPingLinkedContainersAfterRename(t *testing.T) {
defer deleteAllContainers()
out, _, _ := dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10") out, _, _ := dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
idA := stripTrailingCharacters(out) idA := stripTrailingCharacters(out)
out, _, _ = dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10") out, _, _ = dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
@ -116,12 +119,13 @@ func TestLinksPingLinkedContainersAfterRename(t *testing.T) {
dockerCmd(t, "run", "--rm", "--link", "container_new:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1") dockerCmd(t, "run", "--rm", "--link", "container_new:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
dockerCmd(t, "kill", idA) dockerCmd(t, "kill", idA)
dockerCmd(t, "kill", idB) dockerCmd(t, "kill", idB)
deleteAllContainers()
logDone("links - ping linked container after rename") logDone("links - ping linked container after rename")
} }
func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) { func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
defer deleteAllContainers()
testRequires(t, SameHostDaemon) testRequires(t, SameHostDaemon)
dockerCmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "sleep", "10") dockerCmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "sleep", "10")
@ -143,7 +147,6 @@ func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
dockerCmd(t, "kill", "child") dockerCmd(t, "kill", "child")
dockerCmd(t, "kill", "parent") dockerCmd(t, "kill", "parent")
deleteAllContainers()
logDone("link - verify iptables when link and unlink") logDone("link - verify iptables when link and unlink")
} }

View file

@ -10,6 +10,7 @@ import (
func TestNetworkNat(t *testing.T) { func TestNetworkNat(t *testing.T) {
testRequires(t, SameHostDaemon) testRequires(t, SameHostDaemon)
defer deleteAllContainers()
iface, err := net.InterfaceByName("eth0") iface, err := net.InterfaceByName("eth0")
if err != nil { if err != nil {
@ -56,7 +57,6 @@ func TestNetworkNat(t *testing.T) {
if out, _, err = runCommandWithOutput(killCmd); err != nil { if out, _, err = runCommandWithOutput(killCmd); err != nil {
t.Fatalf("failed to kill container: %s, %v", out, err) t.Fatalf("failed to kill container: %s, %v", out, err)
} }
deleteAllContainers()
logDone("network - make sure nat works through the host") logDone("network - make sure nat works through the host")
} }

View file

@ -8,6 +8,8 @@ import (
) )
func TestPortList(t *testing.T) { func TestPortList(t *testing.T) {
defer deleteAllContainers()
// one port // one port
runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top") runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
@ -121,8 +123,6 @@ func TestPortList(t *testing.T) {
t.Fatal(out, err) t.Fatal(out, err)
} }
deleteAllContainers()
logDone("port - test port list") logDone("port - test port list")
} }

View file

@ -11,6 +11,8 @@ import (
) )
func TestPsListContainers(t *testing.T) { func TestPsListContainers(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top") runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
@ -223,8 +225,6 @@ func TestPsListContainers(t *testing.T) {
t.Error("Container list is not in the correct order") t.Error("Container list is not in the correct order")
} }
deleteAllContainers()
logDone("ps - test ps options") logDone("ps - test ps options")
} }
@ -246,6 +246,8 @@ func assertContainerList(out string, expected []string) bool {
} }
func TestPsListContainersSize(t *testing.T) { func TestPsListContainersSize(t *testing.T) {
defer deleteAllContainers()
cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello") cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello")
runCommandWithOutput(cmd) runCommandWithOutput(cmd)
cmd = exec.Command(dockerBinary, "ps", "-s", "-n=1") cmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
@ -296,14 +298,14 @@ func TestPsListContainersSize(t *testing.T) {
t.Fatalf("Expected size %q, got %q", expectedSize, foundSize) t.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
} }
deleteAllContainers()
logDone("ps - test ps size") logDone("ps - test ps size")
} }
func TestPsListContainersFilterStatus(t *testing.T) { func TestPsListContainersFilterStatus(t *testing.T) {
// FIXME: this should test paused, but it makes things hang and its wonky // FIXME: this should test paused, but it makes things hang and its wonky
// this is because paused containers can't be controlled by signals // this is because paused containers can't be controlled by signals
deleteAllContainers() defer deleteAllContainers()
// start exited container // start exited container
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox") runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
@ -347,12 +349,12 @@ func TestPsListContainersFilterStatus(t *testing.T) {
t.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out) t.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
} }
deleteAllContainers()
logDone("ps - test ps filter status") logDone("ps - test ps filter status")
} }
func TestPsListContainersFilterID(t *testing.T) { func TestPsListContainersFilterID(t *testing.T) {
defer deleteAllContainers()
// start container // start container
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox") runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
@ -377,12 +379,12 @@ func TestPsListContainersFilterID(t *testing.T) {
t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out) t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
} }
deleteAllContainers()
logDone("ps - test ps filter id") logDone("ps - test ps filter id")
} }
func TestPsListContainersFilterName(t *testing.T) { func TestPsListContainersFilterName(t *testing.T) {
defer deleteAllContainers()
// start container // start container
runCmd := exec.Command(dockerBinary, "run", "-d", "--name=a_name_to_match", "busybox") runCmd := exec.Command(dockerBinary, "run", "-d", "--name=a_name_to_match", "busybox")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
@ -407,8 +409,6 @@ func TestPsListContainersFilterName(t *testing.T) {
t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out) t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
} }
deleteAllContainers()
logDone("ps - test ps filter name") logDone("ps - test ps filter name")
} }

View file

@ -7,6 +7,8 @@ import (
) )
func TestRenameStoppedContainer(t *testing.T) { func TestRenameStoppedContainer(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
@ -36,12 +38,13 @@ func TestRenameStoppedContainer(t *testing.T) {
if name != "new_name" { if name != "new_name" {
t.Fatal("Failed to rename container ", name) t.Fatal("Failed to rename container ", name)
} }
deleteAllContainers()
logDone("rename - stopped container") logDone("rename - stopped container")
} }
func TestRenameRunningContainer(t *testing.T) { func TestRenameRunningContainer(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
@ -62,12 +65,13 @@ func TestRenameRunningContainer(t *testing.T) {
if name != "new_name" { if name != "new_name" {
t.Fatal("Failed to rename container ") t.Fatal("Failed to rename container ")
} }
deleteAllContainers()
logDone("rename - running container") logDone("rename - running container")
} }
func TestRenameCheckNames(t *testing.T) { func TestRenameCheckNames(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
@ -93,8 +97,6 @@ func TestRenameCheckNames(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
deleteAllContainers()
logDone("rename - running container") logDone("rename - running container")
} }

View file

@ -8,6 +8,8 @@ import (
) )
func TestRestartStoppedContainer(t *testing.T) { func TestRestartStoppedContainer(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar") runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
@ -46,12 +48,12 @@ func TestRestartStoppedContainer(t *testing.T) {
t.Errorf("container should've printed 'foobar' twice") t.Errorf("container should've printed 'foobar' twice")
} }
deleteAllContainers()
logDone("restart - echo foobar for stopped container") logDone("restart - echo foobar for stopped container")
} }
func TestRestartRunningContainer(t *testing.T) { func TestRestartRunningContainer(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'") runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
@ -89,13 +91,13 @@ func TestRestartRunningContainer(t *testing.T) {
t.Errorf("container should've printed 'foobar' twice") t.Errorf("container should've printed 'foobar' twice")
} }
deleteAllContainers()
logDone("restart - echo foobar for running container") logDone("restart - echo foobar for running container")
} }
// Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
func TestRestartWithVolumes(t *testing.T) { func TestRestartWithVolumes(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top") runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
@ -147,8 +149,6 @@ func TestRestartWithVolumes(t *testing.T) {
t.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart) t.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
} }
deleteAllContainers()
logDone("restart - does not create a new volume on restart") logDone("restart - does not create a new volume on restart")
} }

View file

@ -8,6 +8,8 @@ import (
) )
func TestRmContainerWithRemovedVolume(t *testing.T) { func TestRmContainerWithRemovedVolume(t *testing.T) {
defer deleteAllContainers()
cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true") cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
if _, err := runCommand(cmd); err != nil { if _, err := runCommand(cmd); err != nil {
t.Fatal(err) t.Fatal(err)
@ -22,12 +24,12 @@ func TestRmContainerWithRemovedVolume(t *testing.T) {
t.Fatal(out, err) t.Fatal(out, err)
} }
deleteAllContainers()
logDone("rm - removed volume") logDone("rm - removed volume")
} }
func TestRmContainerWithVolume(t *testing.T) { func TestRmContainerWithVolume(t *testing.T) {
defer deleteAllContainers()
cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true") cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
if _, err := runCommand(cmd); err != nil { if _, err := runCommand(cmd); err != nil {
t.Fatal(err) t.Fatal(err)
@ -38,12 +40,12 @@ func TestRmContainerWithVolume(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
deleteAllContainers()
logDone("rm - volume") logDone("rm - volume")
} }
func TestRmRunningContainer(t *testing.T) { func TestRmRunningContainer(t *testing.T) {
defer deleteAllContainers()
createRunningContainer(t, "foo") createRunningContainer(t, "foo")
// Test cannot remove running container // Test cannot remove running container
@ -52,12 +54,12 @@ func TestRmRunningContainer(t *testing.T) {
t.Fatalf("Expected error, can't rm a running container") t.Fatalf("Expected error, can't rm a running container")
} }
deleteAllContainers()
logDone("rm - running container") logDone("rm - running container")
} }
func TestRmRunningContainerCheckError409(t *testing.T) { func TestRmRunningContainerCheckError409(t *testing.T) {
defer deleteAllContainers()
createRunningContainer(t, "foo") createRunningContainer(t, "foo")
endpoint := "/containers/foo" endpoint := "/containers/foo"
@ -70,12 +72,12 @@ func TestRmRunningContainerCheckError409(t *testing.T) {
t.Fatalf("Expected error to contain '409 Conflict' but found %s", err) t.Fatalf("Expected error to contain '409 Conflict' but found %s", err)
} }
deleteAllContainers()
logDone("rm - running container") logDone("rm - running container")
} }
func TestRmForceRemoveRunningContainer(t *testing.T) { func TestRmForceRemoveRunningContainer(t *testing.T) {
defer deleteAllContainers()
createRunningContainer(t, "foo") createRunningContainer(t, "foo")
// Stop then remove with -s // Stop then remove with -s
@ -84,12 +86,12 @@ func TestRmForceRemoveRunningContainer(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
deleteAllContainers()
logDone("rm - running container with --force=true") logDone("rm - running container with --force=true")
} }
func TestRmContainerOrphaning(t *testing.T) { func TestRmContainerOrphaning(t *testing.T) {
defer deleteAllContainers()
dockerfile1 := `FROM busybox:latest dockerfile1 := `FROM busybox:latest
ENTRYPOINT ["/bin/true"]` ENTRYPOINT ["/bin/true"]`
img := "test-container-orphaning" img := "test-container-orphaning"
@ -99,6 +101,7 @@ func TestRmContainerOrphaning(t *testing.T) {
// build first dockerfile // build first dockerfile
img1, err := buildImage(img, dockerfile1, true) img1, err := buildImage(img, dockerfile1, true)
defer deleteImages(img1)
if err != nil { if err != nil {
t.Fatalf("Could not build image %s: %v", img, err) t.Fatalf("Could not build image %s: %v", img, err)
} }
@ -123,9 +126,6 @@ func TestRmContainerOrphaning(t *testing.T) {
t.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out) t.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
} }
deleteAllContainers()
deleteImages(img1)
logDone("rm - container orphaning") logDone("rm - container orphaning")
} }

View file

@ -78,6 +78,8 @@ func TestRmiTag(t *testing.T) {
} }
func TestRmiTagWithExistingContainers(t *testing.T) { func TestRmiTagWithExistingContainers(t *testing.T) {
defer deleteAllContainers()
container := "test-delete-tag" container := "test-delete-tag"
newtag := "busybox:newtag" newtag := "busybox:newtag"
bb := "busybox:latest" bb := "busybox:latest"
@ -95,12 +97,12 @@ func TestRmiTagWithExistingContainers(t *testing.T) {
t.Fatalf("Expected 1 untagged entry got %d: %q", d, out) t.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
} }
deleteAllContainers()
logDone("rmi - delete tag with existing containers") logDone("rmi - delete tag with existing containers")
} }
func TestRmiForceWithExistingContainers(t *testing.T) { func TestRmiForceWithExistingContainers(t *testing.T) {
defer deleteAllContainers()
image := "busybox-clone" image := "busybox-clone"
cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-") cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-")
@ -120,7 +122,5 @@ MAINTAINER foo`)
t.Fatalf("Could not remove image %s: %s, %v", image, out, err) t.Fatalf("Could not remove image %s: %s, %v", image, out, err)
} }
deleteAllContainers()
logDone("rmi - force delete with existing containers") logDone("rmi - force delete with existing containers")
} }

File diff suppressed because it is too large Load diff

View file

@ -56,6 +56,8 @@ func TestRunRedirectStdout(t *testing.T) {
// Test recursive bind mount works by default // Test recursive bind mount works by default
func TestRunWithVolumesIsRecursive(t *testing.T) { func TestRunWithVolumesIsRecursive(t *testing.T) {
defer deleteAllContainers()
tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test") tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -87,7 +89,5 @@ func TestRunWithVolumesIsRecursive(t *testing.T) {
t.Fatal("Recursive bind mount test failed. Expected file not found") t.Fatal("Recursive bind mount test failed. Expected file not found")
} }
deleteAllContainers()
logDone("run - volumes are bind mounted recursively") logDone("run - volumes are bind mounted recursively")
} }