docker_cli_start_test.go 7.6 KB

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