github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/integration/container/daemon_test.go (about)

     1  package container
     2  
     3  import (
     4  	"testing"
     5  
     6  	containertypes "github.com/docker/docker/api/types/container"
     7  	"github.com/docker/docker/integration/internal/container"
     8  	"github.com/docker/docker/testutil"
     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.
    17  func 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  	ctx := testutil.StartSpan(baseContext, t)
    25  
    26  	d := daemon.New(t)
    27  	defer d.Cleanup(t)
    28  
    29  	d.StartWithBusybox(ctx, t, "--iptables=false")
    30  	defer d.Stop(t)
    31  
    32  	apiClient := d.NewClientT(t)
    33  
    34  	// The intention of this container is to ignore stop signals.
    35  	// Sadly this means the test will take longer, but at least this test can be parallelized.
    36  	id := container.Run(ctx, t, apiClient, container.WithCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done"))
    37  	defer func() {
    38  		err := apiClient.ContainerRemove(ctx, id, containertypes.RemoveOptions{Force: true})
    39  		assert.NilError(t, err)
    40  	}()
    41  
    42  	inspect, err := apiClient.ContainerInspect(ctx, id)
    43  	assert.NilError(t, err)
    44  	assert.Assert(t, inspect.State.Running)
    45  
    46  	assert.NilError(t, d.Kill())
    47  	d.Start(t, "--iptables=false")
    48  
    49  	inspect, err = apiClient.ContainerInspect(ctx, id)
    50  	assert.Check(t, is.Nil(err))
    51  	assert.Assert(t, !inspect.State.Running)
    52  }