docker_cli_rm_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strings"
  6. "github.com/docker/docker/pkg/integration/checker"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
  10. testRequires(c, SameHostDaemon)
  11. prefix := ""
  12. slash := "/"
  13. if daemonPlatform == "windows" {
  14. prefix = "c:"
  15. slash = `\`
  16. }
  17. tempDir, err := ioutil.TempDir("", "test-rm-container-with-removed-volume-")
  18. if err != nil {
  19. c.Fatalf("failed to create temporary directory: %s", tempDir)
  20. }
  21. defer os.RemoveAll(tempDir)
  22. dockerCmd(c, "run", "--name", "losemyvolumes", "-v", tempDir+":"+prefix+slash+"test", "busybox", "true")
  23. err = os.RemoveAll(tempDir)
  24. c.Assert(err, check.IsNil)
  25. dockerCmd(c, "rm", "-v", "losemyvolumes")
  26. }
  27. func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) {
  28. prefix := ""
  29. slash := "/"
  30. if daemonPlatform == "windows" {
  31. prefix = "c:"
  32. slash = `\`
  33. }
  34. dockerCmd(c, "run", "--name", "foo", "-v", prefix+slash+"srv", "busybox", "true")
  35. dockerCmd(c, "rm", "-v", "foo")
  36. }
  37. func (s *DockerSuite) TestRmContainerRunning(c *check.C) {
  38. createRunningContainer(c, "foo")
  39. _, _, err := dockerCmdWithError("rm", "foo")
  40. c.Assert(err, checker.NotNil, check.Commentf("Expected error, can't rm a running container"))
  41. }
  42. func (s *DockerSuite) TestRmContainerForceRemoveRunning(c *check.C) {
  43. createRunningContainer(c, "foo")
  44. // Stop then remove with -s
  45. dockerCmd(c, "rm", "-f", "foo")
  46. }
  47. func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
  48. dockerfile1 := `FROM busybox:latest
  49. ENTRYPOINT ["true"]`
  50. img := "test-container-orphaning"
  51. dockerfile2 := `FROM busybox:latest
  52. ENTRYPOINT ["true"]
  53. MAINTAINER Integration Tests`
  54. // build first dockerfile
  55. img1, err := buildImage(img, dockerfile1, true)
  56. c.Assert(err, check.IsNil, check.Commentf("Could not build image %s", img))
  57. // run container on first image
  58. dockerCmd(c, "run", img)
  59. // rebuild dockerfile with a small addition at the end
  60. _, err = buildImage(img, dockerfile2, true)
  61. c.Assert(err, check.IsNil, check.Commentf("Could not rebuild image %s", img))
  62. // try to remove the image, should not error out.
  63. out, _, err := dockerCmdWithError("rmi", img)
  64. c.Assert(err, check.IsNil, check.Commentf("Expected to removing the image, but failed: %s", out))
  65. // check if we deleted the first image
  66. out, _ = dockerCmd(c, "images", "-q", "--no-trunc")
  67. c.Assert(out, checker.Contains, img1, check.Commentf("Orphaned container (could not find %q in docker images): %s", img1, out))
  68. }
  69. func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
  70. if out, _, err := dockerCmdWithError("rm", "unknown"); err == nil {
  71. c.Fatal("Expected error on rm unknown container, got none")
  72. } else if !strings.Contains(out, "Failed to remove container") {
  73. c.Fatalf("Expected output to contain 'Failed to remove container', got %q", out)
  74. }
  75. }
  76. func createRunningContainer(c *check.C, name string) {
  77. runSleepingContainer(c, "-dt", "--name", name)
  78. }