2014-02-25 16:17:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-07-28 18:36:38 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2014-02-25 16:17:48 +00:00
|
|
|
"strings"
|
2016-11-09 22:43:08 +00:00
|
|
|
"time"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2015-10-28 04:18:26 +00:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2014-02-25 16:17:48 +00:00
|
|
|
)
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestKillContainer(c *check.C) {
|
2016-02-24 21:33:25 +00:00
|
|
|
out, _ := runSleepingContainer(c, "-d")
|
2015-04-06 13:21:18 +00:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-05-16 14:59:53 +00:00
|
|
|
c.Assert(waitRun(cleanedContainerID), check.IsNil)
|
2014-02-25 16:17:48 +00:00
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
dockerCmd(c, "kill", cleanedContainerID)
|
2016-11-09 22:43:08 +00:00
|
|
|
c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
|
2014-02-25 16:17:48 +00:00
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ = dockerCmd(c, "ps", "-q")
|
2015-10-28 04:18:26 +00:00
|
|
|
c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
|
|
|
|
|
2014-02-25 16:17:48 +00:00
|
|
|
}
|
2014-08-27 23:34:37 +00:00
|
|
|
|
2016-02-24 21:33:25 +00:00
|
|
|
func (s *DockerSuite) TestKillOffStoppedContainer(c *check.C) {
|
|
|
|
out, _ := runSleepingContainer(c, "-d")
|
2015-04-13 15:17:07 +00:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
dockerCmd(c, "stop", cleanedContainerID)
|
2016-11-09 22:43:08 +00:00
|
|
|
c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
|
2015-04-13 15:17:07 +00:00
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
_, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID)
|
2015-05-27 20:31:54 +00:00
|
|
|
c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID))
|
2015-04-13 15:17:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
|
2016-02-24 21:33:25 +00:00
|
|
|
// TODO Windows: Windows does not yet support -u (Feb 2016).
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top")
|
2015-04-06 13:21:18 +00:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-05-16 14:59:53 +00:00
|
|
|
c.Assert(waitRun(cleanedContainerID), check.IsNil)
|
2014-08-27 23:34:37 +00:00
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
dockerCmd(c, "kill", cleanedContainerID)
|
2016-11-09 22:43:08 +00:00
|
|
|
c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
|
2014-08-27 23:34:37 +00:00
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ = dockerCmd(c, "ps", "-q")
|
2015-10-28 04:18:26 +00:00
|
|
|
c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
|
|
|
|
|
2014-08-27 23:34:37 +00:00
|
|
|
}
|
2015-06-02 15:00:44 +00:00
|
|
|
|
|
|
|
// regression test about correct signal parsing see #13665
|
|
|
|
func (s *DockerSuite) TestKillWithSignal(c *check.C) {
|
2016-10-24 18:10:14 +00:00
|
|
|
// Cannot port to Windows - does not support signals in the same way Linux does
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
|
2015-06-02 15:00:44 +00:00
|
|
|
cid := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
dockerCmd(c, "kill", "-s", "SIGWINCH", cid)
|
2016-11-09 22:43:08 +00:00
|
|
|
time.Sleep(250 * time.Millisecond)
|
2015-06-02 15:00:44 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
running := inspectField(c, cid, "State.Running")
|
2015-10-28 04:18:26 +00:00
|
|
|
|
|
|
|
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after SIGWINCH"))
|
2015-06-02 15:00:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 18:10:14 +00: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)
|
2016-11-09 23:13:42 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--stop-signal=TERM", "--restart=always", "busybox", "top")
|
2016-10-24 18:10:14 +00:00
|
|
|
cid := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2016-11-09 22:43:08 +00:00
|
|
|
// Let docker send a TERM signal to the container
|
|
|
|
// It will kill the process and disable the restart policy
|
2016-10-24 18:10:14 +00:00
|
|
|
dockerCmd(c, "kill", "-s", "TERM", cid)
|
2016-11-09 22:43:08 +00:00
|
|
|
c.Assert(waitExited(cid, 10*time.Second), check.IsNil)
|
2016-10-24 18:10:14 +00:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "ps", "-q")
|
|
|
|
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)
|
2016-11-09 23:13:42 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--stop-signal=CONT", "--restart=always", "busybox", "top")
|
2016-10-24 18:10:14 +00:00
|
|
|
cid := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2016-11-09 22:43:08 +00:00
|
|
|
// Let docker send a TERM signal to the container
|
2016-10-24 18:10:14 +00:00
|
|
|
// It will kill the process, but not disable the restart policy
|
|
|
|
dockerCmd(c, "kill", "-s", "TERM", cid)
|
2016-11-09 22:43:08 +00:00
|
|
|
c.Assert(waitRestart(cid, 10*time.Second), check.IsNil)
|
2016-10-24 18:10:14 +00:00
|
|
|
|
|
|
|
// Restart policy should still be in place, so it should be still running
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(vdemeester) should be a unit test
|
2015-06-02 15:00:44 +00:00
|
|
|
func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) {
|
2016-02-24 21:33:25 +00:00
|
|
|
out, _ := runSleepingContainer(c, "-d")
|
2015-06-02 15:00:44 +00:00
|
|
|
cid := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("kill", "-s", "0", cid)
|
2015-06-02 15:00:44 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2015-10-28 04:18:26 +00:00
|
|
|
c.Assert(out, checker.Contains, "Invalid signal: 0", check.Commentf("Kill with an invalid signal didn't error out correctly"))
|
2015-06-02 15:00:44 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
running := inspectField(c, cid, "State.Running")
|
2015-10-28 04:18:26 +00:00
|
|
|
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
|
2015-06-02 15:00:44 +00:00
|
|
|
|
2016-02-24 21:33:25 +00:00
|
|
|
out, _ = runSleepingContainer(c, "-d")
|
2015-06-02 15:00:44 +00:00
|
|
|
cid = strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err = dockerCmdWithError("kill", "-s", "SIG42", cid)
|
2015-06-02 15:00:44 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
2015-10-28 04:18:26 +00:00
|
|
|
c.Assert(out, checker.Contains, "Invalid signal: SIG42", check.Commentf("Kill with an invalid signal error out correctly"))
|
2015-06-02 15:00:44 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
running = inspectField(c, cid, "State.Running")
|
2015-10-28 04:18:26 +00:00
|
|
|
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
|
|
|
|
|
2015-06-02 15:00:44 +00:00
|
|
|
}
|
2015-07-28 18:36:38 +00:00
|
|
|
|
2015-10-13 07:09:05 +00:00
|
|
|
func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) {
|
2016-10-31 17:15:43 +00:00
|
|
|
testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
|
2016-02-24 21:33:25 +00:00
|
|
|
runSleepingContainer(c, "--name", "docker-kill-test-api", "-d")
|
2015-07-28 18:36:38 +00:00
|
|
|
dockerCmd(c, "stop", "docker-kill-test-api")
|
|
|
|
|
|
|
|
status, _, err := sockRequest("POST", fmt.Sprintf("/v1.19/containers/%s/kill", "docker-kill-test-api"), nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(status, check.Equals, http.StatusNoContent)
|
|
|
|
}
|