2018-03-01 22:51:11 +00:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
2018-01-31 19:41:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-02-09 18:37:55 +00:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2018-01-31 19:41:31 +00:00
|
|
|
"github.com/docker/docker/client"
|
2018-02-09 18:37:55 +00:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2018-04-17 08:22:04 +00:00
|
|
|
"github.com/docker/docker/internal/test/request"
|
2018-06-11 13:32:11 +00:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/poll"
|
|
|
|
"gotest.tools/skip"
|
2018-01-31 19:41:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestKillContainerInvalidSignal(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
ctx := context.Background()
|
2018-02-09 18:37:55 +00:00
|
|
|
id := container.Run(t, ctx, client)
|
2018-01-31 19:41:31 +00:00
|
|
|
|
2018-02-09 18:37:55 +00:00
|
|
|
err := client.ContainerKill(ctx, id, "0")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Error(t, err, "Error response from daemon: Invalid signal: 0")
|
2018-02-22 10:30:51 +00:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-01-31 19:41:31 +00:00
|
|
|
|
2018-02-09 18:37:55 +00:00
|
|
|
err = client.ContainerKill(ctx, id, "SIG42")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Error(t, err, "Error response from daemon: Invalid signal: SIG42")
|
2018-02-22 10:30:51 +00:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-01-31 19:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKillContainer(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
doc string
|
|
|
|
signal string
|
|
|
|
status string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "no signal",
|
|
|
|
signal: "",
|
|
|
|
status: "exited",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "non killing signal",
|
|
|
|
signal: "SIGWINCH",
|
|
|
|
status: "running",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "killing signal",
|
|
|
|
signal: "SIGTERM",
|
|
|
|
status: "exited",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2018-02-09 18:37:55 +00:00
|
|
|
id := container.Run(t, ctx, client)
|
|
|
|
err := client.ContainerKill(ctx, id, tc.signal)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2018-01-31 19:41:31 +00:00
|
|
|
|
2018-02-22 10:30:51 +00:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, tc.status), poll.WithDelay(100*time.Millisecond))
|
2018-01-31 19:41:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKillWithStopSignalAndRestartPolicies(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.OSType != "linux", "Windows only supports 1.25 or later")
|
|
|
|
defer setupTest(t)()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
|
|
|
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()
|
2018-02-09 18:37:55 +00:00
|
|
|
id := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
|
|
|
|
c.Config.StopSignal = tc.stopsignal
|
|
|
|
c.HostConfig.RestartPolicy = containertypes.RestartPolicy{
|
|
|
|
Name: "always",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
err := client.ContainerKill(ctx, id, "TERM")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2018-01-31 19:41:31 +00:00
|
|
|
|
2018-02-22 10:30:51 +00:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, tc.status), poll.WithDelay(100*time.Millisecond))
|
2018-01-31 19:41:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKillStoppedContainer(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.OSType != "linux") // Windows only supports 1.25 or later
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
2018-02-09 18:37:55 +00:00
|
|
|
id := container.Create(t, ctx, client)
|
|
|
|
err := client.ContainerKill(ctx, id, "SIGKILL")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Assert(t, is.ErrorContains(err, ""))
|
|
|
|
assert.Assert(t, is.Contains(err.Error(), "is not running"))
|
2018-01-31 19:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKillStoppedContainerAPIPre120(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.OSType != "linux") // Windows only supports 1.25 or later
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t, client.WithVersion("1.19"))
|
2018-02-09 18:37:55 +00:00
|
|
|
id := container.Create(t, ctx, client)
|
|
|
|
err := client.ContainerKill(ctx, id, "SIGKILL")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2018-01-31 19:41:31 +00:00
|
|
|
}
|
2018-02-09 12:46:38 +00:00
|
|
|
|
|
|
|
func TestKillDifferentUserContainer(t *testing.T) {
|
|
|
|
// TODO Windows: Windows does not yet support -u (Feb 2016).
|
|
|
|
skip.If(t, testEnv.OSType != "linux", "User containers (container.Config.User) are not yet supported on %q platform", testEnv.OSType)
|
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t, client.WithVersion("1.19"))
|
|
|
|
|
2018-02-09 18:37:55 +00:00
|
|
|
id := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
|
|
|
|
c.Config.User = "daemon"
|
2018-02-09 12:46:38 +00:00
|
|
|
})
|
2018-02-22 10:30:51 +00:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-02-09 12:46:38 +00:00
|
|
|
|
2018-02-09 18:37:55 +00:00
|
|
|
err := client.ContainerKill(ctx, id, "SIGKILL")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2018-02-22 10:30:51 +00:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, id, "exited"), poll.WithDelay(100*time.Millisecond))
|
2018-02-09 12:46:38 +00:00
|
|
|
}
|
2018-02-09 13:02:36 +00:00
|
|
|
|
|
|
|
func TestInspectOomKilledTrue(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType != "linux" || !testEnv.DaemonInfo.MemoryLimit || !testEnv.DaemonInfo.SwapLimit)
|
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-03-03 01:06:49 +00:00
|
|
|
cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", "x=a; while true; do x=$x$x$x$x; done"), func(c *container.TestContainerConfig) {
|
2018-02-09 13:02:36 +00:00
|
|
|
c.HostConfig.Resources.Memory = 32 * 1024 * 1024
|
|
|
|
})
|
|
|
|
|
2018-02-22 10:30:51 +00:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
|
2018-02-09 13:02:36 +00:00
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, cID)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(true, inspect.State.OOMKilled))
|
2018-02-09 13:02:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectOomKilledFalse(t *testing.T) {
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType != "linux" || !testEnv.DaemonInfo.MemoryLimit || !testEnv.DaemonInfo.SwapLimit)
|
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-03-03 01:06:49 +00:00
|
|
|
cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", "echo hello world"))
|
2018-02-09 13:02:36 +00:00
|
|
|
|
2018-02-22 10:30:51 +00:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
|
2018-02-09 13:02:36 +00:00
|
|
|
|
|
|
|
inspect, err := client.ContainerInspect(ctx, cID)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(false, inspect.State.OOMKilled))
|
2018-02-09 13:02:36 +00:00
|
|
|
}
|