docker_cli_restart_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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, testEnv.IsLocalDaemon, 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("%s", out))
  66. // Restart the container
  67. dockerCmd(c, "restart", "c0")
  68. c.Assert(err, check.NotNil, check.Commentf("%s", 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("%s", 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. // Skipped for Hyper-V isolated containers. Test is currently written
  122. // such that it assumes there is a host process to kill. In Hyper-V
  123. // containers, the process is inside the utility VM, not on the host.
  124. testRequires(c, testEnv.IsLocalDaemon, IsolationIsProcess)
  125. out := runSleepingContainer(c, "-d", "--restart=always")
  126. id := strings.TrimSpace(out)
  127. c.Assert(waitRun(id), check.IsNil)
  128. pidStr := inspectField(c, id, "State.Pid")
  129. pid, err := strconv.Atoi(pidStr)
  130. c.Assert(err, check.IsNil)
  131. p, err := os.FindProcess(pid)
  132. c.Assert(err, check.IsNil)
  133. c.Assert(p, check.NotNil)
  134. err = p.Kill()
  135. c.Assert(err, check.IsNil)
  136. err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
  137. c.Assert(err, check.IsNil)
  138. err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
  139. c.Assert(err, check.IsNil)
  140. }
  141. func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) {
  142. // TODO Windows. This may be portable following HNS integration post TP5.
  143. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon, NotUserNamespace, NotArm)
  144. dockerCmd(c, "network", "create", "-d", "bridge", "udNet")
  145. dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
  146. c.Assert(waitRun("first"), check.IsNil)
  147. dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
  148. "--link=first:foo", "busybox", "top")
  149. c.Assert(waitRun("second"), check.IsNil)
  150. // ping to first and its alias foo must succeed
  151. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  152. c.Assert(err, check.IsNil)
  153. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  154. c.Assert(err, check.IsNil)
  155. // Now kill the second container and let the restart policy kick in
  156. pidStr := inspectField(c, "second", "State.Pid")
  157. pid, err := strconv.Atoi(pidStr)
  158. c.Assert(err, check.IsNil)
  159. p, err := os.FindProcess(pid)
  160. c.Assert(err, check.IsNil)
  161. c.Assert(p, check.NotNil)
  162. err = p.Kill()
  163. c.Assert(err, check.IsNil)
  164. err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second)
  165. c.Assert(err, check.IsNil)
  166. err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second)
  167. c.Assert(err, check.IsNil)
  168. // ping to first and its alias foo must still succeed
  169. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  170. c.Assert(err, check.IsNil)
  171. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  172. c.Assert(err, check.IsNil)
  173. }
  174. func (s *DockerSuite) TestRestartPolicyAfterRestart(c *check.C) {
  175. // Skipped for Hyper-V isolated containers. Test is currently written
  176. // such that it assumes there is a host process to kill. In Hyper-V
  177. // containers, the process is inside the utility VM, not on the host.
  178. testRequires(c, testEnv.IsLocalDaemon, IsolationIsProcess)
  179. out := runSleepingContainer(c, "-d", "--restart=always")
  180. id := strings.TrimSpace(out)
  181. c.Assert(waitRun(id), check.IsNil)
  182. dockerCmd(c, "restart", id)
  183. c.Assert(waitRun(id), check.IsNil)
  184. pidStr := inspectField(c, id, "State.Pid")
  185. pid, err := strconv.Atoi(pidStr)
  186. c.Assert(err, check.IsNil)
  187. p, err := os.FindProcess(pid)
  188. c.Assert(err, check.IsNil)
  189. c.Assert(p, check.NotNil)
  190. err = p.Kill()
  191. c.Assert(err, check.IsNil)
  192. err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
  193. c.Assert(err, check.IsNil)
  194. err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
  195. c.Assert(err, check.IsNil)
  196. }
  197. func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *check.C) {
  198. out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false")
  199. out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
  200. id1 := strings.TrimSpace(string(out1))
  201. id2 := strings.TrimSpace(string(out2))
  202. waitTimeout := 15 * time.Second
  203. if testEnv.OSType == "windows" {
  204. waitTimeout = 150 * time.Second
  205. }
  206. err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
  207. c.Assert(err, checker.IsNil)
  208. dockerCmd(c, "restart", id1)
  209. dockerCmd(c, "restart", id2)
  210. // Make sure we can stop/start (regression test from a705e166cf3bcca62543150c2b3f9bfeae45ecfa)
  211. dockerCmd(c, "stop", id1)
  212. dockerCmd(c, "stop", id2)
  213. dockerCmd(c, "start", id1)
  214. dockerCmd(c, "start", id2)
  215. // Kill the containers, making sure the are stopped at the end of the test
  216. dockerCmd(c, "kill", id1)
  217. dockerCmd(c, "kill", id2)
  218. err = waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
  219. c.Assert(err, checker.IsNil)
  220. err = waitInspect(id2, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
  221. c.Assert(err, checker.IsNil)
  222. }
  223. func (s *DockerSuite) TestRestartAutoRemoveContainer(c *check.C) {
  224. out := runSleepingContainer(c, "--rm")
  225. id := strings.TrimSpace(string(out))
  226. dockerCmd(c, "restart", id)
  227. err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second)
  228. c.Assert(err, checker.IsNil)
  229. out, _ = dockerCmd(c, "ps")
  230. c.Assert(out, checker.Contains, id[:12], check.Commentf("container should be restarted instead of removed: %v", out))
  231. // Kill the container to make sure it will be removed
  232. dockerCmd(c, "kill", id)
  233. }