docker_cli_rm_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, 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. _, _, err := dockerCmdWithError("rm", "foo")
  30. c.Assert(err, checker.NotNil, check.Commentf("Expected error, can't rm a running container"))
  31. }
  32. func (s *DockerSuite) TestRmContainerForceRemoveRunning(c *check.C) {
  33. createRunningContainer(c, "foo")
  34. // Stop then remove with -s
  35. dockerCmd(c, "rm", "-f", "foo")
  36. }
  37. func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
  38. dockerfile1 := `FROM busybox:latest
  39. ENTRYPOINT ["true"]`
  40. img := "test-container-orphaning"
  41. dockerfile2 := `FROM busybox:latest
  42. ENTRYPOINT ["true"]
  43. MAINTAINER Integration Tests`
  44. // build first dockerfile
  45. img1, err := buildImage(img, dockerfile1, true)
  46. c.Assert(err, check.IsNil, check.Commentf("Could not build image %s", img))
  47. // run container on first image
  48. dockerCmd(c, "run", img)
  49. // rebuild dockerfile with a small addition at the end
  50. _, err = buildImage(img, dockerfile2, true)
  51. c.Assert(err, check.IsNil, check.Commentf("Could not rebuild image %s", img))
  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. if out, _, err := dockerCmdWithError("rm", "unknown"); err == nil {
  61. c.Fatal("Expected error on rm unknown container, got none")
  62. } else if !strings.Contains(out, "Failed to remove container") {
  63. c.Fatalf("Expected output to contain 'Failed to remove container', got %q", out)
  64. }
  65. }
  66. func createRunningContainer(c *check.C, name string) {
  67. runSleepingContainer(c, "-dt", "--name", name)
  68. }