-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwait_test.go
More file actions
209 lines (171 loc) · 4.81 KB
/
wait_test.go
File metadata and controls
209 lines (171 loc) · 4.81 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package docker_test
import (
"context"
"testing"
"time"
"github.com/docker/docker/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/jasoet/pkg/v2/docker"
)
func TestWaitStrategy_WaitForLog(t *testing.T) {
skipIfNoContainerRuntime(t)
ctx := context.Background()
exec, _ := docker.New(
docker.WithImage("nginx:alpine"),
docker.WithPorts("80:0"),
docker.WithAutoRemove(true),
docker.WithWaitStrategy(
docker.WaitForLog("start worker processes").
WithStartupTimeout(30*time.Second),
),
)
err := exec.Start(ctx)
require.NoError(t, err)
defer exec.Terminate(ctx)
running, _ := exec.IsRunning(ctx)
assert.True(t, running)
}
func TestWaitStrategy_WaitForPort(t *testing.T) {
skipIfNoContainerRuntime(t)
ctx := context.Background()
exec, _ := docker.New(
docker.WithImage("nginx:alpine"),
docker.WithPorts("80:8889"),
docker.WithAutoRemove(true),
docker.WithWaitStrategy(
docker.WaitForPort("80").WithStartupTimeout(30*time.Second),
),
)
err := exec.Start(ctx)
require.NoError(t, err)
defer exec.Terminate(ctx)
port, _ := exec.MappedPort(ctx, "80/tcp")
assert.Equal(t, "8889", port)
}
func TestWaitStrategy_WaitForHTTP(t *testing.T) {
skipIfNoContainerRuntime(t)
ctx := context.Background()
exec, _ := docker.New(
docker.WithImage("nginx:alpine"),
docker.WithPorts("80:0"),
docker.WithAutoRemove(true),
docker.WithWaitStrategy(
docker.WaitForHTTP("80", "/", 200).
WithStartupTimeout(30*time.Second),
),
)
err := exec.Start(ctx)
require.NoError(t, err)
defer exec.Terminate(ctx)
running, _ := exec.IsRunning(ctx)
assert.True(t, running)
}
func TestWaitStrategy_ForListeningPort(t *testing.T) {
skipIfNoContainerRuntime(t)
ctx := context.Background()
exec, _ := docker.New(
docker.WithImage("nginx:alpine"),
docker.WithPorts("80:8891"),
docker.WithAutoRemove(true),
docker.WithWaitStrategy(
docker.ForListeningPort("80/tcp").
WithStartupTimeout(30*time.Second),
),
)
err := exec.Start(ctx)
require.NoError(t, err)
defer exec.Terminate(ctx)
port, _ := exec.MappedPort(ctx, "80/tcp")
assert.Equal(t, "8891", port)
}
func TestWaitStrategy_WaitForFunc(t *testing.T) {
skipIfNoContainerRuntime(t)
ctx := context.Background()
customWait := docker.WaitForFunc(func(ctx context.Context, cli *client.Client, containerID string) error {
// Custom wait logic - just wait 1 second
time.Sleep(1 * time.Second)
return nil
}).WithStartupTimeout(10 * time.Second)
exec, _ := docker.New(
docker.WithImage("alpine:latest"),
docker.WithCmd("sleep", "5"),
docker.WithAutoRemove(true),
docker.WithWaitStrategy(customWait),
)
err := exec.Start(ctx)
require.NoError(t, err)
defer exec.Terminate(ctx)
running, _ := exec.IsRunning(ctx)
assert.True(t, running)
}
func TestWaitStrategy_WaitForAll(t *testing.T) {
skipIfNoContainerRuntime(t)
ctx := context.Background()
multiWait := docker.WaitForAll(
docker.WaitForLog("start worker"),
docker.WaitForPort("80"),
).WithStartupTimeout(60 * time.Second)
exec, _ := docker.New(
docker.WithImage("nginx:alpine"),
docker.WithPorts("80:0"),
docker.WithAutoRemove(true),
docker.WithWaitStrategy(multiWait),
)
err := exec.Start(ctx)
require.NoError(t, err)
defer exec.Terminate(ctx)
running, _ := exec.IsRunning(ctx)
assert.True(t, running)
}
func TestWaitStrategy_Timeout(t *testing.T) {
skipIfNoContainerRuntime(t)
ctx := context.Background()
// Wait for a log that will never appear with very short timeout
exec, _ := docker.New(
docker.WithImage("alpine:latest"),
docker.WithCmd("sleep", "10"),
docker.WithAutoRemove(true),
docker.WithWaitStrategy(
docker.WaitForLog("this-will-never-appear").
WithStartupTimeout(1*time.Second),
),
)
err := exec.Start(ctx)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to become ready")
}
func TestWaitStrategy_PortTimeout(t *testing.T) {
skipIfNoContainerRuntime(t)
ctx := context.Background()
// Wait for a port on a container that doesn't expose it
exec, _ := docker.New(
docker.WithImage("alpine:latest"),
docker.WithCmd("sleep", "10"),
docker.WithAutoRemove(true),
docker.WithWaitStrategy(
docker.WaitForPort("8888/tcp").
WithStartupTimeout(2*time.Second),
),
)
err := exec.Start(ctx)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to become ready")
}
func TestWaitStrategy_HTTPTimeout(t *testing.T) {
skipIfNoContainerRuntime(t)
ctx := context.Background()
// Wait for HTTP on a container that doesn't serve HTTP
exec, _ := docker.New(
docker.WithImage("alpine:latest"),
docker.WithCmd("sleep", "10"),
docker.WithAutoRemove(true),
docker.WithWaitStrategy(
docker.WaitForHTTP("8080", "/", 200).
WithStartupTimeout(2*time.Second),
),
)
err := exec.Start(ctx)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to become ready")
}