Browse code

update docker_cli_help_test.go

Signed-off-by: Kun Zhang <zkazure@gmail.com>

Kun Zhang authored on 2015/10/20 18:40:24
Showing 1 changed files
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"unicode"
9 9
 
10 10
 	"github.com/docker/docker/pkg/homedir"
11
+	"github.com/docker/docker/pkg/integration/checker"
11 12
 	"github.com/go-check/check"
12 13
 )
13 14
 
... ...
@@ -50,20 +51,14 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
50 50
 		// Check main help text to make sure its not over 80 chars
51 51
 		helpCmd := exec.Command(dockerBinary, "help")
52 52
 		helpCmd.Env = newEnvs
53
-		out, ec, err := runCommandWithOutput(helpCmd)
54
-		if err != nil || ec != 0 {
55
-			c.Fatalf("docker help should have worked\nout:%s\nec:%d", out, ec)
56
-		}
53
+		out, _, err := runCommandWithOutput(helpCmd)
54
+		c.Assert(err, checker.IsNil, check.Commentf(out))
57 55
 		lines := strings.Split(out, "\n")
58 56
 		for _, line := range lines {
59
-			if len(line) > 80 {
60
-				c.Fatalf("Line is too long(%d chars):\n%s", len(line), line)
61
-			}
57
+			c.Assert(len(line), checker.LessOrEqualThan, 80, check.Commentf("Line is too long:\n%s", line))
62 58
 
63 59
 			// All lines should not end with a space
64
-			if strings.HasSuffix(line, " ") {
65
-				c.Fatalf("Line should not end with a space: %s", line)
66
-			}
60
+			c.Assert(line, checker.Not(checker.HasSuffix), " ", check.Commentf("Line should not end with a space"))
67 61
 
68 62
 			if scanForHome && strings.Contains(line, `=`+home) {
69 63
 				c.Fatalf("Line should use '%q' instead of %q:\n%s", homedir.GetShortcutString(), home, line)
... ...
@@ -81,14 +76,10 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
81 81
 		// Pull the list of commands from the "Commands:" section of docker help
82 82
 		helpCmd = exec.Command(dockerBinary, "help")
83 83
 		helpCmd.Env = newEnvs
84
-		out, ec, err = runCommandWithOutput(helpCmd)
85
-		if err != nil || ec != 0 {
86
-			c.Fatalf("docker help should have worked\nout:%s\nec:%d", out, ec)
87
-		}
84
+		out, _, err = runCommandWithOutput(helpCmd)
85
+		c.Assert(err, checker.IsNil, check.Commentf(out))
88 86
 		i := strings.Index(out, "Commands:")
89
-		if i < 0 {
90
-			c.Fatalf("Missing 'Commands:' in:\n%s", out)
91
-		}
87
+		c.Assert(i, checker.GreaterOrEqualThan, 0, check.Commentf("Missing 'Commands:' in:\n%s", out))
92 88
 
93 89
 		cmds := []string{}
94 90
 		// Grab all chars starting at "Commands:"
... ...
@@ -132,27 +123,17 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
132 132
 			// Check the full usage text
133 133
 			helpCmd := exec.Command(dockerBinary, args...)
134 134
 			helpCmd.Env = newEnvs
135
-			out, stderr, ec, err = runCommandWithStdoutStderr(helpCmd)
136
-			if len(stderr) != 0 {
137
-				c.Fatalf("Error on %q help. non-empty stderr:%q", cmd, stderr)
138
-			}
139
-			if strings.HasSuffix(out, "\n\n") {
140
-				c.Fatalf("Should not have blank line on %q\nout:%q", cmd, out)
141
-			}
142
-			if !strings.Contains(out, "--help=false") {
143
-				c.Fatalf("Should show full usage on %q\nout:%q", cmd, out)
144
-			}
145
-			if err != nil || ec != 0 {
146
-				c.Fatalf("Error on %q help: %s\nexit code:%d", cmd, out, ec)
147
-			}
135
+			out, stderr, _, err = runCommandWithStdoutStderr(helpCmd)
136
+			c.Assert(len(stderr), checker.Equals, 0, check.Commentf("Error on %q help. non-empty stderr:%q", cmd, stderr))
137
+			c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have blank line on %q\n", cmd))
138
+			c.Assert(out, checker.Contains, "--help=false", check.Commentf("Should show full usage on %q\n", cmd))
139
+
140
+			c.Assert(err, checker.IsNil, check.Commentf(out))
148 141
 
149 142
 			// Check each line for lots of stuff
150 143
 			lines := strings.Split(out, "\n")
151 144
 			for _, line := range lines {
152
-				if len(line) > 90 {
153
-					c.Fatalf("Help for %q is too long(%d chars):\n%s", cmd,
154
-						len(line), line)
155
-				}
145
+				c.Assert(len(line), checker.LessOrEqualThan, 90, check.Commentf("Help for %q is too long:\n%s", cmd, line))
156 146
 
157 147
 				if scanForHome && strings.Contains(line, `"`+home) {
158 148
 					c.Fatalf("Help for %q should use ~ instead of %q on:\n%s",
... ...
@@ -166,9 +147,7 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
166 166
 				// If a line starts with 4 spaces then assume someone
167 167
 				// added a multi-line description for an option and we need
168 168
 				// to flag it
169
-				if strings.HasPrefix(line, "    ") {
170
-					c.Fatalf("Help for %q should not have a multi-line option: %s", cmd, line)
171
-				}
169
+				c.Assert(line, checker.Not(checker.HasPrefix), "    ", check.Commentf("Help for %q should not have a multi-line option", cmd))
172 170
 
173 171
 				// Options should NOT end with a period
174 172
 				if strings.HasPrefix(line, "  -") && strings.HasSuffix(line, ".") {
... ...
@@ -176,9 +155,7 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
176 176
 				}
177 177
 
178 178
 				// Options should NOT end with a space
179
-				if strings.HasSuffix(line, " ") {
180
-					c.Fatalf("Help for %q should not end with a space: %s", cmd, line)
181
-				}
179
+				c.Assert(line, checker.Not(checker.HasSuffix), " ", check.Commentf("Help for %q should not end with a space", cmd))
182 180
 
183 181
 			}
184 182
 
... ...
@@ -186,15 +163,10 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
186 186
 			// if we give a bad arg
187 187
 			args = strings.Split(cmd+" --badArg", " ")
188 188
 
189
-			dCmd := exec.Command(dockerBinary, args...)
190
-			out, stderr, ec, err = runCommandWithStdoutStderr(dCmd)
191
-			if len(out) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
192
-				c.Fatalf("Bad results from 'docker %s --badArg'\nec:%d\nstdout:%s\nstderr:%s\nerr:%q", cmd, ec, out, stderr, err)
193
-			}
189
+			out, _, err = dockerCmdWithError(args...)
190
+			c.Assert(err, checker.NotNil, check.Commentf(out))
194 191
 			// Be really picky
195
-			if strings.HasSuffix(stderr, "\n\n") {
196
-				c.Fatalf("Should not have a blank line at the end of 'docker rm'\n%s", stderr)
197
-			}
192
+			c.Assert(stderr, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker rm'\n"))
198 193
 
199 194
 			// Now make sure that each command will print a short-usage
200 195
 			// (not a full usage - meaning no opts section) if we
... ...
@@ -226,7 +198,7 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
226 226
 					"load":   {},
227 227
 				}
228 228
 
229
-				ec = 0
229
+				ec := 0
230 230
 				if _, ok := skipNoArgs[cmd]; !ok {
231 231
 					args = strings.Split(cmd, " ")
232 232
 					dCmd = exec.Command(dockerBinary, args...)
... ...
@@ -244,16 +216,10 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
244 244
 					c.Fatalf("Bad output from %q\nstdout:%q\nstderr:%q\nec:%d\nerr:%q", args, stdout, stderr, ec, err)
245 245
 				}
246 246
 				// Should have just short usage
247
-				if !strings.Contains(stderr, "\nUsage:\t") {
248
-					c.Fatalf("Missing short usage on %q\nstderr:%q", args, stderr)
249
-				}
247
+				c.Assert(stderr, checker.Contains, "\nUsage:\t", check.Commentf("Missing short usage on %q\n", args))
250 248
 				// But shouldn't have full usage
251
-				if strings.Contains(stderr, "--help=false") {
252
-					c.Fatalf("Should not have full usage on %q\nstderr:%q", args, stderr)
253
-				}
254
-				if strings.HasSuffix(stderr, "\n\n") {
255
-					c.Fatalf("Should not have a blank line on %q\nstderr:%q", args, stderr)
256
-				}
249
+				c.Assert(stderr, checker.Not(checker.Contains), "--help=false", check.Commentf("Should not have full usage on %q\n", args))
250
+				c.Assert(stderr, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line on %q\n", args))
257 251
 			}
258 252
 
259 253
 		}
... ...
@@ -265,10 +231,7 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
265 265
 		if isLocalDaemon {
266 266
 			expected++ // for the daemon command
267 267
 		}
268
-		if len(cmds) > expected {
269
-			c.Fatalf("Wrong # of cmds(%d), it should be: %d\nThe list:\n%q",
270
-				len(cmds), expected, cmds)
271
-		}
268
+		c.Assert(len(cmds), checker.LessOrEqualThan, expected, check.Commentf("Wrong # of cmds, it should be: %d\nThe list:\n%q", expected, cmds))
272 269
 	}
273 270
 
274 271
 }
... ...
@@ -279,87 +242,54 @@ func (s *DockerSuite) TestHelpExitCodesHelpOutput(c *check.C) {
279 279
 	// various good and bad cases are what we expect
280 280
 
281 281
 	// docker : stdout=all, stderr=empty, rc=0
282
-	cmd := exec.Command(dockerBinary)
283
-	stdout, stderr, ec, err := runCommandWithStdoutStderr(cmd)
284
-	if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
285
-		c.Fatalf("Bad results from 'docker'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
286
-	}
282
+	out, _, err := dockerCmdWithError()
283
+	c.Assert(err, checker.IsNil, check.Commentf(out))
287 284
 	// Be really pick
288
-	if strings.HasSuffix(stdout, "\n\n") {
289
-		c.Fatalf("Should not have a blank line at the end of 'docker'\n%s", stdout)
290
-	}
285
+	c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker'\n"))
291 286
 
292 287
 	// docker help: stdout=all, stderr=empty, rc=0
293
-	cmd = exec.Command(dockerBinary, "help")
294
-	stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
295
-	if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
296
-		c.Fatalf("Bad results from 'docker help'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
297
-	}
288
+	out, _, err = dockerCmdWithError("help")
289
+	c.Assert(err, checker.IsNil, check.Commentf(out))
298 290
 	// Be really pick
299
-	if strings.HasSuffix(stdout, "\n\n") {
300
-		c.Fatalf("Should not have a blank line at the end of 'docker help'\n%s", stdout)
301
-	}
291
+	c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker help'\n"))
302 292
 
303 293
 	// docker --help: stdout=all, stderr=empty, rc=0
304
-	cmd = exec.Command(dockerBinary, "--help")
305
-	stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
306
-	if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
307
-		c.Fatalf("Bad results from 'docker --help'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
308
-	}
294
+	out, _, err = dockerCmdWithError("--help")
295
+	c.Assert(err, checker.IsNil, check.Commentf(out))
309 296
 	// Be really pick
310
-	if strings.HasSuffix(stdout, "\n\n") {
311
-		c.Fatalf("Should not have a blank line at the end of 'docker --help'\n%s", stdout)
312
-	}
297
+	c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker --help'\n"))
313 298
 
314 299
 	// docker inspect busybox: stdout=all, stderr=empty, rc=0
315 300
 	// Just making sure stderr is empty on valid cmd
316
-	cmd = exec.Command(dockerBinary, "inspect", "busybox")
317
-	stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
318
-	if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
319
-		c.Fatalf("Bad results from 'docker inspect busybox'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
320
-	}
301
+	out, _, err = dockerCmdWithError("inspect", "busybox")
302
+	c.Assert(err, checker.IsNil, check.Commentf(out))
321 303
 	// Be really pick
322
-	if strings.HasSuffix(stdout, "\n\n") {
323
-		c.Fatalf("Should not have a blank line at the end of 'docker inspect busyBox'\n%s", stdout)
324
-	}
304
+	c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker inspect busyBox'\n"))
325 305
 
326 306
 	// docker rm: stdout=empty, stderr=all, rc!=0
327 307
 	// testing the min arg error msg
328
-	cmd = exec.Command(dockerBinary, "rm")
329
-	stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
330
-	if len(stdout) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
331
-		c.Fatalf("Bad results from 'docker rm'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
332
-	}
308
+	cmd := exec.Command(dockerBinary, "rm")
309
+	stdout, stderr, _, err := runCommandWithStdoutStderr(cmd)
310
+	c.Assert(err, checker.NotNil)
311
+	c.Assert(stdout, checker.Equals, "")
333 312
 	// Should not contain full help text but should contain info about
334 313
 	// # of args and Usage line
335
-	if !strings.Contains(stderr, "requires a minimum") {
336
-		c.Fatalf("Missing # of args text from 'docker rm'\nstderr:%s", stderr)
337
-	}
314
+	c.Assert(stderr, checker.Contains, "requires a minimum", check.Commentf("Missing # of args text from 'docker rm'\n"))
338 315
 
339 316
 	// docker rm NoSuchContainer: stdout=empty, stderr=all, rc=0
340 317
 	// testing to make sure no blank line on error
341 318
 	cmd = exec.Command(dockerBinary, "rm", "NoSuchContainer")
342
-	stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
343
-	if len(stdout) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
344
-		c.Fatalf("Bad results from 'docker rm NoSuchContainer'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
345
-	}
319
+	stdout, stderr, _, err = runCommandWithStdoutStderr(cmd)
320
+	c.Assert(err, checker.NotNil)
321
+	c.Assert(len(stderr), checker.Not(checker.Equals), 0)
322
+	c.Assert(stdout, checker.Equals, "")
346 323
 	// Be really picky
347
-	if strings.HasSuffix(stderr, "\n\n") {
348
-		c.Fatalf("Should not have a blank line at the end of 'docker rm'\n%s", stderr)
349
-	}
324
+	c.Assert(stderr, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker rm'\n"))
350 325
 
351 326
 	// docker BadCmd: stdout=empty, stderr=all, rc=0
352 327
 	cmd = exec.Command(dockerBinary, "BadCmd")
353
-	stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
354
-	if len(stdout) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
355
-		c.Fatalf("Bad results from 'docker BadCmd'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
356
-	}
357
-	if stderr != "docker: 'BadCmd' is not a docker command.\nSee 'docker --help'.\n" {
358
-		c.Fatalf("Unexcepted output for 'docker badCmd'\nstderr:%s", stderr)
359
-	}
360
-	// Be really picky
361
-	if strings.HasSuffix(stderr, "\n\n") {
362
-		c.Fatalf("Should not have a blank line at the end of 'docker rm'\n%s", stderr)
363
-	}
364
-
328
+	stdout, stderr, _, err = runCommandWithStdoutStderr(cmd)
329
+	c.Assert(err, checker.NotNil)
330
+	c.Assert(stdout, checker.Equals, "")
331
+	c.Assert(stderr, checker.Equals, "docker: 'BadCmd' is not a docker command.\nSee 'docker --help'.\n", check.Commentf("Unexcepted output for 'docker badCmd'\n"))
365 332
 }