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

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/demonoid81/moby/client"
     8  	"github.com/pkg/errors"
     9  	"gotest.tools/v3/poll"
    10  )
    11  
    12  // IsStopped verifies the container is in stopped state.
    13  func IsStopped(ctx context.Context, client client.APIClient, containerID string) func(log poll.LogT) poll.Result {
    14  	return func(log poll.LogT) poll.Result {
    15  		inspect, err := client.ContainerInspect(ctx, containerID)
    16  
    17  		switch {
    18  		case err != nil:
    19  			return poll.Error(err)
    20  		case !inspect.State.Running:
    21  			return poll.Success()
    22  		default:
    23  			return poll.Continue("waiting for container to be stopped")
    24  		}
    25  	}
    26  }
    27  
    28  // IsInState verifies the container is in one of the specified state, e.g., "running", "exited", etc.
    29  func IsInState(ctx context.Context, client client.APIClient, containerID string, state ...string) func(log poll.LogT) poll.Result {
    30  	return func(log poll.LogT) poll.Result {
    31  		inspect, err := client.ContainerInspect(ctx, containerID)
    32  		if err != nil {
    33  			return poll.Error(err)
    34  		}
    35  		for _, v := range state {
    36  			if inspect.State.Status == v {
    37  				return poll.Success()
    38  			}
    39  		}
    40  		return poll.Continue("waiting for container to be one of (%s), currently %s", strings.Join(state, ", "), inspect.State.Status)
    41  	}
    42  }
    43  
    44  // IsSuccessful verifies state.Status == "exited" && state.ExitCode == 0
    45  func IsSuccessful(ctx context.Context, client client.APIClient, containerID string) func(log poll.LogT) poll.Result {
    46  	return func(log poll.LogT) poll.Result {
    47  		inspect, err := client.ContainerInspect(ctx, containerID)
    48  		if err != nil {
    49  			return poll.Error(err)
    50  		}
    51  		if inspect.State.Status == "exited" {
    52  			if inspect.State.ExitCode == 0 {
    53  				return poll.Success()
    54  			}
    55  			return poll.Error(errors.Errorf("expected exit code 0, got %d", inspect.State.ExitCode))
    56  		}
    57  		return poll.Continue("waiting for container to be \"exited\", currently %s", inspect.State.Status)
    58  	}
    59  }