docker_cli_start_test.go 6.1 KB

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