docker_cli_restart_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. )
  7. func TestDockerRestartStoppedContainer(t *testing.T) {
  8. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
  9. out, _, err := runCommandWithOutput(runCmd)
  10. errorOut(err, t, out)
  11. cleanedContainerID := stripTrailingCharacters(out)
  12. runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
  13. out, _, err = runCommandWithOutput(runCmd)
  14. errorOut(err, t, out)
  15. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  16. out, _, err = runCommandWithOutput(runCmd)
  17. errorOut(err, t, out)
  18. if out != "foobar\n" {
  19. t.Errorf("container should've printed 'foobar'")
  20. }
  21. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  22. out, _, err = runCommandWithOutput(runCmd)
  23. errorOut(err, t, out)
  24. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  25. out, _, err = runCommandWithOutput(runCmd)
  26. errorOut(err, t, out)
  27. if out != "foobar\nfoobar\n" {
  28. t.Errorf("container should've printed 'foobar' twice")
  29. }
  30. deleteAllContainers()
  31. logDone("restart - echo foobar for stopped container")
  32. }
  33. func TestDockerRestartRunningContainer(t *testing.T) {
  34. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
  35. out, _, err := runCommandWithOutput(runCmd)
  36. errorOut(err, t, out)
  37. cleanedContainerID := stripTrailingCharacters(out)
  38. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  39. out, _, err = runCommandWithOutput(runCmd)
  40. errorOut(err, t, out)
  41. if out != "foobar\n" {
  42. t.Errorf("container should've printed 'foobar'")
  43. }
  44. runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID)
  45. out, _, err = runCommandWithOutput(runCmd)
  46. errorOut(err, t, out)
  47. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  48. out, _, err = runCommandWithOutput(runCmd)
  49. errorOut(err, t, out)
  50. if out != "foobar\nfoobar\n" {
  51. t.Errorf("container should've printed 'foobar' twice")
  52. }
  53. deleteAllContainers()
  54. logDone("restart - echo foobar for running container")
  55. }
  56. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  57. func TestDockerRestartWithVolumes(t *testing.T) {
  58. runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
  59. out, _, err := runCommandWithOutput(runCmd)
  60. errorOut(err, t, out)
  61. cleanedContainerID := stripTrailingCharacters(out)
  62. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
  63. out, _, err = runCommandWithOutput(runCmd)
  64. errorOut(err, t, out)
  65. if out = strings.Trim(out, " \n\r"); out != "1" {
  66. t.Errorf("expect 1 volume received %s", out)
  67. }
  68. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
  69. volumes, _, err := runCommandWithOutput(runCmd)
  70. errorOut(err, t, volumes)
  71. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  72. out, _, err = runCommandWithOutput(runCmd)
  73. errorOut(err, t, out)
  74. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
  75. out, _, err = runCommandWithOutput(runCmd)
  76. errorOut(err, t, out)
  77. if out = strings.Trim(out, " \n\r"); out != "1" {
  78. t.Errorf("expect 1 volume after restart received %s", out)
  79. }
  80. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
  81. volumesAfterRestart, _, err := runCommandWithOutput(runCmd)
  82. errorOut(err, t, volumesAfterRestart)
  83. if volumes != volumesAfterRestart {
  84. volumes = strings.Trim(volumes, " \n\r")
  85. volumesAfterRestart = strings.Trim(volumesAfterRestart, " \n\r")
  86. t.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
  87. }
  88. deleteAllContainers()
  89. logDone("restart - does not create a new volume on restart")
  90. }