docker_cli_restart_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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) TestRestartDisconnectedContainer(c *check.C) {
  58. testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
  59. // Run a container on the default bridge network
  60. out, _ := dockerCmd(c, "run", "-d", "--name", "c0", "busybox", "top")
  61. cleanedContainerID := strings.TrimSpace(out)
  62. c.Assert(waitRun(cleanedContainerID), checker.IsNil)
  63. // Disconnect the container from the network
  64. out, err := dockerCmd(c, "network", "disconnect", "bridge", "c0")
  65. c.Assert(err, check.NotNil, check.Commentf(out))
  66. // Restart the container
  67. dockerCmd(c, "restart", "c0")
  68. c.Assert(err, check.NotNil, check.Commentf(out))
  69. }
  70. func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
  71. out, _ := dockerCmd(c, "create", "--restart=no", "busybox")
  72. id := strings.TrimSpace(string(out))
  73. name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
  74. c.Assert(name, checker.Equals, "no")
  75. }
  76. func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
  77. out, _ := dockerCmd(c, "create", "--restart=always", "busybox")
  78. id := strings.TrimSpace(string(out))
  79. name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
  80. c.Assert(name, checker.Equals, "always")
  81. MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  82. // MaximumRetryCount=0 if the restart policy is always
  83. c.Assert(MaximumRetryCount, checker.Equals, "0")
  84. }
  85. func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
  86. out, _, err := dockerCmdWithError("create", "--restart=on-failure:-1", "busybox")
  87. c.Assert(err, check.NotNil, check.Commentf(out))
  88. c.Assert(out, checker.Contains, "maximum retry count cannot be negative")
  89. out, _ = dockerCmd(c, "create", "--restart=on-failure:1", "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, "1")
  95. out, _ = dockerCmd(c, "create", "--restart=on-failure:0", "busybox")
  96. id = strings.TrimSpace(string(out))
  97. name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
  98. maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  99. c.Assert(name, checker.Equals, "on-failure")
  100. c.Assert(maxRetry, checker.Equals, "0")
  101. out, _ = dockerCmd(c, "create", "--restart=on-failure", "busybox")
  102. id = strings.TrimSpace(string(out))
  103. name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
  104. maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  105. c.Assert(name, checker.Equals, "on-failure")
  106. c.Assert(maxRetry, checker.Equals, "0")
  107. }
  108. // a good container with --restart=on-failure:3
  109. // MaximumRetryCount!=0; RestartCount=0
  110. func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *check.C) {
  111. out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
  112. id := strings.TrimSpace(string(out))
  113. err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second)
  114. c.Assert(err, checker.IsNil)
  115. count := inspectField(c, id, "RestartCount")
  116. c.Assert(count, checker.Equals, "0")
  117. MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  118. c.Assert(MaximumRetryCount, checker.Equals, "3")
  119. }
  120. func (s *DockerSuite) TestRestartContainerSuccess(c *check.C) {
  121. testRequires(c, SameHostDaemon)
  122. out := runSleepingContainer(c, "-d", "--restart=always")
  123. id := strings.TrimSpace(out)
  124. c.Assert(waitRun(id), check.IsNil)
  125. pidStr := inspectField(c, id, "State.Pid")
  126. pid, err := strconv.Atoi(pidStr)
  127. c.Assert(err, check.IsNil)
  128. p, err := os.FindProcess(pid)
  129. c.Assert(err, check.IsNil)
  130. c.Assert(p, check.NotNil)
  131. err = p.Kill()
  132. c.Assert(err, check.IsNil)
  133. err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
  134. c.Assert(err, check.IsNil)
  135. err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
  136. c.Assert(err, check.IsNil)
  137. }
  138. func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) {
  139. // TODO Windows. This may be portable following HNS integration post TP5.
  140. testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
  141. dockerCmd(c, "network", "create", "-d", "bridge", "udNet")
  142. dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
  143. c.Assert(waitRun("first"), check.IsNil)
  144. dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
  145. "--link=first:foo", "busybox", "top")
  146. c.Assert(waitRun("second"), check.IsNil)
  147. // ping to first and its alias foo must succeed
  148. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  149. c.Assert(err, check.IsNil)
  150. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  151. c.Assert(err, check.IsNil)
  152. // Now kill the second container and let the restart policy kick in
  153. pidStr := inspectField(c, "second", "State.Pid")
  154. pid, err := strconv.Atoi(pidStr)
  155. c.Assert(err, check.IsNil)
  156. p, err := os.FindProcess(pid)
  157. c.Assert(err, check.IsNil)
  158. c.Assert(p, check.NotNil)
  159. err = p.Kill()
  160. c.Assert(err, check.IsNil)
  161. err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second)
  162. c.Assert(err, check.IsNil)
  163. err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second)
  164. // ping to first and its alias foo must still succeed
  165. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  166. c.Assert(err, check.IsNil)
  167. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  168. c.Assert(err, check.IsNil)
  169. }
  170. func (s *DockerSuite) TestRestartPolicyAfterRestart(c *check.C) {
  171. testRequires(c, SameHostDaemon)
  172. out := runSleepingContainer(c, "-d", "--restart=always")
  173. id := strings.TrimSpace(out)
  174. c.Assert(waitRun(id), check.IsNil)
  175. dockerCmd(c, "restart", id)
  176. c.Assert(waitRun(id), check.IsNil)
  177. pidStr := inspectField(c, id, "State.Pid")
  178. pid, err := strconv.Atoi(pidStr)
  179. c.Assert(err, check.IsNil)
  180. p, err := os.FindProcess(pid)
  181. c.Assert(err, check.IsNil)
  182. c.Assert(p, check.NotNil)
  183. err = p.Kill()
  184. c.Assert(err, check.IsNil)
  185. err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
  186. c.Assert(err, check.IsNil)
  187. err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
  188. c.Assert(err, check.IsNil)
  189. }
  190. func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *check.C) {
  191. out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false")
  192. out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
  193. id1 := strings.TrimSpace(string(out1))
  194. id2 := strings.TrimSpace(string(out2))
  195. waitTimeout := 15 * time.Second
  196. if testEnv.DaemonPlatform() == "windows" {
  197. waitTimeout = 150 * time.Second
  198. }
  199. err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
  200. c.Assert(err, checker.IsNil)
  201. dockerCmd(c, "restart", id1)
  202. dockerCmd(c, "restart", id2)
  203. // Make sure we can stop/start (regression test from a705e166cf3bcca62543150c2b3f9bfeae45ecfa)
  204. dockerCmd(c, "stop", id1)
  205. dockerCmd(c, "stop", id2)
  206. dockerCmd(c, "start", id1)
  207. dockerCmd(c, "start", id2)
  208. // Kill the containers, making sure the are stopped at the end of the test
  209. dockerCmd(c, "kill", id1)
  210. dockerCmd(c, "kill", id2)
  211. err = waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
  212. c.Assert(err, checker.IsNil)
  213. err = waitInspect(id2, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
  214. c.Assert(err, checker.IsNil)
  215. }
  216. func (s *DockerSuite) TestRestartAutoRemoveContainer(c *check.C) {
  217. out := runSleepingContainer(c, "--rm")
  218. id := strings.TrimSpace(string(out))
  219. dockerCmd(c, "restart", id)
  220. err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second)
  221. c.Assert(err, checker.IsNil)
  222. out, _ = dockerCmd(c, "ps")
  223. c.Assert(out, checker.Contains, id[:12], check.Commentf("container should be restarted instead of removed: %v", out))
  224. // Kill the container to make sure it will be removed
  225. dockerCmd(c, "kill", id)
  226. }