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

     1  package main
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/go-check/check"
     7  )
     8  
     9  func (s *DockerSuite) TestKillContainer(c *check.C) {
    10  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    11  	cleanedContainerID := strings.TrimSpace(out)
    12  	c.Assert(waitRun(cleanedContainerID), check.IsNil)
    13  
    14  	dockerCmd(c, "kill", cleanedContainerID)
    15  
    16  	out, _ = dockerCmd(c, "ps", "-q")
    17  	if strings.Contains(out, cleanedContainerID) {
    18  		c.Fatal("killed container is still running")
    19  	}
    20  }
    21  
    22  func (s *DockerSuite) TestKillofStoppedContainer(c *check.C) {
    23  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    24  	cleanedContainerID := strings.TrimSpace(out)
    25  
    26  	dockerCmd(c, "stop", cleanedContainerID)
    27  
    28  	_, _, err := dockerCmdWithError(c, "kill", "-s", "30", cleanedContainerID)
    29  	c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID))
    30  }
    31  
    32  func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
    33  	out, _ := dockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top")
    34  	cleanedContainerID := strings.TrimSpace(out)
    35  	c.Assert(waitRun(cleanedContainerID), check.IsNil)
    36  
    37  	dockerCmd(c, "kill", cleanedContainerID)
    38  
    39  	out, _ = dockerCmd(c, "ps", "-q")
    40  	if strings.Contains(out, cleanedContainerID) {
    41  		c.Fatal("killed container is still running")
    42  	}
    43  }
    44  
    45  // regression test about correct signal parsing see #13665
    46  func (s *DockerSuite) TestKillWithSignal(c *check.C) {
    47  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    48  	cid := strings.TrimSpace(out)
    49  	c.Assert(waitRun(cid), check.IsNil)
    50  
    51  	dockerCmd(c, "kill", "-s", "SIGWINCH", cid)
    52  
    53  	running, _ := inspectField(cid, "State.Running")
    54  	if running != "true" {
    55  		c.Fatal("Container should be in running state after SIGWINCH")
    56  	}
    57  }
    58  
    59  func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) {
    60  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    61  	cid := strings.TrimSpace(out)
    62  	c.Assert(waitRun(cid), check.IsNil)
    63  
    64  	out, _, err := dockerCmdWithError(c, "kill", "-s", "0", cid)
    65  	c.Assert(err, check.NotNil)
    66  	if !strings.ContainsAny(out, "Invalid signal: 0") {
    67  		c.Fatal("Kill with an invalid signal didn't error out correctly")
    68  	}
    69  
    70  	running, _ := inspectField(cid, "State.Running")
    71  	if running != "true" {
    72  		c.Fatal("Container should be in running state after an invalid signal")
    73  	}
    74  
    75  	out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
    76  	cid = strings.TrimSpace(out)
    77  	c.Assert(waitRun(cid), check.IsNil)
    78  
    79  	out, _, err = dockerCmdWithError(c, "kill", "-s", "SIG42", cid)
    80  	c.Assert(err, check.NotNil)
    81  	if !strings.ContainsAny(out, "Invalid signal: SIG42") {
    82  		c.Fatal("Kill with an invalid signal error out correctly")
    83  	}
    84  
    85  	running, _ = inspectField(cid, "State.Running")
    86  	if running != "true" {
    87  		c.Fatal("Container should be in running state after an invalid signal")
    88  	}
    89  }