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