github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/integration-cli/docker_cli_kill_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 "strings" 7 "time" 8 9 "github.com/docker/docker/integration-cli/checker" 10 "github.com/docker/docker/integration-cli/cli" 11 "github.com/docker/docker/integration-cli/request" 12 icmd "github.com/docker/docker/pkg/testutil/cmd" 13 "github.com/go-check/check" 14 ) 15 16 func (s *DockerSuite) TestKillContainer(c *check.C) { 17 out := runSleepingContainer(c, "-d") 18 cleanedContainerID := strings.TrimSpace(out) 19 cli.WaitRun(c, cleanedContainerID) 20 21 cli.DockerCmd(c, "kill", cleanedContainerID) 22 cli.WaitExited(c, cleanedContainerID, 10*time.Second) 23 24 out = cli.DockerCmd(c, "ps", "-q").Combined() 25 c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running")) 26 27 } 28 29 func (s *DockerSuite) TestKillOffStoppedContainer(c *check.C) { 30 out := runSleepingContainer(c, "-d") 31 cleanedContainerID := strings.TrimSpace(out) 32 33 cli.DockerCmd(c, "stop", cleanedContainerID) 34 cli.WaitExited(c, cleanedContainerID, 10*time.Second) 35 36 cli.Docker(cli.Args("kill", "-s", "30", cleanedContainerID)).Assert(c, icmd.Expected{ 37 ExitCode: 1, 38 }) 39 } 40 41 func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) { 42 // TODO Windows: Windows does not yet support -u (Feb 2016). 43 testRequires(c, DaemonIsLinux) 44 out := cli.DockerCmd(c, "run", "-u", "daemon", "-d", "busybox", "top").Combined() 45 cleanedContainerID := strings.TrimSpace(out) 46 cli.WaitRun(c, cleanedContainerID) 47 48 cli.DockerCmd(c, "kill", cleanedContainerID) 49 cli.WaitExited(c, cleanedContainerID, 10*time.Second) 50 51 out = cli.DockerCmd(c, "ps", "-q").Combined() 52 c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running")) 53 54 } 55 56 // regression test about correct signal parsing see #13665 57 func (s *DockerSuite) TestKillWithSignal(c *check.C) { 58 // Cannot port to Windows - does not support signals in the same way Linux does 59 testRequires(c, DaemonIsLinux) 60 out, _ := dockerCmd(c, "run", "-d", "busybox", "top") 61 cid := strings.TrimSpace(out) 62 c.Assert(waitRun(cid), check.IsNil) 63 64 dockerCmd(c, "kill", "-s", "SIGWINCH", cid) 65 time.Sleep(250 * time.Millisecond) 66 67 running := inspectField(c, cid, "State.Running") 68 69 c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after SIGWINCH")) 70 } 71 72 func (s *DockerSuite) TestKillWithStopSignalWithSameSignalShouldDisableRestartPolicy(c *check.C) { 73 // Cannot port to Windows - does not support signals int the same way as Linux does 74 testRequires(c, DaemonIsLinux) 75 out := cli.DockerCmd(c, "run", "-d", "--stop-signal=TERM", "--restart=always", "busybox", "top").Combined() 76 cid := strings.TrimSpace(out) 77 cli.WaitRun(c, cid) 78 79 // Let docker send a TERM signal to the container 80 // It will kill the process and disable the restart policy 81 cli.DockerCmd(c, "kill", "-s", "TERM", cid) 82 cli.WaitExited(c, cid, 10*time.Second) 83 84 out = cli.DockerCmd(c, "ps", "-q").Combined() 85 c.Assert(out, checker.Not(checker.Contains), cid, check.Commentf("killed container is still running")) 86 } 87 88 func (s *DockerSuite) TestKillWithStopSignalWithDifferentSignalShouldKeepRestartPolicy(c *check.C) { 89 // Cannot port to Windows - does not support signals int the same way as Linux does 90 testRequires(c, DaemonIsLinux) 91 out := cli.DockerCmd(c, "run", "-d", "--stop-signal=CONT", "--restart=always", "busybox", "top").Combined() 92 cid := strings.TrimSpace(out) 93 cli.WaitRun(c, cid) 94 95 // Let docker send a TERM signal to the container 96 // It will kill the process, but not disable the restart policy 97 cli.DockerCmd(c, "kill", "-s", "TERM", cid) 98 cli.WaitRestart(c, cid, 10*time.Second) 99 100 // Restart policy should still be in place, so it should be still running 101 cli.WaitRun(c, cid) 102 } 103 104 // FIXME(vdemeester) should be a unit test 105 func (s *DockerSuite) TestKillWithInvalidSignal(c *check.C) { 106 out := runSleepingContainer(c, "-d") 107 cid := strings.TrimSpace(out) 108 c.Assert(waitRun(cid), check.IsNil) 109 110 out, _, err := dockerCmdWithError("kill", "-s", "0", cid) 111 c.Assert(err, check.NotNil) 112 c.Assert(out, checker.Contains, "Invalid signal: 0", check.Commentf("Kill with an invalid signal didn't error out correctly")) 113 114 running := inspectField(c, cid, "State.Running") 115 c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal")) 116 117 out = runSleepingContainer(c, "-d") 118 cid = strings.TrimSpace(out) 119 c.Assert(waitRun(cid), check.IsNil) 120 121 out, _, err = dockerCmdWithError("kill", "-s", "SIG42", cid) 122 c.Assert(err, check.NotNil) 123 c.Assert(out, checker.Contains, "Invalid signal: SIG42", check.Commentf("Kill with an invalid signal error out correctly")) 124 125 running = inspectField(c, cid, "State.Running") 126 c.Assert(running, checker.Equals, "true", check.Commentf("Container should be in running state after an invalid signal")) 127 128 } 129 130 func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) { 131 testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later 132 runSleepingContainer(c, "--name", "docker-kill-test-api", "-d") 133 dockerCmd(c, "stop", "docker-kill-test-api") 134 135 status, _, err := request.SockRequest("POST", fmt.Sprintf("/v1.19/containers/%s/kill", "docker-kill-test-api"), nil, daemonHost()) 136 c.Assert(err, check.IsNil) 137 c.Assert(status, check.Equals, http.StatusNoContent) 138 }