docker_cli_rm_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "github.com/docker/docker/integration-cli/checker"
  6. "github.com/docker/docker/integration-cli/cli/build"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
  10. testRequires(c, SameHostDaemon)
  11. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  12. tempDir, err := ioutil.TempDir("", "test-rm-container-with-removed-volume-")
  13. if err != nil {
  14. c.Fatalf("failed to create temporary directory: %s", tempDir)
  15. }
  16. defer os.RemoveAll(tempDir)
  17. dockerCmd(c, "run", "--name", "losemyvolumes", "-v", tempDir+":"+prefix+slash+"test", "busybox", "true")
  18. err = os.RemoveAll(tempDir)
  19. c.Assert(err, check.IsNil)
  20. dockerCmd(c, "rm", "-v", "losemyvolumes")
  21. }
  22. func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) {
  23. prefix, slash := getPrefixAndSlashFromDaemonPlatform()
  24. dockerCmd(c, "run", "--name", "foo", "-v", prefix+slash+"srv", "busybox", "true")
  25. dockerCmd(c, "rm", "-v", "foo")
  26. }
  27. func (s *DockerSuite) TestRmContainerRunning(c *check.C) {
  28. createRunningContainer(c, "foo")
  29. res, _, err := dockerCmdWithError("rm", "foo")
  30. c.Assert(err, checker.NotNil, check.Commentf("Expected error, can't rm a running container"))
  31. c.Assert(res, checker.Contains, "cannot remove a running container")
  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, build.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, build.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. }