docker_cli_restart_test.go 11 KB

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