docker_cli_start_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "time"
  7. "github.com/docker/docker/pkg/integration/checker"
  8. "github.com/go-check/check"
  9. )
  10. // Regression test for https://github.com/docker/docker/issues/7843
  11. func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
  12. // Windows does not support link
  13. testRequires(c, DaemonIsLinux)
  14. dockerCmd(c, "run", "--name", "test", "busybox")
  15. // Expect this to fail because the above container is stopped, this is what we want
  16. out, _, err := dockerCmdWithError("run", "--name", "test2", "--link", "test:test", "busybox")
  17. // err shouldn't be nil because container test2 try to link to stopped container
  18. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  19. ch := make(chan error)
  20. go func() {
  21. // Attempt to start attached to the container that won't start
  22. // This should return an error immediately since the container can't be started
  23. if out, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
  24. ch <- fmt.Errorf("Expected error but got none:\n%s", out)
  25. }
  26. close(ch)
  27. }()
  28. select {
  29. case err := <-ch:
  30. c.Assert(err, check.IsNil)
  31. case <-time.After(5 * time.Second):
  32. c.Fatalf("Attach did not exit properly")
  33. }
  34. }
  35. // gh#8555: Exit code should be passed through when using start -a
  36. func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
  37. testRequires(c, DaemonIsLinux)
  38. out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
  39. out = strings.TrimSpace(out)
  40. // make sure the container has exited before trying the "start -a"
  41. dockerCmd(c, "wait", out)
  42. startOut, exitCode, err := dockerCmdWithError("start", "-a", out)
  43. // start command should fail
  44. c.Assert(err, checker.NotNil, check.Commentf("startOut: %s", startOut))
  45. // start -a did not respond with proper exit code
  46. c.Assert(exitCode, checker.Equals, 1, check.Commentf("startOut: %s", startOut))
  47. }
  48. func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
  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. c.Assert(startOut, checker.Equals, "test\n")
  56. }
  57. func (s *DockerSuite) TestStartRecordError(c *check.C) {
  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. c.Assert(stateErr, checker.Equals, "")
  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. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  69. stateErr = inspectField(c, "test2", "State.Error")
  70. c.Assert(stateErr, checker.Contains, "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. c.Assert(stateErr, checker.Equals, "")
  77. }
  78. func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
  79. // Windows does not support pausing containers
  80. testRequires(c, DaemonIsLinux)
  81. defer unpauseAllContainers()
  82. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  83. dockerCmd(c, "pause", "testing")
  84. out, _, err := dockerCmdWithError("start", "testing")
  85. // an error should have been shown that you cannot start paused container
  86. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  87. // an error should have been shown that you cannot start paused container
  88. c.Assert(out, checker.Contains, "Cannot start a paused container, try unpause instead.")
  89. }
  90. func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
  91. // Windows does not support --link
  92. testRequires(c, DaemonIsLinux)
  93. // run a container named 'parent' and create two container link to `parent`
  94. dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
  95. for _, container := range []string{"child_first", "child_second"} {
  96. dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
  97. }
  98. // stop 'parent' container
  99. dockerCmd(c, "stop", "parent")
  100. out := inspectField(c, "parent", "State.Running")
  101. // Container should be stopped
  102. c.Assert(out, checker.Equals, "false")
  103. // start all the three containers, container `child_first` start first which should be failed
  104. // container 'parent' start second and then start container 'child_second'
  105. expOut := "Cannot link to a non running container"
  106. expErr := "failed to start containers: [child_first]"
  107. out, _, err := dockerCmdWithError("start", "child_first", "parent", "child_second")
  108. // err shouldn't be nil because start will fail
  109. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  110. // output does not correspond to what was expected
  111. if !(strings.Contains(out, expOut) || strings.Contains(err.Error(), expErr)) {
  112. c.Fatalf("Expected out: %v with err: %v but got out: %v with err: %v", expOut, expErr, out, err)
  113. }
  114. for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
  115. out := inspectField(c, container, "State.Running")
  116. // Container running state wrong
  117. c.Assert(out, checker.Equals, expected)
  118. }
  119. }
  120. func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
  121. // run multiple containers to test
  122. for _, container := range []string{"test1", "test2", "test3"} {
  123. runSleepingContainer(c, "--name", container)
  124. }
  125. // stop all the containers
  126. for _, container := range []string{"test1", "test2", "test3"} {
  127. dockerCmd(c, "stop", container)
  128. }
  129. // test start and attach multiple containers at once, expected error
  130. for _, option := range []string{"-a", "-i", "-ai"} {
  131. out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
  132. // err shouldn't be nil because start will fail
  133. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  134. // output does not correspond to what was expected
  135. c.Assert(out, checker.Contains, "You cannot start and attach multiple containers at once.")
  136. }
  137. // confirm the state of all the containers be stopped
  138. for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
  139. out := inspectField(c, container, "State.Running")
  140. // Container running state wrong
  141. c.Assert(out, checker.Equals, expected)
  142. }
  143. }
  144. // Test case for #23716
  145. func (s *DockerSuite) TestStartAttachWithRename(c *check.C) {
  146. testRequires(c, DaemonIsLinux)
  147. dockerCmd(c, "create", "-t", "--name", "before", "busybox")
  148. go func() {
  149. c.Assert(waitRun("before"), checker.IsNil)
  150. dockerCmd(c, "rename", "before", "after")
  151. dockerCmd(c, "stop", "--time=2", "after")
  152. }()
  153. _, stderr, _, _ := runCommandWithStdoutStderr(exec.Command(dockerBinary, "start", "-a", "before"))
  154. c.Assert(stderr, checker.Not(checker.Contains), "No such container")
  155. }