github.com/ttys3/engine@v17.12.1-ce-rc2+incompatible/integration/container/health_test.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/api/types/container"
    10  	"github.com/docker/docker/api/types/network"
    11  	"github.com/docker/docker/api/types/strslice"
    12  	"github.com/docker/docker/client"
    13  	"github.com/docker/docker/integration/util/request"
    14  	"github.com/gotestyourself/gotestyourself/poll"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  // TestHealthCheckWorkdir verifies that health-checks inherit the containers'
    19  // working-dir.
    20  func TestHealthCheckWorkdir(t *testing.T) {
    21  	defer setupTest(t)()
    22  	ctx := context.Background()
    23  	client := request.NewAPIClient(t)
    24  
    25  	c, err := client.ContainerCreate(ctx,
    26  		&container.Config{
    27  			Image:      "busybox",
    28  			Tty:        true,
    29  			WorkingDir: "/foo",
    30  			Cmd:        strslice.StrSlice([]string{"top"}),
    31  			Healthcheck: &container.HealthConfig{
    32  				Test:     []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"},
    33  				Interval: 50 * time.Millisecond,
    34  				Retries:  3,
    35  			},
    36  		},
    37  		&container.HostConfig{},
    38  		&network.NetworkingConfig{},
    39  		"healthtest",
    40  	)
    41  	require.NoError(t, err)
    42  	err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
    43  	require.NoError(t, err)
    44  
    45  	poll.WaitOn(t, pollForHealthStatus(ctx, client, c.ID, types.Healthy), poll.WithDelay(100*time.Millisecond))
    46  }
    47  
    48  func pollForHealthStatus(ctx context.Context, client client.APIClient, containerID string, healthStatus string) func(log poll.LogT) poll.Result {
    49  	return func(log poll.LogT) poll.Result {
    50  		inspect, err := client.ContainerInspect(ctx, containerID)
    51  
    52  		switch {
    53  		case err != nil:
    54  			return poll.Error(err)
    55  		case inspect.State.Health.Status == healthStatus:
    56  			return poll.Success()
    57  		default:
    58  			return poll.Continue("waiting for container to become %s", healthStatus)
    59  		}
    60  	}
    61  }