forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose.go
352 lines (291 loc) · 9.74 KB
/
compose.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 testcontainers
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/testcontainers/testcontainers-go/wait"
"gopkg.in/yaml.v2"
)
const (
envProjectName = "COMPOSE_PROJECT_NAME"
envComposeFile = "COMPOSE_FILE"
)
// DockerCompose defines the contract for running Docker Compose
type DockerCompose interface {
Down() ExecError
Invoke() ExecError
WaitForService(string, wait.Strategy) DockerCompose
WithCommand([]string) DockerCompose
WithEnv(map[string]string) DockerCompose
WithExposedService(string, int, wait.Strategy) DockerCompose
}
type waitService struct {
service string
publishedPort int
}
// LocalDockerCompose represents a Docker Compose execution using local binary
// docker-compose or docker-compose.exe, depending on the underlying platform
type LocalDockerCompose struct {
Executable string
ComposeFilePaths []string
absComposeFilePaths []string
Identifier string
Cmd []string
Env map[string]string
Services map[string]interface{}
waitStrategySupplied bool
WaitStrategyMap map[waitService]wait.Strategy
}
// NewLocalDockerCompose returns an instance of the local Docker Compose, using an
// array of Docker Compose file paths and an identifier for the Compose execution.
//
// It will iterate through the array adding '-f compose-file-path' flags to the local
// Docker Compose execution. The identifier represents the name of the execution,
// which will define the name of the underlying Docker network and the name of the
// running Compose services.
func NewLocalDockerCompose(filePaths []string, identifier string) *LocalDockerCompose {
dc := &LocalDockerCompose{}
dc.Executable = "docker-compose"
if runtime.GOOS == "windows" {
dc.Executable = "docker-compose.exe"
}
dc.ComposeFilePaths = filePaths
dc.absComposeFilePaths = make([]string, len(filePaths))
for i, cfp := range dc.ComposeFilePaths {
abs, _ := filepath.Abs(cfp)
dc.absComposeFilePaths[i] = abs
}
dc.validate()
dc.Identifier = strings.ToLower(identifier)
dc.waitStrategySupplied = false
dc.WaitStrategyMap = make(map[waitService]wait.Strategy)
return dc
}
// Down executes docker-compose down
func (dc *LocalDockerCompose) Down() ExecError {
return executeCompose(dc, []string{"down", "--remove-orphans"})
}
func (dc *LocalDockerCompose) getDockerComposeEnvironment() map[string]string {
environment := map[string]string{}
composeFileEnvVariableValue := ""
for _, abs := range dc.absComposeFilePaths {
composeFileEnvVariableValue += abs + string(os.PathListSeparator)
}
environment[envProjectName] = dc.Identifier
environment[envComposeFile] = composeFileEnvVariableValue
return environment
}
func (dc *LocalDockerCompose) applyStrategyToRunningContainer() error {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return err
}
for k := range dc.WaitStrategyMap {
// TODO: if the compose file includes a container_name, this won't work,
// as we cannot delegate to the names generated by Docker Compose
containerName := dc.Identifier + "_" + k.service
// Docker compose appends "_1" to every started service by default. Trimming to match running docker container
f := filters.NewArgs(filters.Arg("name", containerName))
containerListOptions := types.ContainerListOptions{Filters: f}
containers, err := cli.ContainerList(context.Background(), containerListOptions)
if err != nil {
return fmt.Errorf("error %w occured while filtering the service %s: %d by name and published port", err, k.service, k.publishedPort)
}
if len(containers) == 0 {
return fmt.Errorf("service with name %s not found in list of running containers", k.service)
}
// The length should always be a list of 1, since we are matching one service name at a time
if l := len(containers); l > 1 {
return fmt.Errorf("expecting only one running container for %s but got %d", k.service, l)
}
container := containers[0]
strategy := dc.WaitStrategyMap[k]
dockerProvider, err := NewDockerProvider()
if err != nil {
return fmt.Errorf("unable to create new Docker Provider: %w", err)
}
dockercontainer := &DockerContainer{ID: container.ID, WaitingFor: strategy, provider: dockerProvider}
err = strategy.WaitUntilReady(context.Background(), dockercontainer)
if err != nil {
return fmt.Errorf("Unable to apply wait strategy %v to service %s due to %w", strategy, k.service, err)
}
}
return nil
}
// Invoke invokes the docker compose
func (dc *LocalDockerCompose) Invoke() ExecError {
return executeCompose(dc, dc.Cmd)
}
// WaitForService sets the strategy for the service that is to be waited on
func (dc *LocalDockerCompose) WaitForService(service string, strategy wait.Strategy) DockerCompose {
dc.waitStrategySupplied = true
dc.WaitStrategyMap[waitService{service: service}] = strategy
return dc
}
// WithCommand assigns the command
func (dc *LocalDockerCompose) WithCommand(cmd []string) DockerCompose {
dc.Cmd = cmd
return dc
}
// WithEnv assigns the environment
func (dc *LocalDockerCompose) WithEnv(env map[string]string) DockerCompose {
dc.Env = env
return dc
}
// WithExposedService sets the strategy for the service that is to be waited on. If multiple strategies
// are given for a single service running on different ports, both strategies will be applied on the same container
func (dc *LocalDockerCompose) WithExposedService(service string, port int, strategy wait.Strategy) DockerCompose {
dc.waitStrategySupplied = true
dc.WaitStrategyMap[waitService{service: service, publishedPort: port}] = strategy
return dc
}
// validate checks if the files to be run in the compose are valid YAML files, setting up
// references to all services in them
func (dc *LocalDockerCompose) validate() error {
type compose struct {
Services map[string]interface{}
}
for _, abs := range dc.absComposeFilePaths {
c := compose{}
yamlFile, err := ioutil.ReadFile(abs)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, &c)
if err != nil {
return err
}
dc.Services = c.Services
}
return nil
}
// ExecError is super struct that holds any information about an execution error, so the client code
// can handle the result
type ExecError struct {
Command []string
Error error
Stdout error
Stderr error
}
// execute executes a program with arguments and environment variables inside a specific directory
func execute(
dirContext string, environment map[string]string, binary string, args []string) ExecError {
var errStdout, errStderr error
cmd := exec.Command(binary, args...)
cmd.Dir = dirContext
cmd.Env = os.Environ()
for key, value := range environment {
cmd.Env = append(cmd.Env, key+"="+value)
}
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()
stdout := newCapturingPassThroughWriter(os.Stdout)
stderr := newCapturingPassThroughWriter(os.Stderr)
err := cmd.Start()
if err != nil {
execCmd := []string{"Starting command", dirContext, binary}
execCmd = append(execCmd, args...)
return ExecError{
// add information about the CMD and arguments used
Command: execCmd,
Error: err,
Stderr: errStderr,
Stdout: errStdout,
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
wg.Done()
}()
_, errStderr = io.Copy(stderr, stderrIn)
wg.Wait()
err = cmd.Wait()
execCmd := []string{"Reading std", dirContext, binary}
execCmd = append(execCmd, args...)
return ExecError{
Command: execCmd,
Error: err,
Stderr: errStderr,
Stdout: errStdout,
}
}
func executeCompose(dc *LocalDockerCompose, args []string) ExecError {
if which(dc.Executable) != nil {
return ExecError{
Command: []string{dc.Executable},
Error: fmt.Errorf("Local Docker Compose not found. Is %s on the PATH?", dc.Executable),
}
}
environment := dc.getDockerComposeEnvironment()
for k, v := range dc.Env {
environment[k] = v
}
cmds := []string{}
pwd := "."
if len(dc.absComposeFilePaths) > 0 {
pwd, _ = filepath.Split(dc.absComposeFilePaths[0])
for _, abs := range dc.absComposeFilePaths {
cmds = append(cmds, "-f", abs)
}
} else {
cmds = append(cmds, "-f", "docker-compose.yml")
}
cmds = append(cmds, args...)
execErr := execute(pwd, environment, dc.Executable, cmds)
err := execErr.Error
if err != nil {
args := strings.Join(dc.Cmd, " ")
return ExecError{
Command: []string{dc.Executable},
Error: fmt.Errorf("Local Docker compose exited abnormally whilst running %s: [%v]. %s", dc.Executable, args, err.Error()),
}
}
if dc.waitStrategySupplied {
// If the wait strategy has been executed once for all services during startup , disable it so that it is not invoked while tearing down
dc.waitStrategySupplied = false
if err := dc.applyStrategyToRunningContainer(); err != nil {
return ExecError{
Error: fmt.Errorf("one or more wait strategies could not be applied to the running containers: %w", err),
}
}
}
return execErr
}
// capturingPassThroughWriter is a writer that remembers
// data written to it and passes it to w
type capturingPassThroughWriter struct {
buf bytes.Buffer
w io.Writer
}
// newCapturingPassThroughWriter creates new capturingPassThroughWriter
func newCapturingPassThroughWriter(w io.Writer) *capturingPassThroughWriter {
return &capturingPassThroughWriter{
w: w,
}
}
func (w *capturingPassThroughWriter) Write(d []byte) (int, error) {
w.buf.Write(d)
return w.w.Write(d)
}
// Bytes returns bytes written to the writer
func (w *capturingPassThroughWriter) Bytes() []byte {
return w.buf.Bytes()
}
// Which checks if a binary is present in PATH
func which(binary string) error {
_, err := exec.LookPath(binary)
return err
}