Browse code

Improve export/import tests cleanup

Signed-off-by: Arnaud Porterie <arnaud.porterie@docker.com>

Arnaud Porterie authored on 2015/04/17 00:50:20
Showing 1 changed files
... ...
@@ -9,21 +9,24 @@ import (
9 9
 
10 10
 // export an image and try to import it into a new one
11 11
 func TestExportContainerAndImportImage(t *testing.T) {
12
-	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
12
+	containerID := "testexportcontainerandimportimage"
13
+
14
+	defer deleteImages("repo/testexp:v1")
15
+	defer deleteContainer(containerID)
16
+
17
+	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", containerID, "busybox", "true")
13 18
 	out, _, err := runCommandWithOutput(runCmd)
14 19
 	if err != nil {
15 20
 		t.Fatal("failed to create a container", out, err)
16 21
 	}
17 22
 
18
-	cleanedContainerID := strings.TrimSpace(out)
19
-
20
-	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
23
+	inspectCmd := exec.Command(dockerBinary, "inspect", containerID)
21 24
 	out, _, err = runCommandWithOutput(inspectCmd)
22 25
 	if err != nil {
23
-		t.Fatalf("output should've been a container id: %s %s ", cleanedContainerID, err)
26
+		t.Fatalf("output should've been a container id: %s %s ", containerID, err)
24 27
 	}
25 28
 
26
-	exportCmd := exec.Command(dockerBinary, "export", cleanedContainerID)
29
+	exportCmd := exec.Command(dockerBinary, "export", containerID)
27 30
 	if out, _, err = runCommandWithOutput(exportCmd); err != nil {
28 31
 		t.Fatalf("failed to export container: %s, %v", out, err)
29 32
 	}
... ...
@@ -42,29 +45,31 @@ func TestExportContainerAndImportImage(t *testing.T) {
42 42
 		t.Fatalf("output should've been an image id: %s, %v", out, err)
43 43
 	}
44 44
 
45
-	deleteContainer(cleanedContainerID)
46
-	deleteImages("repo/testexp:v1")
47
-
48 45
 	logDone("export - export/import a container/image")
49 46
 }
50 47
 
51 48
 // Used to test output flag in the export command
52 49
 func TestExportContainerWithOutputAndImportImage(t *testing.T) {
53
-	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
50
+	containerID := "testexportcontainerwithoutputandimportimage"
51
+
52
+	defer deleteImages("repo/testexp:v1")
53
+	defer deleteContainer(containerID)
54
+
55
+	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", containerID, "busybox", "true")
54 56
 	out, _, err := runCommandWithOutput(runCmd)
55 57
 	if err != nil {
56 58
 		t.Fatal("failed to create a container", out, err)
57 59
 	}
58 60
 
59
-	cleanedContainerID := strings.TrimSpace(out)
60
-
61
-	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
61
+	inspectCmd := exec.Command(dockerBinary, "inspect", containerID)
62 62
 	out, _, err = runCommandWithOutput(inspectCmd)
63 63
 	if err != nil {
64
-		t.Fatalf("output should've been a container id: %s %s ", cleanedContainerID, err)
64
+		t.Fatalf("output should've been a container id: %s %s ", containerID, err)
65 65
 	}
66 66
 
67
-	exportCmd := exec.Command(dockerBinary, "export", "--output=testexp.tar", cleanedContainerID)
67
+	defer os.Remove("testexp.tar")
68
+
69
+	exportCmd := exec.Command(dockerBinary, "export", "--output=testexp.tar", containerID)
68 70
 	if out, _, err = runCommandWithOutput(exportCmd); err != nil {
69 71
 		t.Fatalf("failed to export container: %s, %v", out, err)
70 72
 	}
... ...
@@ -88,10 +93,5 @@ func TestExportContainerWithOutputAndImportImage(t *testing.T) {
88 88
 		t.Fatalf("output should've been an image id: %s, %v", out, err)
89 89
 	}
90 90
 
91
-	deleteContainer(cleanedContainerID)
92
-	deleteImages("repo/testexp:v1")
93
-
94
-	os.Remove("/tmp/testexp.tar")
95
-
96 91
 	logDone("export - export/import a container/image with output flag")
97 92
 }