github.com/eatbyte/docker@v1.6.0/integration-cli/docker_cli_rmi_test.go (about) 1 package main 2 3 import ( 4 "os/exec" 5 "strings" 6 "testing" 7 ) 8 9 func TestRmiWithContainerFails(t *testing.T) { 10 errSubstr := "is using it" 11 12 // create a container 13 runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true") 14 out, _, err := runCommandWithOutput(runCmd) 15 if err != nil { 16 t.Fatalf("failed to create a container: %s, %v", out, err) 17 } 18 19 cleanedContainerID := stripTrailingCharacters(out) 20 21 // try to delete the image 22 runCmd = exec.Command(dockerBinary, "rmi", "busybox") 23 out, _, err = runCommandWithOutput(runCmd) 24 if err == nil { 25 t.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out) 26 } 27 if !strings.Contains(out, errSubstr) { 28 t.Fatalf("Container %q is using image, error message should contain %q: %v", cleanedContainerID, errSubstr, out) 29 } 30 31 // make sure it didn't delete the busybox name 32 images, _, _ := dockerCmd(t, "images") 33 if !strings.Contains(images, "busybox") { 34 t.Fatalf("The name 'busybox' should not have been removed from images: %q", images) 35 } 36 37 deleteContainer(cleanedContainerID) 38 39 logDone("rmi- container using image while rmi, should not remove image name") 40 } 41 42 func TestRmiTag(t *testing.T) { 43 imagesBefore, _, _ := dockerCmd(t, "images", "-a") 44 dockerCmd(t, "tag", "busybox", "utest:tag1") 45 dockerCmd(t, "tag", "busybox", "utest/docker:tag2") 46 dockerCmd(t, "tag", "busybox", "utest:5000/docker:tag3") 47 { 48 imagesAfter, _, _ := dockerCmd(t, "images", "-a") 49 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+3 { 50 t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 51 } 52 } 53 dockerCmd(t, "rmi", "utest/docker:tag2") 54 { 55 imagesAfter, _, _ := dockerCmd(t, "images", "-a") 56 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 { 57 t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 58 } 59 60 } 61 dockerCmd(t, "rmi", "utest:5000/docker:tag3") 62 { 63 imagesAfter, _, _ := dockerCmd(t, "images", "-a") 64 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+1 { 65 t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 66 } 67 68 } 69 dockerCmd(t, "rmi", "utest:tag1") 70 { 71 imagesAfter, _, _ := dockerCmd(t, "images", "-a") 72 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+0 { 73 t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 74 } 75 76 } 77 logDone("rmi - tag,rmi- tagging the same images multiple times then removing tags") 78 } 79 80 func TestRmiTagWithExistingContainers(t *testing.T) { 81 defer deleteAllContainers() 82 83 container := "test-delete-tag" 84 newtag := "busybox:newtag" 85 bb := "busybox:latest" 86 if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil { 87 t.Fatalf("Could not tag busybox: %v: %s", err, out) 88 } 89 if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil { 90 t.Fatalf("Could not run busybox: %v: %s", err, out) 91 } 92 out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag)) 93 if err != nil { 94 t.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out) 95 } 96 if d := strings.Count(out, "Untagged: "); d != 1 { 97 t.Fatalf("Expected 1 untagged entry got %d: %q", d, out) 98 } 99 100 logDone("rmi - delete tag with existing containers") 101 } 102 103 func TestRmiForceWithExistingContainers(t *testing.T) { 104 defer deleteAllContainers() 105 106 image := "busybox-clone" 107 108 cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-") 109 cmd.Stdin = strings.NewReader(`FROM busybox 110 MAINTAINER foo`) 111 112 if out, _, err := runCommandWithOutput(cmd); err != nil { 113 t.Fatalf("Could not build %s: %s, %v", image, out, err) 114 } 115 116 if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "test-force-rmi", image, "/bin/true")); err != nil { 117 t.Fatalf("Could not run container: %s, %v", out, err) 118 } 119 120 out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", "-f", image)) 121 if err != nil { 122 t.Fatalf("Could not remove image %s: %s, %v", image, out, err) 123 } 124 125 logDone("rmi - force delete with existing containers") 126 } 127 128 func TestRmiWithMultipleRepositories(t *testing.T) { 129 defer deleteAllContainers() 130 newRepo := "127.0.0.1:5000/busybox" 131 oldRepo := "busybox" 132 newTag := "busybox:test" 133 cmd := exec.Command(dockerBinary, "tag", oldRepo, newRepo) 134 out, _, err := runCommandWithOutput(cmd) 135 if err != nil { 136 t.Fatalf("Could not tag busybox: %v: %s", err, out) 137 } 138 cmd = exec.Command(dockerBinary, "run", "--name", "test", oldRepo, "touch", "/home/abcd") 139 out, _, err = runCommandWithOutput(cmd) 140 if err != nil { 141 t.Fatalf("failed to run container: %v, output: %s", err, out) 142 } 143 cmd = exec.Command(dockerBinary, "commit", "test", newTag) 144 out, _, err = runCommandWithOutput(cmd) 145 if err != nil { 146 t.Fatalf("failed to commit container: %v, output: %s", err, out) 147 } 148 cmd = exec.Command(dockerBinary, "rmi", newTag) 149 out, _, err = runCommandWithOutput(cmd) 150 if err != nil { 151 t.Fatalf("failed to remove image: %v, output: %s", err, out) 152 } 153 if !strings.Contains(out, "Untagged: "+newTag) { 154 t.Fatalf("Could not remove image %s: %s, %v", newTag, out, err) 155 } 156 157 logDone("rmi - delete a image which its dependency tagged to multiple repositories success") 158 } 159 160 func TestRmiBlank(t *testing.T) { 161 // try to delete a blank image name 162 runCmd := exec.Command(dockerBinary, "rmi", "") 163 out, _, err := runCommandWithOutput(runCmd) 164 165 if err == nil { 166 t.Fatal("Should have failed to delete '' image") 167 } 168 169 if strings.Contains(out, "No such image") { 170 t.Fatalf("Wrong error message generated: %s", out) 171 } 172 logDone("rmi- blank image name") 173 }