docker_cli_start_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. // Regression test for https://github.com/docker/docker/issues/7843
  10. func TestStartAttachReturnsOnError(t *testing.T) {
  11. defer deleteAllContainers()
  12. dockerCmd(t, "run", "-d", "--name", "test", "busybox")
  13. dockerCmd(t, "wait", "test")
  14. // Expect this to fail because the above container is stopped, this is what we want
  15. if _, err := runCommand(exec.Command(dockerBinary, "run", "-d", "--name", "test2", "--link", "test:test", "busybox")); err == nil {
  16. t.Fatal("Expected error but got none")
  17. }
  18. ch := make(chan struct{})
  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 := runCommand(exec.Command(dockerBinary, "start", "-a", "test2")); err == nil {
  23. t.Fatal("Expected error but got none")
  24. }
  25. close(ch)
  26. }()
  27. select {
  28. case <-ch:
  29. case <-time.After(time.Second):
  30. t.Fatalf("Attach did not exit properly")
  31. }
  32. logDone("start - error on start with attach exits")
  33. }
  34. // gh#8555: Exit code should be passed through when using start -a
  35. func TestStartAttachCorrectExitCode(t *testing.T) {
  36. defer deleteAllContainers()
  37. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
  38. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  39. if err != nil {
  40. t.Fatalf("failed to run container: %v, output: %q", err, out)
  41. }
  42. out = strings.TrimSpace(out)
  43. // make sure the container has exited before trying the "start -a"
  44. waitCmd := exec.Command(dockerBinary, "wait", out)
  45. if _, _, err = runCommandWithOutput(waitCmd); err != nil {
  46. t.Fatalf("Failed to wait on container: %v", err)
  47. }
  48. startCmd := exec.Command(dockerBinary, "start", "-a", out)
  49. startOut, exitCode, err := runCommandWithOutput(startCmd)
  50. if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
  51. t.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
  52. }
  53. if exitCode != 1 {
  54. t.Fatalf("start -a did not respond with proper exit code: expected 1, got %d", exitCode)
  55. }
  56. logDone("start - correct exit code returned with -a")
  57. }
  58. func TestStartSilentAttach(t *testing.T) {
  59. defer deleteAllContainers()
  60. name := "teststartattachcorrectexitcode"
  61. runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "echo", "test")
  62. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  63. if err != nil {
  64. t.Fatalf("failed to run container: %v, output: %q", err, out)
  65. }
  66. // make sure the container has exited before trying the "start -a"
  67. waitCmd := exec.Command(dockerBinary, "wait", name)
  68. if _, _, err = runCommandWithOutput(waitCmd); err != nil {
  69. t.Fatalf("wait command failed with error: %v", err)
  70. }
  71. startCmd := exec.Command(dockerBinary, "start", "-a", name)
  72. startOut, _, err := runCommandWithOutput(startCmd)
  73. if err != nil {
  74. t.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
  75. }
  76. if expected := "test\n"; startOut != expected {
  77. t.Fatalf("start -a produced unexpected output: expected %q, got %q", expected, startOut)
  78. }
  79. logDone("start - don't echo container ID when attaching")
  80. }
  81. func TestStartRecordError(t *testing.T) {
  82. defer deleteAllContainers()
  83. // when container runs successfully, we should not have state.Error
  84. dockerCmd(t, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
  85. stateErr, err := inspectField("test", "State.Error")
  86. if err != nil {
  87. t.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
  88. }
  89. if stateErr != "" {
  90. t.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
  91. }
  92. // Expect this to fail and records error because of ports conflict
  93. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top"))
  94. if err == nil {
  95. t.Fatalf("Expected error but got none, output %q", out)
  96. }
  97. stateErr, err = inspectField("test2", "State.Error")
  98. if err != nil {
  99. t.Fatalf("Failed to inspect %q state's error, got error %q", "test2", err)
  100. }
  101. expected := "port is already allocated"
  102. if stateErr == "" || !strings.Contains(stateErr, expected) {
  103. t.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
  104. }
  105. // Expect the conflict to be resolved when we stop the initial container
  106. dockerCmd(t, "stop", "test")
  107. dockerCmd(t, "start", "test2")
  108. stateErr, err = inspectField("test2", "State.Error")
  109. if err != nil {
  110. t.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
  111. }
  112. if stateErr != "" {
  113. t.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
  114. }
  115. logDone("start - set state error when start is unsuccessful")
  116. }
  117. // gh#8726: a failed Start() breaks --volumes-from on subsequent Start()'s
  118. func TestStartVolumesFromFailsCleanly(t *testing.T) {
  119. defer deleteAllContainers()
  120. // Create the first data volume
  121. dockerCmd(t, "run", "-d", "--name", "data_before", "-v", "/foo", "busybox")
  122. // Expect this to fail because the data test after contaienr doesn't exist yet
  123. if _, err := runCommand(exec.Command(dockerBinary, "run", "-d", "--name", "consumer", "--volumes-from", "data_before", "--volumes-from", "data_after", "busybox")); err == nil {
  124. t.Fatal("Expected error but got none")
  125. }
  126. // Create the second data volume
  127. dockerCmd(t, "run", "-d", "--name", "data_after", "-v", "/bar", "busybox")
  128. // Now, all the volumes should be there
  129. dockerCmd(t, "start", "consumer")
  130. // Check that we have the volumes we want
  131. out, _, _ := dockerCmd(t, "inspect", "--format='{{ len .Volumes }}'", "consumer")
  132. nVolumes := strings.Trim(out, " \r\n'")
  133. if nVolumes != "2" {
  134. t.Fatalf("Missing volumes: expected 2, got %s", nVolumes)
  135. }
  136. logDone("start - missing containers in --volumes-from did not affect subsequent runs")
  137. }
  138. func TestStartPausedContainer(t *testing.T) {
  139. defer deleteAllContainers()
  140. defer unpauseAllContainers()
  141. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
  142. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  143. t.Fatal(out, err)
  144. }
  145. runCmd = exec.Command(dockerBinary, "pause", "testing")
  146. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  147. t.Fatal(out, err)
  148. }
  149. runCmd = exec.Command(dockerBinary, "start", "testing")
  150. if out, _, err := runCommandWithOutput(runCmd); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") {
  151. t.Fatalf("an error should have been shown that you cannot start paused container: %s\n%v", out, err)
  152. }
  153. logDone("start - error should show if trying to start paused container")
  154. }
  155. func TestStartMultipleContainers(t *testing.T) {
  156. defer deleteAllContainers()
  157. // run a container named 'parent' and create two container link to `parent`
  158. cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
  159. if out, _, err := runCommandWithOutput(cmd); err != nil {
  160. t.Fatal(out, err)
  161. }
  162. for _, container := range []string{"child_first", "child_second"} {
  163. cmd = exec.Command(dockerBinary, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
  164. if out, _, err := runCommandWithOutput(cmd); err != nil {
  165. t.Fatal(out, err)
  166. }
  167. }
  168. // stop 'parent' container
  169. cmd = exec.Command(dockerBinary, "stop", "parent")
  170. if out, _, err := runCommandWithOutput(cmd); err != nil {
  171. t.Fatal(out, err)
  172. }
  173. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", "parent")
  174. out, _, err := runCommandWithOutput(cmd)
  175. if err != nil {
  176. t.Fatal(out, err)
  177. }
  178. out = strings.Trim(out, "\r\n")
  179. if out != "false" {
  180. t.Fatal("Container should be stopped")
  181. }
  182. // start all the three containers, container `child_first` start first which should be faild
  183. // container 'parent' start second and then start container 'child_second'
  184. cmd = exec.Command(dockerBinary, "start", "child_first", "parent", "child_second")
  185. out, _, err = runCommandWithOutput(cmd)
  186. if !strings.Contains(out, "Cannot start container child_first") || err == nil {
  187. t.Fatal("Expected error but got none")
  188. }
  189. for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
  190. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
  191. out, _, err = runCommandWithOutput(cmd)
  192. if err != nil {
  193. t.Fatal(out, err)
  194. }
  195. out = strings.Trim(out, "\r\n")
  196. if out != expected {
  197. t.Fatal("Container running state wrong")
  198. }
  199. }
  200. logDone("start - start multiple containers continue on one failed")
  201. }
  202. func TestStartAttachMultipleContainers(t *testing.T) {
  203. var cmd *exec.Cmd
  204. defer deleteAllContainers()
  205. // run multiple containers to test
  206. for _, container := range []string{"test1", "test2", "test3"} {
  207. cmd = exec.Command(dockerBinary, "run", "-d", "--name", container, "busybox", "top")
  208. if out, _, err := runCommandWithOutput(cmd); err != nil {
  209. t.Fatal(out, err)
  210. }
  211. }
  212. // stop all the containers
  213. for _, container := range []string{"test1", "test2", "test3"} {
  214. cmd = exec.Command(dockerBinary, "stop", container)
  215. if out, _, err := runCommandWithOutput(cmd); err != nil {
  216. t.Fatal(out, err)
  217. }
  218. }
  219. // test start and attach multiple containers at once, expected error
  220. for _, option := range []string{"-a", "-i", "-ai"} {
  221. cmd = exec.Command(dockerBinary, "start", option, "test1", "test2", "test3")
  222. out, _, err := runCommandWithOutput(cmd)
  223. if !strings.Contains(out, "You cannot start and attach multiple containers at once.") || err == nil {
  224. t.Fatal("Expected error but got none")
  225. }
  226. }
  227. // confirm the state of all the containers be stopped
  228. for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
  229. cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
  230. out, _, err := runCommandWithOutput(cmd)
  231. if err != nil {
  232. t.Fatal(out, err)
  233. }
  234. out = strings.Trim(out, "\r\n")
  235. if out != expected {
  236. t.Fatal("Container running state wrong")
  237. }
  238. }
  239. logDone("start - error on start and attach multiple containers at once")
  240. }