docker_cli_help_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package main
  2. import (
  3. "fmt"
  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 := appendBaseEnv(true)
  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. foundTooLongLine := false
  49. for _, line := range lines {
  50. if !foundTooLongLine && len(line) > 80 {
  51. c.Logf("Line is too long:\n%s", line)
  52. foundTooLongLine = true
  53. }
  54. // All lines should not end with a space
  55. c.Assert(line, checker.Not(checker.HasSuffix), " ", check.Commentf("Line should not end with a space"))
  56. if scanForHome && strings.Contains(line, `=`+home) {
  57. c.Fatalf("Line should use '%q' instead of %q:\n%s", homedir.GetShortcutString(), home, line)
  58. }
  59. if runtime.GOOS != "windows" {
  60. i := strings.Index(line, homedir.GetShortcutString())
  61. if i >= 0 && i != len(line)-1 && line[i+1] != '/' {
  62. c.Fatalf("Main help should not have used home shortcut:\n%s", line)
  63. }
  64. }
  65. }
  66. // Make sure each cmd's help text fits within 90 chars and that
  67. // on non-windows system we use ~ when possible (to shorten things).
  68. // Pull the list of commands from the "Commands:" section of docker help
  69. helpCmd = exec.Command(dockerBinary, "help")
  70. helpCmd.Env = newEnvs
  71. out, _, err = runCommandWithOutput(helpCmd)
  72. c.Assert(err, checker.IsNil, check.Commentf(out))
  73. i := strings.Index(out, "Commands:")
  74. c.Assert(i, checker.GreaterOrEqualThan, 0, check.Commentf("Missing 'Commands:' in:\n%s", out))
  75. cmds := []string{}
  76. // Grab all chars starting at "Commands:"
  77. helpOut := strings.Split(out[i:], "\n")
  78. // First line is just "Commands:"
  79. if isLocalDaemon {
  80. // Replace first line with "daemon" command since it's not part of the list of commands.
  81. helpOut[0] = " daemon"
  82. } else {
  83. // Skip first line
  84. helpOut = helpOut[1:]
  85. }
  86. // Create the list of commands we want to test
  87. cmdsToTest := []string{}
  88. for _, cmd := range helpOut {
  89. // Stop on blank line or non-idented line
  90. if cmd == "" || !unicode.IsSpace(rune(cmd[0])) {
  91. break
  92. }
  93. // Grab just the first word of each line
  94. cmd = strings.Split(strings.TrimSpace(cmd), " ")[0]
  95. cmds = append(cmds, cmd) // Saving count for later
  96. cmdsToTest = append(cmdsToTest, cmd)
  97. }
  98. // Add some 'two word' commands - would be nice to automatically
  99. // calculate this list - somehow
  100. cmdsToTest = append(cmdsToTest, "volume create")
  101. cmdsToTest = append(cmdsToTest, "volume inspect")
  102. cmdsToTest = append(cmdsToTest, "volume ls")
  103. cmdsToTest = append(cmdsToTest, "volume rm")
  104. // Divide the list of commands into go routines and run the func testcommand on the commands in parallel
  105. // to save runtime of test
  106. errChan := make(chan error)
  107. for index := 0; index < len(cmdsToTest); index++ {
  108. go func(index int) {
  109. errChan <- testCommand(cmdsToTest[index], newEnvs, scanForHome, home)
  110. }(index)
  111. }
  112. for index := 0; index < len(cmdsToTest); index++ {
  113. err := <-errChan
  114. if err != nil {
  115. c.Fatal(err)
  116. }
  117. }
  118. // Number of commands for standard release and experimental release
  119. standard := 41
  120. experimental := 1
  121. expected := standard + experimental
  122. if isLocalDaemon {
  123. expected++ // for the daemon command
  124. }
  125. c.Assert(len(cmds), checker.LessOrEqualThan, expected, check.Commentf("Wrong # of cmds, it should be: %d\nThe list:\n%q", expected, cmds))
  126. }
  127. }
  128. func (s *DockerSuite) TestHelpExitCodesHelpOutput(c *check.C) {
  129. testRequires(c, DaemonIsLinux)
  130. // Test to make sure the exit code and output (stdout vs stderr) of
  131. // various good and bad cases are what we expect
  132. // docker : stdout=all, stderr=empty, rc=0
  133. out, _, err := dockerCmdWithError()
  134. c.Assert(err, checker.IsNil, check.Commentf(out))
  135. // Be really pick
  136. c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker'\n"))
  137. // docker help: stdout=all, stderr=empty, rc=0
  138. out, _, err = dockerCmdWithError("help")
  139. c.Assert(err, checker.IsNil, check.Commentf(out))
  140. // Be really pick
  141. c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker help'\n"))
  142. // docker --help: stdout=all, stderr=empty, rc=0
  143. out, _, err = dockerCmdWithError("--help")
  144. c.Assert(err, checker.IsNil, check.Commentf(out))
  145. // Be really pick
  146. c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker --help'\n"))
  147. // docker inspect busybox: stdout=all, stderr=empty, rc=0
  148. // Just making sure stderr is empty on valid cmd
  149. out, _, err = dockerCmdWithError("inspect", "busybox")
  150. c.Assert(err, checker.IsNil, check.Commentf(out))
  151. // Be really pick
  152. 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"))
  153. // docker rm: stdout=empty, stderr=all, rc!=0
  154. // testing the min arg error msg
  155. cmd := exec.Command(dockerBinary, "rm")
  156. stdout, stderr, _, err := runCommandWithStdoutStderr(cmd)
  157. c.Assert(err, checker.NotNil)
  158. c.Assert(stdout, checker.Equals, "")
  159. // Should not contain full help text but should contain info about
  160. // # of args and Usage line
  161. c.Assert(stderr, checker.Contains, "requires a minimum", check.Commentf("Missing # of args text from 'docker rm'\n"))
  162. // docker rm NoSuchContainer: stdout=empty, stderr=all, rc=0
  163. // testing to make sure no blank line on error
  164. cmd = exec.Command(dockerBinary, "rm", "NoSuchContainer")
  165. stdout, stderr, _, err = runCommandWithStdoutStderr(cmd)
  166. c.Assert(err, checker.NotNil)
  167. c.Assert(len(stderr), checker.Not(checker.Equals), 0)
  168. c.Assert(stdout, checker.Equals, "")
  169. // Be really picky
  170. c.Assert(stderr, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have a blank line at the end of 'docker rm'\n"))
  171. // docker BadCmd: stdout=empty, stderr=all, rc=0
  172. cmd = exec.Command(dockerBinary, "BadCmd")
  173. stdout, stderr, _, err = runCommandWithStdoutStderr(cmd)
  174. c.Assert(err, checker.NotNil)
  175. c.Assert(stdout, checker.Equals, "")
  176. c.Assert(stderr, checker.Equals, "docker: 'BadCmd' is not a docker command.\nSee 'docker --help'.\n", check.Commentf("Unexcepted output for 'docker badCmd'\n"))
  177. }
  178. func testCommand(cmd string, newEnvs []string, scanForHome bool, home string) error {
  179. args := strings.Split(cmd+" --help", " ")
  180. // Check the full usage text
  181. helpCmd := exec.Command(dockerBinary, args...)
  182. helpCmd.Env = newEnvs
  183. out, stderr, _, err := runCommandWithStdoutStderr(helpCmd)
  184. if len(stderr) != 0 {
  185. return fmt.Errorf("Error on %q help. non-empty stderr:%q\n", cmd, stderr)
  186. }
  187. if strings.HasSuffix(out, "\n\n") {
  188. return fmt.Errorf("Should not have blank line on %q\n", cmd)
  189. }
  190. if !strings.Contains(out, "--help") {
  191. return fmt.Errorf("All commands should mention '--help'. Command '%v' did not.\n", cmd)
  192. }
  193. if err != nil {
  194. return fmt.Errorf(out)
  195. }
  196. // Check each line for lots of stuff
  197. lines := strings.Split(out, "\n")
  198. for _, line := range lines {
  199. if len(line) > 107 {
  200. return fmt.Errorf("Help for %q is too long:\n%s\n", cmd, line)
  201. }
  202. if scanForHome && strings.Contains(line, `"`+home) {
  203. return fmt.Errorf("Help for %q should use ~ instead of %q on:\n%s\n",
  204. cmd, home, line)
  205. }
  206. i := strings.Index(line, "~")
  207. if i >= 0 && i != len(line)-1 && line[i+1] != '/' {
  208. return fmt.Errorf("Help for %q should not have used ~:\n%s", cmd, line)
  209. }
  210. // If a line starts with 4 spaces then assume someone
  211. // added a multi-line description for an option and we need
  212. // to flag it
  213. if strings.HasPrefix(line, " ") &&
  214. !strings.HasPrefix(strings.TrimLeft(line, " "), "--") {
  215. return fmt.Errorf("Help for %q should not have a multi-line option", cmd)
  216. }
  217. // Options should NOT end with a period
  218. if strings.HasPrefix(line, " -") && strings.HasSuffix(line, ".") {
  219. return fmt.Errorf("Help for %q should not end with a period: %s", cmd, line)
  220. }
  221. // Options should NOT end with a space
  222. if strings.HasSuffix(line, " ") {
  223. return fmt.Errorf("Help for %q should not end with a space: %s", cmd, line)
  224. }
  225. }
  226. // For each command make sure we generate an error
  227. // if we give a bad arg
  228. args = strings.Split(cmd+" --badArg", " ")
  229. out, _, err = dockerCmdWithError(args...)
  230. if err == nil {
  231. return fmt.Errorf(out)
  232. }
  233. // Be really picky
  234. if strings.HasSuffix(stderr, "\n\n") {
  235. return fmt.Errorf("Should not have a blank line at the end of 'docker rm'\n")
  236. }
  237. // Now make sure that each command will print a short-usage
  238. // (not a full usage - meaning no opts section) if we
  239. // are missing a required arg or pass in a bad arg
  240. // These commands will never print a short-usage so don't test
  241. noShortUsage := map[string]string{
  242. "images": "",
  243. "login": "",
  244. "logout": "",
  245. "network": "",
  246. "stats": "",
  247. }
  248. if _, ok := noShortUsage[cmd]; !ok {
  249. // For each command run it w/o any args. It will either return
  250. // valid output or print a short-usage
  251. var dCmd *exec.Cmd
  252. // skipNoArgs are ones that we don't want to try w/o
  253. // any args. Either because it'll hang the test or
  254. // lead to incorrect test result (like false negative).
  255. // Whatever the reason, skip trying to run w/o args and
  256. // jump to trying with a bogus arg.
  257. skipNoArgs := map[string]struct{}{
  258. "daemon": {},
  259. "events": {},
  260. "load": {},
  261. }
  262. ec := 0
  263. if _, ok := skipNoArgs[cmd]; !ok {
  264. args = strings.Split(cmd, " ")
  265. dCmd = exec.Command(dockerBinary, args...)
  266. out, stderr, ec, err = runCommandWithStdoutStderr(dCmd)
  267. }
  268. // If its ok w/o any args then try again with an arg
  269. if ec == 0 {
  270. args = strings.Split(cmd+" badArg", " ")
  271. dCmd = exec.Command(dockerBinary, args...)
  272. out, stderr, ec, err = runCommandWithStdoutStderr(dCmd)
  273. }
  274. if len(out) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
  275. return fmt.Errorf("Bad output from %q\nstdout:%q\nstderr:%q\nec:%d\nerr:%q\n", args, out, stderr, ec, err)
  276. }
  277. // Should have just short usage
  278. if !strings.Contains(stderr, "\nUsage:") {
  279. return fmt.Errorf("Missing short usage on %q\n:%#v", args, stderr)
  280. }
  281. // But shouldn't have full usage
  282. if strings.Contains(stderr, "--help=false") {
  283. return fmt.Errorf("Should not have full usage on %q\n", args)
  284. }
  285. if strings.HasSuffix(stderr, "\n\n") {
  286. return fmt.Errorf("Should not have a blank line on %q\n%v", args, stderr)
  287. }
  288. }
  289. return nil
  290. }