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