docker_cli_help_test.go 13 KB

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