docker_cli_help_test.go 12 KB

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