Better test cleanup with defer

This fixes a few misuses of `deleteAllContainers()` cleanup
method in integration-cli suite by moving call to the
beginning of the method and guaranteeing their execution
(including panics) with `defer`s.

Also added some forgotten cleanup calls while I'm at it.

Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
This commit is contained in:
Ahmet Alp Balkan
2015-02-20 14:04:36 -08:00
parent a78ce5c228
commit 70407ce40c
16 changed files with 290 additions and 251 deletions
+13 -13
View File
@@ -8,6 +8,8 @@ import (
)
func TestRmContainerWithRemovedVolume(t *testing.T) {
defer deleteAllContainers()
cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
if _, err := runCommand(cmd); err != nil {
t.Fatal(err)
@@ -22,12 +24,12 @@ func TestRmContainerWithRemovedVolume(t *testing.T) {
t.Fatal(out, err)
}
deleteAllContainers()
logDone("rm - removed volume")
}
func TestRmContainerWithVolume(t *testing.T) {
defer deleteAllContainers()
cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
if _, err := runCommand(cmd); err != nil {
t.Fatal(err)
@@ -38,12 +40,12 @@ func TestRmContainerWithVolume(t *testing.T) {
t.Fatal(err)
}
deleteAllContainers()
logDone("rm - volume")
}
func TestRmRunningContainer(t *testing.T) {
defer deleteAllContainers()
createRunningContainer(t, "foo")
// Test cannot remove running container
@@ -52,12 +54,12 @@ func TestRmRunningContainer(t *testing.T) {
t.Fatalf("Expected error, can't rm a running container")
}
deleteAllContainers()
logDone("rm - running container")
}
func TestRmRunningContainerCheckError409(t *testing.T) {
defer deleteAllContainers()
createRunningContainer(t, "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)
}
deleteAllContainers()
logDone("rm - running container")
}
func TestRmForceRemoveRunningContainer(t *testing.T) {
defer deleteAllContainers()
createRunningContainer(t, "foo")
// Stop then remove with -s
@@ -84,12 +86,12 @@ func TestRmForceRemoveRunningContainer(t *testing.T) {
t.Fatal(err)
}
deleteAllContainers()
logDone("rm - running container with --force=true")
}
func TestRmContainerOrphaning(t *testing.T) {
defer deleteAllContainers()
dockerfile1 := `FROM busybox:latest
ENTRYPOINT ["/bin/true"]`
img := "test-container-orphaning"
@@ -99,6 +101,7 @@ func TestRmContainerOrphaning(t *testing.T) {
// build first dockerfile
img1, err := buildImage(img, dockerfile1, true)
defer deleteImages(img1)
if err != nil {
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)
}
deleteAllContainers()
deleteImages(img1)
logDone("rm - container orphaning")
}