2014-02-25 11:17:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-07-28 14:36:38 -04:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2014-02-25 11:17:48 -05:00
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-02-25 11:17:48 -05:00
|
|
|
)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestKillContainer(c *check.C) {
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-05-16 10:59:53 -04:00
|
|
|
c.Assert(waitRun(cleanedContainerID), check.IsNil)
|
2014-02-25 11:17:48 -05:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
dockerCmd(c, "kill", cleanedContainerID)
|
2014-02-25 11:17:48 -05:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ = dockerCmd(c, "ps", "-q")
|
2014-02-25 11:17:48 -05:00
|
|
|
if strings.Contains(out, cleanedContainerID) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("killed container is still running")
|
2014-02-25 11:17:48 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-27 19:34:37 -04:00
|
|
|
|
2015-04-13 11:17:07 -04:00
|
|
|
func (s *DockerSuite) TestKillofStoppedContainer(c *check.C) {
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
|
2015-04-13 11:17:07 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
dockerCmd(c, "stop", cleanedContainerID)
|
2015-04-13 11:17:07 -04:00
|
|
|
|
2015-07-27 14:13:25 -04:00
|
|
|
_, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID)
|
2015-05-27 16:31:54 -04:00
|
|
|
c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID))
|
2015-04-13 11:17:07 -04:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top")
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-05-16 10:59:53 -04:00
|
|
|
c.Assert(waitRun(cleanedContainerID), check.IsNil)
|
2014-08-27 19:34:37 -04:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
dockerCmd(c, "kill", cleanedContainerID)
|
2014-08-27 19:34:37 -04:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ = dockerCmd(c, "ps", "-q")
|
2014-08-27 19:34:37 -04:00
|
|
|
if strings.Contains(out, cleanedContainerID) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("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) {
|
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)
|
2015-06-02 11:00:44 -04:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
running, _ := inspectField(cid, "State.Running")
|
2015-06-02 11:00:44 -04:00
|
|
|
if running != "true" {
|
|
|
|
c.Fatal("Container should be in running state after SIGWINCH")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) {
|
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-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)
|
|
|
|
if !strings.ContainsAny(out, "Invalid signal: 0") {
|
|
|
|
c.Fatal("Kill with an invalid signal didn't error out correctly")
|
|
|
|
}
|
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
running, _ := inspectField(cid, "State.Running")
|
2015-06-02 11:00:44 -04:00
|
|
|
if running != "true" {
|
|
|
|
c.Fatal("Container should be in running state after an invalid signal")
|
|
|
|
}
|
|
|
|
|
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-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)
|
|
|
|
if !strings.ContainsAny(out, "Invalid signal: SIG42") {
|
|
|
|
c.Fatal("Kill with an invalid signal error out correctly")
|
|
|
|
}
|
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
running, _ = inspectField(cid, "State.Running")
|
2015-06-02 11:00:44 -04:00
|
|
|
if running != "true" {
|
|
|
|
c.Fatal("Container should be in running state after an invalid signal")
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 14:36:38 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestKillofStoppedContainerAPIPre120(c *check.C) {
|
|
|
|
dockerCmd(c, "run", "--name", "docker-kill-test-api", "-d", "busybox", "top")
|
|
|
|
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)
|
|
|
|
}
|