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

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  	"time"
     7  
     8  	containertypes "github.com/docker/docker/api/types/container"
     9  	"github.com/docker/docker/integration/internal/container"
    10  	"github.com/docker/docker/testutil"
    11  	"gotest.tools/v3/assert"
    12  	"gotest.tools/v3/poll"
    13  	"gotest.tools/v3/skip"
    14  )
    15  
    16  // TestStopContainerWithTimeout checks that ContainerStop with
    17  // a timeout works as documented, i.e. in case of negative timeout
    18  // waiting is not limited (issue #35311).
    19  func TestStopContainerWithTimeout(t *testing.T) {
    20  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    21  	ctx := setupTest(t)
    22  
    23  	apiClient := testEnv.APIClient()
    24  
    25  	testCmd := container.WithCmd("sh", "-c", "sleep 2 && exit 42")
    26  	testData := []struct {
    27  		doc              string
    28  		timeout          int
    29  		expectedExitCode int
    30  	}{
    31  		// In case container is forcefully killed, 137 is returned,
    32  		// otherwise the exit code from the above script
    33  		{
    34  			"zero timeout: expect forceful container kill",
    35  			1, 0x40010004,
    36  		},
    37  		{
    38  			"too small timeout: expect forceful container kill",
    39  			2, 0x40010004,
    40  		},
    41  		{
    42  			"big enough timeout: expect graceful container stop",
    43  			120, 42,
    44  		},
    45  		{
    46  			"unlimited timeout: expect graceful container stop",
    47  			-1, 42,
    48  		},
    49  	}
    50  
    51  	for _, d := range testData {
    52  		d := d
    53  		t.Run(strconv.Itoa(d.timeout), func(t *testing.T) {
    54  			t.Parallel()
    55  			ctx := testutil.StartSpan(ctx, t)
    56  			id := container.Run(ctx, t, apiClient, testCmd)
    57  
    58  			err := apiClient.ContainerStop(ctx, id, containertypes.StopOptions{Timeout: &d.timeout})
    59  			assert.NilError(t, err)
    60  
    61  			poll.WaitOn(t, container.IsStopped(ctx, apiClient, id),
    62  				poll.WithDelay(100*time.Millisecond))
    63  
    64  			inspect, err := apiClient.ContainerInspect(ctx, id)
    65  			assert.NilError(t, err)
    66  			assert.Equal(t, inspect.State.ExitCode, d.expectedExitCode)
    67  		})
    68  	}
    69  }