docker_cli_start_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/docker/docker/integration-cli/checker"
  7. "github.com/docker/docker/integration-cli/cli"
  8. icmd "github.com/docker/docker/pkg/testutil/cmd"
  9. "github.com/go-check/check"
  10. )
  11. // Regression test for https://github.com/docker/docker/issues/7843
  12. func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
  13. // Windows does not support link
  14. testRequires(c, DaemonIsLinux)
  15. dockerCmd(c, "run", "--name", "test", "busybox")
  16. // Expect this to fail because the above container is stopped, this is what we want
  17. out, _, err := dockerCmdWithError("run", "--name", "test2", "--link", "test:test", "busybox")
  18. // err shouldn't be nil because container test2 try to link to stopped container
  19. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  20. ch := make(chan error)
  21. go func() {
  22. // Attempt to start attached to the container that won't start
  23. // This should return an error immediately since the container can't be started
  24. if out, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
  25. ch <- fmt.Errorf("Expected error but got none:\n%s", out)
  26. }
  27. close(ch)
  28. }()
  29. select {
  30. case err := <-ch:
  31. c.Assert(err, check.IsNil)
  32. case <-time.After(5 * time.Second):
  33. c.Fatalf("Attach did not exit properly")
  34. }
  35. }
  36. // gh#8555: Exit code should be passed through when using start -a
  37. func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
  38. testRequires(c, DaemonIsLinux)
  39. out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1").Stdout()
  40. out = strings.TrimSpace(out)
  41. // make sure the container has exited before trying the "start -a"
  42. cli.DockerCmd(c, "wait", out)
  43. cli.Docker(cli.Args("start", "-a", out)).Assert(c, icmd.Expected{
  44. ExitCode: 1,
  45. })
  46. }
  47. func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
  48. name := "teststartattachcorrectexitcode"
  49. dockerCmd(c, "run", "--name", name, "busybox", "echo", "test")
  50. // make sure the container has exited before trying the "start -a"
  51. dockerCmd(c, "wait", name)
  52. startOut, _ := dockerCmd(c, "start", "-a", name)
  53. // start -a produced unexpected output
  54. c.Assert(startOut, checker.Equals, "test\n")
  55. }
  56. func (s *DockerSuite) TestStartRecordError(c *check.C) {
  57. // TODO Windows CI: Requires further porting work. Should be possible.
  58. testRequires(c, DaemonIsLinux)
  59. // when container runs successfully, we should not have state.Error
  60. dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
  61. stateErr := inspectField(c, "test", "State.Error")
  62. // Expected to not have state error
  63. c.Assert(stateErr, checker.Equals, "")
  64. // Expect this to fail and records error because of ports conflict
  65. out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
  66. // err shouldn't be nil because docker run will fail
  67. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  68. stateErr = inspectField(c, "test2", "State.Error")
  69. c.Assert(stateErr, checker.Contains, "port is already allocated")
  70. // Expect the conflict to be resolved when we stop the initial container
  71. dockerCmd(c, "stop", "test")
  72. dockerCmd(c, "start", "test2")
  73. stateErr = inspectField(c, "test2", "State.Error")
  74. // Expected to not have state error but got one
  75. c.Assert(stateErr, checker.Equals, "")
  76. }
  77. func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
  78. // Windows does not support pausing containers
  79. testRequires(c, IsPausable)
  80. defer unpauseAllContainers(c)
  81. runSleepingContainer(c, "-d", "--name", "testing")
  82. dockerCmd(c, "pause", "testing")
  83. out, _, err := dockerCmdWithError("start", "testing")
  84. // an error should have been shown that you cannot start paused container
  85. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  86. // an error should have been shown that you cannot start paused container
  87. c.Assert(out, checker.Contains, "Cannot start a paused container, try unpause instead.")
  88. }
  89. func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
  90. // Windows does not support --link
  91. testRequires(c, DaemonIsLinux)
  92. // run a container named 'parent' and create two container link to `parent`
  93. dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
  94. for _, container := range []string{"child_first", "child_second"} {
  95. dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
  96. }
  97. // stop 'parent' container
  98. dockerCmd(c, "stop", "parent")
  99. out := inspectField(c, "parent", "State.Running")
  100. // Container should be stopped
  101. c.Assert(out, checker.Equals, "false")
  102. // start all the three containers, container `child_first` start first which should be failed
  103. // container 'parent' start second and then start container 'child_second'
  104. expOut := "Cannot link to a non running container"
  105. expErr := "failed to start containers: [child_first]"
  106. out, _, err := dockerCmdWithError("start", "child_first", "parent", "child_second")
  107. // err shouldn't be nil because start will fail
  108. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  109. // output does not correspond to what was expected
  110. if !(strings.Contains(out, expOut) || strings.Contains(err.Error(), expErr)) {
  111. c.Fatalf("Expected out: %v with err: %v but got out: %v with err: %v", expOut, expErr, out, err)
  112. }
  113. for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
  114. out := inspectField(c, container, "State.Running")
  115. // Container running state wrong
  116. c.Assert(out, checker.Equals, expected)
  117. }
  118. }
  119. func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
  120. // run multiple containers to test
  121. for _, container := range []string{"test1", "test2", "test3"} {
  122. runSleepingContainer(c, "--name", container)
  123. }
  124. // stop all the containers
  125. for _, container := range []string{"test1", "test2", "test3"} {
  126. dockerCmd(c, "stop", container)
  127. }
  128. // test start and attach multiple containers at once, expected error
  129. for _, option := range []string{"-a", "-i", "-ai"} {
  130. out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
  131. // err shouldn't be nil because start will fail
  132. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  133. // output does not correspond to what was expected
  134. c.Assert(out, checker.Contains, "You cannot start and attach multiple containers at once.")
  135. }
  136. // confirm the state of all the containers be stopped
  137. for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
  138. out := inspectField(c, container, "State.Running")
  139. // Container running state wrong
  140. c.Assert(out, checker.Equals, expected)
  141. }
  142. }
  143. // Test case for #23716
  144. func (s *DockerSuite) TestStartAttachWithRename(c *check.C) {
  145. testRequires(c, DaemonIsLinux)
  146. dockerCmd(c, "create", "-t", "--name", "before", "busybox")
  147. go func() {
  148. c.Assert(waitRun("before"), checker.IsNil)
  149. dockerCmd(c, "rename", "before", "after")
  150. dockerCmd(c, "stop", "--time=2", "after")
  151. }()
  152. // FIXME(vdemeester) the intent is not clear and potentially racey
  153. result := icmd.RunCommand(dockerBinary, "start", "-a", "before")
  154. result.Assert(c, icmd.Expected{
  155. ExitCode: 137,
  156. Error: "exit status 137",
  157. })
  158. c.Assert(result.Stderr(), checker.Not(checker.Contains), "No such container")
  159. }
  160. func (s *DockerSuite) TestStartReturnCorrectExitCode(c *check.C) {
  161. dockerCmd(c, "create", "--restart=on-failure:2", "--name", "withRestart", "busybox", "sh", "-c", "exit 11")
  162. dockerCmd(c, "create", "--rm", "--name", "withRm", "busybox", "sh", "-c", "exit 12")
  163. _, exitCode, err := dockerCmdWithError("start", "-a", "withRestart")
  164. c.Assert(err, checker.NotNil)
  165. c.Assert(exitCode, checker.Equals, 11)
  166. _, exitCode, err = dockerCmdWithError("start", "-a", "withRm")
  167. c.Assert(err, checker.NotNil)
  168. c.Assert(exitCode, checker.Equals, 12)
  169. }