docker_cli_restart_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "time"
  6. "github.com/go-check/check"
  7. )
  8. func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
  9. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
  10. out, _, err := runCommandWithOutput(runCmd)
  11. if err != nil {
  12. c.Fatal(out, err)
  13. }
  14. cleanedContainerID := strings.TrimSpace(out)
  15. runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
  16. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  17. c.Fatal(out, err)
  18. }
  19. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  20. out, _, err = runCommandWithOutput(runCmd)
  21. if err != nil {
  22. c.Fatal(out, err)
  23. }
  24. if out != "foobar\n" {
  25. c.Errorf("container should've printed 'foobar'")
  26. }
  27. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  28. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  29. c.Fatal(out, err)
  30. }
  31. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  32. out, _, err = runCommandWithOutput(runCmd)
  33. if err != nil {
  34. c.Fatal(out, err)
  35. }
  36. if out != "foobar\nfoobar\n" {
  37. c.Errorf("container should've printed 'foobar' twice")
  38. }
  39. }
  40. func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
  41. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
  42. out, _, err := runCommandWithOutput(runCmd)
  43. if err != nil {
  44. c.Fatal(out, err)
  45. }
  46. cleanedContainerID := strings.TrimSpace(out)
  47. time.Sleep(1 * time.Second)
  48. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  49. out, _, err = runCommandWithOutput(runCmd)
  50. if err != nil {
  51. c.Fatal(out, err)
  52. }
  53. if out != "foobar\n" {
  54. c.Errorf("container should've printed 'foobar'")
  55. }
  56. runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID)
  57. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  58. c.Fatal(out, err)
  59. }
  60. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  61. out, _, err = runCommandWithOutput(runCmd)
  62. if err != nil {
  63. c.Fatal(out, err)
  64. }
  65. time.Sleep(1 * time.Second)
  66. if out != "foobar\nfoobar\n" {
  67. c.Errorf("container should've printed 'foobar' twice")
  68. }
  69. }
  70. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  71. func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
  72. runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
  73. out, _, err := runCommandWithOutput(runCmd)
  74. if err != nil {
  75. c.Fatal(out, err)
  76. }
  77. cleanedContainerID := strings.TrimSpace(out)
  78. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
  79. out, _, err = runCommandWithOutput(runCmd)
  80. if err != nil {
  81. c.Fatal(out, err)
  82. }
  83. if out = strings.Trim(out, " \n\r"); out != "1" {
  84. c.Errorf("expect 1 volume received %s", out)
  85. }
  86. volumes, err := inspectField(cleanedContainerID, "Volumes")
  87. c.Assert(err, check.IsNil)
  88. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  89. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  90. c.Fatal(out, err)
  91. }
  92. runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
  93. out, _, err = runCommandWithOutput(runCmd)
  94. if err != nil {
  95. c.Fatal(out, err)
  96. }
  97. if out = strings.Trim(out, " \n\r"); out != "1" {
  98. c.Errorf("expect 1 volume after restart received %s", out)
  99. }
  100. volumesAfterRestart, err := inspectField(cleanedContainerID, "Volumes")
  101. c.Assert(err, check.IsNil)
  102. if volumes != volumesAfterRestart {
  103. c.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
  104. }
  105. }
  106. func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
  107. cmd := exec.Command(dockerBinary, "run", "-d", "--restart=no", "busybox", "false")
  108. out, _, err := runCommandWithOutput(cmd)
  109. if err != nil {
  110. c.Fatal(err, out)
  111. }
  112. id := strings.TrimSpace(string(out))
  113. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  114. c.Assert(err, check.IsNil)
  115. if name != "no" {
  116. c.Fatalf("Container restart policy name is %s, expected %s", name, "no")
  117. }
  118. }
  119. func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
  120. cmd := exec.Command(dockerBinary, "run", "-d", "--restart=always", "busybox", "false")
  121. out, _, err := runCommandWithOutput(cmd)
  122. if err != nil {
  123. c.Fatal(err, out)
  124. }
  125. id := strings.TrimSpace(string(out))
  126. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  127. c.Assert(err, check.IsNil)
  128. if name != "always" {
  129. c.Fatalf("Container restart policy name is %s, expected %s", name, "always")
  130. }
  131. MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
  132. c.Assert(err, check.IsNil)
  133. // MaximumRetryCount=0 if the restart policy is always
  134. if MaximumRetryCount != "0" {
  135. c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0")
  136. }
  137. }
  138. func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
  139. cmd := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:1", "busybox", "false")
  140. out, _, err := runCommandWithOutput(cmd)
  141. if err != nil {
  142. c.Fatal(err, out)
  143. }
  144. id := strings.TrimSpace(string(out))
  145. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  146. c.Assert(err, check.IsNil)
  147. if name != "on-failure" {
  148. c.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure")
  149. }
  150. }
  151. // a good container with --restart=on-failure:3
  152. // MaximumRetryCount!=0; RestartCount=0
  153. func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) {
  154. out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "true").CombinedOutput()
  155. if err != nil {
  156. c.Fatal(string(out), err)
  157. }
  158. id := strings.TrimSpace(string(out))
  159. if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil {
  160. c.Fatal(err)
  161. }
  162. count, err := inspectField(id, "RestartCount")
  163. c.Assert(err, check.IsNil)
  164. if count != "0" {
  165. c.Fatalf("Container was restarted %s times, expected %d", count, 0)
  166. }
  167. MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
  168. c.Assert(err, check.IsNil)
  169. if MaximumRetryCount != "3" {
  170. c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
  171. }
  172. }