Signed-off-by: Vincent Demeester <vincent@sbr.pm>
| ... | ... |
@@ -6,37 +6,9 @@ import ( |
| 6 | 6 |
|
| 7 | 7 |
"github.com/docker/docker/integration-cli/checker" |
| 8 | 8 |
"github.com/docker/docker/integration-cli/cli" |
| 9 |
- "github.com/docker/docker/integration-cli/request" |
|
| 10 | 9 |
"github.com/go-check/check" |
| 11 |
- "github.com/gotestyourself/gotestyourself/icmd" |
|
| 12 |
- "golang.org/x/net/context" |
|
| 13 | 10 |
) |
| 14 | 11 |
|
| 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 | 12 |
func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
|
| 41 | 13 |
// TODO Windows: Windows does not yet support -u (Feb 2016). |
| 42 | 14 |
testRequires(c, DaemonIsLinux) |
| ... | ... |
@@ -51,88 +23,3 @@ func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
|
| 51 | 51 |
c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
|
| 52 | 52 |
|
| 53 | 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 |
-} |
| 139 | 54 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,182 @@ |
| 0 |
+package container |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "context" |
|
| 4 |
+ "testing" |
|
| 5 |
+ "time" |
|
| 6 |
+ |
|
| 7 |
+ "github.com/docker/docker/api/types" |
|
| 8 |
+ "github.com/docker/docker/api/types/container" |
|
| 9 |
+ "github.com/docker/docker/api/types/network" |
|
| 10 |
+ "github.com/docker/docker/api/types/strslice" |
|
| 11 |
+ "github.com/docker/docker/client" |
|
| 12 |
+ "github.com/docker/docker/integration/util/request" |
|
| 13 |
+ "github.com/gotestyourself/gotestyourself/poll" |
|
| 14 |
+ "github.com/gotestyourself/gotestyourself/skip" |
|
| 15 |
+ "github.com/stretchr/testify/require" |
|
| 16 |
+) |
|
| 17 |
+ |
|
| 18 |
+func TestKillContainerInvalidSignal(t *testing.T) {
|
|
| 19 |
+ defer setupTest(t)() |
|
| 20 |
+ client := request.NewAPIClient(t) |
|
| 21 |
+ t.Parallel() |
|
| 22 |
+ ctx := context.Background() |
|
| 23 |
+ c, err := client.ContainerCreate(ctx, |
|
| 24 |
+ &container.Config{
|
|
| 25 |
+ Image: "busybox", |
|
| 26 |
+ Cmd: strslice.StrSlice([]string{"top"}),
|
|
| 27 |
+ }, |
|
| 28 |
+ &container.HostConfig{},
|
|
| 29 |
+ &network.NetworkingConfig{},
|
|
| 30 |
+ "") |
|
| 31 |
+ require.NoError(t, err) |
|
| 32 |
+ err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
|
|
| 33 |
+ require.NoError(t, err) |
|
| 34 |
+ |
|
| 35 |
+ err = client.ContainerKill(ctx, c.ID, "0") |
|
| 36 |
+ require.EqualError(t, err, "Error response from daemon: Invalid signal: 0") |
|
| 37 |
+ poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond)) |
|
| 38 |
+ |
|
| 39 |
+ err = client.ContainerKill(ctx, c.ID, "SIG42") |
|
| 40 |
+ require.EqualError(t, err, "Error response from daemon: Invalid signal: SIG42") |
|
| 41 |
+ poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond)) |
|
| 42 |
+} |
|
| 43 |
+ |
|
| 44 |
+func TestKillContainer(t *testing.T) {
|
|
| 45 |
+ defer setupTest(t)() |
|
| 46 |
+ client := request.NewAPIClient(t) |
|
| 47 |
+ |
|
| 48 |
+ testCases := []struct {
|
|
| 49 |
+ doc string |
|
| 50 |
+ signal string |
|
| 51 |
+ status string |
|
| 52 |
+ }{
|
|
| 53 |
+ {
|
|
| 54 |
+ doc: "no signal", |
|
| 55 |
+ signal: "", |
|
| 56 |
+ status: "exited", |
|
| 57 |
+ }, |
|
| 58 |
+ {
|
|
| 59 |
+ doc: "non killing signal", |
|
| 60 |
+ signal: "SIGWINCH", |
|
| 61 |
+ status: "running", |
|
| 62 |
+ }, |
|
| 63 |
+ {
|
|
| 64 |
+ doc: "killing signal", |
|
| 65 |
+ signal: "SIGTERM", |
|
| 66 |
+ status: "exited", |
|
| 67 |
+ }, |
|
| 68 |
+ } |
|
| 69 |
+ |
|
| 70 |
+ for _, tc := range testCases {
|
|
| 71 |
+ tc := tc |
|
| 72 |
+ t.Run(tc.doc, func(t *testing.T) {
|
|
| 73 |
+ t.Parallel() |
|
| 74 |
+ ctx := context.Background() |
|
| 75 |
+ c, err := client.ContainerCreate(ctx, |
|
| 76 |
+ &container.Config{
|
|
| 77 |
+ Image: "busybox", |
|
| 78 |
+ Cmd: strslice.StrSlice([]string{"top"}),
|
|
| 79 |
+ }, |
|
| 80 |
+ &container.HostConfig{},
|
|
| 81 |
+ &network.NetworkingConfig{},
|
|
| 82 |
+ "") |
|
| 83 |
+ require.NoError(t, err) |
|
| 84 |
+ err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
|
|
| 85 |
+ require.NoError(t, err) |
|
| 86 |
+ err = client.ContainerKill(ctx, c.ID, tc.signal) |
|
| 87 |
+ require.NoError(t, err) |
|
| 88 |
+ |
|
| 89 |
+ poll.WaitOn(t, containerIsInState(ctx, client, c.ID, tc.status), poll.WithDelay(100*time.Millisecond)) |
|
| 90 |
+ }) |
|
| 91 |
+ } |
|
| 92 |
+} |
|
| 93 |
+ |
|
| 94 |
+func TestKillWithStopSignalAndRestartPolicies(t *testing.T) {
|
|
| 95 |
+ skip.If(t, testEnv.OSType != "linux", "Windows only supports 1.25 or later") |
|
| 96 |
+ defer setupTest(t)() |
|
| 97 |
+ client := request.NewAPIClient(t) |
|
| 98 |
+ |
|
| 99 |
+ testCases := []struct {
|
|
| 100 |
+ doc string |
|
| 101 |
+ stopsignal string |
|
| 102 |
+ status string |
|
| 103 |
+ }{
|
|
| 104 |
+ {
|
|
| 105 |
+ doc: "same-signal-disables-restart-policy", |
|
| 106 |
+ stopsignal: "TERM", |
|
| 107 |
+ status: "exited", |
|
| 108 |
+ }, |
|
| 109 |
+ {
|
|
| 110 |
+ doc: "different-signal-keep-restart-policy", |
|
| 111 |
+ stopsignal: "CONT", |
|
| 112 |
+ status: "running", |
|
| 113 |
+ }, |
|
| 114 |
+ } |
|
| 115 |
+ |
|
| 116 |
+ for _, tc := range testCases {
|
|
| 117 |
+ tc := tc |
|
| 118 |
+ t.Run(tc.doc, func(t *testing.T) {
|
|
| 119 |
+ t.Parallel() |
|
| 120 |
+ ctx := context.Background() |
|
| 121 |
+ c, err := client.ContainerCreate(ctx, |
|
| 122 |
+ &container.Config{
|
|
| 123 |
+ Image: "busybox", |
|
| 124 |
+ Cmd: strslice.StrSlice([]string{"top"}),
|
|
| 125 |
+ StopSignal: tc.stopsignal, |
|
| 126 |
+ }, |
|
| 127 |
+ &container.HostConfig{
|
|
| 128 |
+ RestartPolicy: container.RestartPolicy{
|
|
| 129 |
+ Name: "always", |
|
| 130 |
+ }}, |
|
| 131 |
+ &network.NetworkingConfig{},
|
|
| 132 |
+ "") |
|
| 133 |
+ require.NoError(t, err) |
|
| 134 |
+ err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
|
|
| 135 |
+ require.NoError(t, err) |
|
| 136 |
+ err = client.ContainerKill(ctx, c.ID, "TERM") |
|
| 137 |
+ require.NoError(t, err) |
|
| 138 |
+ |
|
| 139 |
+ poll.WaitOn(t, containerIsInState(ctx, client, c.ID, tc.status), poll.WithDelay(100*time.Millisecond)) |
|
| 140 |
+ }) |
|
| 141 |
+ } |
|
| 142 |
+} |
|
| 143 |
+ |
|
| 144 |
+func TestKillStoppedContainer(t *testing.T) {
|
|
| 145 |
+ skip.If(t, testEnv.OSType != "linux") // Windows only supports 1.25 or later |
|
| 146 |
+ defer setupTest(t)() |
|
| 147 |
+ t.Parallel() |
|
| 148 |
+ ctx := context.Background() |
|
| 149 |
+ client := request.NewAPIClient(t) |
|
| 150 |
+ c, err := client.ContainerCreate(ctx, |
|
| 151 |
+ &container.Config{
|
|
| 152 |
+ Image: "busybox", |
|
| 153 |
+ Cmd: strslice.StrSlice([]string{"top"}),
|
|
| 154 |
+ }, |
|
| 155 |
+ &container.HostConfig{},
|
|
| 156 |
+ &network.NetworkingConfig{},
|
|
| 157 |
+ "") |
|
| 158 |
+ require.NoError(t, err) |
|
| 159 |
+ err = client.ContainerKill(ctx, c.ID, "SIGKILL") |
|
| 160 |
+ require.Error(t, err) |
|
| 161 |
+ require.Contains(t, err.Error(), "is not running") |
|
| 162 |
+} |
|
| 163 |
+ |
|
| 164 |
+func TestKillStoppedContainerAPIPre120(t *testing.T) {
|
|
| 165 |
+ skip.If(t, testEnv.OSType != "linux") // Windows only supports 1.25 or later |
|
| 166 |
+ defer setupTest(t)() |
|
| 167 |
+ t.Parallel() |
|
| 168 |
+ ctx := context.Background() |
|
| 169 |
+ client := request.NewAPIClient(t, client.WithVersion("1.19"))
|
|
| 170 |
+ c, err := client.ContainerCreate(ctx, |
|
| 171 |
+ &container.Config{
|
|
| 172 |
+ Image: "busybox", |
|
| 173 |
+ Cmd: strslice.StrSlice([]string{"top"}),
|
|
| 174 |
+ }, |
|
| 175 |
+ &container.HostConfig{},
|
|
| 176 |
+ &network.NetworkingConfig{},
|
|
| 177 |
+ "") |
|
| 178 |
+ require.NoError(t, err) |
|
| 179 |
+ err = client.ContainerKill(ctx, c.ID, "SIGKILL") |
|
| 180 |
+ require.NoError(t, err) |
|
| 181 |
+} |
| ... | ... |
@@ -6,7 +6,6 @@ import ( |
| 6 | 6 |
"testing" |
| 7 | 7 |
"time" |
| 8 | 8 |
|
| 9 |
- "github.com/docker/docker/api" |
|
| 10 | 9 |
"github.com/docker/docker/client" |
| 11 | 10 |
"github.com/docker/go-connections/sockets" |
| 12 | 11 |
"github.com/docker/go-connections/tlsconfig" |
| ... | ... |
@@ -14,8 +13,9 @@ import ( |
| 14 | 14 |
) |
| 15 | 15 |
|
| 16 | 16 |
// NewAPIClient returns a docker API client configured from environment variables |
| 17 |
-func NewAPIClient(t *testing.T) client.APIClient {
|
|
| 18 |
- clt, err := client.NewEnvClient() |
|
| 17 |
+func NewAPIClient(t *testing.T, ops ...func(*client.Client) error) client.APIClient {
|
|
| 18 |
+ ops = append([]func(*client.Client) error{client.FromEnv}, ops...)
|
|
| 19 |
+ clt, err := client.NewClientWithOpts(ops...) |
|
| 19 | 20 |
require.NoError(t, err) |
| 20 | 21 |
return clt |
| 21 | 22 |
} |
| ... | ... |
@@ -47,7 +47,5 @@ func NewTLSAPIClient(t *testing.T, host, cacertPath, certPath, keyPath string) ( |
| 47 | 47 |
Transport: tr, |
| 48 | 48 |
CheckRedirect: client.CheckRedirect, |
| 49 | 49 |
} |
| 50 |
- verStr := api.DefaultVersion |
|
| 51 |
- customHeaders := map[string]string{}
|
|
| 52 |
- return client.NewClient(host, verStr, httpClient, customHeaders) |
|
| 50 |
+ return client.NewClientWithOpts(client.WithHost(host), client.WithHTTPClient(httpClient)) |
|
| 53 | 51 |
} |