docker_cli_start_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/integration-cli/cli"
  8. "github.com/go-check/check"
  9. "gotest.tools/assert"
  10. "gotest.tools/icmd"
  11. )
  12. // Regression test for https://github.com/docker/docker/issues/7843
  13. func (s *DockerSuite) TestStartAttachReturnsOnError(c *testing.T) {
  14. // Windows does not support link
  15. testRequires(c, DaemonIsLinux)
  16. dockerCmd(c, "run", "--name", "test", "busybox")
  17. // Expect this to fail because the above container is stopped, this is what we want
  18. out, _, err := dockerCmdWithError("run", "--name", "test2", "--link", "test:test", "busybox")
  19. // err shouldn't be nil because container test2 try to link to stopped container
  20. assert.Assert(c, err != nil, fmt.Sprintf("out: %s", out))
  21. ch := make(chan error)
  22. go func() {
  23. // Attempt to start attached to the container that won't start
  24. // This should return an error immediately since the container can't be started
  25. if out, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
  26. ch <- fmt.Errorf("Expected error but got none:\n%s", out)
  27. }
  28. close(ch)
  29. }()
  30. select {
  31. case err := <-ch:
  32. assert.NilError(c, err)
  33. case <-time.After(5 * time.Second):
  34. c.Fatalf("Attach did not exit properly")
  35. }
  36. }
  37. // gh#8555: Exit code should be passed through when using start -a
  38. func (s *DockerSuite) TestStartAttachCorrectExitCode(c *testing.T) {
  39. testRequires(c, DaemonIsLinux)
  40. out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1").Stdout()
  41. out = strings.TrimSpace(out)
  42. // make sure the container has exited before trying the "start -a"
  43. cli.DockerCmd(c, "wait", out)
  44. cli.Docker(cli.Args("start", "-a", out)).Assert(c, icmd.Expected{
  45. ExitCode: 1,
  46. })
  47. }
  48. func (s *DockerSuite) TestStartAttachSilent(c *testing.T) {
  49. name := "teststartattachcorrectexitcode"
  50. dockerCmd(c, "run", "--name", name, "busybox", "echo", "test")
  51. // make sure the container has exited before trying the "start -a"
  52. dockerCmd(c, "wait", name)
  53. startOut, _ := dockerCmd(c, "start", "-a", name)
  54. // start -a produced unexpected output
  55. assert.Equal(c, startOut, "test\n")
  56. }
  57. func (s *DockerSuite) TestStartRecordError(c *testing.T) {
  58. // TODO Windows CI: Requires further porting work. Should be possible.
  59. testRequires(c, DaemonIsLinux)
  60. // when container runs successfully, we should not have state.Error
  61. dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
  62. stateErr := inspectField(c, "test", "State.Error")
  63. // Expected to not have state error
  64. assert.Equal(c, stateErr, "")
  65. // Expect this to fail and records error because of ports conflict
  66. out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
  67. // err shouldn't be nil because docker run will fail
  68. assert.Assert(c, err != nil, fmt.Sprintf("out: %s", out))
  69. stateErr = inspectField(c, "test2", "State.Error")
  70. assert.Assert(c, strings.Contains(stateErr, "port is already allocated"))
  71. // Expect the conflict to be resolved when we stop the initial container
  72. dockerCmd(c, "stop", "test")
  73. dockerCmd(c, "start", "test2")
  74. stateErr = inspectField(c, "test2", "State.Error")
  75. // Expected to not have state error but got one
  76. assert.Equal(c, stateErr, "")
  77. }
  78. func (s *DockerSuite) TestStartPausedContainer(c *testing.T) {
  79. // Windows does not support pausing containers
  80. testRequires(c, IsPausable)
  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. assert.Assert(c, err != nil, fmt.Sprintf("out: %s", out))
  86. // an error should have been shown that you cannot start paused container
  87. assert.Assert(c, strings.Contains(strings.ToLower(out), "cannot start a paused container, try unpause instead"))
  88. }
  89. func (s *DockerSuite) TestStartMultipleContainers(c *testing.T) {
  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. assert.Equal(c, out, "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. assert.Assert(c, err != nil, fmt.Sprintf("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. assert.Equal(c, out, expected)
  117. }
  118. }
  119. func (s *DockerSuite) TestStartAttachMultipleContainers(c *testing.T) {
  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. assert.Assert(c, err != nil, fmt.Sprintf("out: %s", out))
  133. // output does not correspond to what was expected
  134. assert.Assert(c, strings.Contains(out, "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. assert.Equal(c, out, expected)
  141. }
  142. }
  143. // Test case for #23716
  144. func (s *DockerSuite) TestStartAttachWithRename(c *testing.T) {
  145. testRequires(c, DaemonIsLinux)
  146. cli.DockerCmd(c, "create", "-t", "--name", "before", "busybox")
  147. go func() {
  148. cli.WaitRun(c, "before")
  149. cli.DockerCmd(c, "rename", "before", "after")
  150. cli.DockerCmd(c, "stop", "--time=2", "after")
  151. }()
  152. // FIXME(vdemeester) the intent is not clear and potentially racey
  153. result := cli.Docker(cli.Args("start", "-a", "before")).Assert(c, icmd.Expected{
  154. ExitCode: 137,
  155. })
  156. assert.Assert(c, !strings.Contains(result.Stderr(), "No such container"))
  157. }
  158. func (s *DockerSuite) TestStartReturnCorrectExitCode(c *testing.T) {
  159. dockerCmd(c, "create", "--restart=on-failure:2", "--name", "withRestart", "busybox", "sh", "-c", "exit 11")
  160. dockerCmd(c, "create", "--rm", "--name", "withRm", "busybox", "sh", "-c", "exit 12")
  161. out, exitCode, err := dockerCmdWithError("start", "-a", "withRestart")
  162. assert.ErrorContains(c, err, "")
  163. assert.Equal(c, exitCode, 11, fmt.Sprintf("out: %s", out))
  164. out, exitCode, err = dockerCmdWithError("start", "-a", "withRm")
  165. assert.ErrorContains(c, err, "")
  166. assert.Equal(c, exitCode, 12, fmt.Sprintf("out: %s", out))
  167. }