docker_cli_start_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/integration-cli/cli"
  8. "gotest.tools/assert"
  9. "gotest.tools/icmd"
  10. )
  11. // Regression test for https://github.com/docker/docker/issues/7843
  12. func (s *DockerSuite) TestStartAttachReturnsOnError(c *testing.T) {
  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. assert.Assert(c, err != nil, "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. assert.NilError(c, err)
  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 *testing.T) {
  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 *testing.T) {
  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. assert.Equal(c, startOut, "test\n")
  55. }
  56. func (s *DockerSuite) TestStartRecordError(c *testing.T) {
  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. assert.Equal(c, stateErr, "")
  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. assert.Assert(c, err != nil, "out: %s", out)
  68. stateErr = inspectField(c, "test2", "State.Error")
  69. assert.Assert(c, strings.Contains(stateErr, "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. assert.Equal(c, stateErr, "")
  76. }
  77. func (s *DockerSuite) TestStartPausedContainer(c *testing.T) {
  78. // Windows does not support pausing containers
  79. testRequires(c, IsPausable)
  80. runSleepingContainer(c, "-d", "--name", "testing")
  81. dockerCmd(c, "pause", "testing")
  82. out, _, err := dockerCmdWithError("start", "testing")
  83. // an error should have been shown that you cannot start paused container
  84. assert.Assert(c, err != nil, "out: %s", out)
  85. // an error should have been shown that you cannot start paused container
  86. assert.Assert(c, strings.Contains(strings.ToLower(out), "cannot start a paused container, try unpause instead"))
  87. }
  88. func (s *DockerSuite) TestStartMultipleContainers(c *testing.T) {
  89. // Windows does not support --link
  90. testRequires(c, DaemonIsLinux)
  91. // run a container named 'parent' and create two container link to `parent`
  92. dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
  93. for _, container := range []string{"child_first", "child_second"} {
  94. dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
  95. }
  96. // stop 'parent' container
  97. dockerCmd(c, "stop", "parent")
  98. out := inspectField(c, "parent", "State.Running")
  99. // Container should be stopped
  100. assert.Equal(c, out, "false")
  101. // start all the three containers, container `child_first` start first which should be failed
  102. // container 'parent' start second and then start container 'child_second'
  103. expOut := "Cannot link to a non running container"
  104. expErr := "failed to start containers: [child_first]"
  105. out, _, err := dockerCmdWithError("start", "child_first", "parent", "child_second")
  106. // err shouldn't be nil because start will fail
  107. assert.Assert(c, err != nil, "out: %s", out)
  108. // output does not correspond to what was expected
  109. if !(strings.Contains(out, expOut) || strings.Contains(err.Error(), expErr)) {
  110. c.Fatalf("Expected out: %v with err: %v but got out: %v with err: %v", expOut, expErr, out, err)
  111. }
  112. for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
  113. out := inspectField(c, container, "State.Running")
  114. // Container running state wrong
  115. assert.Equal(c, out, expected)
  116. }
  117. }
  118. func (s *DockerSuite) TestStartAttachMultipleContainers(c *testing.T) {
  119. // run multiple containers to test
  120. for _, container := range []string{"test1", "test2", "test3"} {
  121. runSleepingContainer(c, "--name", container)
  122. }
  123. // stop all the containers
  124. for _, container := range []string{"test1", "test2", "test3"} {
  125. dockerCmd(c, "stop", container)
  126. }
  127. // test start and attach multiple containers at once, expected error
  128. for _, option := range []string{"-a", "-i", "-ai"} {
  129. out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
  130. // err shouldn't be nil because start will fail
  131. assert.Assert(c, err != nil, "out: %s", out)
  132. // output does not correspond to what was expected
  133. assert.Assert(c, strings.Contains(out, "you cannot start and attach multiple containers at once"))
  134. }
  135. // confirm the state of all the containers be stopped
  136. for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
  137. out := inspectField(c, container, "State.Running")
  138. // Container running state wrong
  139. assert.Equal(c, out, expected)
  140. }
  141. }
  142. // Test case for #23716
  143. func (s *DockerSuite) TestStartAttachWithRename(c *testing.T) {
  144. testRequires(c, DaemonIsLinux)
  145. cli.DockerCmd(c, "create", "-t", "--name", "before", "busybox")
  146. go func() {
  147. cli.WaitRun(c, "before")
  148. cli.DockerCmd(c, "rename", "before", "after")
  149. cli.DockerCmd(c, "stop", "--time=2", "after")
  150. }()
  151. // FIXME(vdemeester) the intent is not clear and potentially racey
  152. result := cli.Docker(cli.Args("start", "-a", "before")).Assert(c, icmd.Expected{
  153. ExitCode: 137,
  154. })
  155. assert.Assert(c, !strings.Contains(result.Stderr(), "No such container"))
  156. }
  157. func (s *DockerSuite) TestStartReturnCorrectExitCode(c *testing.T) {
  158. dockerCmd(c, "create", "--restart=on-failure:2", "--name", "withRestart", "busybox", "sh", "-c", "exit 11")
  159. dockerCmd(c, "create", "--rm", "--name", "withRm", "busybox", "sh", "-c", "exit 12")
  160. out, exitCode, err := dockerCmdWithError("start", "-a", "withRestart")
  161. assert.ErrorContains(c, err, "")
  162. assert.Equal(c, exitCode, 11, fmt.Sprintf("out: %s", out))
  163. out, exitCode, err = dockerCmdWithError("start", "-a", "withRm")
  164. assert.ErrorContains(c, err, "")
  165. assert.Equal(c, exitCode, 12, fmt.Sprintf("out: %s", out))
  166. }