Browse code

Merge pull request #6780 from unclejack/tweak_cli_integration_tests

Tweak cli integration tests

Tibor Vass authored on 2014/07/03 02:16:26
Showing 3 changed files
... ...
@@ -43,7 +43,7 @@ func TestRemoveContainerWithVolume(t *testing.T) {
43 43
 }
44 44
 
45 45
 func TestRemoveContainerRunning(t *testing.T) {
46
-	cmd := exec.Command(dockerBinary, "run", "-d", "--name", "foo", "busybox", "sleep", "300")
46
+	cmd := exec.Command(dockerBinary, "run", "-dt", "--name", "foo", "busybox", "top")
47 47
 	if _, err := runCommand(cmd); err != nil {
48 48
 		t.Fatal(err)
49 49
 	}
... ...
@@ -8,16 +8,12 @@ import (
8 8
 
9 9
 // tagging a named image in a new unprefixed repo should work
10 10
 func TestTagUnprefixedRepoByName(t *testing.T) {
11
-	pullCmd := exec.Command(dockerBinary, "pull", "busybox")
12
-	out, exitCode, err := runCommandWithOutput(pullCmd)
13
-	errorOut(err, t, fmt.Sprintf("%s %s", out, err))
14
-
15
-	if err != nil || exitCode != 0 {
16
-		t.Fatal("pulling the busybox image from the registry has failed")
11
+	if err := pullImageIfNotExist("busybox:latest"); err != nil {
12
+		t.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
17 13
 	}
18 14
 
19
-	tagCmd := exec.Command(dockerBinary, "tag", "busybox", "testfoobarbaz")
20
-	out, _, err = runCommandWithOutput(tagCmd)
15
+	tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "testfoobarbaz")
16
+	out, _, err := runCommandWithOutput(tagCmd)
21 17
 	errorOut(err, t, fmt.Sprintf("%v %v", out, err))
22 18
 
23 19
 	deleteImages("testfoobarbaz")
... ...
@@ -62,18 +58,14 @@ func TestTagInvalidUnprefixedRepo(t *testing.T) {
62 62
 
63 63
 // ensure we allow the use of valid tags
64 64
 func TestTagValidPrefixedRepo(t *testing.T) {
65
-	pullCmd := exec.Command(dockerBinary, "pull", "busybox")
66
-	out, exitCode, err := runCommandWithOutput(pullCmd)
67
-	errorOut(err, t, fmt.Sprintf("%s %s", out, err))
68
-
69
-	if err != nil || exitCode != 0 {
70
-		t.Fatal("pulling the busybox image from the registry has failed")
65
+	if err := pullImageIfNotExist("busybox:latest"); err != nil {
66
+		t.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
71 67
 	}
72 68
 
73 69
 	validRepos := []string{"fooo/bar", "fooaa/test"}
74 70
 
75 71
 	for _, repo := range validRepos {
76
-		tagCmd := exec.Command(dockerBinary, "tag", "busybox", repo)
72
+		tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", repo)
77 73
 		_, _, err := runCommandWithOutput(tagCmd)
78 74
 		if err != nil {
79 75
 			t.Errorf("tag busybox %v should have worked: %s", repo, err)
... ...
@@ -66,6 +66,27 @@ func deleteImages(images string) error {
66 66
 	return err
67 67
 }
68 68
 
69
+func imageExists(image string) error {
70
+	inspectCmd := exec.Command(dockerBinary, "inspect", image)
71
+	exitCode, err := runCommand(inspectCmd)
72
+	if exitCode != 0 && err == nil {
73
+		err = fmt.Errorf("couldn't find image '%s'", image)
74
+	}
75
+	return err
76
+}
77
+
78
+func pullImageIfNotExist(image string) (err error) {
79
+	if err := imageExists(image); err != nil {
80
+		pullCmd := exec.Command(dockerBinary, "pull", image)
81
+		_, exitCode, err := runCommandWithOutput(pullCmd)
82
+
83
+		if err != nil || exitCode != 0 {
84
+			err = fmt.Errorf("image '%s' wasn't found locally and it couldn't be pulled: %s", image, err)
85
+		}
86
+	}
87
+	return
88
+}
89
+
69 90
 func cmd(t *testing.T, args ...string) (string, int, error) {
70 91
 	out, status, err := runCommandWithOutput(exec.Command(dockerBinary, args...))
71 92
 	errorOut(err, t, fmt.Sprintf("'%s' failed with errors: %v (%v)", strings.Join(args, " "), err, out))