github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/container/wait_test.go (about)

     1  package container // import "github.com/demonoid81/moby/integration/container"
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/demonoid81/moby/integration/internal/container"
     9  	"github.com/demonoid81/moby/testutil/request"
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  	"gotest.tools/v3/poll"
    13  	"gotest.tools/v3/skip"
    14  )
    15  
    16  func TestWaitNonBlocked(t *testing.T) {
    17  	defer setupTest(t)()
    18  	cli := request.NewAPIClient(t)
    19  
    20  	testCases := []struct {
    21  		doc          string
    22  		cmd          string
    23  		expectedCode int64
    24  	}{
    25  		{
    26  			doc:          "wait-nonblocking-exit-0",
    27  			cmd:          "exit 0",
    28  			expectedCode: 0,
    29  		},
    30  		{
    31  			doc:          "wait-nonblocking-exit-random",
    32  			cmd:          "exit 99",
    33  			expectedCode: 99,
    34  		},
    35  	}
    36  
    37  	for _, tc := range testCases {
    38  		tc := tc
    39  		t.Run(tc.doc, func(t *testing.T) {
    40  			t.Parallel()
    41  			ctx := context.Background()
    42  			containerID := container.Run(ctx, t, cli, container.WithCmd("sh", "-c", tc.cmd))
    43  			poll.WaitOn(t, container.IsInState(ctx, cli, containerID, "exited"), poll.WithTimeout(30*time.Second), poll.WithDelay(100*time.Millisecond))
    44  
    45  			waitResC, errC := cli.ContainerWait(ctx, containerID, "")
    46  			select {
    47  			case err := <-errC:
    48  				assert.NilError(t, err)
    49  			case waitRes := <-waitResC:
    50  				assert.Check(t, is.Equal(tc.expectedCode, waitRes.StatusCode))
    51  			}
    52  		})
    53  	}
    54  }
    55  
    56  func TestWaitBlocked(t *testing.T) {
    57  	// Windows busybox does not support trap in this way, not sleep with sub-second
    58  	// granularity. It will always exit 0x40010004.
    59  	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
    60  	defer setupTest(t)()
    61  	cli := request.NewAPIClient(t)
    62  
    63  	testCases := []struct {
    64  		doc          string
    65  		cmd          string
    66  		expectedCode int64
    67  	}{
    68  		{
    69  			doc:          "test-wait-blocked-exit-zero",
    70  			cmd:          "trap 'exit 0' TERM; while true; do usleep 10; done",
    71  			expectedCode: 0,
    72  		},
    73  		{
    74  			doc:          "test-wait-blocked-exit-random",
    75  			cmd:          "trap 'exit 99' TERM; while true; do usleep 10; done",
    76  			expectedCode: 99,
    77  		},
    78  	}
    79  	for _, tc := range testCases {
    80  		tc := tc
    81  		t.Run(tc.doc, func(t *testing.T) {
    82  			t.Parallel()
    83  			ctx := context.Background()
    84  			containerID := container.Run(ctx, t, cli, container.WithCmd("sh", "-c", tc.cmd))
    85  			poll.WaitOn(t, container.IsInState(ctx, cli, containerID, "running"), poll.WithTimeout(30*time.Second), poll.WithDelay(100*time.Millisecond))
    86  
    87  			waitResC, errC := cli.ContainerWait(ctx, containerID, "")
    88  
    89  			err := cli.ContainerStop(ctx, containerID, nil)
    90  			assert.NilError(t, err)
    91  
    92  			select {
    93  			case err := <-errC:
    94  				assert.NilError(t, err)
    95  			case waitRes := <-waitResC:
    96  				assert.Check(t, is.Equal(tc.expectedCode, waitRes.StatusCode))
    97  			case <-time.After(2 * time.Second):
    98  				t.Fatal("timeout waiting for `docker wait`")
    99  			}
   100  		})
   101  	}
   102  }