docker_cli_rm_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "github.com/docker/docker/integration-cli/checker"
  6. "github.com/go-check/check"
  7. )
  8. func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
  9. testRequires(c, SameHostDaemon)
  10. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  11. tempDir, err := ioutil.TempDir("", "test-rm-container-with-removed-volume-")
  12. if err != nil {
  13. c.Fatalf("failed to create temporary directory: %s", tempDir)
  14. }
  15. defer os.RemoveAll(tempDir)
  16. dockerCmd(c, "run", "--name", "losemyvolumes", "-v", tempDir+":"+prefix+slash+"test", "busybox", "true")
  17. err = os.RemoveAll(tempDir)
  18. c.Assert(err, check.IsNil)
  19. dockerCmd(c, "rm", "-v", "losemyvolumes")
  20. }
  21. func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) {
  22. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  23. dockerCmd(c, "run", "--name", "foo", "-v", prefix+slash+"srv", "busybox", "true")
  24. dockerCmd(c, "rm", "-v", "foo")
  25. }
  26. func (s *DockerSuite) TestRmContainerRunning(c *check.C) {
  27. createRunningContainer(c, "foo")
  28. res, _, err := dockerCmdWithError("rm", "foo")
  29. c.Assert(err, checker.NotNil, check.Commentf("Expected error, can't rm a running container"))
  30. c.Assert(res, checker.Contains, "cannot remove a running container")
  31. c.Assert(res, checker.Contains, "Stop the container before attempting removal or force remove")
  32. }
  33. func (s *DockerSuite) TestRmContainerForceRemoveRunning(c *check.C) {
  34. createRunningContainer(c, "foo")
  35. // Stop then remove with -f
  36. dockerCmd(c, "rm", "-f", "foo")
  37. }
  38. func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
  39. dockerfile1 := `FROM busybox:latest
  40. ENTRYPOINT ["true"]`
  41. img := "test-container-orphaning"
  42. dockerfile2 := `FROM busybox:latest
  43. ENTRYPOINT ["true"]
  44. MAINTAINER Integration Tests`
  45. // build first dockerfile
  46. buildImageSuccessfully(c, img, withDockerfile(dockerfile1))
  47. img1 := getIDByName(c, img)
  48. // run container on first image
  49. dockerCmd(c, "run", img)
  50. // rebuild dockerfile with a small addition at the end
  51. buildImageSuccessfully(c, img, withDockerfile(dockerfile2))
  52. // try to remove the image, should not error out.
  53. out, _, err := dockerCmdWithError("rmi", img)
  54. c.Assert(err, check.IsNil, check.Commentf("Expected to removing the image, but failed: %s", out))
  55. // check if we deleted the first image
  56. out, _ = dockerCmd(c, "images", "-q", "--no-trunc")
  57. c.Assert(out, checker.Contains, img1, check.Commentf("Orphaned container (could not find %q in docker images): %s", img1, out))
  58. }
  59. func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
  60. out, _, err := dockerCmdWithError("rm", "unknown")
  61. c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm unknown container, got none"))
  62. c.Assert(out, checker.Contains, "No such container")
  63. }
  64. func createRunningContainer(c *check.C, name string) {
  65. runSleepingContainer(c, "-dt", "--name", name)
  66. }
  67. // #30842
  68. func (s *DockerSuite) TestRmRestartingContainer(c *check.C) {
  69. name := "rst"
  70. dockerCmd(c, "run", "--name", name, "--restart=always", "busybox", "date")
  71. res, _, err := dockerCmdWithError("rm", name)
  72. c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm a restarting container, got none"))
  73. c.Assert(res, checker.Contains, "cannot remove a restarting container")
  74. c.Assert(res, checker.Contains, "Stop the container before attempting removal or force remove")
  75. dockerCmd(c, "rm", "-f", name)
  76. }
  77. // #30842
  78. func (s *DockerSuite) TestRmPausedContainer(c *check.C) {
  79. testRequires(c, IsPausable)
  80. name := "psd"
  81. dockerCmd(c, "run", "--name", name, "-d", "busybox", "sleep", "1m")
  82. dockerCmd(c, "pause", name)
  83. res, _, err := dockerCmdWithError("rm", name)
  84. c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm a paused container, got none"))
  85. c.Assert(res, checker.Contains, "cannot remove a paused container")
  86. c.Assert(res, checker.Contains, "Unpause and then stop the container before attempting removal or force remove")
  87. unpauseContainer(c, name)
  88. dockerCmd(c, "stop", name)
  89. dockerCmd(c, "rm", name)
  90. }