docker_cli_restart_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package main
  2. import (
  3. "os"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/docker/docker/pkg/integration/checker"
  8. "github.com/go-check/check"
  9. )
  10. func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
  11. dockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar")
  12. cleanedContainerID, err := getIDByName("test")
  13. c.Assert(err, check.IsNil)
  14. out, _ := dockerCmd(c, "logs", cleanedContainerID)
  15. c.Assert(out, checker.Equals, "foobar\n")
  16. dockerCmd(c, "restart", cleanedContainerID)
  17. // Wait until the container has stopped
  18. err = waitInspect(cleanedContainerID, "{{.State.Running}}", "false", 20*time.Second)
  19. c.Assert(err, checker.IsNil)
  20. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  21. c.Assert(out, checker.Equals, "foobar\nfoobar\n")
  22. }
  23. func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
  24. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
  25. cleanedContainerID := strings.TrimSpace(out)
  26. c.Assert(waitRun(cleanedContainerID), checker.IsNil)
  27. getLogs := func(c *check.C) (interface{}, check.CommentInterface) {
  28. out, _ := dockerCmd(c, "logs", cleanedContainerID)
  29. return out, nil
  30. }
  31. // Wait 10 seconds for the 'echo' to appear in the logs
  32. waitAndAssert(c, 10*time.Second, getLogs, checker.Equals, "foobar\n")
  33. dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
  34. c.Assert(waitRun(cleanedContainerID), checker.IsNil)
  35. // Wait 10 seconds for first 'echo' appear (again) in the logs
  36. waitAndAssert(c, 10*time.Second, getLogs, checker.Equals, "foobar\nfoobar\n")
  37. }
  38. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  39. func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
  40. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  41. out, _ := runSleepingContainer(c, "-d", "-v", prefix+slash+"test")
  42. cleanedContainerID := strings.TrimSpace(out)
  43. out, err := inspectFilter(cleanedContainerID, "len .Mounts")
  44. c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
  45. out = strings.Trim(out, " \n\r")
  46. c.Assert(out, checker.Equals, "1")
  47. source, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
  48. c.Assert(err, checker.IsNil)
  49. dockerCmd(c, "restart", cleanedContainerID)
  50. out, err = inspectFilter(cleanedContainerID, "len .Mounts")
  51. c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
  52. out = strings.Trim(out, " \n\r")
  53. c.Assert(out, checker.Equals, "1")
  54. sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
  55. c.Assert(err, checker.IsNil)
  56. c.Assert(source, checker.Equals, sourceAfterRestart)
  57. }
  58. func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
  59. out, _ := dockerCmd(c, "create", "--restart=no", "busybox")
  60. id := strings.TrimSpace(string(out))
  61. name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
  62. c.Assert(name, checker.Equals, "no")
  63. }
  64. func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
  65. out, _ := dockerCmd(c, "create", "--restart=always", "busybox")
  66. id := strings.TrimSpace(string(out))
  67. name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
  68. c.Assert(name, checker.Equals, "always")
  69. MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  70. // MaximumRetryCount=0 if the restart policy is always
  71. c.Assert(MaximumRetryCount, checker.Equals, "0")
  72. }
  73. func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
  74. out, _, err := dockerCmdWithError("create", "--restart=on-failure:-1", "busybox")
  75. c.Assert(err, check.NotNil, check.Commentf(out))
  76. c.Assert(out, checker.Contains, "maximum retry count cannot be negative")
  77. out, _ = dockerCmd(c, "create", "--restart=on-failure:1", "busybox")
  78. id := strings.TrimSpace(string(out))
  79. name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
  80. maxRetry := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  81. c.Assert(name, checker.Equals, "on-failure")
  82. c.Assert(maxRetry, checker.Equals, "1")
  83. out, _ = dockerCmd(c, "create", "--restart=on-failure:0", "busybox")
  84. id = strings.TrimSpace(string(out))
  85. name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
  86. maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  87. c.Assert(name, checker.Equals, "on-failure")
  88. c.Assert(maxRetry, checker.Equals, "0")
  89. out, _ = dockerCmd(c, "create", "--restart=on-failure", "busybox")
  90. id = strings.TrimSpace(string(out))
  91. name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
  92. maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  93. c.Assert(name, checker.Equals, "on-failure")
  94. c.Assert(maxRetry, checker.Equals, "0")
  95. }
  96. // a good container with --restart=on-failure:3
  97. // MaximumRetryCount!=0; RestartCount=0
  98. func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *check.C) {
  99. out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
  100. id := strings.TrimSpace(string(out))
  101. err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second)
  102. c.Assert(err, checker.IsNil)
  103. count := inspectField(c, id, "RestartCount")
  104. c.Assert(count, checker.Equals, "0")
  105. MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  106. c.Assert(MaximumRetryCount, checker.Equals, "3")
  107. }
  108. func (s *DockerSuite) TestRestartContainerSuccess(c *check.C) {
  109. testRequires(c, SameHostDaemon)
  110. out, _ := runSleepingContainer(c, "-d", "--restart=always")
  111. id := strings.TrimSpace(out)
  112. c.Assert(waitRun(id), check.IsNil)
  113. pidStr := inspectField(c, id, "State.Pid")
  114. pid, err := strconv.Atoi(pidStr)
  115. c.Assert(err, check.IsNil)
  116. p, err := os.FindProcess(pid)
  117. c.Assert(err, check.IsNil)
  118. c.Assert(p, check.NotNil)
  119. err = p.Kill()
  120. c.Assert(err, check.IsNil)
  121. err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
  122. c.Assert(err, check.IsNil)
  123. err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
  124. c.Assert(err, check.IsNil)
  125. }
  126. func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) {
  127. // TODO Windows. This may be portable following HNS integration post TP5.
  128. testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
  129. dockerCmd(c, "network", "create", "-d", "bridge", "udNet")
  130. dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
  131. c.Assert(waitRun("first"), check.IsNil)
  132. dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
  133. "--link=first:foo", "busybox", "top")
  134. c.Assert(waitRun("second"), check.IsNil)
  135. // ping to first and its alias foo must succeed
  136. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  137. c.Assert(err, check.IsNil)
  138. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  139. c.Assert(err, check.IsNil)
  140. // Now kill the second container and let the restart policy kick in
  141. pidStr := inspectField(c, "second", "State.Pid")
  142. pid, err := strconv.Atoi(pidStr)
  143. c.Assert(err, check.IsNil)
  144. p, err := os.FindProcess(pid)
  145. c.Assert(err, check.IsNil)
  146. c.Assert(p, check.NotNil)
  147. err = p.Kill()
  148. c.Assert(err, check.IsNil)
  149. err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second)
  150. c.Assert(err, check.IsNil)
  151. err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second)
  152. // ping to first and its alias foo must still succeed
  153. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  154. c.Assert(err, check.IsNil)
  155. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  156. c.Assert(err, check.IsNil)
  157. }
  158. func (s *DockerSuite) TestRestartPolicyAfterRestart(c *check.C) {
  159. testRequires(c, SameHostDaemon)
  160. out, _ := runSleepingContainer(c, "-d", "--restart=always")
  161. id := strings.TrimSpace(out)
  162. c.Assert(waitRun(id), check.IsNil)
  163. dockerCmd(c, "restart", id)
  164. c.Assert(waitRun(id), check.IsNil)
  165. pidStr := inspectField(c, id, "State.Pid")
  166. pid, err := strconv.Atoi(pidStr)
  167. c.Assert(err, check.IsNil)
  168. p, err := os.FindProcess(pid)
  169. c.Assert(err, check.IsNil)
  170. c.Assert(p, check.NotNil)
  171. err = p.Kill()
  172. c.Assert(err, check.IsNil)
  173. err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
  174. c.Assert(err, check.IsNil)
  175. err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
  176. c.Assert(err, check.IsNil)
  177. }
  178. func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *check.C) {
  179. out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false")
  180. out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
  181. id1 := strings.TrimSpace(string(out1))
  182. id2 := strings.TrimSpace(string(out2))
  183. waitTimeout := 15 * time.Second
  184. if daemonPlatform == "windows" {
  185. waitTimeout = 150 * time.Second
  186. }
  187. err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
  188. c.Assert(err, checker.IsNil)
  189. dockerCmd(c, "restart", id1)
  190. dockerCmd(c, "restart", id2)
  191. // Make sure we can stop/start (regression test from a705e166cf3bcca62543150c2b3f9bfeae45ecfa)
  192. dockerCmd(c, "stop", id1)
  193. dockerCmd(c, "stop", id2)
  194. dockerCmd(c, "start", id1)
  195. dockerCmd(c, "start", id2)
  196. // Kill the containers, making sure the are stopped at the end of the test
  197. dockerCmd(c, "kill", id1)
  198. dockerCmd(c, "kill", id2)
  199. err = waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
  200. c.Assert(err, checker.IsNil)
  201. err = waitInspect(id2, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
  202. c.Assert(err, checker.IsNil)
  203. }
  204. func (s *DockerSuite) TestRestartAutoRemoveContainer(c *check.C) {
  205. out, _ := runSleepingContainer(c, "--rm")
  206. id := strings.TrimSpace(string(out))
  207. dockerCmd(c, "restart", id)
  208. err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second)
  209. c.Assert(err, checker.IsNil)
  210. out, _ = dockerCmd(c, "ps")
  211. c.Assert(out, checker.Contains, id[:12], check.Commentf("container should be restarted instead of removed: %v", out))
  212. // Kill the container to make sure it will be removed
  213. dockerCmd(c, "kill", id)
  214. }