docker_cli_restart_test.go 9.1 KB

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