docker_cli_restart_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "time"
  6. "github.com/go-check/check"
  7. )
  8. func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
  9. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
  10. out, _, err := runCommandWithOutput(runCmd)
  11. if err != nil {
  12. c.Fatal(out, err)
  13. }
  14. cleanedContainerID := strings.TrimSpace(out)
  15. runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
  16. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  17. c.Fatal(out, err)
  18. }
  19. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  20. out, _, err = runCommandWithOutput(runCmd)
  21. if err != nil {
  22. c.Fatal(out, err)
  23. }
  24. if out != "foobar\n" {
  25. c.Errorf("container should've printed 'foobar'")
  26. }
  27. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  28. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  29. c.Fatal(out, err)
  30. }
  31. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  32. out, _, err = runCommandWithOutput(runCmd)
  33. if err != nil {
  34. c.Fatal(out, err)
  35. }
  36. if out != "foobar\nfoobar\n" {
  37. c.Errorf("container should've printed 'foobar' twice")
  38. }
  39. }
  40. func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
  41. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
  42. out, _, err := runCommandWithOutput(runCmd)
  43. if err != nil {
  44. c.Fatal(out, err)
  45. }
  46. cleanedContainerID := strings.TrimSpace(out)
  47. time.Sleep(1 * time.Second)
  48. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  49. out, _, err = runCommandWithOutput(runCmd)
  50. if err != nil {
  51. c.Fatal(out, err)
  52. }
  53. if out != "foobar\n" {
  54. c.Errorf("container should've printed 'foobar'")
  55. }
  56. runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID)
  57. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  58. c.Fatal(out, err)
  59. }
  60. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  61. out, _, err = runCommandWithOutput(runCmd)
  62. if err != nil {
  63. c.Fatal(out, err)
  64. }
  65. time.Sleep(1 * time.Second)
  66. if out != "foobar\nfoobar\n" {
  67. c.Errorf("container should've printed 'foobar' twice")
  68. }
  69. }
  70. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  71. func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
  72. runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
  73. out, _, err := runCommandWithOutput(runCmd)
  74. if err != nil {
  75. c.Fatal(out, err)
  76. }
  77. cleanedContainerID := strings.TrimSpace(out)
  78. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
  79. out, _, err = runCommandWithOutput(runCmd)
  80. if err != nil {
  81. c.Fatal(out, err)
  82. }
  83. if out = strings.Trim(out, " \n\r"); out != "1" {
  84. c.Errorf("expect 1 volume received %s", out)
  85. }
  86. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
  87. volumes, _, err := runCommandWithOutput(runCmd)
  88. if err != nil {
  89. c.Fatal(volumes, err)
  90. }
  91. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  92. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  93. c.Fatal(out, err)
  94. }
  95. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
  96. out, _, err = runCommandWithOutput(runCmd)
  97. if err != nil {
  98. c.Fatal(out, err)
  99. }
  100. if out = strings.Trim(out, " \n\r"); out != "1" {
  101. c.Errorf("expect 1 volume after restart received %s", out)
  102. }
  103. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
  104. volumesAfterRestart, _, err := runCommandWithOutput(runCmd)
  105. if err != nil {
  106. c.Fatal(volumesAfterRestart, err)
  107. }
  108. if volumes != volumesAfterRestart {
  109. volumes = strings.Trim(volumes, " \n\r")
  110. volumesAfterRestart = strings.Trim(volumesAfterRestart, " \n\r")
  111. c.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
  112. }
  113. }
  114. func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
  115. cmd := exec.Command(dockerBinary, "run", "-d", "--restart=no", "busybox", "false")
  116. out, _, err := runCommandWithOutput(cmd)
  117. if err != nil {
  118. c.Fatal(err, out)
  119. }
  120. id := strings.TrimSpace(string(out))
  121. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  122. if err != nil {
  123. c.Fatal(err, out)
  124. }
  125. if name != "no" {
  126. c.Fatalf("Container restart policy name is %s, expected %s", name, "no")
  127. }
  128. }
  129. func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
  130. cmd := exec.Command(dockerBinary, "run", "-d", "--restart=always", "busybox", "false")
  131. out, _, err := runCommandWithOutput(cmd)
  132. if err != nil {
  133. c.Fatal(err, out)
  134. }
  135. id := strings.TrimSpace(string(out))
  136. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  137. if err != nil {
  138. c.Fatal(err, out)
  139. }
  140. if name != "always" {
  141. c.Fatalf("Container restart policy name is %s, expected %s", name, "always")
  142. }
  143. MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
  144. if err != nil {
  145. c.Fatal(err)
  146. }
  147. // MaximumRetryCount=0 if the restart policy is always
  148. if MaximumRetryCount != "0" {
  149. c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0")
  150. }
  151. }
  152. func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
  153. cmd := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:1", "busybox", "false")
  154. out, _, err := runCommandWithOutput(cmd)
  155. if err != nil {
  156. c.Fatal(err, out)
  157. }
  158. id := strings.TrimSpace(string(out))
  159. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  160. if err != nil {
  161. c.Fatal(err, out)
  162. }
  163. if name != "on-failure" {
  164. c.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure")
  165. }
  166. }
  167. // a good container with --restart=on-failure:3
  168. // MaximumRetryCount!=0; RestartCount=0
  169. func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) {
  170. out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "true").CombinedOutput()
  171. if err != nil {
  172. c.Fatal(string(out), err)
  173. }
  174. id := strings.TrimSpace(string(out))
  175. if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil {
  176. c.Fatal(err)
  177. }
  178. count, err := inspectField(id, "RestartCount")
  179. if err != nil {
  180. c.Fatal(err)
  181. }
  182. if count != "0" {
  183. c.Fatalf("Container was restarted %s times, expected %d", count, 0)
  184. }
  185. MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
  186. if err != nil {
  187. c.Fatal(err)
  188. }
  189. if MaximumRetryCount != "3" {
  190. c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
  191. }
  192. }