github.com/mizzy/docker@v1.5.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  	container := "test-delete-tag"
    82  	newtag := "busybox:newtag"
    83  	bb := "busybox:latest"
    84  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil {
    85  		t.Fatalf("Could not tag busybox: %v: %s", err, out)
    86  	}
    87  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil {
    88  		t.Fatalf("Could not run busybox: %v: %s", err, out)
    89  	}
    90  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag))
    91  	if err != nil {
    92  		t.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
    93  	}
    94  	if d := strings.Count(out, "Untagged: "); d != 1 {
    95  		t.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
    96  	}
    97  
    98  	deleteAllContainers()
    99  
   100  	logDone("rmi - delete tag with existing containers")
   101  }
   102  
   103  func TestRmiForceWithExistingContainers(t *testing.T) {
   104  	image := "busybox-clone"
   105  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "/docker-busybox")); err != nil {
   106  		t.Fatalf("Could not build %s: %s, %v", image, out, err)
   107  	}
   108  
   109  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "test-force-rmi", image, "/bin/true")); err != nil {
   110  		t.Fatalf("Could not run container: %s, %v", out, err)
   111  	}
   112  
   113  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", "-f", image))
   114  	if err != nil {
   115  		t.Fatalf("Could not remove image %s:  %s, %v", image, out, err)
   116  	}
   117  
   118  	deleteAllContainers()
   119  
   120  	logDone("rmi - force delete with existing containers")
   121  }