docker_cli_restart_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. "time"
  7. )
  8. func TestRestartStoppedContainer(t *testing.T) {
  9. defer deleteAllContainers()
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
  11. out, _, err := runCommandWithOutput(runCmd)
  12. if err != nil {
  13. t.Fatal(out, err)
  14. }
  15. cleanedContainerID := stripTrailingCharacters(out)
  16. runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
  17. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  18. t.Fatal(out, err)
  19. }
  20. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  21. out, _, err = runCommandWithOutput(runCmd)
  22. if err != nil {
  23. t.Fatal(out, err)
  24. }
  25. if out != "foobar\n" {
  26. t.Errorf("container should've printed 'foobar'")
  27. }
  28. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  29. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  30. t.Fatal(out, err)
  31. }
  32. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  33. out, _, err = runCommandWithOutput(runCmd)
  34. if err != nil {
  35. t.Fatal(out, err)
  36. }
  37. if out != "foobar\nfoobar\n" {
  38. t.Errorf("container should've printed 'foobar' twice")
  39. }
  40. logDone("restart - echo foobar for stopped container")
  41. }
  42. func TestRestartRunningContainer(t *testing.T) {
  43. defer deleteAllContainers()
  44. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
  45. out, _, err := runCommandWithOutput(runCmd)
  46. if err != nil {
  47. t.Fatal(out, err)
  48. }
  49. cleanedContainerID := stripTrailingCharacters(out)
  50. time.Sleep(1 * time.Second)
  51. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  52. out, _, err = runCommandWithOutput(runCmd)
  53. if err != nil {
  54. t.Fatal(out, err)
  55. }
  56. if out != "foobar\n" {
  57. t.Errorf("container should've printed 'foobar'")
  58. }
  59. runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID)
  60. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  61. t.Fatal(out, err)
  62. }
  63. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  64. out, _, err = runCommandWithOutput(runCmd)
  65. if err != nil {
  66. t.Fatal(out, err)
  67. }
  68. time.Sleep(1 * time.Second)
  69. if out != "foobar\nfoobar\n" {
  70. t.Errorf("container should've printed 'foobar' twice")
  71. }
  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. defer deleteAllContainers()
  77. runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
  78. out, _, err := runCommandWithOutput(runCmd)
  79. if err != nil {
  80. t.Fatal(out, err)
  81. }
  82. cleanedContainerID := stripTrailingCharacters(out)
  83. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
  84. out, _, err = runCommandWithOutput(runCmd)
  85. if err != nil {
  86. t.Fatal(out, err)
  87. }
  88. if out = strings.Trim(out, " \n\r"); out != "1" {
  89. t.Errorf("expect 1 volume received %s", out)
  90. }
  91. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
  92. volumes, _, err := runCommandWithOutput(runCmd)
  93. if err != nil {
  94. t.Fatal(volumes, err)
  95. }
  96. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  97. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  98. t.Fatal(out, err)
  99. }
  100. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
  101. out, _, err = runCommandWithOutput(runCmd)
  102. if err != nil {
  103. t.Fatal(out, err)
  104. }
  105. if out = strings.Trim(out, " \n\r"); out != "1" {
  106. t.Errorf("expect 1 volume after restart received %s", out)
  107. }
  108. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
  109. volumesAfterRestart, _, err := runCommandWithOutput(runCmd)
  110. if err != nil {
  111. t.Fatal(volumesAfterRestart, err)
  112. }
  113. if volumes != volumesAfterRestart {
  114. volumes = strings.Trim(volumes, " \n\r")
  115. volumesAfterRestart = strings.Trim(volumesAfterRestart, " \n\r")
  116. t.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
  117. }
  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. MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. // MaximumRetryCount=0 if the restart policy is always
  157. if MaximumRetryCount != "0" {
  158. t.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0")
  159. }
  160. logDone("restart - recording restart policy name for --restart=always")
  161. }
  162. func TestRestartPolicyOnFailure(t *testing.T) {
  163. defer deleteAllContainers()
  164. cmd := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:1", "busybox", "false")
  165. out, _, err := runCommandWithOutput(cmd)
  166. if err != nil {
  167. t.Fatal(err, out)
  168. }
  169. id := strings.TrimSpace(string(out))
  170. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  171. if err != nil {
  172. t.Fatal(err, out)
  173. }
  174. if name != "on-failure" {
  175. t.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure")
  176. }
  177. logDone("restart - recording restart policy name for --restart=on-failure")
  178. }
  179. // a good container with --restart=on-failure:3
  180. // MaximumRetryCount!=0; RestartCount=0
  181. func TestContainerRestartwithGoodContainer(t *testing.T) {
  182. defer deleteAllContainers()
  183. out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "true").CombinedOutput()
  184. if err != nil {
  185. t.Fatal(string(out), err)
  186. }
  187. id := strings.TrimSpace(string(out))
  188. if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil {
  189. t.Fatal(err)
  190. }
  191. count, err := inspectField(id, "RestartCount")
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. if count != "0" {
  196. t.Fatalf("Container was restarted %s times, expected %d", count, 0)
  197. }
  198. MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. if MaximumRetryCount != "3" {
  203. t.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
  204. }
  205. logDone("restart - for a good container with restart policy, MaximumRetryCount is not 0 and RestartCount is 0")
  206. }