docker_cli_restart_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package main
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/go-check/check"
  6. )
  7. func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
  8. testRequires(c, DaemonIsLinux)
  9. out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "foobar")
  10. cleanedContainerID := strings.TrimSpace(out)
  11. dockerCmd(c, "wait", cleanedContainerID)
  12. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  13. if out != "foobar\n" {
  14. c.Errorf("container should've printed 'foobar'")
  15. }
  16. dockerCmd(c, "restart", cleanedContainerID)
  17. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  18. if out != "foobar\nfoobar\n" {
  19. c.Errorf("container should've printed 'foobar' twice, got %v", out)
  20. }
  21. }
  22. func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
  23. testRequires(c, DaemonIsLinux)
  24. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
  25. cleanedContainerID := strings.TrimSpace(out)
  26. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  27. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  28. if out != "foobar\n" {
  29. c.Errorf("container should've printed 'foobar'")
  30. }
  31. dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
  32. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  33. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  34. if out != "foobar\nfoobar\n" {
  35. c.Errorf("container should've printed 'foobar' twice")
  36. }
  37. }
  38. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  39. func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
  40. testRequires(c, DaemonIsLinux)
  41. out, _ := dockerCmd(c, "run", "-d", "-v", "/test", "busybox", "top")
  42. cleanedContainerID := strings.TrimSpace(out)
  43. out, _ = dockerCmd(c, "inspect", "--format", "{{ len .Mounts }}", cleanedContainerID)
  44. if out = strings.Trim(out, " \n\r"); out != "1" {
  45. c.Errorf("expect 1 volume received %s", out)
  46. }
  47. source, err := inspectMountSourceField(cleanedContainerID, "/test")
  48. c.Assert(err, check.IsNil)
  49. dockerCmd(c, "restart", cleanedContainerID)
  50. out, _ = dockerCmd(c, "inspect", "--format", "{{ len .Mounts }}", cleanedContainerID)
  51. if out = strings.Trim(out, " \n\r"); out != "1" {
  52. c.Errorf("expect 1 volume after restart received %s", out)
  53. }
  54. sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, "/test")
  55. c.Assert(err, check.IsNil)
  56. if source != sourceAfterRestart {
  57. c.Errorf("expected volume path: %s Actual path: %s", source, sourceAfterRestart)
  58. }
  59. }
  60. func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
  61. testRequires(c, DaemonIsLinux)
  62. out, _ := dockerCmd(c, "run", "-d", "--restart=no", "busybox", "false")
  63. id := strings.TrimSpace(string(out))
  64. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  65. c.Assert(err, check.IsNil)
  66. if name != "no" {
  67. c.Fatalf("Container restart policy name is %s, expected %s", name, "no")
  68. }
  69. }
  70. func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
  71. testRequires(c, DaemonIsLinux)
  72. out, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
  73. id := strings.TrimSpace(string(out))
  74. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  75. c.Assert(err, check.IsNil)
  76. if name != "always" {
  77. c.Fatalf("Container restart policy name is %s, expected %s", name, "always")
  78. }
  79. MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
  80. c.Assert(err, check.IsNil)
  81. // MaximumRetryCount=0 if the restart policy is always
  82. if MaximumRetryCount != "0" {
  83. c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0")
  84. }
  85. }
  86. func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
  87. testRequires(c, DaemonIsLinux)
  88. out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:1", "busybox", "false")
  89. id := strings.TrimSpace(string(out))
  90. name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
  91. c.Assert(err, check.IsNil)
  92. if name != "on-failure" {
  93. c.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure")
  94. }
  95. }
  96. // a good container with --restart=on-failure:3
  97. // MaximumRetryCount!=0; RestartCount=0
  98. func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) {
  99. testRequires(c, DaemonIsLinux)
  100. out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
  101. id := strings.TrimSpace(string(out))
  102. if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5*time.Second); err != nil {
  103. c.Fatal(err)
  104. }
  105. count, err := inspectField(id, "RestartCount")
  106. c.Assert(err, check.IsNil)
  107. if count != "0" {
  108. c.Fatalf("Container was restarted %s times, expected %d", count, 0)
  109. }
  110. MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
  111. c.Assert(err, check.IsNil)
  112. if MaximumRetryCount != "3" {
  113. c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
  114. }
  115. }