docker_cli_help_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. cmds := []string{}
  80. // Grab all chars starting at "Commands:"
  81. helpOut := strings.Split(out[i:], "\n")
  82. // First line is just "Commands:"
  83. if isLocalDaemon {
  84. // Replace first line with "daemon" command since it's not part of the list of commands.
  85. helpOut[0] = " daemon"
  86. } else {
  87. // Skip first line
  88. helpOut = helpOut[1:]
  89. }
  90. for _, cmd := range helpOut {
  91. var stderr string
  92. // Stop on blank line or non-idented line
  93. if cmd == "" || !unicode.IsSpace(rune(cmd[0])) {
  94. break
  95. }
  96. // Grab just the first word of each line
  97. cmd = strings.Split(strings.TrimSpace(cmd), " ")[0]
  98. cmds = append(cmds, cmd)
  99. // Check the full usage text
  100. helpCmd := exec.Command(dockerBinary, cmd, "--help")
  101. helpCmd.Env = newEnvs
  102. out, stderr, ec, err = runCommandWithStdoutStderr(helpCmd)
  103. if len(stderr) != 0 {
  104. c.Fatalf("Error on %q help. non-empty stderr:%q", cmd, stderr)
  105. }
  106. if strings.HasSuffix(out, "\n\n") {
  107. c.Fatalf("Should not have blank line on %q\nout:%q", cmd, out)
  108. }
  109. if !strings.Contains(out, "--help=false") {
  110. c.Fatalf("Should show full usage on %q\nout:%q", cmd, out)
  111. }
  112. if err != nil || ec != 0 {
  113. c.Fatalf("Error on %q help: %s\nexit code:%d", cmd, out, ec)
  114. }
  115. // Check each line for lots of stuff
  116. lines := strings.Split(out, "\n")
  117. for _, line := range lines {
  118. if len(line) > 90 {
  119. c.Fatalf("Help for %q is too long(%d chars):\n%s", cmd,
  120. len(line), line)
  121. }
  122. if scanForHome && strings.Contains(line, `"`+home) {
  123. c.Fatalf("Help for %q should use ~ instead of %q on:\n%s",
  124. cmd, home, line)
  125. }
  126. i := strings.Index(line, "~")
  127. if i >= 0 && i != len(line)-1 && line[i+1] != '/' {
  128. c.Fatalf("Help for %q should not have used ~:\n%s", cmd, line)
  129. }
  130. // If a line starts with 4 spaces then assume someone
  131. // added a multi-line description for an option and we need
  132. // to flag it
  133. if strings.HasPrefix(line, " ") {
  134. c.Fatalf("Help for %q should not have a multi-line option: %s", cmd, line)
  135. }
  136. // Options should NOT end with a period
  137. if strings.HasPrefix(line, " -") && strings.HasSuffix(line, ".") {
  138. c.Fatalf("Help for %q should not end with a period: %s", cmd, line)
  139. }
  140. // Options should NOT end with a space
  141. if strings.HasSuffix(line, " ") {
  142. c.Fatalf("Help for %q should not end with a space: %s", cmd, line)
  143. }
  144. }
  145. // For each command make sure we generate an error
  146. // if we give a bad arg
  147. dCmd := exec.Command(dockerBinary, cmd, "--badArg")
  148. out, stderr, ec, err = runCommandWithStdoutStderr(dCmd)
  149. if len(out) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
  150. c.Fatalf("Bad results from 'docker %s --badArg'\nec:%d\nstdout:%s\nstderr:%s\nerr:%q", cmd, ec, out, stderr, err)
  151. }
  152. // Be really picky
  153. if strings.HasSuffix(stderr, "\n\n") {
  154. c.Fatalf("Should not have a blank line at the end of 'docker rm'\n%s", stderr)
  155. }
  156. // Now make sure that each command will print a short-usage
  157. // (not a full usage - meaning no opts section) if we
  158. // are missing a required arg or pass in a bad arg
  159. // These commands will never print a short-usage so don't test
  160. noShortUsage := map[string]string{
  161. "images": "",
  162. "login": "",
  163. "logout": "",
  164. }
  165. if _, ok := noShortUsage[cmd]; !ok {
  166. // For each command run it w/o any args. It will either return
  167. // valid output or print a short-usage
  168. var dCmd *exec.Cmd
  169. var stdout, stderr string
  170. var args []string
  171. // skipNoArgs are ones that we don't want to try w/o
  172. // any args. Either because it'll hang the test or
  173. // lead to incorrect test result (like false negative).
  174. // Whatever the reason, skip trying to run w/o args and
  175. // jump to trying with a bogus arg.
  176. skipNoArgs := map[string]struct{}{
  177. "daemon": {},
  178. "events": {},
  179. "load": {},
  180. }
  181. ec = 0
  182. if _, ok := skipNoArgs[cmd]; !ok {
  183. args = []string{cmd}
  184. dCmd = exec.Command(dockerBinary, args...)
  185. stdout, stderr, ec, err = runCommandWithStdoutStderr(dCmd)
  186. }
  187. // If its ok w/o any args then try again with an arg
  188. if ec == 0 {
  189. args = []string{cmd, "badArg"}
  190. dCmd = exec.Command(dockerBinary, args...)
  191. stdout, stderr, ec, err = runCommandWithStdoutStderr(dCmd)
  192. }
  193. if len(stdout) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
  194. c.Fatalf("Bad output from %q\nstdout:%q\nstderr:%q\nec:%d\nerr:%q", args, stdout, stderr, ec, err)
  195. }
  196. // Should have just short usage
  197. if !strings.Contains(stderr, "\nUsage:\t") {
  198. c.Fatalf("Missing short usage on %q\nstderr:%q", args, stderr)
  199. }
  200. // But shouldn't have full usage
  201. if strings.Contains(stderr, "--help=false") {
  202. c.Fatalf("Should not have full usage on %q\nstderr:%q", args, stderr)
  203. }
  204. if strings.HasSuffix(stderr, "\n\n") {
  205. c.Fatalf("Should not have a blank line on %q\nstderr:%q", args, stderr)
  206. }
  207. }
  208. }
  209. expected := 39
  210. if isLocalDaemon {
  211. expected++ // for the daemon command
  212. }
  213. if len(cmds) != expected {
  214. c.Fatalf("Wrong # of cmds(%d), it should be: %d\nThe list:\n%q",
  215. len(cmds), expected, cmds)
  216. }
  217. }
  218. }
  219. func (s *DockerSuite) TestHelpExitCodesHelpOutput(c *check.C) {
  220. // Test to make sure the exit code and output (stdout vs stderr) of
  221. // various good and bad cases are what we expect
  222. // docker : stdout=all, stderr=empty, rc=0
  223. cmd := exec.Command(dockerBinary)
  224. stdout, stderr, ec, err := runCommandWithStdoutStderr(cmd)
  225. if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
  226. c.Fatalf("Bad results from 'docker'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
  227. }
  228. // Be really pick
  229. if strings.HasSuffix(stdout, "\n\n") {
  230. c.Fatalf("Should not have a blank line at the end of 'docker'\n%s", stdout)
  231. }
  232. // docker help: stdout=all, stderr=empty, rc=0
  233. cmd = exec.Command(dockerBinary, "help")
  234. stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
  235. if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
  236. c.Fatalf("Bad results from 'docker help'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
  237. }
  238. // Be really pick
  239. if strings.HasSuffix(stdout, "\n\n") {
  240. c.Fatalf("Should not have a blank line at the end of 'docker help'\n%s", stdout)
  241. }
  242. // docker --help: stdout=all, stderr=empty, rc=0
  243. cmd = exec.Command(dockerBinary, "--help")
  244. stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
  245. if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
  246. c.Fatalf("Bad results from 'docker --help'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
  247. }
  248. // Be really pick
  249. if strings.HasSuffix(stdout, "\n\n") {
  250. c.Fatalf("Should not have a blank line at the end of 'docker --help'\n%s", stdout)
  251. }
  252. // docker inspect busybox: stdout=all, stderr=empty, rc=0
  253. // Just making sure stderr is empty on valid cmd
  254. cmd = exec.Command(dockerBinary, "inspect", "busybox")
  255. stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
  256. if len(stdout) == 0 || len(stderr) != 0 || ec != 0 || err != nil {
  257. c.Fatalf("Bad results from 'docker inspect busybox'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
  258. }
  259. // Be really pick
  260. if strings.HasSuffix(stdout, "\n\n") {
  261. c.Fatalf("Should not have a blank line at the end of 'docker inspect busyBox'\n%s", stdout)
  262. }
  263. // docker rm: stdout=empty, stderr=all, rc!=0
  264. // testing the min arg error msg
  265. cmd = exec.Command(dockerBinary, "rm")
  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'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
  269. }
  270. // Should not contain full help text but should contain info about
  271. // # of args and Usage line
  272. if !strings.Contains(stderr, "requires a minimum") {
  273. c.Fatalf("Missing # of args text from 'docker rm'\nstderr:%s", stderr)
  274. }
  275. // docker rm NoSuchContainer: stdout=empty, stderr=all, rc=0
  276. // testing to make sure no blank line on error
  277. cmd = exec.Command(dockerBinary, "rm", "NoSuchContainer")
  278. stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
  279. if len(stdout) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
  280. c.Fatalf("Bad results from 'docker rm NoSuchContainer'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
  281. }
  282. // Be really picky
  283. if strings.HasSuffix(stderr, "\n\n") {
  284. c.Fatalf("Should not have a blank line at the end of 'docker rm'\n%s", stderr)
  285. }
  286. // docker BadCmd: stdout=empty, stderr=all, rc=0
  287. cmd = exec.Command(dockerBinary, "BadCmd")
  288. stdout, stderr, ec, err = runCommandWithStdoutStderr(cmd)
  289. if len(stdout) != 0 || len(stderr) == 0 || ec == 0 || err == nil {
  290. c.Fatalf("Bad results from 'docker BadCmd'\nec:%d\nstdout:%s\nstderr:%s\nerr:%v", ec, stdout, stderr, err)
  291. }
  292. if stderr != "docker: 'BadCmd' is not a docker command.\nSee 'docker --help'.\n" {
  293. c.Fatalf("Unexcepted output for 'docker badCmd'\nstderr:%s", stderr)
  294. }
  295. // Be really picky
  296. if strings.HasSuffix(stderr, "\n\n") {
  297. c.Fatalf("Should not have a blank line at the end of 'docker rm'\n%s", stderr)
  298. }
  299. }