docker_cli_restart_test.go 11 KB

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