github.com/hayajo/docker@v1.9.1/integration-cli/docker_cli_kill_test.go (about)

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