docker_cli_restart_test.go 6.1 KB

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