/
container.go
130 lines (112 loc) · 3.47 KB
/
container.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
package testmongo
import (
"context"
"fmt"
"github.com/kyleishie/testdeps/pkg/common"
"github.com/kyleishie/testdeps/pkg/options"
tc "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"testing"
)
const (
image = "mongo"
mappedPort = "27017/tcp"
readyLog = "Waiting for connections"
proto = "mongodb"
)
type Container struct {
tc.Container
ConnectionString string
}
// Run creates and starts a docker Container with the `mongo` image.
// Defaults to `mongo:latest` if no option sets image tag.
// A default context is used with a timeout of two minutes. To customize use RunWithContext.
func Run(opts ...options.Option) (*Container, error) {
ctx, cancel := context.WithTimeout(context.Background(), common.DefaultConnTimeout)
defer cancel()
return RunWithContext(ctx, opts...)
}
// RunWithContext creates and starts a docker Container with the `mongo` image.
// Defaults to `mongo:latest` if no option sets image tag.
// A context can be provided to configure things such as timeout.
func RunWithContext(ctx context.Context, opts ...options.Option) (con *Container, err error) {
cReq, err := makeContainerRequest(opts)
if err != nil {
return
}
c, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{
ContainerRequest: cReq,
Started: true,
})
if err != nil {
return
}
host, err := c.Host(ctx)
port, err := c.MappedPort(ctx, mappedPort)
if err != nil {
return
}
con = &Container{
Container: c,
ConnectionString: fmt.Sprintf("%s://%s%s:%d", proto, makeRootUserPrefix(cReq), host, port.Int()),
}
return
}
// RunTest creates and starts a docker Container with the `mongo` image.
// Defaults to `mongo:latest` if no option sets image tag.
// The Container is automatically terminated after the test is finished.
// A default context is used with a timeout of two minutes. To customize use RunTestWithContext.
func RunTest(t *testing.T, opts ...options.Option) (con *Container, err error) {
ctx, cancel := context.WithTimeout(context.Background(), common.DefaultConnTimeout)
defer cancel()
return RunTestWithContext(t, ctx, opts...)
}
// RunTestWithContext creates and starts a docker Container with the `mongo` image.
// Defaults to `mongo:latest` if no option sets image tag.
// A context can be provided to configure things such as timeout.
// The Container is automatically terminated after the test is finished.
func RunTestWithContext(t *testing.T, ctx context.Context, opts ...options.Option) (con *Container, err error) {
c, err := RunWithContext(ctx, opts...)
if err != nil {
return nil, err
}
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), common.DefaultConnTimeout)
defer cancel()
if err := c.Terminate(ctx); err != nil {
t.Error(err)
}
})
return c, nil
}
func makeContainerRequest(opts []options.Option) (cReq tc.ContainerRequest, err error) {
defer func() {
if err != nil {
cReq = tc.ContainerRequest{}
}
}()
cReq = tc.ContainerRequest{
Image: image,
ExposedPorts: []string{
mappedPort,
},
WaitingFor: wait.ForLog(readyLog),
AutoRemove: true,
}
/// Apply opts
for _, opt := range opts {
err = opt(&cReq)
if err != nil {
return
}
}
return
}
func makeRootUserPrefix(cReq tc.ContainerRequest) (auth string) {
username, uExists := cReq.Env[env_MONGO_INITDB_ROOT_USERNAME]
password, pExists := cReq.Env[env_MONGO_INITDB_ROOT_PASSWORD]
if uExists && pExists {
auth = fmt.Sprintf("%s:%s@", username, password)
}
return
}