docker_cli_rm_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package main
  2. import (
  3. "net/http"
  4. "os"
  5. "os/exec"
  6. "strings"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
  10. testRequires(c, SameHostDaemon)
  11. cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
  12. if _, err := runCommand(cmd); err != nil {
  13. c.Fatal(err)
  14. }
  15. if err := os.Remove("/tmp/testing"); err != nil {
  16. c.Fatal(err)
  17. }
  18. cmd = exec.Command(dockerBinary, "rm", "-v", "losemyvolumes")
  19. if out, _, err := runCommandWithOutput(cmd); err != nil {
  20. c.Fatal(out, err)
  21. }
  22. }
  23. func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) {
  24. cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
  25. if _, err := runCommand(cmd); err != nil {
  26. c.Fatal(err)
  27. }
  28. cmd = exec.Command(dockerBinary, "rm", "-v", "foo")
  29. if _, err := runCommand(cmd); err != nil {
  30. c.Fatal(err)
  31. }
  32. }
  33. func (s *DockerSuite) TestRmRunningContainer(c *check.C) {
  34. createRunningContainer(c, "foo")
  35. // Test cannot remove running container
  36. cmd := exec.Command(dockerBinary, "rm", "foo")
  37. if _, err := runCommand(cmd); err == nil {
  38. c.Fatalf("Expected error, can't rm a running container")
  39. }
  40. }
  41. func (s *DockerSuite) TestRmRunningContainerCheckError409(c *check.C) {
  42. createRunningContainer(c, "foo")
  43. endpoint := "/containers/foo"
  44. status, _, err := sockRequest("DELETE", endpoint, nil)
  45. c.Assert(status, check.Equals, http.StatusConflict)
  46. c.Assert(err, check.IsNil)
  47. }
  48. func (s *DockerSuite) TestRmForceRemoveRunningContainer(c *check.C) {
  49. createRunningContainer(c, "foo")
  50. // Stop then remove with -s
  51. cmd := exec.Command(dockerBinary, "rm", "-f", "foo")
  52. if _, err := runCommand(cmd); err != nil {
  53. c.Fatal(err)
  54. }
  55. }
  56. func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
  57. dockerfile1 := `FROM busybox:latest
  58. ENTRYPOINT ["/bin/true"]`
  59. img := "test-container-orphaning"
  60. dockerfile2 := `FROM busybox:latest
  61. ENTRYPOINT ["/bin/true"]
  62. MAINTAINER Integration Tests`
  63. // build first dockerfile
  64. img1, err := buildImage(img, dockerfile1, true)
  65. if err != nil {
  66. c.Fatalf("Could not build image %s: %v", img, err)
  67. }
  68. // run container on first image
  69. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", img)); err != nil {
  70. c.Fatalf("Could not run image %s: %v: %s", img, err, out)
  71. }
  72. // rebuild dockerfile with a small addition at the end
  73. if _, err := buildImage(img, dockerfile2, true); err != nil {
  74. c.Fatalf("Could not rebuild image %s: %v", img, err)
  75. }
  76. // try to remove the image, should error out.
  77. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", img)); err == nil {
  78. c.Fatalf("Expected to error out removing the image, but succeeded: %s", out)
  79. }
  80. // check if we deleted the first image
  81. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "images", "-q", "--no-trunc"))
  82. if err != nil {
  83. c.Fatalf("%v: %s", err, out)
  84. }
  85. if !strings.Contains(out, img1) {
  86. c.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
  87. }
  88. }
  89. func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
  90. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "unknown")); err == nil {
  91. c.Fatal("Expected error on rm unknown container, got none")
  92. } else if !strings.Contains(out, "failed to remove one or more containers") {
  93. c.Fatalf("Expected output to contain 'failed to remove one or more containers', got %q", out)
  94. }
  95. }
  96. func createRunningContainer(c *check.C, name string) {
  97. cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top")
  98. if _, err := runCommand(cmd); err != nil {
  99. c.Fatal(err)
  100. }
  101. }