github.com/getong/docker@v1.13.1/integration-cli/docker_cli_kill_test.go (about)

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