docker_cli_start_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. cmd(t, "run", "-d", "--name", "test", "busybox")
  13. cmd(t, "stop", "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 = stripTrailingCharacters(out)
  43. // make sure the container has exited before trying the "start -a"
  44. waitCmd := exec.Command(dockerBinary, "wait", out)
  45. if out, _, err = runCommandWithOutput(waitCmd); err != nil {
  46. t.Fatal(out, 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 TestStartRecordError(t *testing.T) {
  59. defer deleteAllContainers()
  60. // when container runs successfully, we should not have state.Error
  61. cmd(t, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
  62. stateErr, err := inspectField("test", "State.Error")
  63. if err != nil {
  64. t.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
  65. }
  66. if stateErr != "" {
  67. t.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
  68. }
  69. // Expect this to fail and records error because of ports conflict
  70. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top"))
  71. if err == nil {
  72. t.Fatalf("Expected error but got none, output %q", out)
  73. }
  74. stateErr, err = inspectField("test2", "State.Error")
  75. if err != nil {
  76. t.Fatalf("Failed to inspect %q state's error, got error %q", "test2", err)
  77. }
  78. expected := "port is already allocated"
  79. if stateErr == "" || !strings.Contains(stateErr, expected) {
  80. t.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
  81. }
  82. // Expect the conflict to be resolved when we stop the initial container
  83. cmd(t, "stop", "test")
  84. cmd(t, "start", "test2")
  85. stateErr, err = inspectField("test2", "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. logDone("start - set state error when start fails")
  93. }