Browse code

Add TestDaemonRestartWithRunningContainersPorts

Docker-DCO-1.1-Signed-off-by: Tibor Vass <teabee89@gmail.com> (github: tiborvass)

Tibor Vass authored on 2014/07/24 03:39:14
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,50 @@
0
+package main
1
+
2
+import (
3
+	"strings"
4
+	"testing"
5
+)
6
+
7
+func TestDaemonRestartWithRunningContainersPorts(t *testing.T) {
8
+	d := NewDaemon(t)
9
+	if err := d.StartWithBusybox(); err != nil {
10
+		t.Fatalf("Could not start daemon with busybox: %v", err)
11
+	}
12
+	defer d.Stop()
13
+
14
+	if out, err := d.Cmd("run", "-d", "--name", "top1", "-p", "1234:80", "--restart", "always", "busybox:latest", "top"); err != nil {
15
+		t.Fatalf("Could not run top1: err=%v\n%s", err, out)
16
+	}
17
+	// --restart=no by default
18
+	if out, err := d.Cmd("run", "-d", "--name", "top2", "-p", "80", "busybox:latest", "top"); err != nil {
19
+		t.Fatalf("Could not run top2: err=%v\n%s", err, out)
20
+	}
21
+
22
+	testRun := func(m map[string]bool, prefix string) {
23
+		var format string
24
+		for c, shouldRun := range m {
25
+			out, err := d.Cmd("ps")
26
+			if err != nil {
27
+				t.Fatalf("Could not run ps: err=%v\n%q", err, out)
28
+			}
29
+			if shouldRun {
30
+				format = "%scontainer %q is not running"
31
+			} else {
32
+				format = "%scontainer %q is running"
33
+			}
34
+			if shouldRun != strings.Contains(out, c) {
35
+				t.Fatalf(format, prefix, c)
36
+			}
37
+		}
38
+	}
39
+
40
+	testRun(map[string]bool{"top1": true, "top2": true}, "")
41
+
42
+	if err := d.Restart(); err != nil {
43
+		t.Fatalf("Could not restart daemon: %v", err)
44
+	}
45
+
46
+	testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
47
+
48
+	logDone("daemon - running containers on daemon restart")
49
+}