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

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	containertypes "github.com/docker/docker/api/types/container"
    13  	"github.com/docker/docker/integration/internal/container"
    14  	"gotest.tools/v3/assert"
    15  	"gotest.tools/v3/icmd"
    16  	"gotest.tools/v3/poll"
    17  	"gotest.tools/v3/skip"
    18  )
    19  
    20  // TestStopContainerWithTimeout checks that ContainerStop with
    21  // a timeout works as documented, i.e. in case of negative timeout
    22  // waiting is not limited (issue #35311).
    23  func TestStopContainerWithTimeout(t *testing.T) {
    24  	defer setupTest(t)()
    25  	client := testEnv.APIClient()
    26  	ctx := context.Background()
    27  
    28  	testCmd := container.WithCmd("sh", "-c", "sleep 2 && exit 42")
    29  	testData := []struct {
    30  		doc              string
    31  		timeout          int
    32  		expectedExitCode int
    33  	}{
    34  		// In case container is forcefully killed, 137 is returned,
    35  		// otherwise the exit code from the above script
    36  		{
    37  			"zero timeout: expect forceful container kill",
    38  			0, 137,
    39  		},
    40  		{
    41  			"too small timeout: expect forceful container kill",
    42  			1, 137,
    43  		},
    44  		{
    45  			"big enough timeout: expect graceful container stop",
    46  			3, 42,
    47  		},
    48  		{
    49  			"unlimited timeout: expect graceful container stop",
    50  			-1, 42,
    51  		},
    52  	}
    53  
    54  	for _, d := range testData {
    55  		d := d
    56  		t.Run(strconv.Itoa(d.timeout), func(t *testing.T) {
    57  			t.Parallel()
    58  			id := container.Run(ctx, t, client, testCmd)
    59  
    60  			err := client.ContainerStop(ctx, id, containertypes.StopOptions{Timeout: &d.timeout})
    61  			assert.NilError(t, err)
    62  
    63  			poll.WaitOn(t, container.IsStopped(ctx, client, id),
    64  				poll.WithDelay(100*time.Millisecond))
    65  
    66  			inspect, err := client.ContainerInspect(ctx, id)
    67  			assert.NilError(t, err)
    68  			assert.Equal(t, inspect.State.ExitCode, d.expectedExitCode)
    69  		})
    70  	}
    71  }
    72  
    73  func TestDeleteDevicemapper(t *testing.T) {
    74  	skip.If(t, testEnv.DaemonInfo.Driver != "devicemapper")
    75  	skip.If(t, testEnv.IsRemoteDaemon)
    76  
    77  	defer setupTest(t)()
    78  	client := testEnv.APIClient()
    79  	ctx := context.Background()
    80  
    81  	id := container.Run(ctx, t, client, container.WithName("foo-"+t.Name()), container.WithCmd("echo"))
    82  
    83  	poll.WaitOn(t, container.IsStopped(ctx, client, id), poll.WithDelay(100*time.Millisecond))
    84  
    85  	inspect, err := client.ContainerInspect(ctx, id)
    86  	assert.NilError(t, err)
    87  
    88  	deviceID := inspect.GraphDriver.Data["DeviceId"]
    89  
    90  	// Find pool name from device name
    91  	deviceName := inspect.GraphDriver.Data["DeviceName"]
    92  	devicePrefix := deviceName[:strings.LastIndex(deviceName, "-")]
    93  	devicePool := fmt.Sprintf("/dev/mapper/%s-pool", devicePrefix)
    94  
    95  	result := icmd.RunCommand("dmsetup", "message", devicePool, "0", fmt.Sprintf("delete %s", deviceID))
    96  	result.Assert(t, icmd.Success)
    97  
    98  	err = client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{})
    99  	assert.NilError(t, err)
   100  }