forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reaper_test.go
149 lines (120 loc) · 3.96 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
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
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{
RyukPrivileged: true,
},
},
{
name: "docker-host in context",
req: createContainerRequest(func(req ContainerRequest) ContainerRequest {
req.Mounts = Mounts(BindMount("/value/in/context.sock", "/var/run/docker.sock"))
return req
}),
config: TestContainersConfig{},
ctx: context.WithValue(context.TODO(), dockerHostContextKey, "unix:///value/in/context.sock"),
},
}
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,
}
if test.ctx == nil {
test.ctx = context.TODO()
}
_, err := NewReaper(test.ctx, "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")
})
}
}
func Test_ExtractDockerHost(t *testing.T) {
t.Run("Docker Host as environment variable", func(t *testing.T) {
t.Setenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/path/to/docker.sock")
host := extractDockerHost(context.Background())
assert.Equal(t, "/path/to/docker.sock", host)
})
t.Run("Default Docker Host", func(t *testing.T) {
host := extractDockerHost(context.Background())
assert.Equal(t, "/var/run/docker.sock", host)
})
t.Run("Malformed Docker Host is passed in context", func(t *testing.T) {
ctx := context.Background()
host := extractDockerHost(context.WithValue(ctx, dockerHostContextKey, "path-to-docker-sock"))
assert.Equal(t, "/var/run/docker.sock", host)
})
t.Run("Malformed Schema Docker Host is passed in context", func(t *testing.T) {
ctx := context.Background()
host := extractDockerHost(context.WithValue(ctx, dockerHostContextKey, "http://path to docker sock"))
assert.Equal(t, "/var/run/docker.sock", host)
})
t.Run("Unix Docker Host is passed in context", func(t *testing.T) {
ctx := context.Background()
host := extractDockerHost(context.WithValue(ctx, dockerHostContextKey, "unix:///this/is/a/sample.sock"))
assert.Equal(t, "/this/is/a/sample.sock", host)
})
}