github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/integration/container/health_test.go (about)

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	containertypes "github.com/docker/docker/api/types/container"
    10  	"github.com/docker/docker/client"
    11  	"github.com/docker/docker/integration/internal/container"
    12  	"github.com/docker/docker/integration/internal/request"
    13  	"github.com/gotestyourself/gotestyourself/poll"
    14  )
    15  
    16  // TestHealthCheckWorkdir verifies that health-checks inherit the containers'
    17  // working-dir.
    18  func TestHealthCheckWorkdir(t *testing.T) {
    19  	defer setupTest(t)()
    20  	ctx := context.Background()
    21  	client := request.NewAPIClient(t)
    22  
    23  	cID := container.Run(t, ctx, client, container.WithTty(true), container.WithWorkingDir("/foo"), func(c *container.TestContainerConfig) {
    24  		c.Config.Healthcheck = &containertypes.HealthConfig{
    25  			Test:     []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"},
    26  			Interval: 50 * time.Millisecond,
    27  			Retries:  3,
    28  		}
    29  	})
    30  
    31  	poll.WaitOn(t, pollForHealthStatus(ctx, client, cID, types.Healthy), poll.WithDelay(100*time.Millisecond))
    32  }
    33  
    34  func pollForHealthStatus(ctx context.Context, client client.APIClient, containerID string, healthStatus string) func(log poll.LogT) poll.Result {
    35  	return func(log poll.LogT) poll.Result {
    36  		inspect, err := client.ContainerInspect(ctx, containerID)
    37  
    38  		switch {
    39  		case err != nil:
    40  			return poll.Error(err)
    41  		case inspect.State.Health.Status == healthStatus:
    42  			return poll.Success()
    43  		default:
    44  			return poll.Continue("waiting for container to become %s", healthStatus)
    45  		}
    46  	}
    47  }