Browse code

Port multiple attach test to cli tests Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/04/18 11:14:00
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,51 @@
0
+package main
1
+
2
+import (
3
+	"os/exec"
4
+	"strings"
5
+	"sync"
6
+	"testing"
7
+	"time"
8
+)
9
+
10
+func TestMultipleAttachRestart(t *testing.T) {
11
+	cmd := exec.Command(dockerBinary, "run", "--name", "attacher", "-d", "busybox",
12
+		"/bin/sh", "-c", "sleep 1 && echo hello")
13
+
14
+	group := sync.WaitGroup{}
15
+	group.Add(4)
16
+
17
+	go func() {
18
+		defer group.Done()
19
+		out, _, err := runCommandWithOutput(cmd)
20
+		if err != nil {
21
+			t.Fatal(err, out)
22
+		}
23
+	}()
24
+	time.Sleep(500 * time.Millisecond)
25
+
26
+	for i := 0; i < 3; i++ {
27
+		go func() {
28
+			defer group.Done()
29
+			c := exec.Command(dockerBinary, "attach", "attacher")
30
+
31
+			out, _, err := runCommandWithOutput(c)
32
+			if err != nil {
33
+				t.Fatal(err, out)
34
+			}
35
+			if actual := strings.Trim(out, "\r\n"); actual != "hello" {
36
+				t.Fatalf("unexpected output %s expected hello", actual)
37
+			}
38
+		}()
39
+	}
40
+
41
+	group.Wait()
42
+
43
+	cmd = exec.Command(dockerBinary, "kill", "attacher")
44
+	if _, err := runCommand(cmd); err != nil {
45
+		t.Fatal(err)
46
+	}
47
+	deleteAllContainers()
48
+
49
+	logDone("run - multiple attach")
50
+}
... ...
@@ -16,101 +16,6 @@ import (
16 16
 	"time"
17 17
 )
18 18
 
19
-func TestMultipleAttachRestart(t *testing.T) {
20
-	daemon := mkDaemon(t)
21
-	defer nuke(daemon)
22
-	container, _, _ := mkContainer(
23
-		daemon,
24
-		[]string{"_", "/bin/sh", "-c", "i=1; while [ $i -le 5 ]; do i=`expr $i + 1`;  echo hello; done"},
25
-		t,
26
-	)
27
-	defer daemon.Destroy(container)
28
-
29
-	// Simulate 3 client attaching to the container and stop/restart
30
-
31
-	stdout1, err := container.StdoutPipe()
32
-	if err != nil {
33
-		t.Fatal(err)
34
-	}
35
-	stdout2, err := container.StdoutPipe()
36
-	if err != nil {
37
-		t.Fatal(err)
38
-	}
39
-	stdout3, err := container.StdoutPipe()
40
-	if err != nil {
41
-		t.Fatal(err)
42
-	}
43
-	if err := container.Start(); err != nil {
44
-		t.Fatal(err)
45
-	}
46
-	l1, err := bufio.NewReader(stdout1).ReadString('\n')
47
-	if err != nil {
48
-		t.Fatal(err)
49
-	}
50
-	if strings.Trim(l1, " \r\n") != "hello" {
51
-		t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
52
-	}
53
-	l2, err := bufio.NewReader(stdout2).ReadString('\n')
54
-	if err != nil {
55
-		t.Fatal(err)
56
-	}
57
-	if strings.Trim(l2, " \r\n") != "hello" {
58
-		t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
59
-	}
60
-	l3, err := bufio.NewReader(stdout3).ReadString('\n')
61
-	if err != nil {
62
-		t.Fatal(err)
63
-	}
64
-	if strings.Trim(l3, " \r\n") != "hello" {
65
-		t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
66
-	}
67
-
68
-	if err := container.Stop(10); err != nil {
69
-		t.Fatal(err)
70
-	}
71
-
72
-	stdout1, err = container.StdoutPipe()
73
-	if err != nil {
74
-		t.Fatal(err)
75
-	}
76
-	stdout2, err = container.StdoutPipe()
77
-	if err != nil {
78
-		t.Fatal(err)
79
-	}
80
-	stdout3, err = container.StdoutPipe()
81
-	if err != nil {
82
-		t.Fatal(err)
83
-	}
84
-	if err := container.Start(); err != nil {
85
-		t.Fatal(err)
86
-	}
87
-
88
-	setTimeout(t, "Timeout reading from the process", 3*time.Second, func() {
89
-		l1, err = bufio.NewReader(stdout1).ReadString('\n')
90
-		if err != nil {
91
-			t.Fatal(err)
92
-		}
93
-		if strings.Trim(l1, " \r\n") != "hello" {
94
-			t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
95
-		}
96
-		l2, err = bufio.NewReader(stdout2).ReadString('\n')
97
-		if err != nil {
98
-			t.Fatal(err)
99
-		}
100
-		if strings.Trim(l2, " \r\n") != "hello" {
101
-			t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
102
-		}
103
-		l3, err = bufio.NewReader(stdout3).ReadString('\n')
104
-		if err != nil {
105
-			t.Fatal(err)
106
-		}
107
-		if strings.Trim(l3, " \r\n") != "hello" {
108
-			t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
109
-		}
110
-	})
111
-	container.Wait()
112
-}
113
-
114 19
 func TestDiff(t *testing.T) {
115 20
 	eng := NewTestEngine(t)
116 21
 	daemon := mkDaemonFromEngine(eng, t)