docker_cli_restart_test.go 9.8 KB

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