2014-02-25 11:17:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2016-11-09 17:43:08 -05:00
|
|
|
"time"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2017-04-11 15:18:30 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2017-09-12 08:53:20 -04:00
|
|
|
"github.com/docker/docker/integration-cli/request"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2017-08-23 17:01:29 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/icmd"
|
2017-05-23 23:56:26 -04:00
|
|
|
"golang.org/x/net/context"
|
2014-02-25 11:17:48 -05:00
|
|
|
)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestKillContainer(c *check.C) {
|
2017-04-16 17:39:30 -04:00
|
|
|
out := runSleepingContainer(c, "-d")
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.WaitRun(c, cleanedContainerID)
|
2014-02-25 11:17:48 -05:00
|
|
|
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.DockerCmd(c, "kill", cleanedContainerID)
|
|
|
|
cli.WaitExited(c, cleanedContainerID, 10*time.Second)
|
2014-02-25 11:17:48 -05:00
|
|
|
|
2017-04-11 15:18:30 -04:00
|
|
|
out = cli.DockerCmd(c, "ps", "-q").Combined()
|
2015-10-28 00:18:26 -04:00
|
|
|
c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
|
|
|
|
|
2014-02-25 11:17:48 -05:00
|
|
|
}
|
2014-08-27 19:34:37 -04:00
|
|
|
|
2016-02-24 16:33:25 -05:00
|
|
|
func (s *DockerSuite) TestKillOffStoppedContainer(c *check.C) {
|
2017-04-16 17:39:30 -04:00
|
|
|
out := runSleepingContainer(c, "-d")
|
2015-04-13 11:17:07 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
|
|
|
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.DockerCmd(c, "stop", cleanedContainerID)
|
|
|
|
cli.WaitExited(c, cleanedContainerID, 10*time.Second)
|
2015-04-13 11:17:07 -04:00
|
|
|
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.Docker(cli.Args("kill", "-s", "30", cleanedContainerID)).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
})
|
2015-04-13 11:17:07 -04:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
|
2016-02-24 16:33:25 -05:00
|
|
|
// TODO Windows: Windows does not yet support -u (Feb 2016).
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-04-11 15:18:30 -04:00
|
|
|
out := cli.DockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top").Combined()
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.WaitRun(c, cleanedContainerID)
|
2014-08-27 19:34:37 -04:00
|
|
|
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.DockerCmd(c, "kill", cleanedContainerID)
|
|
|
|
cli.WaitExited(c, cleanedContainerID, 10*time.Second)
|
2014-08-27 19:34:37 -04:00
|
|
|
|
2017-04-11 15:18:30 -04:00
|
|
|
out = cli.DockerCmd(c, "ps", "-q").Combined()
|
2015-10-28 00:18:26 -04:00
|
|
|
c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
|
|
|
|
|
2014-08-27 19:34:37 -04:00
|
|
|
}
|
2015-06-02 11:00:44 -04:00
|
|
|
|
|
|
|
// regression test about correct signal parsing see #13665
|
|
|
|
func (s *DockerSuite) TestKillWithSignal(c *check.C) {
|
2016-10-24 14:10:14 -04:00
|
|
|
// Cannot port to Windows - does not support signals in the same way Linux does
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
|
2015-06-02 11:00:44 -04:00
|
|
|
cid := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
dockerCmd(c, "kill", "-s", "SIGWINCH", cid)
|
2016-11-09 17:43:08 -05:00
|
|
|
time.Sleep(250 * time.Millisecond)
|
2015-06-02 11:00:44 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
running := inspectField(c, cid, "State.Running")
|
2015-10-28 00:18:26 -04:00
|
|
|
|
|
|
|
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after SIGWINCH"))
|
2015-06-02 11:00:44 -04:00
|
|
|
}
|
|
|
|
|
2016-10-24 14:10:14 -04:00
|
|
|
func (s *DockerSuite) TestKillWithStopSignalWithSameSignalShouldDisableRestartPolicy(c *check.C) {
|
|
|
|
// Cannot port to Windows - does not support signals int the same way as Linux does
|
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-04-11 15:18:30 -04:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "--stop-signal=TERM", "--restart=always", "busybox", "top").Combined()
|
2016-10-24 14:10:14 -04:00
|
|
|
cid := strings.TrimSpace(out)
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.WaitRun(c, cid)
|
2016-10-24 14:10:14 -04:00
|
|
|
|
2016-11-09 17:43:08 -05:00
|
|
|
// Let docker send a TERM signal to the container
|
|
|
|
// It will kill the process and disable the restart policy
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.DockerCmd(c, "kill", "-s", "TERM", cid)
|
|
|
|
cli.WaitExited(c, cid, 10*time.Second)
|
2016-10-24 14:10:14 -04:00
|
|
|
|
2017-04-11 15:18:30 -04:00
|
|
|
out = cli.DockerCmd(c, "ps", "-q").Combined()
|
2016-10-24 14:10:14 -04:00
|
|
|
c.Assert(out, checker.Not(checker.Contains), cid, check.Commentf("killed container is still running"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestKillWithStopSignalWithDifferentSignalShouldKeepRestartPolicy(c *check.C) {
|
|
|
|
// Cannot port to Windows - does not support signals int the same way as Linux does
|
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-04-11 15:18:30 -04:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "--stop-signal=CONT", "--restart=always", "busybox", "top").Combined()
|
2016-10-24 14:10:14 -04:00
|
|
|
cid := strings.TrimSpace(out)
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.WaitRun(c, cid)
|
2016-10-24 14:10:14 -04:00
|
|
|
|
2016-11-09 17:43:08 -05:00
|
|
|
// Let docker send a TERM signal to the container
|
2016-10-24 14:10:14 -04:00
|
|
|
// It will kill the process, but not disable the restart policy
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.DockerCmd(c, "kill", "-s", "TERM", cid)
|
|
|
|
cli.WaitRestart(c, cid, 10*time.Second)
|
2016-10-24 14:10:14 -04:00
|
|
|
|
|
|
|
// Restart policy should still be in place, so it should be still running
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.WaitRun(c, cid)
|
2016-10-24 14:10:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(vdemeester) should be a unit test
|
2015-06-02 11:00:44 -04:00
|
|
|
func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) {
|
2017-04-16 17:39:30 -04:00
|
|
|
out := runSleepingContainer(c, "-d")
|
2015-06-02 11:00:44 -04:00
|
|
|
cid := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2015-07-27 14:13:25 -04:00
|
|
|
out, _, err := dockerCmdWithError("kill", "-s", "0", cid)
|
2015-06-02 11:00:44 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
2015-10-28 00:18:26 -04:00
|
|
|
c.Assert(out, checker.Contains, "Invalid signal: 0", check.Commentf("Kill with an invalid signal didn't error out correctly"))
|
2015-06-02 11:00:44 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
running := inspectField(c, cid, "State.Running")
|
2015-10-28 00:18:26 -04:00
|
|
|
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
|
2015-06-02 11:00:44 -04:00
|
|
|
|
2017-04-16 17:39:30 -04:00
|
|
|
out = runSleepingContainer(c, "-d")
|
2015-06-02 11:00:44 -04:00
|
|
|
cid = strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2015-07-27 14:13:25 -04:00
|
|
|
out, _, err = dockerCmdWithError("kill", "-s", "SIG42", cid)
|
2015-06-02 11:00:44 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
2015-10-28 00:18:26 -04:00
|
|
|
c.Assert(out, checker.Contains, "Invalid signal: SIG42", check.Commentf("Kill with an invalid signal error out correctly"))
|
2015-06-02 11:00:44 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
running = inspectField(c, cid, "State.Running")
|
2015-10-28 00:18:26 -04:00
|
|
|
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
|
|
|
|
|
2015-06-02 11:00:44 -04:00
|
|
|
}
|
2015-07-28 14:36:38 -04:00
|
|
|
|
2015-10-13 03:09:05 -04:00
|
|
|
func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) {
|
2016-10-31 13:15:43 -04:00
|
|
|
testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
|
2016-02-24 16:33:25 -05:00
|
|
|
runSleepingContainer(c, "--name", "docker-kill-test-api", "-d")
|
2015-07-28 14:36:38 -04:00
|
|
|
dockerCmd(c, "stop", "docker-kill-test-api")
|
2017-09-12 08:53:20 -04:00
|
|
|
cli, err := request.NewEnvClientWithVersion("v1.19")
|
2017-05-23 23:56:26 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
defer cli.Close()
|
|
|
|
err = cli.ContainerKill(context.Background(), "docker-kill-test-api", "SIGKILL")
|
2015-07-28 14:36:38 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|