зеркало из https://github.com/microsoft/docker.git
Merge pull request #17207 from azurezk/assert_docker_cli_help_test
update docker_cli_help_test.go
This commit is contained in:
Коммит
22ef779db9
|
@ -8,6 +8,7 @@ import (
|
|||
"unicode"
|
||||
|
||||
"github.com/docker/docker/pkg/homedir"
|
||||
"github.com/docker/docker/pkg/integration/checker"
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
|
@ -50,20 +51,14 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
|
|||
// Check main help text to make sure its not over 80 chars
|
||||
helpCmd := exec.Command(dockerBinary, "help")
|
||||
helpCmd.Env = newEnvs
|
||||
out, ec, err := runCommandWithOutput(helpCmd)
|
||||
if err != nil || ec != 0 {
|
||||
c.Fatalf("docker help should have worked\nout:%s\nec:%d", out, ec)
|
||||
}
|
||||
out, _, err := runCommandWithOutput(helpCmd)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
lines := strings.Split(out, "\n")
|
||||
for _, line := range lines {
|
||||
if len(line) > 80 {
|
||||
c.Fatalf("Line is too long(%d chars):\n%s", len(line), line)
|
||||
}
|
||||
c.Assert(len(line), checker.LessOrEqualThan, 80, check.Commentf("Line is too long:\n%s", line))
|
||||
|
||||
// All lines should not end with a space
|
||||
if strings.HasSuffix(line, " ") {
|
||||
c.Fatalf("Line should not end with a space: %s", line)
|
||||
}
|
||||
c.Assert(line, checker.Not(checker.HasSuffix), " ", check.Commentf("Line should not end with a space"))
|
||||
|
||||
if scanForHome && strings.Contains(line, `=`+home) {
|
||||
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) {
|
|||
// Pull the list of commands from the "Commands:" section of docker help
|
||||
helpCmd = exec.Command(dockerBinary, "help")
|
||||
helpCmd.Env = newEnvs
|
||||
out, ec, err = runCommandWithOutput(helpCmd)
|
||||
if err != nil || ec != 0 {
|
||||
c.Fatalf("docker help should have worked\nout:%s\nec:%d", out, ec)
|
||||
}
|
||||
out, _, err = runCommandWithOutput(helpCmd)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
i := strings.Index(out, "Commands:")
|
||||
if i < 0 {
|
||||
c.Fatalf("Missing 'Commands:' in:\n%s", out)
|
||||
}
|
||||
c.Assert(i, checker.GreaterOrEqualThan, 0, check.Commentf("Missing 'Commands:' in:\n%s", out))
|
||||
|
||||
cmds := []string{}
|
||||
// Grab all chars starting at "Commands:"
|
||||
|
@ -132,27 +123,17 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
|
|||
// Check the full usage text
|
||||
helpCmd := exec.Command(dockerBinary, args...)
|
||||
helpCmd.Env = newEnvs
|
||||
out, stderr, ec, err = runCommandWithStdoutStderr(helpCmd)
|
||||
if len(stderr) != 0 {
|
||||
c.Fatalf("Error on %q help. non-empty stderr:%q", cmd, stderr)
|
||||
}
|
||||
if strings.HasSuffix(out, "\n\n") {
|
||||
c.Fatalf("Should not have blank line on %q\nout:%q", cmd, out)
|
||||
}
|
||||
if !strings.Contains(out, "--help=false") {
|
||||
c.Fatalf("Should show full usage on %q\nout:%q", cmd, out)
|
||||
}
|
||||
if err != nil || ec != 0 {
|
||||
c.Fatalf("Error on %q help: %s\nexit code:%d", cmd, out, ec)
|
||||
}
|
||||
out, stderr, _, err = runCommandWithStdoutStderr(helpCmd)
|
||||
c.Assert(len(stderr), checker.Equals, 0, check.Commentf("Error on %q help. non-empty stderr:%q", cmd, stderr))
|
||||
c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have blank line on %q\n", cmd))
|
||||
c.Assert(out, checker.Contains, "--help=false", check.Commentf("Should show full usage on %q\n", cmd))
|
||||
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
|
||||
// Check each line for lots of stuff
|
||||
lines := strings.Split(out, "\n")
|
||||
for _, line := range lines {
|
||||
if len(line) > 90 {
|
||||
c.Fatalf("Help for %q is too long(%d chars):\n%s", cmd,
|
||||
len(line), line)
|
||||
}
|
||||
c.Assert(len(line), checker.LessOrEqualThan, 90, check.Commentf("Help for %q is too long:\n%s", cmd, line))
|
||||
|
||||
if scanForHome && strings.Contains(line, `"`+home) {
|
||||
c.Fatalf("Help for %q should use ~ instead of %q on:\n%s",
|
||||
|
@ -166,9 +147,7 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
|
|||
// If a line starts with 4 spaces then assume someone
|
||||
// added a multi-line description for an option and we need
|
||||
// to flag it
|
||||
if strings.HasPrefix(line, " ") {
|
||||
c.Fatalf("Help for %q should not have a multi-line option: %s", cmd, line)
|
||||
}
|
||||
c.Assert(line, checker.Not(checker.HasPrefix), " ", check.Commentf("Help for %q should not have a multi-line option", cmd))
|
||||
|
||||
// Options should NOT end with a period
|
||||
if strings.HasPrefix(line, " -") && strings.HasSuffix(line, ".") {
|
||||
|
@ -176,9 +155,7 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
|
|||
}
|
||||
|
||||
// Options should NOT end with a space
|
||||
if strings.HasSuffix(line, " ") {
|
||||
c.Fatalf("Help for %q should not end with a space: %s", cmd, line)
|
||||
}
|
||||
c.Assert(line, checker.Not(checker.HasSuffix), " ", check.Commentf("Help for %q should not end with a space", cmd))
|
||||
|
||||
}
|
||||
|
||||
|
@ -186,15 +163,10 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
|
|||
// if we give a bad arg
|
||||
args = strings.Split(cmd+" --badArg", " ")
|
||||
|
||||
dCmd := exec.Command(dockerBinary, args...)
|
||||
out, stderr, ec, err = runCommandWithStdoutStderr(dCmd)
|
||||
if len(out) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
|
||||
c.Fatalf("Bad results from 'docker %s --badArg'\nec:%d\nstdout:%s\nstderr:%s\nerr:%q", cmd, ec, out, stderr, err)
|
||||
}
|
||||
out, _, err = dockerCmdWithError(args...)
|
||||
c.Assert(err, checker.NotNil, check.Commentf(out))
|
||||
// Be really picky
|
||||
if strings.HasSuffix(stderr, "\n\n") {
|
||||
c.Fatalf("Should not have a blank line at the end of 'docker rm'\n%s", stderr)
|
||||
}
|
||||
c.Assert(stderr, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker rm'\n"))
|
||||
|
||||
// Now make sure that each command will print a short-usage
|
||||
// (not a full usage - meaning no opts section) if we
|
||||
|
@ -226,7 +198,7 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
|
|||
"load": {},
|
||||
}
|
||||
|
||||
ec = 0
|
||||
ec := 0
|
||||
if _, ok := skipNoArgs[cmd]; !ok {
|
||||
args = strings.Split(cmd, " ")
|
||||
dCmd = exec.Command(dockerBinary, args...)
|
||||
|
@ -244,16 +216,10 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
|
|||
c.Fatalf("Bad output from %q\nstdout:%q\nstderr:%q\nec:%d\nerr:%q", args, stdout, stderr, ec, err)
|
||||
}
|
||||
// Should have just short usage
|
||||
if !strings.Contains(stderr, "\nUsage:\t") {
|
||||
c.Fatalf("Missing short usage on %q\nstderr:%q", args, stderr)
|
||||
}
|
||||
c.Assert(stderr, checker.Contains, "\nUsage:\t", check.Commentf("Missing short usage on %q\n", args))
|
||||
// But shouldn't have full usage
|
||||
if strings.Contains(stderr, "--help=false") {
|
||||
c.Fatalf("Should not have full usage on %q\nstderr:%q", args, stderr)
|
||||
}
|
||||
if strings.HasSuffix(stderr, "\n\n") {
|
||||
c.Fatalf("Should not have a blank line on %q\nstderr:%q", args, stderr)
|
||||
}
|
||||
c.Assert(stderr, checker.Not(checker.Contains), "--help=false", check.Commentf("Should not have full usage on %q\n", args))
|
||||
c.Assert(stderr, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line on %q\n", args))
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -265,10 +231,7 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
|
|||
if isLocalDaemon {
|
||||
expected++ // for the daemon command
|
||||
}
|
||||
if len(cmds) > expected {
|
||||
c.Fatalf("Wrong # of cmds(%d), it should be: %d\nThe list:\n%q",
|
||||
len(cmds), expected, cmds)
|
||||
}
|
||||
c.Assert(len(cmds), checker.LessOrEqualThan, expected, check.Commentf("Wrong # of cmds, it should be: %d\nThe list:\n%q", expected, cmds))
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -279,87 +242,54 @@ func (s *DockerSuite) TestHelpExitCodesHelpOutput(c *check.C) {
|
|||
// various good and bad cases are what we expect
|
||||
|
||||
// docker : stdout=all, stderr=empty, rc=0
|
||||
cmd := exec.Command(dockerBinary)
|
||||
stdout, stderr, ec, err := runCommandWithStdoutStderr(cmd)
|
||||
if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
|
||||
c.Fatalf("Bad results from 'docker'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
|
||||
}
|
||||
out, _, err := dockerCmdWithError()
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
// Be really pick
|
||||
if strings.HasSuffix(stdout, "\n\n") {
|
||||
c.Fatalf("Should not have a blank line at the end of 'docker'\n%s", stdout)
|
||||
}
|
||||
c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker'\n"))
|
||||
|
||||
// docker help: stdout=all, stderr=empty, rc=0
|
||||
cmd = exec.Command(dockerBinary, "help")
|
||||
stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
|
||||
if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
|
||||
c.Fatalf("Bad results from 'docker help'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
|
||||
}
|
||||
out, _, err = dockerCmdWithError("help")
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
// Be really pick
|
||||
if strings.HasSuffix(stdout, "\n\n") {
|
||||
c.Fatalf("Should not have a blank line at the end of 'docker help'\n%s", stdout)
|
||||
}
|
||||
c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker help'\n"))
|
||||
|
||||
// docker --help: stdout=all, stderr=empty, rc=0
|
||||
cmd = exec.Command(dockerBinary, "--help")
|
||||
stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
|
||||
if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
|
||||
c.Fatalf("Bad results from 'docker --help'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
|
||||
}
|
||||
out, _, err = dockerCmdWithError("--help")
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
// Be really pick
|
||||
if strings.HasSuffix(stdout, "\n\n") {
|
||||
c.Fatalf("Should not have a blank line at the end of 'docker --help'\n%s", stdout)
|
||||
}
|
||||
c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker --help'\n"))
|
||||
|
||||
// docker inspect busybox: stdout=all, stderr=empty, rc=0
|
||||
// Just making sure stderr is empty on valid cmd
|
||||
cmd = exec.Command(dockerBinary, "inspect", "busybox")
|
||||
stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
|
||||
if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
|
||||
c.Fatalf("Bad results from 'docker inspect busybox'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
|
||||
}
|
||||
out, _, err = dockerCmdWithError("inspect", "busybox")
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
// Be really pick
|
||||
if strings.HasSuffix(stdout, "\n\n") {
|
||||
c.Fatalf("Should not have a blank line at the end of 'docker inspect busyBox'\n%s", stdout)
|
||||
}
|
||||
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"))
|
||||
|
||||
// docker rm: stdout=empty, stderr=all, rc!=0
|
||||
// testing the min arg error msg
|
||||
cmd = exec.Command(dockerBinary, "rm")
|
||||
stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
|
||||
if len(stdout) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
|
||||
c.Fatalf("Bad results from 'docker rm'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
|
||||
}
|
||||
cmd := exec.Command(dockerBinary, "rm")
|
||||
stdout, stderr, _, err := runCommandWithStdoutStderr(cmd)
|
||||
c.Assert(err, checker.NotNil)
|
||||
c.Assert(stdout, checker.Equals, "")
|
||||
// Should not contain full help text but should contain info about
|
||||
// # of args and Usage line
|
||||
if !strings.Contains(stderr, "requires a minimum") {
|
||||
c.Fatalf("Missing # of args text from 'docker rm'\nstderr:%s", stderr)
|
||||
}
|
||||
c.Assert(stderr, checker.Contains, "requires a minimum", check.Commentf("Missing # of args text from 'docker rm'\n"))
|
||||
|
||||
// docker rm NoSuchContainer: stdout=empty, stderr=all, rc=0
|
||||
// testing to make sure no blank line on error
|
||||
cmd = exec.Command(dockerBinary, "rm", "NoSuchContainer")
|
||||
stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
|
||||
if len(stdout) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
|
||||
c.Fatalf("Bad results from 'docker rm NoSuchContainer'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
|
||||
}
|
||||
stdout, stderr, _, err = runCommandWithStdoutStderr(cmd)
|
||||
c.Assert(err, checker.NotNil)
|
||||
c.Assert(len(stderr), checker.Not(checker.Equals), 0)
|
||||
c.Assert(stdout, checker.Equals, "")
|
||||
// Be really picky
|
||||
if strings.HasSuffix(stderr, "\n\n") {
|
||||
c.Fatalf("Should not have a blank line at the end of 'docker rm'\n%s", stderr)
|
||||
}
|
||||
c.Assert(stderr, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker rm'\n"))
|
||||
|
||||
// docker BadCmd: stdout=empty, stderr=all, rc=0
|
||||
cmd = exec.Command(dockerBinary, "BadCmd")
|
||||
stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
|
||||
if len(stdout) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
|
||||
c.Fatalf("Bad results from 'docker BadCmd'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
|
||||
}
|
||||
if stderr != "docker: 'BadCmd' is not a docker command.\nSee 'docker --help'.\n" {
|
||||
c.Fatalf("Unexcepted output for 'docker badCmd'\nstderr:%s", stderr)
|
||||
}
|
||||
// Be really picky
|
||||
if strings.HasSuffix(stderr, "\n\n") {
|
||||
c.Fatalf("Should not have a blank line at the end of 'docker rm'\n%s", stderr)
|
||||
}
|
||||
|
||||
stdout, stderr, _, err = runCommandWithStdoutStderr(cmd)
|
||||
c.Assert(err, checker.NotNil)
|
||||
c.Assert(stdout, checker.Equals, "")
|
||||
c.Assert(stderr, checker.Equals, "docker: 'BadCmd' is not a docker command.\nSee 'docker --help'.\n", check.Commentf("Unexcepted output for 'docker badCmd'\n"))
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче