docker_cli_start_test.go 7.7 KB

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