docker_cli_restart_test.go 11 KB

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