github.com/dlintw/docker@v1.5.0-rc4/integration-cli/docker_cli_rm_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestRmContainerWithRemovedVolume(t *testing.T) {
    11  	cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
    12  	if _, err := runCommand(cmd); err != nil {
    13  		t.Fatal(err)
    14  	}
    15  
    16  	if err := os.Remove("/tmp/testing"); err != nil {
    17  		t.Fatal(err)
    18  	}
    19  
    20  	cmd = exec.Command(dockerBinary, "rm", "-v", "losemyvolumes")
    21  	if out, _, err := runCommandWithOutput(cmd); err != nil {
    22  		t.Fatal(out, err)
    23  	}
    24  
    25  	deleteAllContainers()
    26  
    27  	logDone("rm - removed volume")
    28  }
    29  
    30  func TestRmContainerWithVolume(t *testing.T) {
    31  	cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
    32  	if _, err := runCommand(cmd); err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	cmd = exec.Command(dockerBinary, "rm", "-v", "foo")
    37  	if _, err := runCommand(cmd); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	deleteAllContainers()
    42  
    43  	logDone("rm - volume")
    44  }
    45  
    46  func TestRmRunningContainer(t *testing.T) {
    47  	createRunningContainer(t, "foo")
    48  
    49  	// Test cannot remove running container
    50  	cmd := exec.Command(dockerBinary, "rm", "foo")
    51  	if _, err := runCommand(cmd); err == nil {
    52  		t.Fatalf("Expected error, can't rm a running container")
    53  	}
    54  
    55  	deleteAllContainers()
    56  
    57  	logDone("rm - running container")
    58  }
    59  
    60  func TestRmRunningContainerCheckError409(t *testing.T) {
    61  	createRunningContainer(t, "foo")
    62  
    63  	endpoint := "/containers/foo"
    64  	_, err := sockRequest("DELETE", endpoint, nil)
    65  
    66  	if err == nil {
    67  		t.Fatalf("Expected error, can't rm a running container")
    68  	}
    69  	if !strings.Contains(err.Error(), "409 Conflict") {
    70  		t.Fatalf("Expected error to contain '409 Conflict' but found %s", err)
    71  	}
    72  
    73  	deleteAllContainers()
    74  
    75  	logDone("rm - running container")
    76  }
    77  
    78  func TestRmForceRemoveRunningContainer(t *testing.T) {
    79  	createRunningContainer(t, "foo")
    80  
    81  	// Stop then remove with -s
    82  	cmd := exec.Command(dockerBinary, "rm", "-f", "foo")
    83  	if _, err := runCommand(cmd); err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	deleteAllContainers()
    88  
    89  	logDone("rm - running container with --force=true")
    90  }
    91  
    92  func TestRmContainerOrphaning(t *testing.T) {
    93  	dockerfile1 := `FROM busybox:latest
    94  	ENTRYPOINT ["/bin/true"]`
    95  	img := "test-container-orphaning"
    96  	dockerfile2 := `FROM busybox:latest
    97  	ENTRYPOINT ["/bin/true"]
    98  	MAINTAINER Integration Tests`
    99  
   100  	// build first dockerfile
   101  	img1, err := buildImage(img, dockerfile1, true)
   102  	if err != nil {
   103  		t.Fatalf("Could not build image %s: %v", img, err)
   104  	}
   105  	// run container on first image
   106  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", img)); err != nil {
   107  		t.Fatalf("Could not run image %s: %v: %s", img, err, out)
   108  	}
   109  	// rebuild dockerfile with a small addition at the end
   110  	if _, err := buildImage(img, dockerfile2, true); err != nil {
   111  		t.Fatalf("Could not rebuild image %s: %v", img, err)
   112  	}
   113  	// try to remove the image, should error out.
   114  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", img)); err == nil {
   115  		t.Fatalf("Expected to error out removing the image, but succeeded: %s", out)
   116  	}
   117  	// check if we deleted the first image
   118  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "images", "-q", "--no-trunc"))
   119  	if err != nil {
   120  		t.Fatalf("%v: %s", err, out)
   121  	}
   122  	if !strings.Contains(out, img1) {
   123  		t.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
   124  	}
   125  
   126  	deleteAllContainers()
   127  	deleteImages(img1)
   128  
   129  	logDone("rm - container orphaning")
   130  }
   131  
   132  func TestRmInvalidContainer(t *testing.T) {
   133  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "unknown")); err == nil {
   134  		t.Fatal("Expected error on rm unknown container, got none")
   135  	} else if !strings.Contains(out, "failed to remove one or more containers") {
   136  		t.Fatalf("Expected output to contain 'failed to remove one or more containers', got %q", out)
   137  	}
   138  
   139  	logDone("rm - delete unknown container")
   140  }
   141  
   142  func createRunningContainer(t *testing.T, name string) {
   143  	cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top")
   144  	if _, err := runCommand(cmd); err != nil {
   145  		t.Fatal(err)
   146  	}
   147  }