github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/integration-cli/docker_cli_kill_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  
     7  	"github.com/go-check/check"
     8  )
     9  
    10  func (s *DockerSuite) TestKillContainer(c *check.C) {
    11  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
    12  	out, _, err := runCommandWithOutput(runCmd)
    13  	if err != nil {
    14  		c.Fatal(out, err)
    15  	}
    16  
    17  	cleanedContainerID := strings.TrimSpace(out)
    18  	c.Assert(waitRun(cleanedContainerID), check.IsNil)
    19  
    20  	killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
    21  	if out, _, err = runCommandWithOutput(killCmd); err != nil {
    22  		c.Fatalf("failed to kill container: %s, %v", out, err)
    23  	}
    24  
    25  	listRunningContainersCmd := exec.Command(dockerBinary, "ps", "-q")
    26  	out, _, err = runCommandWithOutput(listRunningContainersCmd)
    27  	if err != nil {
    28  		c.Fatalf("failed to list running containers: %s, %v", out, err)
    29  	}
    30  
    31  	if strings.Contains(out, cleanedContainerID) {
    32  		c.Fatal("killed container is still running")
    33  	}
    34  }
    35  
    36  func (s *DockerSuite) TestKillofStoppedContainer(c *check.C) {
    37  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
    38  	out, _, err := runCommandWithOutput(runCmd)
    39  	c.Assert(err, check.IsNil)
    40  
    41  	cleanedContainerID := strings.TrimSpace(out)
    42  
    43  	stopCmd := exec.Command(dockerBinary, "stop", cleanedContainerID)
    44  	out, _, err = runCommandWithOutput(stopCmd)
    45  	c.Assert(err, check.IsNil)
    46  
    47  	killCmd := exec.Command(dockerBinary, "kill", "-s", "30", cleanedContainerID)
    48  	_, _, err = runCommandWithOutput(killCmd)
    49  	c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID))
    50  }
    51  
    52  func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
    53  	runCmd := exec.Command(dockerBinary, "run", "-u", "daemon", "-d", "busybox", "top")
    54  	out, _, err := runCommandWithOutput(runCmd)
    55  	if err != nil {
    56  		c.Fatal(out, err)
    57  	}
    58  
    59  	cleanedContainerID := strings.TrimSpace(out)
    60  	c.Assert(waitRun(cleanedContainerID), check.IsNil)
    61  
    62  	killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
    63  	if out, _, err = runCommandWithOutput(killCmd); err != nil {
    64  		c.Fatalf("failed to kill container: %s, %v", out, err)
    65  	}
    66  
    67  	listRunningContainersCmd := exec.Command(dockerBinary, "ps", "-q")
    68  	out, _, err = runCommandWithOutput(listRunningContainersCmd)
    69  	if err != nil {
    70  		c.Fatalf("failed to list running containers: %s, %v", out, err)
    71  	}
    72  
    73  	if strings.Contains(out, cleanedContainerID) {
    74  		c.Fatal("killed container is still running")
    75  	}
    76  }
    77  
    78  // regression test about correct signal parsing see #13665
    79  func (s *DockerSuite) TestKillWithSignal(c *check.C) {
    80  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
    81  	out, _, err := runCommandWithOutput(runCmd)
    82  	c.Assert(err, check.IsNil)
    83  
    84  	cid := strings.TrimSpace(out)
    85  	c.Assert(waitRun(cid), check.IsNil)
    86  
    87  	killCmd := exec.Command(dockerBinary, "kill", "-s", "SIGWINCH", cid)
    88  	_, err = runCommand(killCmd)
    89  	c.Assert(err, check.IsNil)
    90  
    91  	running, err := inspectField(cid, "State.Running")
    92  	if running != "true" {
    93  		c.Fatal("Container should be in running state after SIGWINCH")
    94  	}
    95  }
    96  
    97  func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) {
    98  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
    99  	out, _, err := runCommandWithOutput(runCmd)
   100  	c.Assert(err, check.IsNil)
   101  
   102  	cid := strings.TrimSpace(out)
   103  	c.Assert(waitRun(cid), check.IsNil)
   104  
   105  	killCmd := exec.Command(dockerBinary, "kill", "-s", "0", cid)
   106  	out, _, err = runCommandWithOutput(killCmd)
   107  	c.Assert(err, check.NotNil)
   108  	if !strings.ContainsAny(out, "Invalid signal: 0") {
   109  		c.Fatal("Kill with an invalid signal didn't error out correctly")
   110  	}
   111  
   112  	running, err := inspectField(cid, "State.Running")
   113  	if running != "true" {
   114  		c.Fatal("Container should be in running state after an invalid signal")
   115  	}
   116  
   117  	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
   118  	out, _, err = runCommandWithOutput(runCmd)
   119  	c.Assert(err, check.IsNil)
   120  
   121  	cid = strings.TrimSpace(out)
   122  	c.Assert(waitRun(cid), check.IsNil)
   123  
   124  	killCmd = exec.Command(dockerBinary, "kill", "-s", "SIG42", cid)
   125  	out, _, err = runCommandWithOutput(killCmd)
   126  	c.Assert(err, check.NotNil)
   127  	if !strings.ContainsAny(out, "Invalid signal: SIG42") {
   128  		c.Fatal("Kill with an invalid signal error out correctly")
   129  	}
   130  
   131  	running, err = inspectField(cid, "State.Running")
   132  	if running != "true" {
   133  		c.Fatal("Container should be in running state after an invalid signal")
   134  	}
   135  }