docker_cli_start_test.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "time"
  7. "github.com/go-check/check"
  8. )
  9. // Regression test for https://github.com/docker/docker/issues/7843
  10. func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
  11. dockerCmd(c, "run", "-d", "--name", "test", "busybox")
  12. dockerCmd(c, "wait", "test")
  13. // Expect this to fail because the above container is stopped, this is what we want
  14. if _, err := runCommand(exec.Command(dockerBinary, "run", "-d", "--name", "test2", "--link", "test:test", "busybox")); err == nil {
  15. c.Fatal("Expected error but got none")
  16. }
  17. ch := make(chan struct{})
  18. go func() {
  19. // Attempt to start attached to the container that won't start
  20. // This should return an error immediately since the container can't be started
  21. if _, err := runCommand(exec.Command(dockerBinary, "start", "-a", "test2")); err == nil {
  22. c.Fatal("Expected error but got none")
  23. }
  24. close(ch)
  25. }()
  26. select {
  27. case <-ch:
  28. case <-time.After(time.Second):
  29. c.Fatalf("Attach did not exit properly")
  30. }
  31. }
  32. // gh#8555: Exit code should be passed through when using start -a
  33. func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
  34. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
  35. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  36. if err != nil {
  37. c.Fatalf("failed to run container: %v, output: %q", err, out)
  38. }
  39. out = strings.TrimSpace(out)
  40. // make sure the container has exited before trying the "start -a"
  41. waitCmd := exec.Command(dockerBinary, "wait", out)
  42. if _, _, err = runCommandWithOutput(waitCmd); err != nil {
  43. c.Fatalf("Failed to wait on container: %v", err)
  44. }
  45. startCmd := exec.Command(dockerBinary, "start", "-a", out)
  46. startOut, exitCode, err := runCommandWithOutput(startCmd)
  47. if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
  48. c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
  49. }
  50. if exitCode != 1 {
  51. c.Fatalf("start -a did not respond with proper exit code: expected 1, got %d", exitCode)
  52. }
  53. }
  54. func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
  55. name := "teststartattachcorrectexitcode"
  56. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "echo", "test")
  57. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  58. if err != nil {
  59. c.Fatalf("failed to run container: %v, output: %q", err, out)
  60. }
  61. // make sure the container has exited before trying the "start -a"
  62. waitCmd := exec.Command(dockerBinary, "wait", name)
  63. if _, _, err = runCommandWithOutput(waitCmd); err != nil {
  64. c.Fatalf("wait command failed with error: %v", err)
  65. }
  66. startCmd := exec.Command(dockerBinary, "start", "-a", name)
  67. startOut, _, err := runCommandWithOutput(startCmd)
  68. if err != nil {
  69. c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
  70. }
  71. if expected := "test\n"; startOut != expected {
  72. c.Fatalf("start -a produced unexpected output: expected %q, got %q", expected, startOut)
  73. }
  74. }
  75. func (s *DockerSuite) TestStartRecordError(c *check.C) {
  76. // when container runs successfully, we should not have state.Error
  77. dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
  78. stateErr, err := inspectField("test", "State.Error")
  79. if err != nil {
  80. c.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
  81. }
  82. if stateErr != "" {
  83. c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
  84. }
  85. // Expect this to fail and records error because of ports conflict
  86. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top"))
  87. if err == nil {
  88. c.Fatalf("Expected error but got none, output %q", out)
  89. }
  90. stateErr, err = inspectField("test2", "State.Error")
  91. if err != nil {
  92. c.Fatalf("Failed to inspect %q state's error, got error %q", "test2", err)
  93. }
  94. expected := "port is already allocated"
  95. if stateErr == "" || !strings.Contains(stateErr, expected) {
  96. c.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
  97. }
  98. // Expect the conflict to be resolved when we stop the initial container
  99. dockerCmd(c, "stop", "test")
  100. dockerCmd(c, "start", "test2")
  101. stateErr, err = inspectField("test2", "State.Error")
  102. if err != nil {
  103. c.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
  104. }
  105. if stateErr != "" {
  106. c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
  107. }
  108. }
  109. // gh#8726: a failed Start() breaks --volumes-from on subsequent Start()'s
  110. func (s *DockerSuite) TestStartVolumesFromFailsCleanly(c *check.C) {
  111. // Create the first data volume
  112. dockerCmd(c, "run", "-d", "--name", "data_before", "-v", "/foo", "busybox")
  113. // Expect this to fail because the data test after contaienr doesn't exist yet
  114. if _, err := runCommand(exec.Command(dockerBinary, "run", "-d", "--name", "consumer", "--volumes-from", "data_before", "--volumes-from", "data_after", "busybox")); err == nil {
  115. c.Fatal("Expected error but got none")
  116. }
  117. // Create the second data volume
  118. dockerCmd(c, "run", "-d", "--name", "data_after", "-v", "/bar", "busybox")
  119. // Now, all the volumes should be there
  120. dockerCmd(c, "start", "consumer")
  121. // Check that we have the volumes we want
  122. out, _ := dockerCmd(c, "inspect", "--format='{{ len .Volumes }}'", "consumer")
  123. nVolumes := strings.Trim(out, " \r\n'")
  124. if nVolumes != "2" {
  125. c.Fatalf("Missing volumes: expected 2, got %s", nVolumes)
  126. }
  127. }
  128. func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
  129. defer unpauseAllContainers()
  130. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
  131. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  132. c.Fatal(out, err)
  133. }
  134. runCmd = exec.Command(dockerBinary, "pause", "testing")
  135. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  136. c.Fatal(out, err)
  137. }
  138. runCmd = exec.Command(dockerBinary, "start", "testing")
  139. if out, _, err := runCommandWithOutput(runCmd); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") {
  140. c.Fatalf("an error should have been shown that you cannot start paused container: %s\n%v", out, err)
  141. }
  142. }
  143. func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
  144. // run a container named 'parent' and create two container link to `parent`
  145. cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
  146. if out, _, err := runCommandWithOutput(cmd); err != nil {
  147. c.Fatal(out, err)
  148. }
  149. for _, container := range []string{"child_first", "child_second"} {
  150. cmd = exec.Command(dockerBinary, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
  151. if out, _, err := runCommandWithOutput(cmd); err != nil {
  152. c.Fatal(out, err)
  153. }
  154. }
  155. // stop 'parent' container
  156. cmd = exec.Command(dockerBinary, "stop", "parent")
  157. if out, _, err := runCommandWithOutput(cmd); err != nil {
  158. c.Fatal(out, err)
  159. }
  160. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", "parent")
  161. out, _, err := runCommandWithOutput(cmd)
  162. if err != nil {
  163. c.Fatal(out, err)
  164. }
  165. out = strings.Trim(out, "\r\n")
  166. if out != "false" {
  167. c.Fatal("Container should be stopped")
  168. }
  169. // start all the three containers, container `child_first` start first which should be failed
  170. // container 'parent' start second and then start container 'child_second'
  171. cmd = exec.Command(dockerBinary, "start", "child_first", "parent", "child_second")
  172. out, _, err = runCommandWithOutput(cmd)
  173. if !strings.Contains(out, "Cannot start container child_first") || err == nil {
  174. c.Fatal("Expected error but got none")
  175. }
  176. for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
  177. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
  178. out, _, err = runCommandWithOutput(cmd)
  179. if err != nil {
  180. c.Fatal(out, err)
  181. }
  182. out = strings.Trim(out, "\r\n")
  183. if out != expected {
  184. c.Fatal("Container running state wrong")
  185. }
  186. }
  187. }
  188. func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
  189. var cmd *exec.Cmd
  190. // run multiple containers to test
  191. for _, container := range []string{"test1", "test2", "test3"} {
  192. cmd = exec.Command(dockerBinary, "run", "-d", "--name", container, "busybox", "top")
  193. if out, _, err := runCommandWithOutput(cmd); err != nil {
  194. c.Fatal(out, err)
  195. }
  196. }
  197. // stop all the containers
  198. for _, container := range []string{"test1", "test2", "test3"} {
  199. cmd = exec.Command(dockerBinary, "stop", container)
  200. if out, _, err := runCommandWithOutput(cmd); err != nil {
  201. c.Fatal(out, err)
  202. }
  203. }
  204. // test start and attach multiple containers at once, expected error
  205. for _, option := range []string{"-a", "-i", "-ai"} {
  206. cmd = exec.Command(dockerBinary, "start", option, "test1", "test2", "test3")
  207. out, _, err := runCommandWithOutput(cmd)
  208. if !strings.Contains(out, "You cannot start and attach multiple containers at once.") || err == nil {
  209. c.Fatal("Expected error but got none")
  210. }
  211. }
  212. // confirm the state of all the containers be stopped
  213. for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
  214. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
  215. out, _, err := runCommandWithOutput(cmd)
  216. if err != nil {
  217. c.Fatal(out, err)
  218. }
  219. out = strings.Trim(out, "\r\n")
  220. if out != expected {
  221. c.Fatal("Container running state wrong")
  222. }
  223. }
  224. }