docker_cli_restart_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. }
  120. func TestRestartPolicyNO(t *testing.T) {
  121. defer deleteAllContainers()
  122. cmd := exec.Command(dockerBinary, "run", "-d", "--restart=no", "busybox", "false")
  123. out, _, err := runCommandWithOutput(cmd)
  124. if err != nil {
  125. t.Fatal(err, out)
  126. }
  127. id := strings.TrimSpace(string(out))
  128. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  129. if err != nil {
  130. t.Fatal(err, out)
  131. }
  132. if name != "no" {
  133. t.Fatalf("Container restart policy name is %s, expected %s", name, "no")
  134. }
  135. logDone("restart - recording restart policy name for --restart=no")
  136. }
  137. func TestRestartPolicyAlways(t *testing.T) {
  138. defer deleteAllContainers()
  139. cmd := exec.Command(dockerBinary, "run", "-d", "--restart=always", "busybox", "false")
  140. out, _, err := runCommandWithOutput(cmd)
  141. if err != nil {
  142. t.Fatal(err, out)
  143. }
  144. id := strings.TrimSpace(string(out))
  145. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  146. if err != nil {
  147. t.Fatal(err, out)
  148. }
  149. if name != "always" {
  150. t.Fatalf("Container restart policy name is %s, expected %s", name, "always")
  151. }
  152. logDone("restart - recording restart policy name for --restart=always")
  153. }
  154. func TestRestartPolicyOnFailure(t *testing.T) {
  155. defer deleteAllContainers()
  156. cmd := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:1", "busybox", "false")
  157. out, _, err := runCommandWithOutput(cmd)
  158. if err != nil {
  159. t.Fatal(err, out)
  160. }
  161. id := strings.TrimSpace(string(out))
  162. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  163. if err != nil {
  164. t.Fatal(err, out)
  165. }
  166. if name != "on-failure" {
  167. t.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure")
  168. }
  169. logDone("restart - recording restart policy name for --restart=on-failure")
  170. }