docker_cli_help_test.go 11 KB

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