github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/container/health_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/api/types"
     9  	containertypes "github.com/demonoid81/moby/api/types/container"
    10  	"github.com/demonoid81/moby/client"
    11  	"github.com/demonoid81/moby/integration/internal/container"
    12  	"gotest.tools/v3/assert"
    13  	"gotest.tools/v3/poll"
    14  	"gotest.tools/v3/skip"
    15  )
    16  
    17  // TestHealthCheckWorkdir verifies that health-checks inherit the containers'
    18  // working-dir.
    19  func TestHealthCheckWorkdir(t *testing.T) {
    20  	skip.If(t, testEnv.OSType == "windows", "FIXME")
    21  	defer setupTest(t)()
    22  	ctx := context.Background()
    23  	client := testEnv.APIClient()
    24  
    25  	cID := container.Run(ctx, t, client, container.WithTty(true), container.WithWorkingDir("/foo"), func(c *container.TestContainerConfig) {
    26  		c.Config.Healthcheck = &containertypes.HealthConfig{
    27  			Test:     []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"},
    28  			Interval: 50 * time.Millisecond,
    29  			Retries:  3,
    30  		}
    31  	})
    32  
    33  	poll.WaitOn(t, pollForHealthStatus(ctx, client, cID, types.Healthy), poll.WithDelay(100*time.Millisecond))
    34  }
    35  
    36  // GitHub #37263
    37  // Do not stop healthchecks just because we sent a signal to the container
    38  func TestHealthKillContainer(t *testing.T) {
    39  	skip.If(t, testEnv.OSType == "windows", "Windows only supports SIGKILL and SIGTERM? See https://github.com/moby/moby/issues/39574")
    40  	defer setupTest(t)()
    41  
    42  	ctx := context.Background()
    43  	client := testEnv.APIClient()
    44  
    45  	id := container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
    46  		c.Config.Healthcheck = &containertypes.HealthConfig{
    47  			Test:     []string{"CMD-SHELL", "sleep 1"},
    48  			Interval: time.Second,
    49  			Retries:  5,
    50  		}
    51  	})
    52  
    53  	ctxPoll, cancel := context.WithTimeout(ctx, 30*time.Second)
    54  	defer cancel()
    55  	poll.WaitOn(t, pollForHealthStatus(ctxPoll, client, id, "healthy"), poll.WithDelay(100*time.Millisecond))
    56  
    57  	err := client.ContainerKill(ctx, id, "SIGUSR1")
    58  	assert.NilError(t, err)
    59  
    60  	ctxPoll, cancel = context.WithTimeout(ctx, 30*time.Second)
    61  	defer cancel()
    62  	poll.WaitOn(t, pollForHealthStatus(ctxPoll, client, id, "healthy"), poll.WithDelay(100*time.Millisecond))
    63  }
    64  
    65  func pollForHealthStatus(ctx context.Context, client client.APIClient, containerID string, healthStatus string) func(log poll.LogT) poll.Result {
    66  	return func(log poll.LogT) poll.Result {
    67  		inspect, err := client.ContainerInspect(ctx, containerID)
    68  
    69  		switch {
    70  		case err != nil:
    71  			return poll.Error(err)
    72  		case inspect.State.Health.Status == healthStatus:
    73  			return poll.Success()
    74  		default:
    75  			return poll.Continue("waiting for container to become %s", healthStatus)
    76  		}
    77  	}
    78  }