docker_cli_restart_test.go 4.7 KB

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