docker_cli_start_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/docker/docker/pkg/integration/checker"
  7. "github.com/go-check/check"
  8. )
  9. // Regression test for https://github.com/docker/docker/issues/7843
  10. func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
  11. testRequires(c, DaemonIsLinux)
  12. dockerCmd(c, "run", "-d", "--name", "test", "busybox")
  13. dockerCmd(c, "wait", "test")
  14. // Expect this to fail because the above container is stopped, this is what we want
  15. out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "--link", "test:test", "busybox")
  16. // err shouldn't be nil because container test2 try to link to stopped container
  17. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  18. ch := make(chan error)
  19. go func() {
  20. // Attempt to start attached to the container that won't start
  21. // This should return an error immediately since the container can't be started
  22. if _, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
  23. ch <- fmt.Errorf("Expected error but got none")
  24. }
  25. close(ch)
  26. }()
  27. select {
  28. case err := <-ch:
  29. c.Assert(err, check.IsNil)
  30. case <-time.After(time.Second):
  31. c.Fatalf("Attach did not exit properly")
  32. }
  33. }
  34. // gh#8555: Exit code should be passed through when using start -a
  35. func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
  36. testRequires(c, DaemonIsLinux)
  37. out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
  38. out = strings.TrimSpace(out)
  39. // make sure the container has exited before trying the "start -a"
  40. dockerCmd(c, "wait", out)
  41. startOut, exitCode, err := dockerCmdWithError("start", "-a", out)
  42. // start command should fail
  43. c.Assert(err, checker.NotNil, check.Commentf("startOut: %s", startOut))
  44. // start -a did not respond with proper exit code
  45. c.Assert(exitCode, checker.Equals, 1, check.Commentf("startOut: %s", startOut))
  46. }
  47. func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
  48. testRequires(c, DaemonIsLinux)
  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. testRequires(c, DaemonIsLinux)
  59. // when container runs successfully, we should not have state.Error
  60. dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
  61. stateErr, err := inspectField("test", "State.Error")
  62. c.Assert(err, checker.IsNil, check.Commentf("stateErr: %s", stateErr))
  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, err = inspectField("test2", "State.Error")
  70. c.Assert(err, check.IsNil, check.Commentf("stateErr: %s", stateErr))
  71. c.Assert(stateErr, checker.Contains, "port is already allocated")
  72. // Expect the conflict to be resolved when we stop the initial container
  73. dockerCmd(c, "stop", "test")
  74. dockerCmd(c, "start", "test2")
  75. stateErr, err = inspectField("test2", "State.Error")
  76. c.Assert(err, check.IsNil, check.Commentf("stateErr: %s", stateErr))
  77. // Expected to not have state error but got one
  78. c.Assert(stateErr, checker.Equals, "")
  79. }
  80. func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
  81. testRequires(c, DaemonIsLinux)
  82. defer unpauseAllContainers()
  83. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  84. dockerCmd(c, "pause", "testing")
  85. out, _, err := dockerCmdWithError("start", "testing")
  86. // an error should have been shown that you cannot start paused container
  87. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  88. // an error should have been shown that you cannot start paused container
  89. c.Assert(out, checker.Contains, "Cannot start a paused container, try unpause instead.")
  90. }
  91. func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
  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, err := inspectField("parent", "State.Running")
  101. c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
  102. // Container should be stopped
  103. c.Assert(out, checker.Equals, "false")
  104. // start all the three containers, container `child_first` start first which should be failed
  105. // container 'parent' start second and then start container 'child_second'
  106. out, _, err = dockerCmdWithError("start", "child_first", "parent", "child_second")
  107. // err shouldn't be nil because start will fail
  108. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  109. // output does not correspond to what was expected
  110. c.Assert(out, checker.Contains, "Cannot start container child_first")
  111. for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
  112. out, err := inspectField(container, "State.Running")
  113. // Container running state wrong
  114. c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
  115. // Container running state wrong
  116. c.Assert(out, checker.Equals, expected)
  117. }
  118. }
  119. func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
  120. testRequires(c, DaemonIsLinux)
  121. // run multiple containers to test
  122. for _, container := range []string{"test1", "test2", "test3"} {
  123. dockerCmd(c, "run", "-d", "--name", container, "busybox", "top")
  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, err := inspectField(container, "State.Running")
  140. // Container running state wrong
  141. c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
  142. // Container running state wrong
  143. c.Assert(out, checker.Equals, expected)
  144. }
  145. }