Browse code

Use c.Assert in integration-cli/docker_cli_logs_test.go

Signed-off-by: Jian Zhang <zhangjian.fnst@cn.fujitsu.com>

Jian Zhang authored on 2015/10/13 10:50:17
Showing 1 changed files
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	"strings"
11 11
 	"time"
12 12
 
13
+	"github.com/docker/docker/pkg/integration/checker"
13 14
 	"github.com/docker/docker/pkg/timeutils"
14 15
 	"github.com/go-check/check"
15 16
 )
... ...
@@ -19,13 +20,13 @@ func (s *DockerSuite) TestLogsContainerSmallerThanPage(c *check.C) {
19 19
 	testRequires(c, DaemonIsLinux)
20 20
 	testLen := 32767
21 21
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
22
-	cleanedContainerID := strings.TrimSpace(out)
23 22
 
24
-	dockerCmd(c, "wait", cleanedContainerID)
25
-	out, _ = dockerCmd(c, "logs", cleanedContainerID)
26
-	if len(out) != testLen+1 {
27
-		c.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
28
-	}
23
+	id := strings.TrimSpace(out)
24
+	dockerCmd(c, "wait", id)
25
+
26
+	out, _ = dockerCmd(c, "logs", id)
27
+
28
+	c.Assert(out, checker.HasLen, testLen+1)
29 29
 }
30 30
 
31 31
 // Regression test: When going over the PageSize, it used to panic (gh#4851)
... ...
@@ -34,14 +35,12 @@ func (s *DockerSuite) TestLogsContainerBiggerThanPage(c *check.C) {
34 34
 	testLen := 32768
35 35
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
36 36
 
37
-	cleanedContainerID := strings.TrimSpace(out)
38
-	dockerCmd(c, "wait", cleanedContainerID)
37
+	id := strings.TrimSpace(out)
38
+	dockerCmd(c, "wait", id)
39 39
 
40
-	out, _ = dockerCmd(c, "logs", cleanedContainerID)
40
+	out, _ = dockerCmd(c, "logs", id)
41 41
 
42
-	if len(out) != testLen+1 {
43
-		c.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
44
-	}
42
+	c.Assert(out, checker.HasLen, testLen+1)
45 43
 }
46 44
 
47 45
 // Regression test: When going much over the PageSize, it used to block (gh#4851)
... ...
@@ -50,14 +49,12 @@ func (s *DockerSuite) TestLogsContainerMuchBiggerThanPage(c *check.C) {
50 50
 	testLen := 33000
51 51
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
52 52
 
53
-	cleanedContainerID := strings.TrimSpace(out)
54
-	dockerCmd(c, "wait", cleanedContainerID)
53
+	id := strings.TrimSpace(out)
54
+	dockerCmd(c, "wait", id)
55 55
 
56
-	out, _ = dockerCmd(c, "logs", cleanedContainerID)
56
+	out, _ = dockerCmd(c, "logs", id)
57 57
 
58
-	if len(out) != testLen+1 {
59
-		c.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
60
-	}
58
+	c.Assert(out, checker.HasLen, testLen+1)
61 59
 }
62 60
 
63 61
 func (s *DockerSuite) TestLogsTimestamps(c *check.C) {
... ...
@@ -65,28 +62,23 @@ func (s *DockerSuite) TestLogsTimestamps(c *check.C) {
65 65
 	testLen := 100
66 66
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
67 67
 
68
-	cleanedContainerID := strings.TrimSpace(out)
69
-	dockerCmd(c, "wait", cleanedContainerID)
68
+	id := strings.TrimSpace(out)
69
+	dockerCmd(c, "wait", id)
70 70
 
71
-	out, _ = dockerCmd(c, "logs", "-t", cleanedContainerID)
71
+	out, _ = dockerCmd(c, "logs", "-t", id)
72 72
 
73 73
 	lines := strings.Split(out, "\n")
74 74
 
75
-	if len(lines) != testLen+1 {
76
-		c.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
77
-	}
75
+	c.Assert(lines, checker.HasLen, testLen+1)
78 76
 
79 77
 	ts := regexp.MustCompile(`^.* `)
80 78
 
81 79
 	for _, l := range lines {
82 80
 		if l != "" {
83 81
 			_, err := time.Parse(timeutils.RFC3339NanoFixed+" ", ts.FindString(l))
84
-			if err != nil {
85
-				c.Fatalf("Failed to parse timestamp from %v: %v", l, err)
86
-			}
87
-			if l[29] != 'Z' { // ensure we have padded 0's
88
-				c.Fatalf("Timestamp isn't padded properly: %s", l)
89
-			}
82
+			c.Assert(err, checker.IsNil, check.Commentf("Failed to parse timestamp from %v", l))
83
+			// ensure we have padded 0's
84
+			c.Assert(l[29], checker.Equals, uint8('Z'))
90 85
 		}
91 86
 	}
92 87
 }
... ...
@@ -96,19 +88,16 @@ func (s *DockerSuite) TestLogsSeparateStderr(c *check.C) {
96 96
 	msg := "stderr_log"
97 97
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
98 98
 
99
-	cleanedContainerID := strings.TrimSpace(out)
100
-	dockerCmd(c, "wait", cleanedContainerID)
99
+	id := strings.TrimSpace(out)
100
+	dockerCmd(c, "wait", id)
101 101
 
102
-	stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", cleanedContainerID)
102
+	stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", id)
103 103
 
104
-	if stdout != "" {
105
-		c.Fatalf("Expected empty stdout stream, got %v", stdout)
106
-	}
104
+	c.Assert(stdout, checker.Equals, "")
107 105
 
108 106
 	stderr = strings.TrimSpace(stderr)
109
-	if stderr != msg {
110
-		c.Fatalf("Expected %v in stderr stream, got %v", msg, stderr)
111
-	}
107
+
108
+	c.Assert(stderr, checker.Equals, msg)
112 109
 }
113 110
 
114 111
 func (s *DockerSuite) TestLogsStderrInStdout(c *check.C) {
... ...
@@ -116,18 +105,14 @@ func (s *DockerSuite) TestLogsStderrInStdout(c *check.C) {
116 116
 	msg := "stderr_log"
117 117
 	out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
118 118
 
119
-	cleanedContainerID := strings.TrimSpace(out)
120
-	dockerCmd(c, "wait", cleanedContainerID)
119
+	id := strings.TrimSpace(out)
120
+	dockerCmd(c, "wait", id)
121 121
 
122
-	stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", cleanedContainerID)
123
-	if stderr != "" {
124
-		c.Fatalf("Expected empty stderr stream, got %v", stderr)
125
-	}
122
+	stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", id)
123
+	c.Assert(stderr, checker.Equals, "")
126 124
 
127 125
 	stdout = strings.TrimSpace(stdout)
128
-	if stdout != msg {
129
-		c.Fatalf("Expected %v in stdout stream, got %v", msg, stdout)
130
-	}
126
+	c.Assert(stdout, checker.Equals, msg)
131 127
 }
132 128
 
133 129
 func (s *DockerSuite) TestLogsTail(c *check.C) {
... ...
@@ -135,43 +120,37 @@ func (s *DockerSuite) TestLogsTail(c *check.C) {
135 135
 	testLen := 100
136 136
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
137 137
 
138
-	cleanedContainerID := strings.TrimSpace(out)
139
-	dockerCmd(c, "wait", cleanedContainerID)
138
+	id := strings.TrimSpace(out)
139
+	dockerCmd(c, "wait", id)
140 140
 
141
-	out, _ = dockerCmd(c, "logs", "--tail", "5", cleanedContainerID)
141
+	out, _ = dockerCmd(c, "logs", "--tail", "5", id)
142 142
 
143 143
 	lines := strings.Split(out, "\n")
144 144
 
145
-	if len(lines) != 6 {
146
-		c.Fatalf("Expected log %d lines, received %d\n", 6, len(lines))
147
-	}
148
-	out, _ = dockerCmd(c, "logs", "--tail", "all", cleanedContainerID)
145
+	c.Assert(lines, checker.HasLen, 6)
146
+
147
+	out, _ = dockerCmd(c, "logs", "--tail", "all", id)
149 148
 
150 149
 	lines = strings.Split(out, "\n")
151 150
 
152
-	if len(lines) != testLen+1 {
153
-		c.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
154
-	}
155
-	out, _, _ = dockerCmdWithStdoutStderr(c, "logs", "--tail", "random", cleanedContainerID)
151
+	c.Assert(lines, checker.HasLen, testLen+1)
152
+
153
+	out, _, _ = dockerCmdWithStdoutStderr(c, "logs", "--tail", "random", id)
156 154
 
157 155
 	lines = strings.Split(out, "\n")
158 156
 
159
-	if len(lines) != testLen+1 {
160
-		c.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
161
-	}
157
+	c.Assert(lines, checker.HasLen, testLen+1)
162 158
 }
163 159
 
164 160
 func (s *DockerSuite) TestLogsFollowStopped(c *check.C) {
165 161
 	testRequires(c, DaemonIsLinux)
166 162
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
167 163
 
168
-	cleanedContainerID := strings.TrimSpace(out)
169
-	dockerCmd(c, "wait", cleanedContainerID)
164
+	id := strings.TrimSpace(out)
165
+	dockerCmd(c, "wait", id)
170 166
 
171
-	logsCmd := exec.Command(dockerBinary, "logs", "-f", cleanedContainerID)
172
-	if err := logsCmd.Start(); err != nil {
173
-		c.Fatal(err)
174
-	}
167
+	logsCmd := exec.Command(dockerBinary, "logs", "-f", id)
168
+	c.Assert(logsCmd.Start(), checker.IsNil)
175 169
 
176 170
 	errChan := make(chan error)
177 171
 	go func() {
... ...
@@ -181,7 +160,7 @@ func (s *DockerSuite) TestLogsFollowStopped(c *check.C) {
181 181
 
182 182
 	select {
183 183
 	case err := <-errChan:
184
-		c.Assert(err, check.IsNil)
184
+		c.Assert(err, checker.IsNil)
185 185
 	case <-time.After(1 * time.Second):
186 186
 		c.Fatal("Following logs is hanged")
187 187
 	}
... ...
@@ -194,16 +173,14 @@ func (s *DockerSuite) TestLogsSince(c *check.C) {
194 194
 
195 195
 	log2Line := strings.Split(strings.Split(out, "\n")[1], " ")
196 196
 	t, err := strconv.ParseInt(log2Line[0], 10, 64) // the timestamp log2 is written
197
-	c.Assert(err, check.IsNil)
197
+	c.Assert(err, checker.IsNil)
198 198
 	since := t + 1 // add 1s so log1 & log2 doesn't show up
199 199
 	out, _ = dockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name)
200 200
 
201 201
 	// Skip 2 seconds
202 202
 	unexpected := []string{"log1", "log2"}
203 203
 	for _, v := range unexpected {
204
-		if strings.Contains(out, v) {
205
-			c.Fatalf("unexpected log message returned=%v, since=%v\nout=%v", v, since, out)
206
-		}
204
+		c.Assert(out, checker.Not(checker.Contains), v, check.Commentf("unexpected log message returned, since=%v", since))
207 205
 	}
208 206
 	// Test with default value specified and parameter omitted
209 207
 	expected := []string{"log1", "log2", "log3"}
... ...
@@ -212,13 +189,9 @@ func (s *DockerSuite) TestLogsSince(c *check.C) {
212 212
 		exec.Command(dockerBinary, "logs", "-t", "--since=0", name),
213 213
 	} {
214 214
 		out, _, err = runCommandWithOutput(cmd)
215
-		if err != nil {
216
-			c.Fatalf("failed to log container: %s, %v", out, err)
217
-		}
215
+		c.Assert(err, checker.IsNil, check.Commentf("failed to log container: %s", out))
218 216
 		for _, v := range expected {
219
-			if !strings.Contains(out, v) {
220
-				c.Fatalf("'%v' does not contain=%v\nout=%s", cmd.Args, v, out)
221
-			}
217
+			c.Assert(out, checker.Contains, v)
222 218
 		}
223 219
 	}
224 220
 }
... ...
@@ -226,23 +199,17 @@ func (s *DockerSuite) TestLogsSince(c *check.C) {
226 226
 func (s *DockerSuite) TestLogsSinceFutureFollow(c *check.C) {
227 227
 	testRequires(c, DaemonIsLinux)
228 228
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", `for i in $(seq 1 5); do date +%s; sleep 1; done`)
229
-	cleanedContainerID := strings.TrimSpace(out)
229
+	id := strings.TrimSpace(out)
230 230
 
231 231
 	now := daemonTime(c).Unix()
232 232
 	since := now + 2
233
-	out, _ = dockerCmd(c, "logs", "-f", fmt.Sprintf("--since=%v", since), cleanedContainerID)
233
+	out, _ = dockerCmd(c, "logs", "-f", fmt.Sprintf("--since=%v", since), id)
234 234
 	lines := strings.Split(strings.TrimSpace(out), "\n")
235
-	if len(lines) == 0 {
236
-		c.Fatal("got no log lines")
237
-	}
235
+	c.Assert(lines, checker.Not(checker.HasLen), 0)
238 236
 	for _, v := range lines {
239 237
 		ts, err := strconv.ParseInt(v, 10, 64)
240
-		if err != nil {
241
-			c.Fatalf("cannot parse timestamp output from log: '%v'\nout=%s", v, out)
242
-		}
243
-		if ts < since {
244
-			c.Fatalf("earlier log found. since=%v logdate=%v", since, ts)
245
-		}
238
+		c.Assert(err, checker.IsNil, check.Commentf("cannot parse timestamp output from log: '%v'\nout=%s", v, out))
239
+		c.Assert(ts >= since, checker.Equals, true, check.Commentf("earlier log found. since=%v logdate=%v", since, ts))
246 240
 	}
247 241
 }
248 242
 
... ...
@@ -251,33 +218,31 @@ func (s *DockerSuite) TestLogsFollowSlowStdoutConsumer(c *check.C) {
251 251
 	testRequires(c, DaemonIsLinux)
252 252
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", `usleep 200000;yes X | head -c 200000`)
253 253
 
254
-	cleanedContainerID := strings.TrimSpace(out)
254
+	id := strings.TrimSpace(out)
255 255
 
256 256
 	stopSlowRead := make(chan bool)
257 257
 
258 258
 	go func() {
259
-		exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
259
+		exec.Command(dockerBinary, "wait", id).Run()
260 260
 		stopSlowRead <- true
261 261
 	}()
262 262
 
263
-	logCmd := exec.Command(dockerBinary, "logs", "-f", cleanedContainerID)
263
+	logCmd := exec.Command(dockerBinary, "logs", "-f", id)
264 264
 	stdout, err := logCmd.StdoutPipe()
265
-	c.Assert(err, check.IsNil)
266
-	c.Assert(logCmd.Start(), check.IsNil)
265
+	c.Assert(err, checker.IsNil)
266
+	c.Assert(logCmd.Start(), checker.IsNil)
267 267
 
268 268
 	// First read slowly
269 269
 	bytes1, err := consumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead)
270
-	c.Assert(err, check.IsNil)
270
+	c.Assert(err, checker.IsNil)
271 271
 
272 272
 	// After the container has finished we can continue reading fast
273 273
 	bytes2, err := consumeWithSpeed(stdout, 32*1024, 0, nil)
274
-	c.Assert(err, check.IsNil)
274
+	c.Assert(err, checker.IsNil)
275 275
 
276 276
 	actual := bytes1 + bytes2
277 277
 	expected := 200000
278
-	if actual != expected {
279
-		c.Fatalf("Invalid bytes read: %d, expected %d", actual, expected)
280
-	}
278
+	c.Assert(actual, checker.Equals, expected)
281 279
 
282 280
 }
283 281
 
... ...
@@ -285,7 +250,7 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
285 285
 	testRequires(c, DaemonIsLinux)
286 286
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 2; done")
287 287
 	id := strings.TrimSpace(out)
288
-	c.Assert(waitRun(id), check.IsNil)
288
+	c.Assert(waitRun(id), checker.IsNil)
289 289
 
290 290
 	type info struct {
291 291
 		NGoroutines int
... ...
@@ -293,9 +258,9 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
293 293
 	getNGoroutines := func() int {
294 294
 		var i info
295 295
 		status, b, err := sockRequest("GET", "/info", nil)
296
-		c.Assert(err, check.IsNil)
297
-		c.Assert(status, check.Equals, 200)
298
-		c.Assert(json.Unmarshal(b, &i), check.IsNil)
296
+		c.Assert(err, checker.IsNil)
297
+		c.Assert(status, checker.Equals, 200)
298
+		c.Assert(json.Unmarshal(b, &i), checker.IsNil)
299 299
 		return i.NGoroutines
300 300
 	}
301 301
 
... ...
@@ -304,7 +269,7 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
304 304
 	cmd := exec.Command(dockerBinary, "logs", "-f", id)
305 305
 	r, w := io.Pipe()
306 306
 	cmd.Stdout = w
307
-	c.Assert(cmd.Start(), check.IsNil)
307
+	c.Assert(cmd.Start(), checker.IsNil)
308 308
 
309 309
 	// Make sure pipe is written to
310 310
 	chErr := make(chan error)
... ...
@@ -313,17 +278,17 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
313 313
 		_, err := r.Read(b)
314 314
 		chErr <- err
315 315
 	}()
316
-	c.Assert(<-chErr, check.IsNil)
317
-	c.Assert(cmd.Process.Kill(), check.IsNil)
316
+	c.Assert(<-chErr, checker.IsNil)
317
+	c.Assert(cmd.Process.Kill(), checker.IsNil)
318 318
 
319 319
 	// NGoroutines is not updated right away, so we need to wait before failing
320 320
 	t := time.After(30 * time.Second)
321 321
 	for {
322 322
 		select {
323 323
 		case <-t:
324
-			if n := getNGoroutines(); n > nroutines {
325
-				c.Fatalf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n)
326
-			}
324
+			n := getNGoroutines()
325
+			c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
326
+
327 327
 		default:
328 328
 			if n := getNGoroutines(); n <= nroutines {
329 329
 				return
... ...
@@ -337,7 +302,7 @@ func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
337 337
 	testRequires(c, DaemonIsLinux)
338 338
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 2; done")
339 339
 	id := strings.TrimSpace(out)
340
-	c.Assert(waitRun(id), check.IsNil)
340
+	c.Assert(waitRun(id), checker.IsNil)
341 341
 
342 342
 	type info struct {
343 343
 		NGoroutines int
... ...
@@ -345,27 +310,27 @@ func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
345 345
 	getNGoroutines := func() int {
346 346
 		var i info
347 347
 		status, b, err := sockRequest("GET", "/info", nil)
348
-		c.Assert(err, check.IsNil)
349
-		c.Assert(status, check.Equals, 200)
350
-		c.Assert(json.Unmarshal(b, &i), check.IsNil)
348
+		c.Assert(err, checker.IsNil)
349
+		c.Assert(status, checker.Equals, 200)
350
+		c.Assert(json.Unmarshal(b, &i), checker.IsNil)
351 351
 		return i.NGoroutines
352 352
 	}
353 353
 
354 354
 	nroutines := getNGoroutines()
355 355
 
356 356
 	cmd := exec.Command(dockerBinary, "logs", "-f", id)
357
-	c.Assert(cmd.Start(), check.IsNil)
357
+	c.Assert(cmd.Start(), checker.IsNil)
358 358
 	time.Sleep(200 * time.Millisecond)
359
-	c.Assert(cmd.Process.Kill(), check.IsNil)
359
+	c.Assert(cmd.Process.Kill(), checker.IsNil)
360 360
 
361 361
 	// NGoroutines is not updated right away, so we need to wait before failing
362 362
 	t := time.After(30 * time.Second)
363 363
 	for {
364 364
 		select {
365 365
 		case <-t:
366
-			if n := getNGoroutines(); n > nroutines {
367
-				c.Fatalf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n)
368
-			}
366
+			n := getNGoroutines()
367
+			c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
368
+
369 369
 		default:
370 370
 			if n := getNGoroutines(); n <= nroutines {
371 371
 				return
... ...
@@ -379,5 +344,5 @@ func (s *DockerSuite) TestLogsCLIContainerNotFound(c *check.C) {
379 379
 	name := "testlogsnocontainer"
380 380
 	out, _, _ := dockerCmdWithError("logs", name)
381 381
 	message := fmt.Sprintf(".*no such id: %s.*\n", name)
382
-	c.Assert(out, check.Matches, message)
382
+	c.Assert(out, checker.Matches, message)
383 383
 }