github.com/kim0/docker@v0.6.2-0.20161130212042-4addda3f07e7/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/docker/docker/pkg/integration/checker"
     9  	"github.com/go-check/check"
    10  )
    11  
    12  func (s *DockerSuite) TestKillContainer(c *check.C) {
    13  	out, _ := runSleepingContainer(c, "-d")
    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  	c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
    21  
    22  }
    23  
    24  func (s *DockerSuite) TestKillOffStoppedContainer(c *check.C) {
    25  	out, _ := runSleepingContainer(c, "-d")
    26  	cleanedContainerID := strings.TrimSpace(out)
    27  
    28  	dockerCmd(c, "stop", cleanedContainerID)
    29  
    30  	_, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID)
    31  	c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID))
    32  }
    33  
    34  func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
    35  	// TODO Windows: Windows does not yet support -u (Feb 2016).
    36  	testRequires(c, DaemonIsLinux)
    37  	out, _ := dockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top")
    38  	cleanedContainerID := strings.TrimSpace(out)
    39  	c.Assert(waitRun(cleanedContainerID), check.IsNil)
    40  
    41  	dockerCmd(c, "kill", cleanedContainerID)
    42  
    43  	out, _ = dockerCmd(c, "ps", "-q")
    44  	c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
    45  
    46  }
    47  
    48  // regression test about correct signal parsing see #13665
    49  func (s *DockerSuite) TestKillWithSignal(c *check.C) {
    50  	// Cannot port to Windows - does not support signals in the same way Linux does
    51  	testRequires(c, DaemonIsLinux)
    52  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
    53  	cid := strings.TrimSpace(out)
    54  	c.Assert(waitRun(cid), check.IsNil)
    55  
    56  	dockerCmd(c, "kill", "-s", "SIGWINCH", cid)
    57  
    58  	running := inspectField(c, cid, "State.Running")
    59  
    60  	c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after SIGWINCH"))
    61  }
    62  
    63  func (s *DockerSuite) TestKillWithStopSignalWithSameSignalShouldDisableRestartPolicy(c *check.C) {
    64  	// Cannot port to Windows - does not support signals int the same way as Linux does
    65  	testRequires(c, DaemonIsLinux)
    66  	out, _ := dockerCmd(c, "run", "-d", "--stop-signal=TERM", "busybox", "top")
    67  	cid := strings.TrimSpace(out)
    68  	c.Assert(waitRun(cid), check.IsNil)
    69  
    70  	// Let's docker send a CONT signal to the container
    71  	dockerCmd(c, "kill", "-s", "TERM", cid)
    72  
    73  	out, _ = dockerCmd(c, "ps", "-q")
    74  	c.Assert(out, checker.Not(checker.Contains), cid, check.Commentf("killed container is still running"))
    75  }
    76  
    77  func (s *DockerSuite) TestKillWithStopSignalWithDifferentSignalShouldKeepRestartPolicy(c *check.C) {
    78  	// Cannot port to Windows - does not support signals int the same way as Linux does
    79  	testRequires(c, DaemonIsLinux)
    80  	out, _ := dockerCmd(c, "run", "-d", "--stop-signal=CONT", "busybox", "top")
    81  	cid := strings.TrimSpace(out)
    82  	c.Assert(waitRun(cid), check.IsNil)
    83  
    84  	// Let's docker send a TERM signal to the container
    85  	// It will kill the process, but not disable the restart policy
    86  	dockerCmd(c, "kill", "-s", "TERM", cid)
    87  
    88  	// Restart policy should still be in place, so it should be still running
    89  	c.Assert(waitRun(cid), check.IsNil)
    90  }
    91  
    92  // FIXME(vdemeester) should be a unit test
    93  func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) {
    94  	out, _ := runSleepingContainer(c, "-d")
    95  	cid := strings.TrimSpace(out)
    96  	c.Assert(waitRun(cid), check.IsNil)
    97  
    98  	out, _, err := dockerCmdWithError("kill", "-s", "0", cid)
    99  	c.Assert(err, check.NotNil)
   100  	c.Assert(out, checker.Contains, "Invalid signal: 0", check.Commentf("Kill with an invalid signal didn't error out correctly"))
   101  
   102  	running := inspectField(c, cid, "State.Running")
   103  	c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
   104  
   105  	out, _ = runSleepingContainer(c, "-d")
   106  	cid = strings.TrimSpace(out)
   107  	c.Assert(waitRun(cid), check.IsNil)
   108  
   109  	out, _, err = dockerCmdWithError("kill", "-s", "SIG42", cid)
   110  	c.Assert(err, check.NotNil)
   111  	c.Assert(out, checker.Contains, "Invalid signal: SIG42", check.Commentf("Kill with an invalid signal error out correctly"))
   112  
   113  	running = inspectField(c, cid, "State.Running")
   114  	c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
   115  
   116  }
   117  
   118  func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) {
   119  	runSleepingContainer(c, "--name", "docker-kill-test-api", "-d")
   120  	dockerCmd(c, "stop", "docker-kill-test-api")
   121  
   122  	status, _, err := sockRequest("POST", fmt.Sprintf("/v1.19/containers/%s/kill", "docker-kill-test-api"), nil)
   123  	c.Assert(err, check.IsNil)
   124  	c.Assert(status, check.Equals, http.StatusNoContent)
   125  }