docker_cli_rm_test.go 3.3 KB

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