docker_cli_rm_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. _, _, err := dockerCmdWithError("rm", "foo")
  29. c.Assert(err, checker.NotNil, check.Commentf("Expected error, can't rm a running container"))
  30. }
  31. func (s *DockerSuite) TestRmContainerForceRemoveRunning(c *check.C) {
  32. createRunningContainer(c, "foo")
  33. // Stop then remove with -f
  34. dockerCmd(c, "rm", "-f", "foo")
  35. }
  36. func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
  37. dockerfile1 := `FROM busybox:latest
  38. ENTRYPOINT ["true"]`
  39. img := "test-container-orphaning"
  40. dockerfile2 := `FROM busybox:latest
  41. ENTRYPOINT ["true"]
  42. MAINTAINER Integration Tests`
  43. // build first dockerfile
  44. buildImageSuccessfully(c, img, withDockerfile(dockerfile1))
  45. img1 := getIDByName(c, img)
  46. // run container on first image
  47. dockerCmd(c, "run", img)
  48. // rebuild dockerfile with a small addition at the end
  49. buildImageSuccessfully(c, img, withDockerfile(dockerfile2))
  50. // try to remove the image, should not error out.
  51. out, _, err := dockerCmdWithError("rmi", img)
  52. c.Assert(err, check.IsNil, check.Commentf("Expected to removing the image, but failed: %s", out))
  53. // check if we deleted the first image
  54. out, _ = dockerCmd(c, "images", "-q", "--no-trunc")
  55. c.Assert(out, checker.Contains, img1, check.Commentf("Orphaned container (could not find %q in docker images): %s", img1, out))
  56. }
  57. func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
  58. out, _, err := dockerCmdWithError("rm", "unknown")
  59. c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm unknown container, got none"))
  60. c.Assert(out, checker.Contains, "No such container")
  61. }
  62. func createRunningContainer(c *check.C, name string) {
  63. runSleepingContainer(c, "-dt", "--name", name)
  64. }