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

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	containertypes "github.com/docker/docker/api/types/container"
     9  	"github.com/docker/docker/integration/internal/container"
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  	"gotest.tools/v3/poll"
    13  )
    14  
    15  func TestUpdateRestartPolicy(t *testing.T) {
    16  	defer setupTest(t)()
    17  	client := testEnv.APIClient()
    18  	ctx := context.Background()
    19  
    20  	cID := container.Run(ctx, t, client, container.WithCmd("sh", "-c", "sleep 1 && false"), func(c *container.TestContainerConfig) {
    21  		c.HostConfig.RestartPolicy = containertypes.RestartPolicy{
    22  			Name:              "on-failure",
    23  			MaximumRetryCount: 3,
    24  		}
    25  	})
    26  
    27  	_, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
    28  		RestartPolicy: containertypes.RestartPolicy{
    29  			Name:              "on-failure",
    30  			MaximumRetryCount: 5,
    31  		},
    32  	})
    33  	assert.NilError(t, err)
    34  
    35  	timeout := 60 * time.Second
    36  	if testEnv.OSType == "windows" {
    37  		timeout = 180 * time.Second
    38  	}
    39  
    40  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(timeout))
    41  
    42  	inspect, err := client.ContainerInspect(ctx, cID)
    43  	assert.NilError(t, err)
    44  	assert.Check(t, is.Equal(inspect.RestartCount, 5))
    45  	assert.Check(t, is.Equal(inspect.HostConfig.RestartPolicy.MaximumRetryCount, 5))
    46  }
    47  
    48  func TestUpdateRestartWithAutoRemove(t *testing.T) {
    49  	defer setupTest(t)()
    50  	client := testEnv.APIClient()
    51  	ctx := context.Background()
    52  
    53  	cID := container.Run(ctx, t, client, container.WithAutoRemove)
    54  
    55  	_, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
    56  		RestartPolicy: containertypes.RestartPolicy{
    57  			Name: "always",
    58  		},
    59  	})
    60  	assert.Check(t, is.ErrorContains(err, "Restart policy cannot be updated because AutoRemove is enabled for the container"))
    61  }