docker_cli_start_test.go 7.6 KB

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