docker_cli_rm_test.go 2.7 KB

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