docker_cli_rmi_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. )
  7. func TestRmiWithContainerFails(t *testing.T) {
  8. errSubstr := "is using it"
  9. // create a container
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  11. out, _, err := runCommandWithOutput(runCmd)
  12. if err != nil {
  13. t.Fatalf("failed to create a container: %s, %v", out, err)
  14. }
  15. cleanedContainerID := stripTrailingCharacters(out)
  16. // try to delete the image
  17. runCmd = exec.Command(dockerBinary, "rmi", "busybox")
  18. out, _, err = runCommandWithOutput(runCmd)
  19. if err == nil {
  20. t.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out)
  21. }
  22. if !strings.Contains(out, errSubstr) {
  23. t.Fatalf("Container %q is using image, error message should contain %q: %v", cleanedContainerID, errSubstr, out)
  24. }
  25. // make sure it didn't delete the busybox name
  26. images, _, _ := dockerCmd(t, "images")
  27. if !strings.Contains(images, "busybox") {
  28. t.Fatalf("The name 'busybox' should not have been removed from images: %q", images)
  29. }
  30. deleteContainer(cleanedContainerID)
  31. logDone("rmi- container using image while rmi, should not remove image name")
  32. }
  33. func TestRmiTag(t *testing.T) {
  34. imagesBefore, _, _ := dockerCmd(t, "images", "-a")
  35. dockerCmd(t, "tag", "busybox", "utest:tag1")
  36. dockerCmd(t, "tag", "busybox", "utest/docker:tag2")
  37. dockerCmd(t, "tag", "busybox", "utest:5000/docker:tag3")
  38. {
  39. imagesAfter, _, _ := dockerCmd(t, "images", "-a")
  40. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+3 {
  41. t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  42. }
  43. }
  44. dockerCmd(t, "rmi", "utest/docker:tag2")
  45. {
  46. imagesAfter, _, _ := dockerCmd(t, "images", "-a")
  47. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 {
  48. t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  49. }
  50. }
  51. dockerCmd(t, "rmi", "utest:5000/docker:tag3")
  52. {
  53. imagesAfter, _, _ := dockerCmd(t, "images", "-a")
  54. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+1 {
  55. t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  56. }
  57. }
  58. dockerCmd(t, "rmi", "utest:tag1")
  59. {
  60. imagesAfter, _, _ := dockerCmd(t, "images", "-a")
  61. if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+0 {
  62. t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
  63. }
  64. }
  65. logDone("rmi - tag,rmi- tagging the same images multiple times then removing tags")
  66. }
  67. func TestRmiTagWithExistingContainers(t *testing.T) {
  68. container := "test-delete-tag"
  69. newtag := "busybox:newtag"
  70. bb := "busybox:latest"
  71. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil {
  72. t.Fatalf("Could not tag busybox: %v: %s", err, out)
  73. }
  74. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil {
  75. t.Fatalf("Could not run busybox: %v: %s", err, out)
  76. }
  77. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag))
  78. if err != nil {
  79. t.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
  80. }
  81. if d := strings.Count(out, "Untagged: "); d != 1 {
  82. t.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
  83. }
  84. deleteAllContainers()
  85. logDone("rmi - delete tag with existing containers")
  86. }
  87. func TestRmiForceWithExistingContainers(t *testing.T) {
  88. image := "busybox-clone"
  89. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "/docker-busybox")); err != nil {
  90. t.Fatalf("Could not build %s: %s, %v", image, out, err)
  91. }
  92. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "test-force-rmi", image, "/bin/true")); err != nil {
  93. t.Fatalf("Could not run container: %s, %v", out, err)
  94. }
  95. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", "-f", image))
  96. if err != nil {
  97. t.Fatalf("Could not remove image %s: %s, %v", image, out, err)
  98. }
  99. deleteAllContainers()
  100. logDone("rmi - force delete with existing containers")
  101. }