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

     1  package container
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/client"
     7  	"github.com/gotestyourself/gotestyourself/poll"
     8  	"golang.org/x/net/context"
     9  )
    10  
    11  // IsStopped verifies the container is in stopped state.
    12  func IsStopped(ctx context.Context, client client.APIClient, containerID string) func(log poll.LogT) poll.Result {
    13  	return func(log poll.LogT) poll.Result {
    14  		inspect, err := client.ContainerInspect(ctx, containerID)
    15  
    16  		switch {
    17  		case err != nil:
    18  			return poll.Error(err)
    19  		case !inspect.State.Running:
    20  			return poll.Success()
    21  		default:
    22  			return poll.Continue("waiting for container to be stopped")
    23  		}
    24  	}
    25  }
    26  
    27  // IsInState verifies the container is in one of the specified state, e.g., "running", "exited", etc.
    28  func IsInState(ctx context.Context, client client.APIClient, containerID string, state ...string) func(log poll.LogT) poll.Result {
    29  	return func(log poll.LogT) poll.Result {
    30  		inspect, err := client.ContainerInspect(ctx, containerID)
    31  		if err != nil {
    32  			return poll.Error(err)
    33  		}
    34  		for _, v := range state {
    35  			if inspect.State.Status == v {
    36  				return poll.Success()
    37  			}
    38  		}
    39  		return poll.Continue("waiting for container to be one of (%s), currently %s", strings.Join(state, ", "), inspect.State.Status)
    40  	}
    41  }