docker_cli_start_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. }