integration-cli/docker_cli_kill_test.go
6db32fde
 package main
 
 import (
621e3d85
 	"fmt"
 	"net/http"
6db32fde
 	"strings"
dc944ea7
 
d0762a60
 	"github.com/docker/docker/pkg/integration/checker"
dc944ea7
 	"github.com/go-check/check"
6db32fde
 )
 
dc944ea7
 func (s *DockerSuite) TestKillContainer(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
a268e367
 	c.Assert(waitRun(cleanedContainerID), check.IsNil)
6db32fde
 
668e2369
 	dockerCmd(c, "kill", cleanedContainerID)
6db32fde
 
668e2369
 	out, _ = dockerCmd(c, "ps", "-q")
d0762a60
 	c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
 
6db32fde
 }
eb9379c5
 
c92377e3
 func (s *DockerSuite) TestKillofStoppedContainer(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
c92377e3
 	cleanedContainerID := strings.TrimSpace(out)
 
668e2369
 	dockerCmd(c, "stop", cleanedContainerID)
c92377e3
 
693ba98c
 	_, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID)
35a7f0c5
 	c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID))
c92377e3
 }
 
dc944ea7
 func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	out, _ := dockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top")
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
a268e367
 	c.Assert(waitRun(cleanedContainerID), check.IsNil)
eb9379c5
 
668e2369
 	dockerCmd(c, "kill", cleanedContainerID)
eb9379c5
 
668e2369
 	out, _ = dockerCmd(c, "ps", "-q")
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) {
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)
39eec4c2
 
668e2369
 	running, _ := inspectField(cid, "State.Running")
d0762a60
 
 	c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after SIGWINCH"))
39eec4c2
 }
 
 func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
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
 
668e2369
 	running, _ := inspectField(cid, "State.Running")
d0762a60
 	c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal"))
39eec4c2
 
668e2369
 	out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
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
 
668e2369
 	running, _ = inspectField(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) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
621e3d85
 	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)
 }