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

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"testing"
     7  	"time"
     8  
     9  	containerderrdefs "github.com/containerd/containerd/errdefs"
    10  	"github.com/docker/docker/api/types"
    11  	containertypes "github.com/docker/docker/api/types/container"
    12  	"github.com/docker/docker/api/types/events"
    13  	"github.com/docker/docker/api/types/filters"
    14  	"github.com/docker/docker/api/types/versions"
    15  	"github.com/docker/docker/integration/internal/container"
    16  	"github.com/docker/docker/testutil/request"
    17  	"gotest.tools/v3/assert"
    18  	is "gotest.tools/v3/assert/cmp"
    19  	"gotest.tools/v3/poll"
    20  	"gotest.tools/v3/skip"
    21  )
    22  
    23  func TestPause(t *testing.T) {
    24  	skip.If(t, testEnv.DaemonInfo.OSType == "windows" && testEnv.DaemonInfo.Isolation == "process")
    25  	skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
    26  
    27  	defer setupTest(t)()
    28  	client := testEnv.APIClient()
    29  	ctx := context.Background()
    30  
    31  	cID := container.Run(ctx, t, client)
    32  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
    33  
    34  	since := request.DaemonUnixTime(ctx, t, client, testEnv)
    35  
    36  	err := client.ContainerPause(ctx, cID)
    37  	assert.NilError(t, err)
    38  
    39  	inspect, err := client.ContainerInspect(ctx, cID)
    40  	assert.NilError(t, err)
    41  	assert.Check(t, is.Equal(true, inspect.State.Paused))
    42  
    43  	err = client.ContainerUnpause(ctx, cID)
    44  	assert.NilError(t, err)
    45  
    46  	until := request.DaemonUnixTime(ctx, t, client, testEnv)
    47  
    48  	messages, errs := client.Events(ctx, types.EventsOptions{
    49  		Since:   since,
    50  		Until:   until,
    51  		Filters: filters.NewArgs(filters.Arg("container", cID)),
    52  	})
    53  	assert.Check(t, is.DeepEqual([]string{"pause", "unpause"}, getEventActions(t, messages, errs)))
    54  }
    55  
    56  func TestPauseFailsOnWindowsServerContainers(t *testing.T) {
    57  	skip.If(t, testEnv.DaemonInfo.OSType != "windows" || testEnv.DaemonInfo.Isolation != "process")
    58  
    59  	defer setupTest(t)()
    60  	client := testEnv.APIClient()
    61  	ctx := context.Background()
    62  
    63  	cID := container.Run(ctx, t, client)
    64  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
    65  
    66  	err := client.ContainerPause(ctx, cID)
    67  	assert.Check(t, is.ErrorContains(err, containerderrdefs.ErrNotImplemented.Error()))
    68  }
    69  
    70  func TestPauseStopPausedContainer(t *testing.T) {
    71  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    72  	skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.31"), "broken in earlier versions")
    73  	skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
    74  	defer setupTest(t)()
    75  	client := testEnv.APIClient()
    76  	ctx := context.Background()
    77  
    78  	cID := container.Run(ctx, t, client)
    79  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
    80  
    81  	err := client.ContainerPause(ctx, cID)
    82  	assert.NilError(t, err)
    83  
    84  	err = client.ContainerStop(ctx, cID, containertypes.StopOptions{})
    85  	assert.NilError(t, err)
    86  
    87  	poll.WaitOn(t, container.IsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
    88  }
    89  
    90  func getEventActions(t *testing.T, messages <-chan events.Message, errs <-chan error) []string {
    91  	var actions []string
    92  	for {
    93  		select {
    94  		case err := <-errs:
    95  			assert.Check(t, err == nil || err == io.EOF)
    96  			return actions
    97  		case e := <-messages:
    98  			actions = append(actions, e.Status)
    99  		}
   100  	}
   101  }