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