6db32fde |
package main
import (
"strings" |
ff42a1ab |
"time" |
dc944ea7 |
|
33968e6c |
"github.com/docker/docker/integration-cli/checker" |
db35c2a5 |
"github.com/docker/docker/integration-cli/cli" |
86f9eb4a |
"github.com/docker/docker/integration-cli/request" |
dc944ea7 |
"github.com/go-check/check" |
92427b3a |
"github.com/gotestyourself/gotestyourself/icmd" |
0fd5a654 |
"golang.org/x/net/context" |
6db32fde |
)
|
dc944ea7 |
func (s *DockerSuite) TestKillContainer(c *check.C) { |
a899aa67 |
out := runSleepingContainer(c, "-d") |
475c6531 |
cleanedContainerID := strings.TrimSpace(out) |
db35c2a5 |
cli.WaitRun(c, cleanedContainerID) |
6db32fde |
|
db35c2a5 |
cli.DockerCmd(c, "kill", cleanedContainerID)
cli.WaitExited(c, cleanedContainerID, 10*time.Second) |
6db32fde |
|
db35c2a5 |
out = cli.DockerCmd(c, "ps", "-q").Combined() |
d0762a60 |
c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
|
6db32fde |
} |
eb9379c5 |
|
03e2ff32 |
func (s *DockerSuite) TestKillOffStoppedContainer(c *check.C) { |
a899aa67 |
out := runSleepingContainer(c, "-d") |
c92377e3 |
cleanedContainerID := strings.TrimSpace(out)
|
db35c2a5 |
cli.DockerCmd(c, "stop", cleanedContainerID)
cli.WaitExited(c, cleanedContainerID, 10*time.Second) |
c92377e3 |
|
db35c2a5 |
cli.Docker(cli.Args("kill", "-s", "30", cleanedContainerID)).Assert(c, icmd.Expected{
ExitCode: 1,
}) |
c92377e3 |
}
|
dc944ea7 |
func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) { |
03e2ff32 |
// TODO Windows: Windows does not yet support -u (Feb 2016). |
f9a3558a |
testRequires(c, DaemonIsLinux) |
db35c2a5 |
out := cli.DockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top").Combined() |
475c6531 |
cleanedContainerID := strings.TrimSpace(out) |
db35c2a5 |
cli.WaitRun(c, cleanedContainerID) |
eb9379c5 |
|
db35c2a5 |
cli.DockerCmd(c, "kill", cleanedContainerID)
cli.WaitExited(c, cleanedContainerID, 10*time.Second) |
eb9379c5 |
|
db35c2a5 |
out = cli.DockerCmd(c, "ps", "-q").Combined() |
d0762a60 |
c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
|
eb9379c5 |
} |
39eec4c2 |
// regression test about correct signal parsing see #13665
func (s *DockerSuite) TestKillWithSignal(c *check.C) { |
d2e64247 |
// Cannot port to Windows - does not support signals in the same way Linux does |
f9a3558a |
testRequires(c, DaemonIsLinux) |
668e2369 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "top") |
39eec4c2 |
cid := strings.TrimSpace(out)
c.Assert(waitRun(cid), check.IsNil)
|
668e2369 |
dockerCmd(c, "kill", "-s", "SIGWINCH", cid) |
ff42a1ab |
time.Sleep(250 * time.Millisecond) |
39eec4c2 |
|
62a856e9 |
running := inspectField(c, cid, "State.Running") |
d0762a60 |
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after SIGWINCH")) |
39eec4c2 |
}
|
d2e64247 |
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) |
db35c2a5 |
out := cli.DockerCmd(c, "run", "-d", "--stop-signal=TERM", "--restart=always", "busybox", "top").Combined() |
d2e64247 |
cid := strings.TrimSpace(out) |
db35c2a5 |
cli.WaitRun(c, cid) |
d2e64247 |
|
ff42a1ab |
// Let docker send a TERM signal to the container
// It will kill the process and disable the restart policy |
db35c2a5 |
cli.DockerCmd(c, "kill", "-s", "TERM", cid)
cli.WaitExited(c, cid, 10*time.Second) |
d2e64247 |
|
db35c2a5 |
out = cli.DockerCmd(c, "ps", "-q").Combined() |
d2e64247 |
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) |
db35c2a5 |
out := cli.DockerCmd(c, "run", "-d", "--stop-signal=CONT", "--restart=always", "busybox", "top").Combined() |
d2e64247 |
cid := strings.TrimSpace(out) |
db35c2a5 |
cli.WaitRun(c, cid) |
d2e64247 |
|
ff42a1ab |
// Let docker send a TERM signal to the container |
d2e64247 |
// It will kill the process, but not disable the restart policy |
db35c2a5 |
cli.DockerCmd(c, "kill", "-s", "TERM", cid)
cli.WaitRestart(c, cid, 10*time.Second) |
d2e64247 |
// Restart policy should still be in place, so it should be still running |
db35c2a5 |
cli.WaitRun(c, cid) |
d2e64247 |
}
// FIXME(vdemeester) should be a unit test |
39eec4c2 |
func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) { |
a899aa67 |
out := runSleepingContainer(c, "-d") |
39eec4c2 |
cid := strings.TrimSpace(out)
c.Assert(waitRun(cid), check.IsNil)
|
693ba98c |
out, _, err := dockerCmdWithError("kill", "-s", "0", cid) |
39eec4c2 |
c.Assert(err, check.NotNil) |
d0762a60 |
c.Assert(out, checker.Contains, "Invalid signal: 0", check.Commentf("Kill with an invalid signal didn't error out correctly")) |
39eec4c2 |
|
62a856e9 |
running := inspectField(c, cid, "State.Running") |
d0762a60 |
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal")) |
39eec4c2 |
|
a899aa67 |
out = runSleepingContainer(c, "-d") |
39eec4c2 |
cid = strings.TrimSpace(out)
c.Assert(waitRun(cid), check.IsNil)
|
693ba98c |
out, _, err = dockerCmdWithError("kill", "-s", "SIG42", cid) |
39eec4c2 |
c.Assert(err, check.NotNil) |
d0762a60 |
c.Assert(out, checker.Contains, "Invalid signal: SIG42", check.Commentf("Kill with an invalid signal error out correctly")) |
39eec4c2 |
|
62a856e9 |
running = inspectField(c, cid, "State.Running") |
d0762a60 |
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
|
39eec4c2 |
} |
621e3d85 |
|
8c63ce4f |
func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) { |
f811d5b1 |
testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later |
03e2ff32 |
runSleepingContainer(c, "--name", "docker-kill-test-api", "-d") |
621e3d85 |
dockerCmd(c, "stop", "docker-kill-test-api") |
86f9eb4a |
cli, err := request.NewEnvClientWithVersion("v1.19") |
0fd5a654 |
c.Assert(err, check.IsNil)
defer cli.Close()
err = cli.ContainerKill(context.Background(), "docker-kill-test-api", "SIGKILL") |
621e3d85 |
c.Assert(err, check.IsNil)
} |