-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
reaper_test.go
96 lines (81 loc) · 2.32 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
package testcontainers
import (
"context"
"errors"
"testing"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go/wait"
)
type mockReaperProvider struct {
req ContainerRequest
config TestContainersConfig
}
var errExpected = errors.New("expected")
func (m *mockReaperProvider) RunContainer(ctx context.Context, req ContainerRequest) (Container, error) {
m.req = req
// 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",
ExposedPorts: []string{"8080/tcp"},
Labels: map[string]string{
TestcontainerLabel: "true",
TestcontainerLabelIsReaper: "true",
TestcontainerLabelSessionID: "sessionId",
},
SkipReaper: true,
Mounts: Mounts(BindMount("/var/run/docker.sock", "/var/run/docker.sock")),
AutoRemove: true,
WaitingFor: wait.ForListeningPort(nat.Port("8080/tcp")),
NetworkMode: "bridge",
}
if customize == nil {
return req
}
return customize(req)
}
func Test_NewReaper(t *testing.T) {
type cases struct {
name string
req ContainerRequest
config TestContainersConfig
}
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{
RyukPrivileged: true,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// make sure we re-initialize the singleton
reaper = nil
provider := &mockReaperProvider{
config: test.config,
}
_, err := NewReaper(context.TODO(), "sessionId", provider, "reaperImage")
// we should have errored out see mockReaperProvider.RunContainer
assert.EqualError(t, err, "expected")
assert.Equal(t, test.req, provider.req, "expected ContainerRequest doesn't match the submitted request")
})
}
}