-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathgnomock.go
352 lines (284 loc) · 9.15 KB
/
gnomock.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Package gnomock contains a framework to set up temporary docker containers
// for integration and end-to-end testing of other applications. It handles
// pulling images, starting containers, waiting for them to become available,
// setting up their initial state and cleaning up in the end.
//
// Its power is in a variety of Presets, each implementing a specific database,
// service or other tools. Each preset provides ways of setting up its initial
// state as easily as possible: SQL schema creation, test data upload into S3,
// sending test events to Splunk, etc.
//
// All containers created using Gnomock have a self-destruct mechanism that
// kicks-in right after the test execution completes.
//
// To debug cases where containers don't behave as expected, there are options
// like `WithDebugMode()` or `WithLogWriter()`.
//
// For the list of presets, please refer to
// https://pkg.go.dev/github.com/orlangure/gnomock/preset.
//
// Each preset can then be used in the following way:
//
// p := redis.Preset() // replace "redis" with whatever you need
// container, err := gnomock.Start(p)
// addr := container.DefaultAddress() // e.g localhost:54321
package gnomock
import (
"context"
"errors"
"fmt"
"io"
"net"
"strings"
"time"
"github.com/docker/docker/pkg/stdcopy"
"github.com/google/uuid"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
const defaultTag = "latest"
// newG creates a new Gnomock session with a unique identifier and a dedicated
// logger. It allows to follow a specific action while having multiple
// operations running in parallel.
func newG(debug bool) (*g, error) {
if !debug {
return &g{log: zap.NewNop().Sugar()}, nil
}
id, err := uuid.NewRandom()
if err != nil {
return nil, fmt.Errorf("can't generate a unique session id")
}
cfg := zap.NewDevelopmentConfig()
cfg.Encoding = "json"
l, err := cfg.Build(zap.WithCaller(false))
if err != nil {
return nil, fmt.Errorf("can't setup logger")
}
l = l.With(zap.String("id", id.String()))
return &g{id: id, log: l.Sugar()}, nil
}
// g is a Gnomock operation wrapper, mostly for debug purposes.
type g struct {
id uuid.UUID
log *zap.SugaredLogger
}
// StartCustom creates a new container using provided image and binds random
// ports on the host to the provided ports inside the container. Image may
// include tag, which is set to "latest" by default. Optional configuration is
// available through Option functions. The returned container must be stopped
// when no longer needed using its Stop() method.
func StartCustom(image string, ports NamedPorts, opts ...Option) (*Container, error) {
config, image := buildConfig(opts...), buildImage(image)
g, err := newG(config.Debug)
if err != nil {
return nil, fmt.Errorf("can't create new gnomock session: %w", err)
}
defer func() { _ = g.log.Sync() }()
if config.CustomNamedPorts != nil {
ports = config.CustomNamedPorts
}
if config.CustomImage != "" {
image = config.CustomImage
}
g.log.Infow("starting", "image", image, "ports", ports)
g.log.Infow("using config", "image", image, "ports", ports, "config", config)
c, err := newContainer(g, image, ports, config)
if err != nil {
return c, err
}
g.log.Infow("container is ready to use", "id", c.ID, "ports", c.Ports)
return c, nil
}
func newContainer(g *g, image string, ports NamedPorts, config *Options) (c *Container, err error) {
ctx, cancel := context.WithTimeout(config.ctx, config.Timeout)
defer cancel()
cli, err := g.dockerConnect()
if err != nil {
return nil, fmt.Errorf("can't create docker client: %w", err)
}
defer func() { _ = cli.stopClient() }()
c, err = cli.startContainer(ctx, image, ports, config)
if err != nil {
return nil, fmt.Errorf("can't start container: %w", err)
}
defer func() {
if err != nil {
if !config.Debug && Stop(c) == nil {
c = nil
}
}
}()
err = g.setupLogForwarding(c, cli, config)
if err != nil {
return nil, fmt.Errorf("can't setup log forwarding: %w", err)
}
err = g.wait(ctx, c, config)
if err != nil {
return c, fmt.Errorf("can't connect to container: %w", err)
}
err = g.initf(ctx, c, config)
if err != nil {
return c, fmt.Errorf("can't init container: %w", err)
}
return c, nil
}
func copyf(dst io.Writer, src io.Reader) func() error {
return func() error {
_, err := stdcopy.StdCopy(dst, dst, src)
if err != nil && !errors.Is(err, io.EOF) {
return err
}
return nil
}
}
func closeLogReader(logReader io.ReadCloser, g *errgroup.Group) func() error {
return func() error {
err := logReader.Close()
if err != nil {
return err
}
err = g.Wait()
if err != nil {
return err
}
return nil
}
}
// Start creates a container using the provided Preset. The Preset provides its
// own Options to configure Gnomock container. Usually this is enough, but it
// is still possible to extend/override Preset options with new values. For
// example, wait timeout defined in the Preset, if at all, might be not enough
// for this particular usage, so it can't be changed during this call.
//
// All provided Options are applied. First, Preset options are applied. Then,
// custom Options. If both Preset and custom Options change the same
// configuration, custom Options are used.
//
// It is recommended, but not required, to call `gnomock.Stop()` when the tests
// complete to cleanup the containers.
func Start(p Preset, opts ...Option) (*Container, error) {
presetOpts := p.Options()
mergedOpts := make([]Option, 0, len(opts)+len(presetOpts))
mergedOpts = append(mergedOpts, presetOpts...)
mergedOpts = append(mergedOpts, opts...)
return StartCustom(p.Image(), p.Ports(), mergedOpts...)
}
// Stop stops the provided container and lets docker remove them from the
// system. Stop returns an error if any one of the containers couldn't stop. If
// these containers have sidecar containers, they are stopped as well.
func Stop(cs ...*Container) error {
g, err := newG(isInDocker())
if err != nil {
return err
}
defer func() { _ = g.log.Sync() }()
var eg errgroup.Group
for _, c := range cs {
container := c
eg.Go(func() error {
return g.stop(container)
})
}
return eg.Wait()
}
func (g *g) stop(c *Container) error {
if c == nil {
return nil
}
g.log.Infow("stopping", "container", c)
cli, err := g.dockerConnect()
if err != nil {
return fmt.Errorf("can't create docker client: %w", err)
}
defer func() { _ = cli.stopClient() }()
id, sidecar := parseID(c.ID)
if sidecar != "" {
go func() {
// stop sidecar container when the main one is requested to stop;
// error in this case won't matter, the container has a self-destruct
// timer
_ = cli.stopContainer(context.Background(), sidecar)
_ = cli.stopClient()
}()
}
err = cli.stopContainer(context.Background(), id)
if err != nil {
return fmt.Errorf("can't stop container: %w", err)
}
if c.onStop != nil {
err = c.onStop()
if err != nil {
return fmt.Errorf("can't perform last cleanup: %w", err)
}
}
return cli.removeContainer(context.Background(), id)
}
func buildImage(image string) string {
parts := strings.Split(image, ":")
if noTagSet := len(parts) == 1; noTagSet {
image = fmt.Sprintf("%s:%s", parts[0], defaultTag)
}
return image
}
func (g *g) setupLogForwarding(c *Container, cli *docker, config *Options) error {
logReader, err := cli.readLogs(context.Background(), c.DockerID())
if err != nil {
return fmt.Errorf("can't create log reader: %w", err)
}
eg := &errgroup.Group{}
eg.Go(copyf(config.logWriter, logReader))
c.onStop = closeLogReader(logReader, eg)
return nil
}
func (g *g) wait(ctx context.Context, c *Container, config *Options) error {
g.log.Info("waiting for healthcheck to pass")
delay := time.NewTicker(config.healthcheckInterval)
defer delay.Stop()
var lastErr error
for {
select {
case <-ctx.Done():
return fmt.Errorf("canceled after error: %w", lastErr)
case <-delay.C:
err := config.healthcheck(ctx, envAwareClone(c))
if err == nil {
g.log.Info("container is healthy")
return nil
}
g.log.Infof("healthcheck failed: %s", err.Error())
lastErr = err
}
}
}
func (g *g) initf(ctx context.Context, c *Container, config *Options) error {
g.log.Info("starting initial state setup")
return config.init(ctx, envAwareClone(c))
}
// envAwareClone returns a copy of the provided container adjusted for usage
// inside current environment. For example, if current process runs directly on
// the host where container ports are exposed, an exact copy will be returned.
// For a process running itself inside a container, Host value will be replaced
// by docker host IP address. Anyway, calling Address() on the returned
// container will allow to communicate with it both from inside another
// container or from docker host.
func envAwareClone(c *Container) *Container {
containerCopy := &Container{
ID: c.ID,
Host: c.Host,
Ports: c.Ports,
}
// when gnomock runs inside docker container, the other container is only
// accessible through the host
if isInDocker() {
if isHostDockerInternalAvailable() {
containerCopy.Host = "host.docker.internal"
} else {
containerCopy.Host = c.gateway
}
}
return containerCopy
}
func isHostDockerInternalAvailable() bool {
_, err := net.ResolveIPAddr("ip", "host.docker.internal")
return err == nil
}