2020-03-01 16:06:34 +00:00
|
|
|
package wait
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-21 21:04:28 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-03-01 16:06:34 +00:00
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2020-03-01 16:06:34 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
2023-08-21 21:04:28 +00:00
|
|
|
"github.com/testcontainers/testcontainers-go/exec"
|
2020-03-01 16:06:34 +00:00
|
|
|
)
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
// Strategy defines the basic interface for a Wait Strategy
|
2020-03-01 16:06:34 +00:00
|
|
|
type Strategy interface {
|
|
|
|
WaitUntilReady(context.Context, StrategyTarget) error
|
|
|
|
}
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
// StrategyTimeout allows MultiStrategy to configure a Strategy's Timeout
|
|
|
|
type StrategyTimeout interface {
|
|
|
|
Timeout() *time.Duration
|
|
|
|
}
|
|
|
|
|
2020-03-01 16:06:34 +00:00
|
|
|
type StrategyTarget interface {
|
|
|
|
Host(context.Context) (string, error)
|
2023-08-21 21:04:28 +00:00
|
|
|
Ports(ctx context.Context) (nat.PortMap, error)
|
2020-03-01 16:06:34 +00:00
|
|
|
MappedPort(context.Context, nat.Port) (nat.Port, error)
|
|
|
|
Logs(context.Context) (io.ReadCloser, error)
|
2023-08-21 21:04:28 +00:00
|
|
|
Exec(context.Context, []string, ...exec.ProcessOption) (int, io.Reader, error)
|
|
|
|
State(context.Context) (*types.ContainerState, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkTarget(ctx context.Context, target StrategyTarget) error {
|
|
|
|
state, err := target.State(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return checkState(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkState(state *types.ContainerState) error {
|
|
|
|
switch {
|
|
|
|
case state.Running:
|
|
|
|
return nil
|
|
|
|
case state.OOMKilled:
|
|
|
|
return errors.New("container crashed with out-of-memory (OOMKilled)")
|
|
|
|
case state.Status == "exited":
|
|
|
|
return fmt.Errorf("container exited with code %d", state.ExitCode)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected container status %q", state.Status)
|
|
|
|
}
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultStartupTimeout() time.Duration {
|
|
|
|
return 60 * time.Second
|
|
|
|
}
|
2021-01-17 18:00:46 +00:00
|
|
|
|
|
|
|
func defaultPollInterval() time.Duration {
|
|
|
|
return 100 * time.Millisecond
|
|
|
|
}
|