docker_cli_rm_test.go 2.6 KB

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