2018-03-01 17:51:11 -05:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
2018-01-31 14:41:31 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/client"
|
2018-02-09 13:37:55 -05:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-01-31 14:41:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestKillContainerInvalidSignal(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-01-31 14:41:31 -05:00
|
|
|
ctx := context.Background()
|
2019-06-06 07:15:31 -04:00
|
|
|
id := container.Run(ctx, t, client)
|
2018-01-31 14:41:31 -05:00
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
err := client.ContainerKill(ctx, id, "0")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Error(t, err, "Error response from daemon: Invalid signal: 0")
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-01-31 14:41:31 -05:00
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
err = client.ContainerKill(ctx, id, "SIG42")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Error(t, err, "Error response from daemon: Invalid signal: SIG42")
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-01-31 14:41:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKillContainer(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-01-31 14:41:31 -05:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
doc string
|
|
|
|
signal string
|
|
|
|
status string
|
2020-09-15 15:43:16 -04:00
|
|
|
skipOs string
|
2018-01-31 14:41:31 -05:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "no signal",
|
|
|
|
signal: "",
|
|
|
|
status: "exited",
|
2020-09-15 15:43:16 -04:00
|
|
|
skipOs: "",
|
2018-01-31 14:41:31 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "non killing signal",
|
|
|
|
signal: "SIGWINCH",
|
|
|
|
status: "running",
|
2020-09-15 15:43:16 -04:00
|
|
|
skipOs: "windows",
|
2018-01-31 14:41:31 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "killing signal",
|
|
|
|
signal: "SIGTERM",
|
|
|
|
status: "exited",
|
2020-09-15 15:43:16 -04:00
|
|
|
skipOs: "",
|
2018-01-31 14:41:31 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
2020-09-15 15:43:16 -04:00
|
|
|
skip.If(t, testEnv.OSType == tc.skipOs, "Windows does not support SIGWINCH")
|
2018-01-31 14:41:31 -05:00
|
|
|
ctx := context.Background()
|
2019-06-06 07:15:31 -04:00
|
|
|
id := container.Run(ctx, t, client)
|
2018-02-09 13:37:55 -05:00
|
|
|
err := client.ContainerKill(ctx, id, tc.signal)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-31 14:41:31 -05:00
|
|
|
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, tc.status), poll.WithDelay(100*time.Millisecond))
|
2018-01-31 14:41:31 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKillWithStopSignalAndRestartPolicies(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "Windows only supports 1.25 or later")
|
2018-01-31 14:41:31 -05:00
|
|
|
defer setupTest(t)()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-01-31 14:41:31 -05:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
doc string
|
|
|
|
stopsignal string
|
|
|
|
status string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "same-signal-disables-restart-policy",
|
|
|
|
stopsignal: "TERM",
|
|
|
|
status: "exited",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "different-signal-keep-restart-policy",
|
|
|
|
stopsignal: "CONT",
|
|
|
|
status: "running",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2019-06-06 07:15:31 -04:00
|
|
|
id := container.Run(ctx, t, client,
|
2019-02-14 19:03:26 -05:00
|
|
|
container.WithRestartPolicy("always"),
|
|
|
|
func(c *container.TestContainerConfig) {
|
|
|
|
c.Config.StopSignal = tc.stopsignal
|
|
|
|
})
|
2018-02-09 13:37:55 -05:00
|
|
|
err := client.ContainerKill(ctx, id, "TERM")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-31 14:41:31 -05:00
|
|
|
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, tc.status), poll.WithDelay(100*time.Millisecond))
|
2018-01-31 14:41:31 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKillStoppedContainer(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "Windows only supports 1.25 or later")
|
2018-01-31 14:41:31 -05:00
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2019-06-06 07:00:37 -04:00
|
|
|
id := container.Create(ctx, t, client)
|
2018-02-09 13:37:55 -05:00
|
|
|
err := client.ContainerKill(ctx, id, "SIGKILL")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, is.ErrorContains(err, ""))
|
|
|
|
assert.Assert(t, is.Contains(err.Error(), "is not running"))
|
2018-01-31 14:41:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKillStoppedContainerAPIPre120(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "Windows only supports 1.25 or later")
|
2018-01-31 14:41:31 -05:00
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t, client.WithVersion("1.19"))
|
2019-06-06 07:00:37 -04:00
|
|
|
id := container.Create(ctx, t, client)
|
2018-02-09 13:37:55 -05:00
|
|
|
err := client.ContainerKill(ctx, id, "SIGKILL")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-31 14:41:31 -05:00
|
|
|
}
|
2018-02-09 07:46:38 -05:00
|
|
|
|
|
|
|
func TestKillDifferentUserContainer(t *testing.T) {
|
|
|
|
// TODO Windows: Windows does not yet support -u (Feb 2016).
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "User containers (container.Config.User) are not yet supported on %q platform", testEnv.OSType)
|
2018-02-09 07:46:38 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t, client.WithVersion("1.19"))
|
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
id := container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
|
2018-02-09 13:37:55 -05:00
|
|
|
c.Config.User = "daemon"
|
2018-02-09 07:46:38 -05:00
|
|
|
})
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-02-09 07:46:38 -05:00
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
err := client.ContainerKill(ctx, id, "SIGKILL")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, "exited"), poll.WithDelay(100*time.Millisecond))
|
2018-02-09 07:46:38 -05:00
|
|
|
}
|
2018-02-09 08:02:36 -05:00
|
|
|
|
|
|
|
func TestInspectOomKilledTrue(t *testing.T) {
|
2020-02-18 04:43:56 -05:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
|
|
|
skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
|
|
|
|
skip.If(t, !testEnv.DaemonInfo.MemoryLimit || !testEnv.DaemonInfo.SwapLimit)
|
2021-01-22 03:47:30 -05:00
|
|
|
skip.If(t, testEnv.DaemonInfo.CgroupVersion == "2", "FIXME: flaky on cgroup v2 (https://github.com/moby/moby/issues/41929)")
|
2018-02-09 08:02:36 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-09 08:02:36 -05:00
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, client, container.WithCmd("sh", "-c", "x=a; while true; do x=$x$x$x$x; done"), func(c *container.TestContainerConfig) {
|
2018-02-09 08:02:36 -05:00
|
|
|
c.HostConfig.Resources.Memory = 32 * 1024 * 1024
|
|
|
|
})
|
|
|
|
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
|
2018-02-09 08:02:36 -05:00
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, cID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(true, inspect.State.OOMKilled))
|
2018-02-09 08:02:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectOomKilledFalse(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows" || !testEnv.DaemonInfo.MemoryLimit || !testEnv.DaemonInfo.SwapLimit)
|
2018-02-09 08:02:36 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-09 08:02:36 -05:00
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, client, container.WithCmd("sh", "-c", "echo hello world"))
|
2018-02-09 08:02:36 -05:00
|
|
|
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
|
2018-02-09 08:02:36 -05:00
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, cID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(false, inspect.State.OOMKilled))
|
2018-02-09 08:02:36 -05:00
|
|
|
}
|