forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reaper_test.go
190 lines (159 loc) · 6.14 KB
/
reaper_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
package testcontainers
import (
"context"
"errors"
"testing"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go/internal"
"github.com/testcontainers/testcontainers-go/internal/config"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/wait"
)
type mockReaperProvider struct {
req ContainerRequest
hostConfig *container.HostConfig
enpointSettings map[string]*network.EndpointSettings
config TestcontainersConfig
}
var errExpected = errors.New("expected")
func (m *mockReaperProvider) RunContainer(ctx context.Context, req ContainerRequest) (Container, error) {
m.req = req
m.hostConfig = &container.HostConfig{}
m.enpointSettings = map[string]*network.EndpointSettings{}
if req.HostConfigModifier == nil {
req.HostConfigModifier = defaultHostConfigModifier(req)
}
req.HostConfigModifier(m.hostConfig)
if req.EnpointSettingsModifier != nil {
req.EnpointSettingsModifier(m.enpointSettings)
}
// we're only interested in the request, so instead of mocking the Docker client
// we'll error out here
return nil, errExpected
}
func (m *mockReaperProvider) Config() TestcontainersConfig {
return m.config
}
// createContainerRequest creates the expected request and allows for customization
func createContainerRequest(customize func(ContainerRequest) ContainerRequest) ContainerRequest {
req := ContainerRequest{
Image: "reaperImage",
ReaperImage: "reaperImage",
ExposedPorts: []string{"8080/tcp"},
Labels: map[string]string{
TestcontainerLabel: "true",
TestcontainerLabelIsReaper: "true",
testcontainersdocker.LabelReaper: "true",
testcontainersdocker.LabelLang: "go",
testcontainersdocker.LabelVersion: internal.Version,
},
Mounts: Mounts(BindMount(testcontainersdocker.ExtractDockerSocket(context.Background()), "/var/run/docker.sock")),
WaitingFor: wait.ForListeningPort(nat.Port("8080/tcp")),
ReaperOptions: []ContainerOption{
WithImageName("reaperImage"),
},
}
if customize == nil {
return req
}
return customize(req)
}
func Test_NewReaper(t *testing.T) {
type cases struct {
name string
req ContainerRequest
config TestcontainersConfig
ctx context.Context
}
tests := []cases{
{
name: "non-privileged",
req: createContainerRequest(nil),
config: TestcontainersConfig{},
},
{
name: "privileged",
req: createContainerRequest(func(req ContainerRequest) ContainerRequest {
req.Privileged = true
return req
}),
config: TestcontainersConfig{
Config: config.Config{
RyukPrivileged: true,
},
},
},
{
name: "docker-host in context",
req: createContainerRequest(func(req ContainerRequest) ContainerRequest {
req.Mounts = Mounts(BindMount(testcontainersdocker.ExtractDockerSocket(context.Background()), "/var/run/docker.sock"))
return req
}),
config: TestcontainersConfig{},
ctx: context.WithValue(context.TODO(), testcontainersdocker.DockerHostContextKey, testcontainersdocker.DockerSocketPathWithSchema),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
provider := &mockReaperProvider{
config: test.config,
}
if test.ctx == nil {
test.ctx = context.TODO()
}
_, err := newReaper(test.ctx, "sessionId", provider, test.req.ReaperOptions...)
// we should have errored out see mockReaperProvider.RunContainer
assert.EqualError(t, err, "expected")
assert.Equal(t, test.req.Image, provider.req.Image, "expected image doesn't match the submitted request")
assert.Equal(t, test.req.ExposedPorts, provider.req.ExposedPorts, "expected exposed ports don't match the submitted request")
assert.Equal(t, test.req.Labels, provider.req.Labels, "expected labels don't match the submitted request")
assert.Equal(t, test.req.Mounts, provider.req.Mounts, "expected mounts don't match the submitted request")
assert.Equal(t, test.req.WaitingFor, provider.req.WaitingFor, "expected waitingFor don't match the submitted request")
// checks for reaper's preCreationCallback fields
assert.Equal(t, container.NetworkMode(Bridge), provider.hostConfig.NetworkMode, "expected networkMode doesn't match the submitted request")
assert.Equal(t, true, provider.hostConfig.AutoRemove, "expected networkMode doesn't match the submitted request")
})
}
}
func Test_ReaperForNetwork(t *testing.T) {
ctx := context.Background()
networkName := "test-network-with-custom-reaper"
req := GenericNetworkRequest{
NetworkRequest: NetworkRequest{
Name: networkName,
CheckDuplicate: true,
ReaperOptions: []ContainerOption{
WithImageName("reaperImage"),
},
},
}
provider := &mockReaperProvider{
config: TestcontainersConfig{},
}
_, err := newReaper(ctx, "sessionId", provider, req.ReaperOptions...)
assert.EqualError(t, err, "expected")
assert.Equal(t, "reaperImage", provider.req.Image)
assert.Equal(t, "reaperImage", provider.req.ReaperImage)
}
func Test_ReaperReusedIfHealthy(t *testing.T) {
SkipIfProviderIsNotHealthy(&testing.T{})
ctx := context.Background()
// As other integration tests run with the (shared) Reaper as well, re-use the instance to not interrupt other tests
wasReaperRunning := reaperInstance != nil
provider, _ := ProviderDocker.GetProvider()
reaper, err := reuseOrCreateReaper(context.WithValue(ctx, testcontainersdocker.DockerHostContextKey, provider.(*DockerProvider).host), "sessionId", provider)
assert.NoError(t, err, "creating the Reaper should not error")
reaperReused, _ := reuseOrCreateReaper(context.WithValue(ctx, testcontainersdocker.DockerHostContextKey, provider.(*DockerProvider).host), "sessionId", provider)
assert.Same(t, reaper, reaperReused, "expecting the same reaper instance is returned if running and healthy")
terminate, err := reaper.Connect()
defer func(term chan bool) {
term <- true
}(terminate)
assert.NoError(t, err, "connecting to Reaper should be successful")
if !wasReaperRunning {
terminateContainerOnEnd(t, ctx, reaper.container)
}
}