2014-02-25 20:17:48 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-07-28 21:36:38 +03:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2014-02-25 20:17:48 +04:00
|
|
|
"strings"
|
2015-04-18 19:46:47 +03:00
|
|
|
|
2015-10-28 07:18:26 +03:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-04-18 19:46:47 +03:00
|
|
|
"github.com/go-check/check"
|
2014-02-25 20:17:48 +04:00
|
|
|
)
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestKillContainer(c *check.C) {
|
2016-02-25 00:33:25 +03:00
|
|
|
out, _ := runSleepingContainer(c, "-d")
|
2015-04-06 16:21:18 +03:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-05-16 17:59:53 +03:00
|
|
|
c.Assert(waitRun(cleanedContainerID), check.IsNil)
|
2014-02-25 20:17:48 +04:00
|
|
|
|
2015-07-20 09:55:40 +03:00
|
|
|
dockerCmd(c, "kill", cleanedContainerID)
|
2014-02-25 20:17:48 +04:00
|
|
|
|
2015-07-20 09:55:40 +03:00
|
|
|
out, _ = dockerCmd(c, "ps", "-q")
|
2015-10-28 07:18:26 +03:00
|
|
|
c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
|
|
|
|
|
2014-02-25 20:17:48 +04:00
|
|
|
}
|
2014-08-28 03:34:37 +04:00
|
|
|
|
2016-02-25 00:33:25 +03:00
|
|
|
func (s *DockerSuite) TestKillOffStoppedContainer(c *check.C) {
|
|
|
|
out, _ := runSleepingContainer(c, "-d")
|
2015-04-13 18:17:07 +03:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
|
|
|
|
2015-07-20 09:55:40 +03:00
|
|
|
dockerCmd(c, "stop", cleanedContainerID)
|
2015-04-13 18:17:07 +03:00
|
|
|
|
2015-07-27 21:13:25 +03:00
|
|
|
_, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID)
|
2015-05-27 23:31:54 +03:00
|
|
|
c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID))
|
2015-04-13 18:17:07 +03:00
|
|
|
}
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
|
2016-02-25 00:33:25 +03:00
|
|
|
// TODO Windows: Windows does not yet support -u (Feb 2016).
|
2015-08-28 20:36:42 +03:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 09:55:40 +03:00
|
|
|
out, _ := dockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top")
|
2015-04-06 16:21:18 +03:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-05-16 17:59:53 +03:00
|
|
|
c.Assert(waitRun(cleanedContainerID), check.IsNil)
|
2014-08-28 03:34:37 +04:00
|
|
|
|
2015-07-20 09:55:40 +03:00
|
|
|
dockerCmd(c, "kill", cleanedContainerID)
|
2014-08-28 03:34:37 +04:00
|
|
|
|
2015-07-20 09:55:40 +03:00
|
|
|
out, _ = dockerCmd(c, "ps", "-q")
|
2015-10-28 07:18:26 +03:00
|
|
|
c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
|
|
|
|
|
2014-08-28 03:34:37 +04:00
|
|
|
}
|
2015-06-02 18:00:44 +03:00
|
|
|
|
|
|
|
// regression test about correct signal parsing see #13665
|
|
|
|
func (s *DockerSuite) TestKillWithSignal(c *check.C) {
|
2016-02-25 00:33:25 +03:00
|
|
|
// Cannot port to Windows - does not support signals in the same was a Linux does
|
2015-08-28 20:36:42 +03:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 09:55:40 +03:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
|
2015-06-02 18:00:44 +03:00
|
|
|
cid := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2015-07-20 09:55:40 +03:00
|
|
|
dockerCmd(c, "kill", "-s", "SIGWINCH", cid)
|
2015-06-02 18:00:44 +03:00
|
|
|
|
2016-01-28 17:19:25 +03:00
|
|
|
running := inspectField(c, cid, "State.Running")
|
2015-10-28 07:18:26 +03:00
|
|
|
|
|
|
|
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after SIGWINCH"))
|
2015-06-02 18:00:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) {
|
2016-02-25 00:33:25 +03:00
|
|
|
out, _ := runSleepingContainer(c, "-d")
|
2015-06-02 18:00:44 +03:00
|
|
|
cid := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2015-07-27 21:13:25 +03:00
|
|
|
out, _, err := dockerCmdWithError("kill", "-s", "0", cid)
|
2015-06-02 18:00:44 +03:00
|
|
|
c.Assert(err, check.NotNil)
|
2015-10-28 07:18:26 +03:00
|
|
|
c.Assert(out, checker.Contains, "Invalid signal: 0", check.Commentf("Kill with an invalid signal didn't error out correctly"))
|
2015-06-02 18:00:44 +03:00
|
|
|
|
2016-01-28 17:19:25 +03:00
|
|
|
running := inspectField(c, cid, "State.Running")
|
2015-10-28 07:18:26 +03:00
|
|
|
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
|
2015-06-02 18:00:44 +03:00
|
|
|
|
2016-02-25 00:33:25 +03:00
|
|
|
out, _ = runSleepingContainer(c, "-d")
|
2015-06-02 18:00:44 +03:00
|
|
|
cid = strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2015-07-27 21:13:25 +03:00
|
|
|
out, _, err = dockerCmdWithError("kill", "-s", "SIG42", cid)
|
2015-06-02 18:00:44 +03:00
|
|
|
c.Assert(err, check.NotNil)
|
2015-10-28 07:18:26 +03:00
|
|
|
c.Assert(out, checker.Contains, "Invalid signal: SIG42", check.Commentf("Kill with an invalid signal error out correctly"))
|
2015-06-02 18:00:44 +03:00
|
|
|
|
2016-01-28 17:19:25 +03:00
|
|
|
running = inspectField(c, cid, "State.Running")
|
2015-10-28 07:18:26 +03:00
|
|
|
c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
|
|
|
|
|
2015-06-02 18:00:44 +03:00
|
|
|
}
|
2015-07-28 21:36:38 +03:00
|
|
|
|
2015-10-13 10:09:05 +03:00
|
|
|
func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) {
|
2016-02-25 00:33:25 +03:00
|
|
|
runSleepingContainer(c, "--name", "docker-kill-test-api", "-d")
|
2015-07-28 21:36:38 +03: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)
|
|
|
|
}
|