2018-02-22 05:30:51 -05:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2018-02-22 05:30:51 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/client"
|
2018-10-10 06:20:13 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/poll"
|
2018-02-22 05:30:51 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsStopped verifies the container is in stopped state.
|
|
|
|
func IsStopped(ctx context.Context, client client.APIClient, containerID string) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
inspect, err := client.ContainerInspect(ctx, containerID)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return poll.Error(err)
|
|
|
|
case !inspect.State.Running:
|
|
|
|
return poll.Success()
|
|
|
|
default:
|
|
|
|
return poll.Continue("waiting for container to be stopped")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInState verifies the container is in one of the specified state, e.g., "running", "exited", etc.
|
|
|
|
func IsInState(ctx context.Context, client client.APIClient, containerID string, state ...string) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
inspect, err := client.ContainerInspect(ctx, containerID)
|
|
|
|
if err != nil {
|
|
|
|
return poll.Error(err)
|
|
|
|
}
|
|
|
|
for _, v := range state {
|
|
|
|
if inspect.State.Status == v {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for container to be one of (%s), currently %s", strings.Join(state, ", "), inspect.State.Status)
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 06:20:13 -04:00
|
|
|
|
|
|
|
// IsSuccessful verifies state.Status == "exited" && state.ExitCode == 0
|
|
|
|
func IsSuccessful(ctx context.Context, client client.APIClient, containerID string) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
inspect, err := client.ContainerInspect(ctx, containerID)
|
|
|
|
if err != nil {
|
|
|
|
return poll.Error(err)
|
|
|
|
}
|
|
|
|
if inspect.State.Status == "exited" {
|
|
|
|
if inspect.State.ExitCode == 0 {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Error(errors.Errorf("expected exit code 0, got %d", inspect.State.ExitCode))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for container to be \"exited\", currently %s", inspect.State.Status)
|
|
|
|
}
|
|
|
|
}
|