-
-
Notifications
You must be signed in to change notification settings - Fork 502
/
options_test.go
240 lines (210 loc) · 6.67 KB
/
options_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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package testcontainers_test
import (
"context"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/wait"
)
func TestOverrideContainerRequest(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{
"BAR": "BAR",
},
Image: "foo",
ExposedPorts: []string{"12345/tcp"},
WaitingFor: wait.ForNop(
func(ctx context.Context, target wait.StrategyTarget) error {
return nil
},
),
Networks: []string{"foo", "bar", "baaz"},
NetworkAliases: map[string][]string{
"foo": {"foo0", "foo1", "foo2", "foo3"},
},
},
}
toBeMergedRequest := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{
"FOO": "FOO",
},
Image: "bar",
ExposedPorts: []string{"67890/tcp"},
Networks: []string{"foo1", "bar1"},
NetworkAliases: map[string][]string{
"foo1": {"bar"},
},
WaitingFor: wait.ForLog("foo"),
},
}
// the toBeMergedRequest should be merged into the req
err := testcontainers.CustomizeRequest(toBeMergedRequest)(&req)
require.NoError(t, err)
// toBeMergedRequest should not be changed
assert.Equal(t, "", toBeMergedRequest.Env["BAR"])
require.Len(t, toBeMergedRequest.ExposedPorts, 1)
assert.Equal(t, "67890/tcp", toBeMergedRequest.ExposedPorts[0])
// req should be merged with toBeMergedRequest
assert.Equal(t, "FOO", req.Env["FOO"])
assert.Equal(t, "BAR", req.Env["BAR"])
assert.Equal(t, "bar", req.Image)
assert.Equal(t, []string{"12345/tcp", "67890/tcp"}, req.ExposedPorts)
assert.Equal(t, []string{"foo", "bar", "baaz", "foo1", "bar1"}, req.Networks)
assert.Equal(t, []string{"foo0", "foo1", "foo2", "foo3"}, req.NetworkAliases["foo"])
assert.Equal(t, []string{"bar"}, req.NetworkAliases["foo1"])
assert.Equal(t, wait.ForLog("foo"), req.WaitingFor)
}
type msgsLogConsumer struct {
msgs []string
}
// Accept prints the log to stdout
func (lc *msgsLogConsumer) Accept(l testcontainers.Log) {
lc.msgs = append(lc.msgs, string(l.Content))
}
func TestWithLogConsumers(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "mysql:8.0.36",
WaitingFor: wait.ForLog("port: 3306 MySQL Community Server - GPL"),
},
Started: true,
}
lc := &msgsLogConsumer{}
err := testcontainers.WithLogConsumers(lc)(&req)
require.NoError(t, err)
ctx := context.Background()
c, err := testcontainers.GenericContainer(ctx, req)
testcontainers.CleanupContainer(t, c)
// we expect an error because the MySQL environment variables are not set
// but this is expected because we just want to test the log consumer
require.ErrorContains(t, err, "container exited with code 1")
require.NotEmpty(t, lc.msgs)
}
func TestWithStartupCommand(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "alpine",
Entrypoint: []string{"tail", "-f", "/dev/null"},
},
Started: true,
}
testExec := testcontainers.NewRawCommand([]string{"touch", "/tmp/.testcontainers"})
err := testcontainers.WithStartupCommand(testExec)(&req)
require.NoError(t, err)
require.Len(t, req.LifecycleHooks, 1)
require.Len(t, req.LifecycleHooks[0].PostStarts, 1)
c, err := testcontainers.GenericContainer(context.Background(), req)
testcontainers.CleanupContainer(t, c)
require.NoError(t, err)
_, reader, err := c.Exec(context.Background(), []string{"ls", "/tmp/.testcontainers"}, exec.Multiplexed())
require.NoError(t, err)
content, err := io.ReadAll(reader)
require.NoError(t, err)
assert.Equal(t, "/tmp/.testcontainers\n", string(content))
}
func TestWithAfterReadyCommand(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "alpine",
Entrypoint: []string{"tail", "-f", "/dev/null"},
},
Started: true,
}
testExec := testcontainers.NewRawCommand([]string{"touch", "/tmp/.testcontainers"})
err := testcontainers.WithAfterReadyCommand(testExec)(&req)
require.NoError(t, err)
require.Len(t, req.LifecycleHooks, 1)
require.Len(t, req.LifecycleHooks[0].PostReadies, 1)
c, err := testcontainers.GenericContainer(context.Background(), req)
testcontainers.CleanupContainer(t, c)
require.NoError(t, err)
_, reader, err := c.Exec(context.Background(), []string{"ls", "/tmp/.testcontainers"}, exec.Multiplexed())
require.NoError(t, err)
content, err := io.ReadAll(reader)
require.NoError(t, err)
assert.Equal(t, "/tmp/.testcontainers\n", string(content))
}
func TestWithEnv(t *testing.T) {
tests := map[string]struct {
req *testcontainers.GenericContainerRequest
env map[string]string
expect map[string]string
}{
"add": {
req: &testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{"KEY1": "VAL1"},
},
},
env: map[string]string{"KEY2": "VAL2"},
expect: map[string]string{
"KEY1": "VAL1",
"KEY2": "VAL2",
},
},
"add-nil": {
req: &testcontainers.GenericContainerRequest{},
env: map[string]string{"KEY2": "VAL2"},
expect: map[string]string{"KEY2": "VAL2"},
},
"override": {
req: &testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{
"KEY1": "VAL1",
"KEY2": "VAL2",
},
},
},
env: map[string]string{"KEY2": "VAL3"},
expect: map[string]string{
"KEY1": "VAL1",
"KEY2": "VAL3",
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
opt := testcontainers.WithEnv(tc.env)
require.NoError(t, opt.Customize(tc.req))
require.Equal(t, tc.expect, tc.req.Env)
})
}
}
func TestWithHostPortAccess(t *testing.T) {
tests := []struct {
name string
req *testcontainers.GenericContainerRequest
hostPorts []int
expect []int
}{
{
name: "add to existing",
req: &testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
HostAccessPorts: []int{1, 2},
},
},
hostPorts: []int{3, 4},
expect: []int{1, 2, 3, 4},
},
{
name: "add to nil",
req: &testcontainers.GenericContainerRequest{},
hostPorts: []int{3, 4},
expect: []int{3, 4},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
opt := testcontainers.WithHostPortAccess(tc.hostPorts...)
require.NoError(t, opt.Customize(tc.req))
require.Equal(t, tc.expect, tc.req.HostAccessPorts)
})
}
}