forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
99 lines (84 loc) · 2.93 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
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
testcontainers.CustomizeRequest(toBeMergedRequest)(&req)
// toBeMergedRequest should not be changed
assert.Equal(t, "", toBeMergedRequest.Env["BAR"])
assert.Equal(t, 1, len(toBeMergedRequest.ExposedPorts))
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)
}
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"})
testcontainers.WithStartupCommand(testExec)(&req)
assert.Equal(t, 1, len(req.LifecycleHooks))
assert.Equal(t, 1, len(req.LifecycleHooks[0].PostStarts))
c, err := testcontainers.GenericContainer(context.Background(), req)
require.NoError(t, err)
defer func() {
err = c.Terminate(context.Background())
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))
}