github.com/rumpl/bof@v23.0.0-rc.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/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  }
    60  
    61  // IsRemoved verifies the container has been removed
    62  func IsRemoved(ctx context.Context, cli client.APIClient, containerID string) func(log poll.LogT) poll.Result {
    63  	return func(log poll.LogT) poll.Result {
    64  		inspect, err := cli.ContainerInspect(ctx, containerID)
    65  		if err != nil {
    66  			if client.IsErrNotFound(err) {
    67  				return poll.Success()
    68  			}
    69  			return poll.Error(err)
    70  		}
    71  		return poll.Continue("waiting for container to be removed, currently %s", inspect.State.Status)
    72  	}
    73  }