Browse code

Move `docker restart` tests to integration cli

Docker-DCO-1.1-Signed-off-by: Fabio Falci <fabiofalci@gmail.com> (github: fabiofalci)

Fabio Falci authored on 2014/06/28 00:47:32
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,122 @@
0
+package main
1
+
2
+import (
3
+	"os/exec"
4
+	"strings"
5
+	"testing"
6
+)
7
+
8
+func TestDockerRestartStoppedContainer(t *testing.T) {
9
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
10
+	out, _, err := runCommandWithOutput(runCmd)
11
+	errorOut(err, t, out)
12
+
13
+	cleanedContainerID := stripTrailingCharacters(out)
14
+
15
+	runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
16
+	out, _, err = runCommandWithOutput(runCmd)
17
+	errorOut(err, t, out)
18
+
19
+	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
20
+	out, _, err = runCommandWithOutput(runCmd)
21
+	errorOut(err, t, out)
22
+
23
+	if out != "foobar\n" {
24
+		t.Errorf("container should've printed 'foobar'")
25
+	}
26
+
27
+	runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
28
+	out, _, err = runCommandWithOutput(runCmd)
29
+	errorOut(err, t, out)
30
+
31
+	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
32
+	out, _, err = runCommandWithOutput(runCmd)
33
+	errorOut(err, t, out)
34
+
35
+	if out != "foobar\nfoobar\n" {
36
+		t.Errorf("container should've printed 'foobar' twice")
37
+	}
38
+
39
+	deleteAllContainers()
40
+
41
+	logDone("restart - echo foobar for stopped container")
42
+}
43
+
44
+func TestDockerRestartRunningContainer(t *testing.T) {
45
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
46
+	out, _, err := runCommandWithOutput(runCmd)
47
+	errorOut(err, t, out)
48
+
49
+	cleanedContainerID := stripTrailingCharacters(out)
50
+
51
+	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
52
+	out, _, err = runCommandWithOutput(runCmd)
53
+	errorOut(err, t, out)
54
+
55
+	if out != "foobar\n" {
56
+		t.Errorf("container should've printed 'foobar'")
57
+	}
58
+
59
+	runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID)
60
+	out, _, err = runCommandWithOutput(runCmd)
61
+	errorOut(err, t, out)
62
+
63
+	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
64
+	out, _, err = runCommandWithOutput(runCmd)
65
+	errorOut(err, t, out)
66
+
67
+	if out != "foobar\nfoobar\n" {
68
+		t.Errorf("container should've printed 'foobar' twice")
69
+	}
70
+
71
+	deleteAllContainers()
72
+
73
+	logDone("restart - echo foobar for running container")
74
+}
75
+
76
+// Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
77
+func TestDockerRestartWithVolumes(t *testing.T) {
78
+	runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
79
+	out, _, err := runCommandWithOutput(runCmd)
80
+	errorOut(err, t, out)
81
+
82
+	cleanedContainerID := stripTrailingCharacters(out)
83
+
84
+	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
85
+	out, _, err = runCommandWithOutput(runCmd)
86
+	errorOut(err, t, out)
87
+
88
+	if out = strings.Trim(out, " \n\r"); out != "1" {
89
+		t.Errorf("expect 1 volume received %s", out)
90
+	}
91
+
92
+	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
93
+	volumes, _, err := runCommandWithOutput(runCmd)
94
+	errorOut(err, t, volumes)
95
+
96
+	runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
97
+	out, _, err = runCommandWithOutput(runCmd)
98
+	errorOut(err, t, out)
99
+
100
+	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
101
+	out, _, err = runCommandWithOutput(runCmd)
102
+	errorOut(err, t, out)
103
+
104
+	if out = strings.Trim(out, " \n\r"); out != "1" {
105
+		t.Errorf("expect 1 volume after restart received %s", out)
106
+	}
107
+
108
+	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
109
+	volumesAfterRestart, _, err := runCommandWithOutput(runCmd)
110
+	errorOut(err, t, volumesAfterRestart)
111
+
112
+	if volumes != volumesAfterRestart {
113
+		volumes = strings.Trim(volumes, " \n\r")
114
+		volumesAfterRestart = strings.Trim(volumesAfterRestart, " \n\r")
115
+		t.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
116
+	}
117
+
118
+	deleteAllContainers()
119
+
120
+	logDone("restart - does not create a new volume on restart")
121
+}
... ...
@@ -71,37 +71,6 @@ func TestKillDifferentUser(t *testing.T) {
71 71
 	}
72 72
 }
73 73
 
74
-func TestRestart(t *testing.T) {
75
-	daemon := mkDaemon(t)
76
-	defer nuke(daemon)
77
-	container, _, err := daemon.Create(&runconfig.Config{
78
-		Image: GetTestImage(daemon).ID,
79
-		Cmd:   []string{"echo", "-n", "foobar"},
80
-	},
81
-		"",
82
-	)
83
-	if err != nil {
84
-		t.Fatal(err)
85
-	}
86
-	defer daemon.Destroy(container)
87
-	output, err := container.Output()
88
-	if err != nil {
89
-		t.Fatal(err)
90
-	}
91
-	if string(output) != "foobar" {
92
-		t.Error(string(output))
93
-	}
94
-
95
-	// Run the container again and check the output
96
-	output, err = container.Output()
97
-	if err != nil {
98
-		t.Fatal(err)
99
-	}
100
-	if string(output) != "foobar" {
101
-		t.Error(string(output))
102
-	}
103
-}
104
-
105 74
 func TestRestartStdin(t *testing.T) {
106 75
 	daemon := mkDaemon(t)
107 76
 	defer nuke(daemon)
... ...
@@ -526,47 +495,3 @@ func TestBindMounts(t *testing.T) {
526 526
 		t.Fatal("Container failed to write to bind mount file")
527 527
 	}
528 528
 }
529
-
530
-// Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
531
-func TestRestartWithVolumes(t *testing.T) {
532
-	daemon := mkDaemon(t)
533
-	defer nuke(daemon)
534
-
535
-	container, _, err := daemon.Create(&runconfig.Config{
536
-		Image:   GetTestImage(daemon).ID,
537
-		Cmd:     []string{"echo", "-n", "foobar"},
538
-		Volumes: map[string]struct{}{"/test": {}},
539
-	},
540
-		"",
541
-	)
542
-	if err != nil {
543
-		t.Fatal(err)
544
-	}
545
-	defer daemon.Destroy(container)
546
-
547
-	for key := range container.Config.Volumes {
548
-		if key != "/test" {
549
-			t.Fail()
550
-		}
551
-	}
552
-
553
-	_, err = container.Output()
554
-	if err != nil {
555
-		t.Fatal(err)
556
-	}
557
-
558
-	expected := container.Volumes["/test"]
559
-	if expected == "" {
560
-		t.Fail()
561
-	}
562
-	// Run the container again to verify the volume path persists
563
-	_, err = container.Output()
564
-	if err != nil {
565
-		t.Fatal(err)
566
-	}
567
-
568
-	actual := container.Volumes["/test"]
569
-	if expected != actual {
570
-		t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
571
-	}
572
-}