docker_cli_restart_test.go 3.8 KB

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