github.com/jthurman42/docker@v1.6.0-rc1/integration-cli/docker_cli_kill_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestKillContainer(t *testing.T) {
    10  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10")
    11  	out, _, err := runCommandWithOutput(runCmd)
    12  	if err != nil {
    13  		t.Fatal(out, err)
    14  	}
    15  
    16  	cleanedContainerID := stripTrailingCharacters(out)
    17  
    18  	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
    19  	if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
    20  		t.Fatalf("out should've been a container id: %s, %v", out, err)
    21  	}
    22  
    23  	killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
    24  	if out, _, err = runCommandWithOutput(killCmd); err != nil {
    25  		t.Fatalf("failed to kill container: %s, %v", out, err)
    26  	}
    27  
    28  	listRunningContainersCmd := exec.Command(dockerBinary, "ps", "-q")
    29  	out, _, err = runCommandWithOutput(listRunningContainersCmd)
    30  	if err != nil {
    31  		t.Fatalf("failed to list running containers: %s, %v", out, err)
    32  	}
    33  
    34  	if strings.Contains(out, cleanedContainerID) {
    35  		t.Fatal("killed container is still running")
    36  	}
    37  
    38  	deleteContainer(cleanedContainerID)
    39  
    40  	logDone("kill - kill container running sleep 10")
    41  }
    42  
    43  func TestKillDifferentUserContainer(t *testing.T) {
    44  	runCmd := exec.Command(dockerBinary, "run", "-u", "daemon", "-d", "busybox", "sh", "-c", "sleep 10")
    45  	out, _, err := runCommandWithOutput(runCmd)
    46  	if err != nil {
    47  		t.Fatal(out, err)
    48  	}
    49  
    50  	cleanedContainerID := stripTrailingCharacters(out)
    51  
    52  	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
    53  	if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
    54  		t.Fatalf("out should've been a container id: %s, %v", out, err)
    55  	}
    56  
    57  	killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
    58  	if out, _, err = runCommandWithOutput(killCmd); err != nil {
    59  		t.Fatalf("failed to kill container: %s, %v", out, err)
    60  	}
    61  
    62  	listRunningContainersCmd := exec.Command(dockerBinary, "ps", "-q")
    63  	out, _, err = runCommandWithOutput(listRunningContainersCmd)
    64  	if err != nil {
    65  		t.Fatalf("failed to list running containers: %s, %v", out, err)
    66  	}
    67  
    68  	if strings.Contains(out, cleanedContainerID) {
    69  		t.Fatal("killed container is still running")
    70  	}
    71  
    72  	deleteContainer(cleanedContainerID)
    73  
    74  	logDone("kill - kill container running sleep 10 from a different user")
    75  }