1package container
2
3import (
4	"context"
5	"testing"
6
7	"github.com/docker/docker/api/types"
8	"github.com/docker/docker/integration/internal/container"
9	"github.com/docker/docker/testutil/daemon"
10	"gotest.tools/v3/assert"
11	is "gotest.tools/v3/assert/cmp"
12	"gotest.tools/v3/skip"
13)
14
15// Make sure a container that does not exit when it upon receiving it's stop signal is actually shutdown on daemon
16// startup.
17func TestContainerKillOnDaemonStart(t *testing.T) {
18	skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
19	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
20	skip.If(t, testEnv.IsRootless, "scenario doesn't work with rootless mode")
21
22	t.Parallel()
23
24	d := daemon.New(t)
25	defer d.Cleanup(t)
26
27	d.StartWithBusybox(t, "--iptables=false")
28	defer d.Stop(t)
29
30	client := d.NewClientT(t)
31	ctx := context.Background()
32
33	// The intention of this container is to ignore stop signals.
34	// Sadly this means the test will take longer, but at least this test can be parallelized.
35	id := container.Run(ctx, t, client, container.WithCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done"))
36	defer func() {
37		err := client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{Force: true})
38		assert.NilError(t, err)
39	}()
40
41	inspect, err := client.ContainerInspect(ctx, id)
42	assert.NilError(t, err)
43	assert.Assert(t, inspect.State.Running)
44
45	assert.NilError(t, d.Kill())
46	d.Start(t)
47
48	inspect, err = client.ContainerInspect(ctx, id)
49	assert.Check(t, is.Nil(err))
50	assert.Assert(t, !inspect.State.Running)
51}
52