Browse code

Add some push test coverage

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

Arnaud Porterie authored on 2015/01/14 03:46:32
Showing 3 changed files
... ...
@@ -3,30 +3,28 @@ package main
3 3
 import (
4 4
 	"fmt"
5 5
 	"os/exec"
6
+	"strings"
6 7
 	"testing"
8
+	"time"
7 9
 )
8 10
 
9
-// these tests need a freshly started empty private docker registry
10
-
11 11
 // pulling an image from the central registry should work
12 12
 func TestPushBusyboxImage(t *testing.T) {
13
-	reg, err := newTestRegistryV2(t)
14
-	if err != nil {
15
-		t.Fatal(err)
16
-	}
17
-	defer reg.Close()
18
-	repoName := fmt.Sprintf("%v/dockercli/busybox", reg.URL)
13
+	defer setupRegistry(t)()
14
+
15
+	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
19 16
 	// tag the image to upload it tot he private registry
20 17
 	tagCmd := exec.Command(dockerBinary, "tag", "busybox", repoName)
21 18
 	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
22 19
 		t.Fatalf("image tagging failed: %s, %v", out, err)
23 20
 	}
24 21
 	defer deleteImages(repoName)
22
+
25 23
 	pushCmd := exec.Command(dockerBinary, "push", repoName)
26 24
 	if out, _, err := runCommandWithOutput(pushCmd); err != nil {
27 25
 		t.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
28 26
 	}
29
-	logDone("push - push busybox to private registry")
27
+	logDone("push - busybox to private registry")
30 28
 }
31 29
 
32 30
 // pushing an image without a prefix should throw an error
... ...
@@ -35,5 +33,50 @@ func TestPushUnprefixedRepo(t *testing.T) {
35 35
 	if out, _, err := runCommandWithOutput(pushCmd); err == nil {
36 36
 		t.Fatalf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out)
37 37
 	}
38
-	logDone("push - push unprefixed busybox repo --> must fail")
38
+	logDone("push - unprefixed busybox repo must fail")
39
+}
40
+
41
+func TestPushUntagged(t *testing.T) {
42
+	defer setupRegistry(t)()
43
+
44
+	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
45
+
46
+	expected := "does not exist"
47
+	pushCmd := exec.Command(dockerBinary, "push", repoName)
48
+	if out, _, err := runCommandWithOutput(pushCmd); err == nil {
49
+		t.Fatalf("pushing the image to the private registry should have failed: outuput %q", out)
50
+	} else if !strings.Contains(out, expected) {
51
+		t.Fatalf("pushing the image failed with an unexpected message: expected %q, got %q", expected, out)
52
+	}
53
+	logDone("push - untagged image")
54
+}
55
+
56
+func TestPushInterrupt(t *testing.T) {
57
+	defer setupRegistry(t)()
58
+
59
+	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
60
+	// tag the image to upload it tot he private registry
61
+	tagCmd := exec.Command(dockerBinary, "tag", "busybox", repoName)
62
+	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
63
+		t.Fatalf("image tagging failed: %s, %v", out, err)
64
+	}
65
+	defer deleteImages(repoName)
66
+
67
+	pushCmd := exec.Command(dockerBinary, "push", repoName)
68
+	if err := pushCmd.Start(); err != nil {
69
+		t.Fatalf("Failed to start pushing to private registry: %v", err)
70
+	}
71
+
72
+	// Interrupt push (yes, we have no idea at what point it will get killed).
73
+	time.Sleep(200 * time.Millisecond)
74
+	if err := pushCmd.Process.Kill(); err != nil {
75
+		t.Fatalf("Failed to kill push process: %v", err)
76
+	}
77
+	// Try agin
78
+	pushCmd = exec.Command(dockerBinary, "push", repoName)
79
+	if err := pushCmd.Start(); err != nil {
80
+		t.Fatalf("Failed to start pushing to private registry: %v", err)
81
+	}
82
+
83
+	logDone("push - interrupted")
39 84
 }
... ...
@@ -864,3 +864,11 @@ func readContainerFile(containerId, filename string) ([]byte, error) {
864 864
 
865 865
 	return content, nil
866 866
 }
867
+
868
+func setupRegistry(t *testing.T) func() {
869
+	reg, err := newTestRegistryV2(t)
870
+	if err != nil {
871
+		t.Fatal(err)
872
+	}
873
+	return func() { reg.Close() }
874
+}
... ...
@@ -12,7 +12,6 @@ import (
12 12
 const v2binary = "registry-v2"
13 13
 
14 14
 type testRegistryV2 struct {
15
-	URL string
16 15
 	cmd *exec.Cmd
17 16
 	dir string
18 17
 }
... ...
@@ -24,7 +23,7 @@ storage:
24 24
     filesystem:
25 25
         rootdirectory: %s
26 26
 http:
27
-    addr: :%s`
27
+    addr: %s`
28 28
 	tmp, err := ioutil.TempDir("", "registry-test-")
29 29
 	if err != nil {
30 30
 		return nil, err
... ...
@@ -34,7 +33,7 @@ http:
34 34
 	if err != nil {
35 35
 		return nil, err
36 36
 	}
37
-	if _, err := fmt.Fprintf(config, template, tmp, "5000"); err != nil {
37
+	if _, err := fmt.Fprintf(config, template, tmp, privateRegistryURL); err != nil {
38 38
 		os.RemoveAll(tmp)
39 39
 		return nil, err
40 40
 	}
... ...
@@ -50,7 +49,6 @@ http:
50 50
 	return &testRegistryV2{
51 51
 		cmd: cmd,
52 52
 		dir: tmp,
53
-		URL: "localhost:5000",
54 53
 	}, nil
55 54
 }
56 55