docker_cli_restart_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. if err != nil {
  12. t.Fatal(out, err)
  13. }
  14. cleanedContainerID := stripTrailingCharacters(out)
  15. runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
  16. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  17. t.Fatal(out, err)
  18. }
  19. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  20. out, _, err = runCommandWithOutput(runCmd)
  21. if err != nil {
  22. t.Fatal(out, err)
  23. }
  24. if out != "foobar\n" {
  25. t.Errorf("container should've printed 'foobar'")
  26. }
  27. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  28. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  29. t.Fatal(out, err)
  30. }
  31. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  32. out, _, err = runCommandWithOutput(runCmd)
  33. if err != nil {
  34. t.Fatal(out, err)
  35. }
  36. if out != "foobar\nfoobar\n" {
  37. t.Errorf("container should've printed 'foobar' twice")
  38. }
  39. deleteAllContainers()
  40. logDone("restart - echo foobar for stopped container")
  41. }
  42. func TestRestartRunningContainer(t *testing.T) {
  43. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
  44. out, _, err := runCommandWithOutput(runCmd)
  45. if err != nil {
  46. t.Fatal(out, err)
  47. }
  48. cleanedContainerID := stripTrailingCharacters(out)
  49. time.Sleep(1 * time.Second)
  50. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  51. out, _, err = runCommandWithOutput(runCmd)
  52. if err != nil {
  53. t.Fatal(out, err)
  54. }
  55. if out != "foobar\n" {
  56. t.Errorf("container should've printed 'foobar'")
  57. }
  58. runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID)
  59. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  60. t.Fatal(out, err)
  61. }
  62. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  63. out, _, err = runCommandWithOutput(runCmd)
  64. if err != nil {
  65. t.Fatal(out, err)
  66. }
  67. time.Sleep(1 * time.Second)
  68. if out != "foobar\nfoobar\n" {
  69. t.Errorf("container should've printed 'foobar' twice")
  70. }
  71. deleteAllContainers()
  72. logDone("restart - echo foobar for running container")
  73. }
  74. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  75. func TestRestartWithVolumes(t *testing.T) {
  76. runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
  77. out, _, err := runCommandWithOutput(runCmd)
  78. if err != nil {
  79. t.Fatal(out, err)
  80. }
  81. cleanedContainerID := stripTrailingCharacters(out)
  82. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
  83. out, _, err = runCommandWithOutput(runCmd)
  84. if err != nil {
  85. t.Fatal(out, err)
  86. }
  87. if out = strings.Trim(out, " \n\r"); out != "1" {
  88. t.Errorf("expect 1 volume received %s", out)
  89. }
  90. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
  91. volumes, _, err := runCommandWithOutput(runCmd)
  92. if err != nil {
  93. t.Fatal(volumes, err)
  94. }
  95. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  96. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  97. t.Fatal(out, err)
  98. }
  99. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
  100. out, _, err = runCommandWithOutput(runCmd)
  101. if err != nil {
  102. t.Fatal(out, err)
  103. }
  104. if out = strings.Trim(out, " \n\r"); out != "1" {
  105. t.Errorf("expect 1 volume after restart received %s", out)
  106. }
  107. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
  108. volumesAfterRestart, _, err := runCommandWithOutput(runCmd)
  109. if err != nil {
  110. t.Fatal(volumesAfterRestart, err)
  111. }
  112. if volumes != volumesAfterRestart {
  113. volumes = strings.Trim(volumes, " \n\r")
  114. volumesAfterRestart = strings.Trim(volumesAfterRestart, " \n\r")
  115. t.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
  116. }
  117. deleteAllContainers()
  118. logDone("restart - does not create a new volume on restart")
  119. }