docker_cli_rm_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  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, 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 -s
  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. img1, err := buildImage(img, dockerfile1, true)
  45. c.Assert(err, check.IsNil, check.Commentf("Could not build image %s", img))
  46. // run container on first image
  47. dockerCmd(c, "run", img)
  48. // rebuild dockerfile with a small addition at the end
  49. _, err = buildImage(img, dockerfile2, true)
  50. c.Assert(err, check.IsNil, check.Commentf("Could not rebuild image %s", img))
  51. // try to remove the image, should not error out.
  52. out, _, err := dockerCmdWithError("rmi", img)
  53. c.Assert(err, check.IsNil, check.Commentf("Expected to removing the image, but failed: %s", out))
  54. // check if we deleted the first image
  55. out, _ = dockerCmd(c, "images", "-q", "--no-trunc")
  56. c.Assert(out, checker.Contains, img1, check.Commentf("Orphaned container (could not find %q in docker images): %s", img1, out))
  57. }
  58. func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
  59. out, _, err := dockerCmdWithError("rm", "unknown")
  60. c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm unknown container, got none"))
  61. c.Assert(out, checker.Contains, "No such container")
  62. }
  63. func createRunningContainer(c *check.C, name string) {
  64. runSleepingContainer(c, "-dt", "--name", name)
  65. }