Browse code

Merge pull request #15412 from vdemeester/dont-sleep-too-much

time.Sleep in integration tests — comment and waitRun/waitInspect

Brian Goff authored on 2015/08/22 09:43:55
Showing 17 changed files
... ...
@@ -767,9 +767,7 @@ func (s *DockerSuite) TestContainerApiPause(c *check.C) {
767 767
 func (s *DockerSuite) TestContainerApiTop(c *check.C) {
768 768
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "top")
769 769
 	id := strings.TrimSpace(string(out))
770
-	if err := waitRun(id); err != nil {
771
-		c.Fatal(err)
772
-	}
770
+	c.Assert(waitRun(id), check.IsNil)
773 771
 
774 772
 	type topResp struct {
775 773
 		Titles    []string
... ...
@@ -14,9 +14,7 @@ import (
14 14
 func (s *DockerSuite) TestLogsApiWithStdout(c *check.C) {
15 15
 	out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 1; done")
16 16
 	id := strings.TrimSpace(out)
17
-	if err := waitRun(id); err != nil {
18
-		c.Fatal(err)
19
-	}
17
+	c.Assert(waitRun(id), check.IsNil)
20 18
 
21 19
 	type logOut struct {
22 20
 		out string
... ...
@@ -17,8 +17,7 @@ func (s *DockerSuite) TestCliStatsNoStreamGetCpu(c *check.C) {
17 17
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true;do echo 'Hello'; usleep 100000; done")
18 18
 
19 19
 	id := strings.TrimSpace(out)
20
-	err := waitRun(id)
21
-	c.Assert(err, check.IsNil)
20
+	c.Assert(waitRun(id), check.IsNil)
22 21
 
23 22
 	resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
24 23
 	c.Assert(err, check.IsNil)
... ...
@@ -79,8 +78,7 @@ func (s *DockerSuite) TestApiNetworkStats(c *check.C) {
79 79
 	// Run container for 30 secs
80 80
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
81 81
 	id := strings.TrimSpace(out)
82
-	err := waitRun(id)
83
-	c.Assert(err, check.IsNil)
82
+	c.Assert(waitRun(id), check.IsNil)
84 83
 
85 84
 	// Retrieve the container address
86 85
 	contIP := findContainerIP(c, id)
... ...
@@ -90,9 +90,7 @@ func (s *DockerSuite) TestAttachTtyWithoutStdin(c *check.C) {
90 90
 	out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
91 91
 
92 92
 	id := strings.TrimSpace(out)
93
-	if err := waitRun(id); err != nil {
94
-		c.Fatal(err)
95
-	}
93
+	c.Assert(waitRun(id), check.IsNil)
96 94
 
97 95
 	defer func() {
98 96
 		cmd := exec.Command(dockerBinary, "kill", id)
... ...
@@ -19,9 +19,7 @@ func (s *DockerSuite) TestAttachClosedOnContainerStop(c *check.C) {
19 19
 	out, _ := dockerCmd(c, "run", "-dti", "busybox", "sleep", "2")
20 20
 
21 21
 	id := strings.TrimSpace(out)
22
-	if err := waitRun(id); err != nil {
23
-		c.Fatal(err)
24
-	}
22
+	c.Assert(waitRun(id), check.IsNil)
25 23
 
26 24
 	errChan := make(chan error)
27 25
 	go func() {
... ...
@@ -73,10 +71,8 @@ func (s *DockerSuite) TestAttachAfterDetach(c *check.C) {
73 73
 		close(errChan)
74 74
 	}()
75 75
 
76
-	time.Sleep(500 * time.Millisecond)
77
-	if err := waitRun(name); err != nil {
78
-		c.Fatal(err)
79
-	}
76
+	c.Assert(waitRun(name), check.IsNil)
77
+
80 78
 	cpty.Write([]byte{16})
81 79
 	time.Sleep(100 * time.Millisecond)
82 80
 	cpty.Write([]byte{17})
... ...
@@ -137,9 +133,7 @@ func (s *DockerSuite) TestAttachAfterDetach(c *check.C) {
137 137
 func (s *DockerSuite) TestAttachDetach(c *check.C) {
138 138
 	out, _ := dockerCmd(c, "run", "-itd", "busybox", "cat")
139 139
 	id := strings.TrimSpace(out)
140
-	if err := waitRun(id); err != nil {
141
-		c.Fatal(err)
142
-	}
140
+	c.Assert(waitRun(id), check.IsNil)
143 141
 
144 142
 	cpty, tty, err := pty.Open()
145 143
 	if err != nil {
... ...
@@ -157,9 +151,7 @@ func (s *DockerSuite) TestAttachDetach(c *check.C) {
157 157
 	if err := cmd.Start(); err != nil {
158 158
 		c.Fatal(err)
159 159
 	}
160
-	if err := waitRun(id); err != nil {
161
-		c.Fatalf("error waiting for container to start: %v", err)
162
-	}
160
+	c.Assert(waitRun(id), check.IsNil)
163 161
 
164 162
 	if _, err := cpty.Write([]byte("hello\n")); err != nil {
165 163
 		c.Fatal(err)
... ...
@@ -211,9 +203,7 @@ func (s *DockerSuite) TestAttachDetach(c *check.C) {
211 211
 func (s *DockerSuite) TestAttachDetachTruncatedID(c *check.C) {
212 212
 	out, _ := dockerCmd(c, "run", "-itd", "busybox", "cat")
213 213
 	id := stringid.TruncateID(strings.TrimSpace(out))
214
-	if err := waitRun(id); err != nil {
215
-		c.Fatal(err)
216
-	}
214
+	c.Assert(waitRun(id), check.IsNil)
217 215
 
218 216
 	cpty, tty, err := pty.Open()
219 217
 	if err != nil {
... ...
@@ -20,12 +20,11 @@ func (s *DockerSuite) TestEventsTimestampFormats(c *check.C) {
20 20
 	image := "busybox"
21 21
 
22 22
 	// Start stopwatch, generate an event
23
-	time.Sleep(time.Second) // so that we don't grab events from previous test occurred in the same second
23
+	time.Sleep(1 * time.Second) // so that we don't grab events from previous test occured in the same second
24 24
 	start := daemonTime(c)
25
-	time.Sleep(time.Second) // remote API precision is only a second, wait a while before creating an event
26 25
 	dockerCmd(c, "tag", image, "timestamptest:1")
27 26
 	dockerCmd(c, "rmi", "timestamptest:1")
28
-	time.Sleep(time.Second) // so that until > since
27
+	time.Sleep(1 * time.Second) // so that until > since
29 28
 	end := daemonTime(c)
30 29
 
31 30
 	// List of available time formats to --since
... ...
@@ -223,7 +222,7 @@ func (s *DockerSuite) TestEventsImageUntagDelete(c *check.C) {
223 223
 }
224 224
 
225 225
 func (s *DockerSuite) TestEventsImageTag(c *check.C) {
226
-	time.Sleep(time.Second * 2) // because API has seconds granularity
226
+	time.Sleep(1 * time.Second) // because API has seconds granularity
227 227
 	since := daemonTime(c).Unix()
228 228
 	image := "testimageevents:tag"
229 229
 	dockerCmd(c, "tag", "busybox", image)
... ...
@@ -186,9 +186,7 @@ func (s *DockerSuite) TestExecTtyCloseStdin(c *check.C) {
186 186
 func (s *DockerSuite) TestExecTtyWithoutStdin(c *check.C) {
187 187
 	out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
188 188
 	id := strings.TrimSpace(out)
189
-	if err := waitRun(id); err != nil {
190
-		c.Fatal(err)
191
-	}
189
+	c.Assert(waitRun(id), check.IsNil)
192 190
 
193 191
 	errChan := make(chan error)
194 192
 	go func() {
... ...
@@ -58,14 +58,14 @@ func (s *DockerSuite) TestImagesOrderedByCreationDate(c *check.C) {
58 58
 	if err != nil {
59 59
 		c.Fatal(err)
60 60
 	}
61
-	time.Sleep(time.Second)
61
+	time.Sleep(1 * time.Second)
62 62
 	id2, err := buildImage("order:test_c",
63 63
 		`FROM scratch
64 64
 		MAINTAINER dockerio2`, true)
65 65
 	if err != nil {
66 66
 		c.Fatal(err)
67 67
 	}
68
-	time.Sleep(time.Second)
68
+	time.Sleep(1 * time.Second)
69 69
 	id3, err := buildImage("order:test_b",
70 70
 		`FROM scratch
71 71
 		MAINTAINER dockerio3`, true)
... ...
@@ -6,7 +6,6 @@ import (
6 6
 	"reflect"
7 7
 	"regexp"
8 8
 	"strings"
9
-	"time"
10 9
 )
11 10
 
12 11
 func (s *DockerSuite) TestLinksPingUnlinkedContainers(c *check.C) {
... ...
@@ -138,7 +137,7 @@ func (s *DockerSuite) TestLinksHostsFilesInject(c *check.C) {
138 138
 	out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top")
139 139
 	idTwo := strings.TrimSpace(out)
140 140
 
141
-	time.Sleep(1 * time.Second)
141
+	c.Assert(waitRun(idTwo), check.IsNil)
142 142
 
143 143
 	contentOne, err := readContainerFileWithExec(idOne, "/etc/hosts")
144 144
 	if err != nil {
... ...
@@ -30,17 +30,13 @@ func (s *DockerSuite) TestPsListContainers(c *check.C) {
30 30
 	fourthID := strings.TrimSpace(out)
31 31
 
32 32
 	// make sure the second is running
33
-	if err := waitRun(secondID); err != nil {
34
-		c.Fatalf("waiting for container failed: %v", err)
35
-	}
33
+	c.Assert(waitRun(secondID), check.IsNil)
36 34
 
37 35
 	// make sure third one is not running
38 36
 	dockerCmd(c, "wait", thirdID)
39 37
 
40 38
 	// make sure the forth is running
41
-	if err := waitRun(fourthID); err != nil {
42
-		c.Fatalf("waiting for container failed: %v", err)
43
-	}
39
+	c.Assert(waitRun(fourthID), check.IsNil)
44 40
 
45 41
 	// all
46 42
 	out, _ = dockerCmd(c, "ps", "-a")
... ...
@@ -2,7 +2,6 @@ package main
2 2
 
3 3
 import (
4 4
 	"strings"
5
-	"time"
6 5
 
7 6
 	"github.com/go-check/check"
8 7
 )
... ...
@@ -31,7 +30,7 @@ func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
31 31
 
32 32
 	cleanedContainerID := strings.TrimSpace(out)
33 33
 
34
-	time.Sleep(1 * time.Second)
34
+	c.Assert(waitRun(cleanedContainerID), check.IsNil)
35 35
 
36 36
 	out, _ = dockerCmd(c, "logs", cleanedContainerID)
37 37
 	if out != "foobar\n" {
... ...
@@ -42,7 +41,7 @@ func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
42 42
 
43 43
 	out, _ = dockerCmd(c, "logs", cleanedContainerID)
44 44
 
45
-	time.Sleep(1 * time.Second)
45
+	c.Assert(waitRun(cleanedContainerID), check.IsNil)
46 46
 
47 47
 	if out != "foobar\nfoobar\n" {
48 48
 		c.Errorf("container should've printed 'foobar' twice")
... ...
@@ -1995,9 +1995,7 @@ func (s *DockerSuite) TestContainerNetworkMode(c *check.C) {
1995 1995
 
1996 1996
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
1997 1997
 	id := strings.TrimSpace(out)
1998
-	if err := waitRun(id); err != nil {
1999
-		c.Fatal(err)
2000
-	}
1998
+	c.Assert(waitRun(id), check.IsNil)
2001 1999
 	pid1, err := inspectField(id, "State.Pid")
2002 2000
 	c.Assert(err, check.IsNil)
2003 2001
 
... ...
@@ -2362,7 +2360,8 @@ func (s *DockerSuite) TestRunPidHostWithChildIsKillable(c *check.C) {
2362 2362
 	name := "ibuildthecloud"
2363 2363
 	dockerCmd(c, "run", "-d", "--pid=host", "--name", name, "busybox", "sh", "-c", "sleep 30; echo hi")
2364 2364
 
2365
-	time.Sleep(1 * time.Second)
2365
+	c.Assert(waitRun(name), check.IsNil)
2366
+
2366 2367
 	errchan := make(chan error)
2367 2368
 	go func() {
2368 2369
 		if out, _, err := dockerCmdWithError("kill", name); err != nil {
... ...
@@ -2778,9 +2777,7 @@ func (s *DockerSuite) TestPtraceContainerProcsFromHost(c *check.C) {
2778 2778
 
2779 2779
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
2780 2780
 	id := strings.TrimSpace(out)
2781
-	if err := waitRun(id); err != nil {
2782
-		c.Fatal(err)
2783
-	}
2781
+	c.Assert(waitRun(id), check.IsNil)
2784 2782
 	pid1, err := inspectField(id, "State.Pid")
2785 2783
 	c.Assert(err, check.IsNil)
2786 2784
 
... ...
@@ -198,9 +198,7 @@ func (s *DockerSuite) TestRunAttachDetach(c *check.C) {
198 198
 	if err := cmd.Start(); err != nil {
199 199
 		c.Fatal(err)
200 200
 	}
201
-	if err := waitRun(name); err != nil {
202
-		c.Fatal(err)
203
-	}
201
+	c.Assert(waitRun(name), check.IsNil)
204 202
 
205 203
 	if _, err := cpty.Write([]byte("hello\n")); err != nil {
206 204
 		c.Fatal(err)
... ...
@@ -11,9 +11,7 @@ import (
11 11
 func (s *DockerSuite) TestCliStatsNoStream(c *check.C) {
12 12
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
13 13
 	id := strings.TrimSpace(out)
14
-	if err := waitRun(id); err != nil {
15
-		c.Fatalf("error waiting for container to start: %v", err)
16
-	}
14
+	c.Assert(waitRun(id), check.IsNil)
17 15
 
18 16
 	statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
19 17
 	chErr := make(chan error)
... ...
@@ -14,16 +14,8 @@ func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
14 14
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "true")
15 15
 	containerID := strings.TrimSpace(out)
16 16
 
17
-	status := "true"
18
-	var err error
19
-	for i := 0; status != "false"; i++ {
20
-		status, err = inspectField(containerID, "State.Running")
21
-		c.Assert(err, check.IsNil)
22
-
23
-		time.Sleep(time.Second)
24
-		if i >= 60 {
25
-			c.Fatal("Container should have stopped by now")
26
-		}
17
+	if err := waitInspect(containerID, "{{.State.Running}}", "false", 1); err != nil {
18
+		c.Fatal("Container should have stopped by now")
27 19
 	}
28 20
 
29 21
 	out, _ = dockerCmd(c, "wait", containerID)
... ...
@@ -38,9 +30,7 @@ func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
38 38
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' TERM; while true; do sleep 0.01; done")
39 39
 	containerID := strings.TrimSpace(out)
40 40
 
41
-	if err := waitRun(containerID); err != nil {
42
-		c.Fatal(err)
43
-	}
41
+	c.Assert(waitRun(containerID), check.IsNil)
44 42
 
45 43
 	chWait := make(chan string)
46 44
 	go func() {
... ...
@@ -67,16 +57,8 @@ func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
67 67
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "exit 99")
68 68
 	containerID := strings.TrimSpace(out)
69 69
 
70
-	status := "true"
71
-	var err error
72
-	for i := 0; status != "false"; i++ {
73
-		status, err = inspectField(containerID, "State.Running")
74
-		c.Assert(err, check.IsNil)
75
-
76
-		time.Sleep(time.Second)
77
-		if i >= 60 {
78
-			c.Fatal("Container should have stopped by now")
79
-		}
70
+	if err := waitInspect(containerID, "{{.State.Running}}", "false", 1); err != nil {
71
+		c.Fatal("Container should have stopped by now")
80 72
 	}
81 73
 
82 74
 	out, _ = dockerCmd(c, "wait", containerID)
... ...
@@ -90,12 +72,7 @@ func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
90 90
 func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
91 91
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 99' TERM; while true; do sleep 0.01; done")
92 92
 	containerID := strings.TrimSpace(out)
93
-	if err := waitRun(containerID); err != nil {
94
-		c.Fatal(err)
95
-	}
96
-	if err := waitRun(containerID); err != nil {
97
-		c.Fatal(err)
98
-	}
93
+	c.Assert(waitRun(containerID), check.IsNil)
99 94
 
100 95
 	chWait := make(chan error)
101 96
 	waitCmd := exec.Command(dockerBinary, "wait", containerID)
... ...
@@ -1240,10 +1240,12 @@ func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, err
1240 1240
 		return nil, fmt.Errorf("%v: %q", err, out)
1241 1241
 	}
1242 1242
 
1243
-	time.Sleep(1 * time.Second)
1244
-
1245 1243
 	contID := strings.TrimSpace(out)
1246 1244
 
1245
+	if err := waitRun(contID); err != nil {
1246
+		return nil, fmt.Errorf("%v: %q", contID, err)
1247
+	}
1248
+
1247 1249
 	return readContainerFile(contID, filename)
1248 1250
 }
1249 1251
 
... ...
@@ -196,10 +196,14 @@ func waitForContainer(contID string, args ...string) error {
196 196
 	return nil
197 197
 }
198 198
 
199
+// waitRun will wait for the specified container to be running, maximum 5 seconds.
199 200
 func waitRun(contID string) error {
200 201
 	return waitInspect(contID, "{{.State.Running}}", "true", 5)
201 202
 }
202 203
 
204
+// waitInspect will wait for the specified container to have the specified string
205
+// in the inspect output. It will wait until the specified timeout (in seconds)
206
+// is reached.
203 207
 func waitInspect(name, expr, expected string, timeout int) error {
204 208
 	after := time.After(time.Duration(timeout) * time.Second)
205 209