forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_test.go
152 lines (124 loc) · 4.51 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
package testcontainers_test
import (
"context"
"io"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"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 TestWithNetwork(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{},
}
testcontainers.WithNetwork("new-network", "alias")(&req)
assert.Equal(t, []string{"new-network"}, req.Networks)
assert.Equal(t, map[string][]string{"new-network": {"alias"}}, req.NetworkAliases)
client, err := testcontainers.NewDockerClientWithOpts(context.Background())
require.NoError(t, err)
args := filters.NewArgs()
args.Add("name", "new-network")
resources, err := client.NetworkList(context.Background(), types.NetworkListOptions{
Filters: args,
})
require.NoError(t, err)
assert.Len(t, resources, 1)
assert.Equal(t, "new-network", resources[0].Name)
}
func TestWithNetworkMultipleCallsWithSameNameReuseTheNetwork(t *testing.T) {
for int := 0; int < 100; int++ {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{},
}
testcontainers.WithNetwork("new-network", "alias")(&req)
assert.Equal(t, []string{"new-network"}, req.Networks)
assert.Equal(t, map[string][]string{"new-network": {"alias"}}, req.NetworkAliases)
}
client, err := testcontainers.NewDockerClientWithOpts(context.Background())
require.NoError(t, err)
args := filters.NewArgs()
args.Add("name", "new-network")
resources, err := client.NetworkList(context.Background(), types.NetworkListOptions{
Filters: args,
})
require.NoError(t, err)
assert.Len(t, resources, 1)
assert.Equal(t, "new-network", resources[0].Name)
}
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))
}