docker_cli_start_test.go 7.9 KB

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