docker_cli_start_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/go-check/check"
  7. )
  8. // Regression test for https://github.com/docker/docker/issues/7843
  9. func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
  10. testRequires(c, DaemonIsLinux)
  11. dockerCmd(c, "run", "-d", "--name", "test", "busybox")
  12. dockerCmd(c, "wait", "test")
  13. // Expect this to fail because the above container is stopped, this is what we want
  14. if _, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "--link", "test:test", "busybox"); err == nil {
  15. c.Fatal("Expected error but got none")
  16. }
  17. ch := make(chan error)
  18. go func() {
  19. // Attempt to start attached to the container that won't start
  20. // This should return an error immediately since the container can't be started
  21. if _, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
  22. ch <- fmt.Errorf("Expected error but got none")
  23. }
  24. close(ch)
  25. }()
  26. select {
  27. case err := <-ch:
  28. c.Assert(err, check.IsNil)
  29. case <-time.After(time.Second):
  30. c.Fatalf("Attach did not exit properly")
  31. }
  32. }
  33. // gh#8555: Exit code should be passed through when using start -a
  34. func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
  35. testRequires(c, DaemonIsLinux)
  36. out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
  37. out = strings.TrimSpace(out)
  38. // make sure the container has exited before trying the "start -a"
  39. dockerCmd(c, "wait", out)
  40. startOut, exitCode, err := dockerCmdWithError("start", "-a", out)
  41. if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
  42. c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
  43. }
  44. if exitCode != 1 {
  45. c.Fatalf("start -a did not respond with proper exit code: expected 1, got %d", exitCode)
  46. }
  47. }
  48. func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
  49. testRequires(c, DaemonIsLinux)
  50. name := "teststartattachcorrectexitcode"
  51. dockerCmd(c, "run", "--name", name, "busybox", "echo", "test")
  52. // make sure the container has exited before trying the "start -a"
  53. dockerCmd(c, "wait", name)
  54. startOut, _ := dockerCmd(c, "start", "-a", name)
  55. if expected := "test\n"; startOut != expected {
  56. c.Fatalf("start -a produced unexpected output: expected %q, got %q", expected, startOut)
  57. }
  58. }
  59. func (s *DockerSuite) TestStartRecordError(c *check.C) {
  60. testRequires(c, DaemonIsLinux)
  61. // when container runs successfully, we should not have state.Error
  62. dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
  63. stateErr, err := inspectField("test", "State.Error")
  64. c.Assert(err, check.IsNil)
  65. if stateErr != "" {
  66. c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
  67. }
  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. if err == nil {
  71. c.Fatalf("Expected error but got none, output %q", out)
  72. }
  73. stateErr, err = inspectField("test2", "State.Error")
  74. c.Assert(err, check.IsNil)
  75. expected := "port is already allocated"
  76. if stateErr == "" || !strings.Contains(stateErr, expected) {
  77. c.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
  78. }
  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, err = inspectField("test2", "State.Error")
  83. c.Assert(err, check.IsNil)
  84. if stateErr != "" {
  85. c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
  86. }
  87. }
  88. func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
  89. testRequires(c, DaemonIsLinux)
  90. defer unpauseAllContainers()
  91. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  92. dockerCmd(c, "pause", "testing")
  93. if out, _, err := dockerCmdWithError("start", "testing"); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") {
  94. c.Fatalf("an error should have been shown that you cannot start paused container: %s\n%v", out, err)
  95. }
  96. }
  97. func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
  98. testRequires(c, DaemonIsLinux)
  99. // run a container named 'parent' and create two container link to `parent`
  100. dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
  101. for _, container := range []string{"child_first", "child_second"} {
  102. dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
  103. }
  104. // stop 'parent' container
  105. dockerCmd(c, "stop", "parent")
  106. out, err := inspectField("parent", "State.Running")
  107. c.Assert(err, check.IsNil)
  108. if out != "false" {
  109. c.Fatal("Container should be stopped")
  110. }
  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. out, _, err = dockerCmdWithError("start", "child_first", "parent", "child_second")
  114. if !strings.Contains(out, "Cannot start container child_first") || err == nil {
  115. c.Fatal("Expected error but got none")
  116. }
  117. for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
  118. out, err := inspectField(container, "State.Running")
  119. c.Assert(err, check.IsNil)
  120. if out != expected {
  121. c.Fatal("Container running state wrong")
  122. }
  123. }
  124. }
  125. func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
  126. testRequires(c, DaemonIsLinux)
  127. // run multiple containers to test
  128. for _, container := range []string{"test1", "test2", "test3"} {
  129. dockerCmd(c, "run", "-d", "--name", container, "busybox", "top")
  130. }
  131. // stop all the containers
  132. for _, container := range []string{"test1", "test2", "test3"} {
  133. dockerCmd(c, "stop", container)
  134. }
  135. // test start and attach multiple containers at once, expected error
  136. for _, option := range []string{"-a", "-i", "-ai"} {
  137. out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
  138. if !strings.Contains(out, "You cannot start and attach multiple containers at once.") || err == nil {
  139. c.Fatal("Expected error but got none")
  140. }
  141. }
  142. // confirm the state of all the containers be stopped
  143. for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
  144. out, err := inspectField(container, "State.Running")
  145. if err != nil {
  146. c.Fatal(out, err)
  147. }
  148. if out != expected {
  149. c.Fatal("Container running state wrong")
  150. }
  151. }
  152. }