2014-09-09 20:32:14 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-09-19 13:52:20 -04:00
|
|
|
func TestRmiWithContainerFails(t *testing.T) {
|
2014-09-09 20:32:14 -04:00
|
|
|
errSubstr := "is using it"
|
|
|
|
|
|
|
|
// create a container
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-14 16:09:19 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create a container: %s, %v", out, err)
|
|
|
|
}
|
2014-09-09 20:32:14 -04:00
|
|
|
|
|
|
|
cleanedContainerID := stripTrailingCharacters(out)
|
|
|
|
|
|
|
|
// try to delete the image
|
|
|
|
runCmd = exec.Command(dockerBinary, "rmi", "busybox")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out)
|
|
|
|
}
|
|
|
|
if !strings.Contains(out, errSubstr) {
|
|
|
|
t.Fatalf("Container %q is using image, error message should contain %q: %v", cleanedContainerID, errSubstr, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure it didn't delete the busybox name
|
2014-11-24 10:32:38 -05:00
|
|
|
images, _, _ := dockerCmd(t, "images")
|
2014-09-09 20:32:14 -04:00
|
|
|
if !strings.Contains(images, "busybox") {
|
|
|
|
t.Fatalf("The name 'busybox' should not have been removed from images: %q", images)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteContainer(cleanedContainerID)
|
|
|
|
|
|
|
|
logDone("rmi- container using image while rmi, should not remove image name")
|
|
|
|
}
|
|
|
|
|
2014-09-19 13:52:20 -04:00
|
|
|
func TestRmiTag(t *testing.T) {
|
2014-11-24 10:32:38 -05:00
|
|
|
imagesBefore, _, _ := dockerCmd(t, "images", "-a")
|
|
|
|
dockerCmd(t, "tag", "busybox", "utest:tag1")
|
|
|
|
dockerCmd(t, "tag", "busybox", "utest/docker:tag2")
|
|
|
|
dockerCmd(t, "tag", "busybox", "utest:5000/docker:tag3")
|
2014-09-09 20:32:14 -04:00
|
|
|
{
|
2014-11-24 10:32:38 -05:00
|
|
|
imagesAfter, _, _ := dockerCmd(t, "images", "-a")
|
2014-12-03 20:47:28 -05:00
|
|
|
if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+3 {
|
2014-09-09 20:32:14 -04:00
|
|
|
t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
|
|
|
|
}
|
|
|
|
}
|
2014-11-24 10:32:38 -05:00
|
|
|
dockerCmd(t, "rmi", "utest/docker:tag2")
|
2014-09-09 20:32:14 -04:00
|
|
|
{
|
2014-11-24 10:32:38 -05:00
|
|
|
imagesAfter, _, _ := dockerCmd(t, "images", "-a")
|
2014-12-03 20:47:28 -05:00
|
|
|
if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 {
|
2014-09-09 20:32:14 -04:00
|
|
|
t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-11-24 10:32:38 -05:00
|
|
|
dockerCmd(t, "rmi", "utest:5000/docker:tag3")
|
2014-09-09 20:32:14 -04:00
|
|
|
{
|
2014-11-24 10:32:38 -05:00
|
|
|
imagesAfter, _, _ := dockerCmd(t, "images", "-a")
|
2014-12-03 20:47:28 -05:00
|
|
|
if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+1 {
|
2014-09-09 20:32:14 -04:00
|
|
|
t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-11-24 10:32:38 -05:00
|
|
|
dockerCmd(t, "rmi", "utest:tag1")
|
2014-09-09 20:32:14 -04:00
|
|
|
{
|
2014-11-24 10:32:38 -05:00
|
|
|
imagesAfter, _, _ := dockerCmd(t, "images", "-a")
|
2014-12-03 20:47:28 -05:00
|
|
|
if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+0 {
|
2014-09-09 20:32:14 -04:00
|
|
|
t.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-11-11 11:30:07 -05:00
|
|
|
logDone("rmi - tag,rmi- tagging the same images multiple times then removing tags")
|
2014-09-09 20:32:14 -04:00
|
|
|
}
|
2014-10-02 19:39:39 -04:00
|
|
|
|
|
|
|
func TestRmiTagWithExistingContainers(t *testing.T) {
|
|
|
|
container := "test-delete-tag"
|
|
|
|
newtag := "busybox:newtag"
|
|
|
|
bb := "busybox:latest"
|
|
|
|
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil {
|
|
|
|
t.Fatalf("Could not tag busybox: %v: %s", err, out)
|
|
|
|
}
|
|
|
|
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil {
|
|
|
|
t.Fatalf("Could not run busybox: %v: %s", err, out)
|
|
|
|
}
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
|
|
|
|
}
|
|
|
|
if d := strings.Count(out, "Untagged: "); d != 1 {
|
|
|
|
t.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteAllContainers()
|
|
|
|
|
|
|
|
logDone("rmi - delete tag with existing containers")
|
|
|
|
}
|
2014-11-17 15:49:29 -05:00
|
|
|
|
|
|
|
func TestRmiForceWithExistingContainers(t *testing.T) {
|
|
|
|
image := "busybox-clone"
|
|
|
|
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "/docker-busybox")); err != nil {
|
|
|
|
t.Fatalf("Could not build %s: %s, %v", image, out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "test-force-rmi", image, "/bin/true")); err != nil {
|
|
|
|
t.Fatalf("Could not run container: %s, %v", out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", "-f", image))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not remove image %s: %s, %v", image, out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteAllContainers()
|
|
|
|
|
|
|
|
logDone("rmi - force delete with existing containers")
|
|
|
|
}
|