docker_cli_start_test.go 8.0 KB

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