github.com/rumpl/bof@v23.0.0-rc.2+incompatible/integration/container/stop_windows_test.go (about)

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  	"testing"
     7  	"time"
     8  
     9  	containertypes "github.com/docker/docker/api/types/container"
    10  	"github.com/docker/docker/integration/internal/container"
    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.OSType == "windows")
    21  	defer setupTest(t)()
    22  	client := testEnv.APIClient()
    23  	ctx := context.Background()
    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  			id := container.Run(ctx, t, client, testCmd)
    56  
    57  			err := client.ContainerStop(ctx, id, containertypes.StopOptions{Timeout: &d.timeout})
    58  			assert.NilError(t, err)
    59  
    60  			poll.WaitOn(t, container.IsStopped(ctx, client, id),
    61  				poll.WithDelay(100*time.Millisecond))
    62  
    63  			inspect, err := client.ContainerInspect(ctx, id)
    64  			assert.NilError(t, err)
    65  			assert.Equal(t, inspect.State.ExitCode, d.expectedExitCode)
    66  		})
    67  	}
    68  }