github.com/rish1988/moby@v25.0.2+incompatible/integration/internal/container/states.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/docker/docker/client"
     8  	"github.com/docker/docker/errdefs"
     9  	"github.com/pkg/errors"
    10  	"gotest.tools/v3/poll"
    11  )
    12  
    13  // RunningStateFlagIs polls for the container's Running state flag to be equal to running.
    14  func RunningStateFlagIs(ctx context.Context, apiClient client.APIClient, containerID string, running bool) func(log poll.LogT) poll.Result {
    15  	return func(log poll.LogT) poll.Result {
    16  		inspect, err := apiClient.ContainerInspect(ctx, containerID)
    17  
    18  		switch {
    19  		case err != nil:
    20  			return poll.Error(err)
    21  		case inspect.State.Running == running:
    22  			return poll.Success()
    23  		default:
    24  			return poll.Continue("waiting for container to be %s", map[bool]string{true: "running", false: "stopped"}[running])
    25  		}
    26  	}
    27  }
    28  
    29  // IsStopped verifies the container is in stopped state.
    30  func IsStopped(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result {
    31  	return RunningStateFlagIs(ctx, apiClient, containerID, false)
    32  }
    33  
    34  // IsInState verifies the container is in one of the specified state, e.g., "running", "exited", etc.
    35  func IsInState(ctx context.Context, apiClient client.APIClient, containerID string, state ...string) func(log poll.LogT) poll.Result {
    36  	return func(log poll.LogT) poll.Result {
    37  		inspect, err := apiClient.ContainerInspect(ctx, containerID)
    38  		if err != nil {
    39  			return poll.Error(err)
    40  		}
    41  		for _, v := range state {
    42  			if inspect.State.Status == v {
    43  				return poll.Success()
    44  			}
    45  		}
    46  		return poll.Continue("waiting for container to be one of (%s), currently %s", strings.Join(state, ", "), inspect.State.Status)
    47  	}
    48  }
    49  
    50  // IsSuccessful verifies state.Status == "exited" && state.ExitCode == 0
    51  func IsSuccessful(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result {
    52  	return func(log poll.LogT) poll.Result {
    53  		inspect, err := apiClient.ContainerInspect(ctx, containerID)
    54  		if err != nil {
    55  			return poll.Error(err)
    56  		}
    57  		if inspect.State.Status == "exited" {
    58  			if inspect.State.ExitCode == 0 {
    59  				return poll.Success()
    60  			}
    61  			return poll.Error(errors.Errorf("expected exit code 0, got %d", inspect.State.ExitCode))
    62  		}
    63  		return poll.Continue("waiting for container to be \"exited\", currently %s", inspect.State.Status)
    64  	}
    65  }
    66  
    67  // IsRemoved verifies the container has been removed
    68  func IsRemoved(ctx context.Context, apiClient client.APIClient, containerID string) func(log poll.LogT) poll.Result {
    69  	return func(log poll.LogT) poll.Result {
    70  		inspect, err := apiClient.ContainerInspect(ctx, containerID)
    71  		if err != nil {
    72  			if errdefs.IsNotFound(err) {
    73  				return poll.Success()
    74  			}
    75  			return poll.Error(err)
    76  		}
    77  		return poll.Continue("waiting for container to be removed, currently %s", inspect.State.Status)
    78  	}
    79  }