docker_cli_restart_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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, "run", "-d", "--restart=no", "busybox", "false")
  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, "run", "-d", "--restart=always", "busybox", "false")
  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, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:1", "busybox", "false")
  71. id := strings.TrimSpace(string(out))
  72. name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
  73. c.Assert(name, checker.Equals, "on-failure")
  74. }
  75. // a good container with --restart=on-failure:3
  76. // MaximumRetryCount!=0; RestartCount=0
  77. func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *check.C) {
  78. out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
  79. id := strings.TrimSpace(string(out))
  80. err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second)
  81. c.Assert(err, checker.IsNil)
  82. count := inspectField(c, id, "RestartCount")
  83. c.Assert(count, checker.Equals, "0")
  84. MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  85. c.Assert(MaximumRetryCount, checker.Equals, "3")
  86. }
  87. func (s *DockerSuite) TestRestartContainerSuccess(c *check.C) {
  88. testRequires(c, SameHostDaemon)
  89. out, _ := runSleepingContainer(c, "-d", "--restart=always")
  90. id := strings.TrimSpace(out)
  91. c.Assert(waitRun(id), check.IsNil)
  92. pidStr := inspectField(c, id, "State.Pid")
  93. pid, err := strconv.Atoi(pidStr)
  94. c.Assert(err, check.IsNil)
  95. p, err := os.FindProcess(pid)
  96. c.Assert(err, check.IsNil)
  97. c.Assert(p, check.NotNil)
  98. err = p.Kill()
  99. c.Assert(err, check.IsNil)
  100. err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
  101. c.Assert(err, check.IsNil)
  102. err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
  103. c.Assert(err, check.IsNil)
  104. }
  105. func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) {
  106. // TODO Windows. This may be portable following HNS integration post TP5.
  107. testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
  108. dockerCmd(c, "network", "create", "-d", "bridge", "udNet")
  109. dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
  110. c.Assert(waitRun("first"), check.IsNil)
  111. dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
  112. "--link=first:foo", "busybox", "top")
  113. c.Assert(waitRun("second"), check.IsNil)
  114. // ping to first and its alias foo must succeed
  115. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  116. c.Assert(err, check.IsNil)
  117. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  118. c.Assert(err, check.IsNil)
  119. // Now kill the second container and let the restart policy kick in
  120. pidStr := inspectField(c, "second", "State.Pid")
  121. pid, err := strconv.Atoi(pidStr)
  122. c.Assert(err, check.IsNil)
  123. p, err := os.FindProcess(pid)
  124. c.Assert(err, check.IsNil)
  125. c.Assert(p, check.NotNil)
  126. err = p.Kill()
  127. c.Assert(err, check.IsNil)
  128. err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second)
  129. c.Assert(err, check.IsNil)
  130. err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second)
  131. // ping to first and its alias foo must still 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. }
  137. func (s *DockerSuite) TestRestartPolicyAfterRestart(c *check.C) {
  138. testRequires(c, SameHostDaemon)
  139. out, _ := runSleepingContainer(c, "-d", "--restart=always")
  140. id := strings.TrimSpace(out)
  141. c.Assert(waitRun(id), check.IsNil)
  142. dockerCmd(c, "restart", id)
  143. c.Assert(waitRun(id), check.IsNil)
  144. pidStr := inspectField(c, id, "State.Pid")
  145. pid, err := strconv.Atoi(pidStr)
  146. c.Assert(err, check.IsNil)
  147. p, err := os.FindProcess(pid)
  148. c.Assert(err, check.IsNil)
  149. c.Assert(p, check.NotNil)
  150. err = p.Kill()
  151. c.Assert(err, check.IsNil)
  152. err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
  153. c.Assert(err, check.IsNil)
  154. err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
  155. c.Assert(err, check.IsNil)
  156. }
  157. func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *check.C) {
  158. out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false")
  159. out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
  160. id1 := strings.TrimSpace(string(out1))
  161. id2 := strings.TrimSpace(string(out2))
  162. waitTimeout := 15 * time.Second
  163. if daemonPlatform == "windows" {
  164. waitTimeout = 150 * time.Second
  165. }
  166. err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
  167. c.Assert(err, checker.IsNil)
  168. dockerCmd(c, "restart", id1)
  169. dockerCmd(c, "restart", id2)
  170. dockerCmd(c, "stop", id1)
  171. dockerCmd(c, "stop", id2)
  172. dockerCmd(c, "start", id1)
  173. dockerCmd(c, "start", id2)
  174. }