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