docker_cli_restart_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. testRequires(c, DaemonIsLinux)
  12. out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "foobar")
  13. cleanedContainerID := strings.TrimSpace(out)
  14. dockerCmd(c, "wait", cleanedContainerID)
  15. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  16. c.Assert(out, checker.Equals, "foobar\n")
  17. dockerCmd(c, "restart", cleanedContainerID)
  18. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  19. c.Assert(out, checker.Equals, "foobar\nfoobar\n")
  20. }
  21. func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
  22. testRequires(c, DaemonIsLinux)
  23. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
  24. cleanedContainerID := strings.TrimSpace(out)
  25. c.Assert(waitRun(cleanedContainerID), checker.IsNil)
  26. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  27. c.Assert(out, checker.Equals, "foobar\n")
  28. dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
  29. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  30. c.Assert(waitRun(cleanedContainerID), checker.IsNil)
  31. c.Assert(out, checker.Equals, "foobar\nfoobar\n")
  32. }
  33. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  34. func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
  35. testRequires(c, DaemonIsLinux)
  36. out, _ := dockerCmd(c, "run", "-d", "-v", "/test", "busybox", "top")
  37. cleanedContainerID := strings.TrimSpace(out)
  38. out, err := inspectFilter(cleanedContainerID, "len .Mounts")
  39. c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
  40. out = strings.Trim(out, " \n\r")
  41. c.Assert(out, checker.Equals, "1")
  42. source, err := inspectMountSourceField(cleanedContainerID, "/test")
  43. c.Assert(err, checker.IsNil)
  44. dockerCmd(c, "restart", cleanedContainerID)
  45. out, err = inspectFilter(cleanedContainerID, "len .Mounts")
  46. c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
  47. out = strings.Trim(out, " \n\r")
  48. c.Assert(out, checker.Equals, "1")
  49. sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, "/test")
  50. c.Assert(err, checker.IsNil)
  51. c.Assert(source, checker.Equals, sourceAfterRestart)
  52. }
  53. func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
  54. testRequires(c, DaemonIsLinux)
  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. testRequires(c, DaemonIsLinux)
  62. out, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
  63. id := strings.TrimSpace(string(out))
  64. name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
  65. c.Assert(name, checker.Equals, "always")
  66. MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  67. // MaximumRetryCount=0 if the restart policy is always
  68. c.Assert(MaximumRetryCount, checker.Equals, "0")
  69. }
  70. func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
  71. testRequires(c, DaemonIsLinux)
  72. out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:1", "busybox", "false")
  73. id := strings.TrimSpace(string(out))
  74. name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
  75. c.Assert(name, checker.Equals, "on-failure")
  76. }
  77. // a good container with --restart=on-failure:3
  78. // MaximumRetryCount!=0; RestartCount=0
  79. func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) {
  80. testRequires(c, DaemonIsLinux)
  81. out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
  82. id := strings.TrimSpace(string(out))
  83. err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5*time.Second)
  84. c.Assert(err, checker.IsNil)
  85. count := inspectField(c, id, "RestartCount")
  86. c.Assert(count, checker.Equals, "0")
  87. MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
  88. c.Assert(MaximumRetryCount, checker.Equals, "3")
  89. }
  90. func (s *DockerSuite) TestContainerRestartSuccess(c *check.C) {
  91. testRequires(c, DaemonIsLinux, SameHostDaemon)
  92. out, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "top")
  93. id := strings.TrimSpace(out)
  94. c.Assert(waitRun(id), check.IsNil)
  95. pidStr := inspectField(c, id, "State.Pid")
  96. pid, err := strconv.Atoi(pidStr)
  97. c.Assert(err, check.IsNil)
  98. p, err := os.FindProcess(pid)
  99. c.Assert(err, check.IsNil)
  100. c.Assert(p, check.NotNil)
  101. err = p.Kill()
  102. c.Assert(err, check.IsNil)
  103. err = waitInspect(id, "{{.RestartCount}}", "1", 5*time.Second)
  104. c.Assert(err, check.IsNil)
  105. err = waitInspect(id, "{{.State.Status}}", "running", 5*time.Second)
  106. c.Assert(err, check.IsNil)
  107. }
  108. func (s *DockerSuite) TestUserDefinedNetworkWithRestartPolicy(c *check.C) {
  109. testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
  110. dockerCmd(c, "network", "create", "-d", "bridge", "udNet")
  111. dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
  112. c.Assert(waitRun("first"), check.IsNil)
  113. dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
  114. "--link=first:foo", "busybox", "top")
  115. c.Assert(waitRun("second"), check.IsNil)
  116. // ping to first and its alias foo must succeed
  117. _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  118. c.Assert(err, check.IsNil)
  119. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  120. c.Assert(err, check.IsNil)
  121. // Now kill the second container and let the restart policy kick in
  122. pidStr := inspectField(c, "second", "State.Pid")
  123. pid, err := strconv.Atoi(pidStr)
  124. c.Assert(err, check.IsNil)
  125. p, err := os.FindProcess(pid)
  126. c.Assert(err, check.IsNil)
  127. c.Assert(p, check.NotNil)
  128. err = p.Kill()
  129. c.Assert(err, check.IsNil)
  130. err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second)
  131. c.Assert(err, check.IsNil)
  132. err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second)
  133. // ping to first and its alias foo must still succeed
  134. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
  135. c.Assert(err, check.IsNil)
  136. _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
  137. c.Assert(err, check.IsNil)
  138. }