forked from testcontainers/testcontainers-go
/
exec_test.go
141 lines (118 loc) · 3.45 KB
/
exec_test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package wait_test
import (
"context"
"errors"
"io"
"log"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
tcexec "github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/wait"
)
func ExampleExecStrategy() {
ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "localstack/localstack:latest",
WaitingFor: wait.ForExec([]string{"awslocal", "dynamodb", "list-tables"}),
}
localstack, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
panic(err)
}
defer func() {
if err := localstack.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()
// Here you have a running container
}
type mockExecTarget struct {
waitDuration time.Duration
successAfter time.Time
exitCode int
failure error
}
func (st mockExecTarget) Host(_ context.Context) (string, error) {
return "", errors.New("not implemented")
}
func (st mockExecTarget) Ports(ctx context.Context) (nat.PortMap, error) {
return nil, errors.New("not implemented")
}
func (st mockExecTarget) MappedPort(_ context.Context, n nat.Port) (nat.Port, error) {
return n, errors.New("not implemented")
}
func (st mockExecTarget) Logs(_ context.Context) (io.ReadCloser, error) {
return nil, errors.New("not implemented")
}
func (st mockExecTarget) Exec(ctx context.Context, _ []string, options ...tcexec.ProcessOption) (int, io.Reader, error) {
time.Sleep(st.waitDuration)
if err := ctx.Err(); err != nil {
return st.exitCode, nil, err
}
if !st.successAfter.IsZero() && time.Now().After(st.successAfter) {
return 0, nil, st.failure
}
return st.exitCode, nil, st.failure
}
func (st mockExecTarget) State(_ context.Context) (*types.ContainerState, error) {
return nil, errors.New("not implemented")
}
func TestExecStrategyWaitUntilReady(t *testing.T) {
target := mockExecTarget{}
wg := wait.NewExecStrategy([]string{"true"}).
WithStartupTimeout(30 * time.Second)
err := wg.WaitUntilReady(context.Background(), target)
if err != nil {
t.Fatal(err)
}
}
func TestExecStrategyWaitUntilReadyForExec(t *testing.T) {
target := mockExecTarget{}
wg := wait.ForExec([]string{"true"})
err := wg.WaitUntilReady(context.Background(), target)
if err != nil {
t.Fatal(err)
}
}
func TestExecStrategyWaitUntilReady_MultipleChecks(t *testing.T) {
target := mockExecTarget{
exitCode: 10,
successAfter: time.Now().Add(2 * time.Second),
}
wg := wait.NewExecStrategy([]string{"true"}).
WithPollInterval(500 * time.Millisecond)
err := wg.WaitUntilReady(context.Background(), target)
if err != nil {
t.Fatal(err)
}
}
func TestExecStrategyWaitUntilReady_DeadlineExceeded(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
target := mockExecTarget{
waitDuration: 1 * time.Second,
}
wg := wait.NewExecStrategy([]string{"true"})
err := wg.WaitUntilReady(ctx, target)
if err != context.DeadlineExceeded {
t.Fatal(err)
}
}
func TestExecStrategyWaitUntilReady_CustomExitCode(t *testing.T) {
target := mockExecTarget{
exitCode: 10,
}
wg := wait.NewExecStrategy([]string{"true"}).WithExitCodeMatcher(func(exitCode int) bool {
return exitCode == 10
})
err := wg.WaitUntilReady(context.Background(), target)
if err != nil {
t.Fatal(err)
}
}