docker_cli_help_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. "runtime"
  6. "strings"
  7. "unicode"
  8. "github.com/docker/docker/pkg/homedir"
  9. "github.com/docker/docker/pkg/integration/checker"
  10. "github.com/go-check/check"
  11. )
  12. func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
  13. testRequires(c, DaemonIsLinux)
  14. // Make sure main help text fits within 80 chars and that
  15. // on non-windows system we use ~ when possible (to shorten things).
  16. // Test for HOME set to its default value and set to "/" on linux
  17. // Yes on windows setting up an array and looping (right now) isn't
  18. // necessary because we just have one value, but we'll need the
  19. // array/loop on linux so we might as well set it up so that we can
  20. // test any number of home dirs later on and all we need to do is
  21. // modify the array - the rest of the testing infrastructure should work
  22. homes := []string{homedir.Get()}
  23. // Non-Windows machines need to test for this special case of $HOME
  24. if runtime.GOOS != "windows" {
  25. homes = append(homes, "/")
  26. }
  27. homeKey := homedir.Key()
  28. baseEnvs := os.Environ()
  29. // Remove HOME env var from list so we can add a new value later.
  30. for i, env := range baseEnvs {
  31. if strings.HasPrefix(env, homeKey+"=") {
  32. baseEnvs = append(baseEnvs[:i], baseEnvs[i+1:]...)
  33. break
  34. }
  35. }
  36. for _, home := range homes {
  37. // Dup baseEnvs and add our new HOME value
  38. newEnvs := make([]string, len(baseEnvs)+1)
  39. copy(newEnvs, baseEnvs)
  40. newEnvs[len(newEnvs)-1] = homeKey + "=" + home
  41. scanForHome := runtime.GOOS != "windows" && home != "/"
  42. // Check main help text to make sure its not over 80 chars
  43. helpCmd := exec.Command(dockerBinary, "help")
  44. helpCmd.Env = newEnvs
  45. out, _, err := runCommandWithOutput(helpCmd)
  46. c.Assert(err, checker.IsNil, check.Commentf(out))
  47. lines := strings.Split(out, "\n")
  48. for _, line := range lines {
  49. c.Assert(len(line), checker.LessOrEqualThan, 80, check.Commentf("Line is too long:\n%s", line))
  50. // All lines should not end with a space
  51. c.Assert(line, checker.Not(checker.HasSuffix), " ", check.Commentf("Line should not end with a space"))
  52. if scanForHome && strings.Contains(line, `=`+home) {
  53. c.Fatalf("Line should use '%q' instead of %q:\n%s", homedir.GetShortcutString(), home, line)
  54. }
  55. if runtime.GOOS != "windows" {
  56. i := strings.Index(line, homedir.GetShortcutString())
  57. if i >= 0 && i != len(line)-1 && line[i+1] != '/' {
  58. c.Fatalf("Main help should not have used home shortcut:\n%s", line)
  59. }
  60. }
  61. }
  62. // Make sure each cmd's help text fits within 90 chars and that
  63. // on non-windows system we use ~ when possible (to shorten things).
  64. // Pull the list of commands from the "Commands:" section of docker help
  65. helpCmd = exec.Command(dockerBinary, "help")
  66. helpCmd.Env = newEnvs
  67. out, _, err = runCommandWithOutput(helpCmd)
  68. c.Assert(err, checker.IsNil, check.Commentf(out))
  69. i := strings.Index(out, "Commands:")
  70. c.Assert(i, checker.GreaterOrEqualThan, 0, check.Commentf("Missing 'Commands:' in:\n%s", out))
  71. cmds := []string{}
  72. // Grab all chars starting at "Commands:"
  73. helpOut := strings.Split(out[i:], "\n")
  74. // First line is just "Commands:"
  75. if isLocalDaemon {
  76. // Replace first line with "daemon" command since it's not part of the list of commands.
  77. helpOut[0] = " daemon"
  78. } else {
  79. // Skip first line
  80. helpOut = helpOut[1:]
  81. }
  82. // Create the list of commands we want to test
  83. cmdsToTest := []string{}
  84. for _, cmd := range helpOut {
  85. // Stop on blank line or non-idented line
  86. if cmd == "" || !unicode.IsSpace(rune(cmd[0])) {
  87. break
  88. }
  89. // Grab just the first word of each line
  90. cmd = strings.Split(strings.TrimSpace(cmd), " ")[0]
  91. cmds = append(cmds, cmd) // Saving count for later
  92. cmdsToTest = append(cmdsToTest, cmd)
  93. }
  94. // Add some 'two word' commands - would be nice to automatically
  95. // calculate this list - somehow
  96. cmdsToTest = append(cmdsToTest, "volume create")
  97. cmdsToTest = append(cmdsToTest, "volume inspect")
  98. cmdsToTest = append(cmdsToTest, "volume ls")
  99. cmdsToTest = append(cmdsToTest, "volume rm")
  100. for _, cmd := range cmdsToTest {
  101. var stderr string
  102. args := strings.Split(cmd+" --help", " ")
  103. // Check the full usage text
  104. helpCmd := exec.Command(dockerBinary, args...)
  105. helpCmd.Env = newEnvs
  106. out, stderr, _, err = runCommandWithStdoutStderr(helpCmd)
  107. c.Assert(len(stderr), checker.Equals, 0, check.Commentf("Error on %q help. non-empty stderr:%q", cmd, stderr))
  108. c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have blank line on %q\n", cmd))
  109. c.Assert(out, checker.Contains, "--help", check.Commentf("All commands should mention '--help'. Command '%v' did not.\n", cmd))
  110. c.Assert(err, checker.IsNil, check.Commentf(out))
  111. // Check each line for lots of stuff
  112. lines := strings.Split(out, "\n")
  113. for _, line := range lines {
  114. c.Assert(len(line), checker.LessOrEqualThan, 103, check.Commentf("Help for %q is too long:\n%s", cmd, line))
  115. if scanForHome && strings.Contains(line, `"`+home) {
  116. c.Fatalf("Help for %q should use ~ instead of %q on:\n%s",
  117. cmd, home, line)
  118. }
  119. i := strings.Index(line, "~")
  120. if i >= 0 && i != len(line)-1 && line[i+1] != '/' {
  121. c.Fatalf("Help for %q should not have used ~:\n%s", cmd, line)
  122. }
  123. // If a line starts with 4 spaces then assume someone
  124. // added a multi-line description for an option and we need
  125. // to flag it
  126. c.Assert(line, checker.Not(checker.HasPrefix), " ", check.Commentf("Help for %q should not have a multi-line option", cmd))
  127. // Options should NOT end with a period
  128. if strings.HasPrefix(line, " -") && strings.HasSuffix(line, ".") {
  129. c.Fatalf("Help for %q should not end with a period: %s", cmd, line)
  130. }
  131. // Options should NOT end with a space
  132. c.Assert(line, checker.Not(checker.HasSuffix), " ", check.Commentf("Help for %q should not end with a space", cmd))
  133. }
  134. // For each command make sure we generate an error
  135. // if we give a bad arg
  136. args = strings.Split(cmd+" --badArg", " ")
  137. out, _, err = dockerCmdWithError(args...)
  138. c.Assert(err, checker.NotNil, check.Commentf(out))
  139. // Be really picky
  140. c.Assert(stderr, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker rm'\n"))
  141. // Now make sure that each command will print a short-usage
  142. // (not a full usage - meaning no opts section) if we
  143. // are missing a required arg or pass in a bad arg
  144. // These commands will never print a short-usage so don't test
  145. noShortUsage := map[string]string{
  146. "images": "",
  147. "login": "",
  148. "logout": "",
  149. "network": "",
  150. "stats": "",
  151. }
  152. if _, ok := noShortUsage[cmd]; !ok {
  153. // For each command run it w/o any args. It will either return
  154. // valid output or print a short-usage
  155. var dCmd *exec.Cmd
  156. var stdout, stderr string
  157. var args []string
  158. // skipNoArgs are ones that we don't want to try w/o
  159. // any args. Either because it'll hang the test or
  160. // lead to incorrect test result (like false negative).
  161. // Whatever the reason, skip trying to run w/o args and
  162. // jump to trying with a bogus arg.
  163. skipNoArgs := map[string]struct{}{
  164. "daemon": {},
  165. "events": {},
  166. "load": {},
  167. }
  168. ec := 0
  169. if _, ok := skipNoArgs[cmd]; !ok {
  170. args = strings.Split(cmd, " ")
  171. dCmd = exec.Command(dockerBinary, args...)
  172. stdout, stderr, ec, err = runCommandWithStdoutStderr(dCmd)
  173. }
  174. // If its ok w/o any args then try again with an arg
  175. if ec == 0 {
  176. args = strings.Split(cmd+" badArg", " ")
  177. dCmd = exec.Command(dockerBinary, args...)
  178. stdout, stderr, ec, err = runCommandWithStdoutStderr(dCmd)
  179. }
  180. if len(stdout) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
  181. c.Fatalf("Bad output from %q\nstdout:%q\nstderr:%q\nec:%d\nerr:%q", args, stdout, stderr, ec, err)
  182. }
  183. // Should have just short usage
  184. c.Assert(stderr, checker.Contains, "\nUsage:\t", check.Commentf("Missing short usage on %q\n", args))
  185. // But shouldn't have full usage
  186. c.Assert(stderr, checker.Not(checker.Contains), "--help=false", check.Commentf("Should not have full usage on %q\n", args))
  187. c.Assert(stderr, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line on %q\n", args))
  188. }
  189. }
  190. // Number of commands for standard release and experimental release
  191. standard := 41
  192. experimental := 1
  193. expected := standard + experimental
  194. if isLocalDaemon {
  195. expected++ // for the daemon command
  196. }
  197. c.Assert(len(cmds), checker.LessOrEqualThan, expected, check.Commentf("Wrong # of cmds, it should be: %d\nThe list:\n%q", expected, cmds))
  198. }
  199. }
  200. func (s *DockerSuite) TestHelpExitCodesHelpOutput(c *check.C) {
  201. testRequires(c, DaemonIsLinux)
  202. // Test to make sure the exit code and output (stdout vs stderr) of
  203. // various good and bad cases are what we expect
  204. // docker : stdout=all, stderr=empty, rc=0
  205. out, _, err := dockerCmdWithError()
  206. c.Assert(err, checker.IsNil, check.Commentf(out))
  207. // Be really pick
  208. c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker'\n"))
  209. // docker help: stdout=all, stderr=empty, rc=0
  210. out, _, err = dockerCmdWithError("help")
  211. c.Assert(err, checker.IsNil, check.Commentf(out))
  212. // Be really pick
  213. c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker help'\n"))
  214. // docker --help: stdout=all, stderr=empty, rc=0
  215. out, _, err = dockerCmdWithError("--help")
  216. c.Assert(err, checker.IsNil, check.Commentf(out))
  217. // Be really pick
  218. c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker --help'\n"))
  219. // docker inspect busybox: stdout=all, stderr=empty, rc=0
  220. // Just making sure stderr is empty on valid cmd
  221. out, _, err = dockerCmdWithError("inspect", "busybox")
  222. c.Assert(err, checker.IsNil, check.Commentf(out))
  223. // Be really pick
  224. 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"))
  225. // docker rm: stdout=empty, stderr=all, rc!=0
  226. // testing the min arg error msg
  227. cmd := exec.Command(dockerBinary, "rm")
  228. stdout, stderr, _, err := runCommandWithStdoutStderr(cmd)
  229. c.Assert(err, checker.NotNil)
  230. c.Assert(stdout, checker.Equals, "")
  231. // Should not contain full help text but should contain info about
  232. // # of args and Usage line
  233. c.Assert(stderr, checker.Contains, "requires a minimum", check.Commentf("Missing # of args text from 'docker rm'\n"))
  234. // docker rm NoSuchContainer: stdout=empty, stderr=all, rc=0
  235. // testing to make sure no blank line on error
  236. cmd = exec.Command(dockerBinary, "rm", "NoSuchContainer")
  237. stdout, stderr, _, err = runCommandWithStdoutStderr(cmd)
  238. c.Assert(err, checker.NotNil)
  239. c.Assert(len(stderr), checker.Not(checker.Equals), 0)
  240. c.Assert(stdout, checker.Equals, "")
  241. // Be really picky
  242. c.Assert(stderr, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker rm'\n"))
  243. // docker BadCmd: stdout=empty, stderr=all, rc=0
  244. cmd = exec.Command(dockerBinary, "BadCmd")
  245. stdout, stderr, _, err = runCommandWithStdoutStderr(cmd)
  246. c.Assert(err, checker.NotNil)
  247. c.Assert(stdout, checker.Equals, "")
  248. c.Assert(stderr, checker.Equals, "docker: 'BadCmd' is not a docker command.\nSee 'docker --help'.\n", check.Commentf("Unexcepted output for 'docker badCmd'\n"))
  249. }