forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait.go
65 lines (54 loc) · 1.7 KB
/
wait.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package wait
import (
"context"
"errors"
"fmt"
"io"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go/exec"
)
// Strategy defines the basic interface for a Wait Strategy
type Strategy interface {
WaitUntilReady(context.Context, StrategyTarget) error
}
// StrategyTimeout allows MultiStrategy to configure a Strategy's Timeout
type StrategyTimeout interface {
Timeout() *time.Duration
}
type StrategyTarget interface {
Host(context.Context) (string, error)
Inspect(context.Context) (*types.ContainerJSON, error)
Ports(ctx context.Context) (nat.PortMap, error) // Deprecated: use Inspect instead
MappedPort(context.Context, nat.Port) (nat.Port, error)
Logs(context.Context) (io.ReadCloser, error)
Exec(context.Context, []string, ...exec.ProcessOption) (int, io.Reader, error)
State(context.Context) (*types.ContainerState, error)
CopyFileFromContainer(ctx context.Context, filePath string) (io.ReadCloser, error)
}
func checkTarget(ctx context.Context, target StrategyTarget) error {
state, err := target.State(ctx)
if err != nil {
return fmt.Errorf("get state: %w", 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)
}
}
func defaultStartupTimeout() time.Duration {
return 60 * time.Second
}
func defaultPollInterval() time.Duration {
return 100 * time.Millisecond
}