-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
docker_compose.go
99 lines (82 loc) · 3.18 KB
/
docker_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
package docker
import (
"regexp"
"strings"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/shell"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
"gotest.tools/v3/icmd"
)
// Options are Docker options.
type Options struct {
WorkingDir string
EnvVars map[string]string
// Whether ot not to enable buildkit. You can find more information about buildkit here https://docs.docker.com/build/buildkit/#getting-started.
EnableBuildKit bool
// Set a logger that should be used. See the logger package for more info.
Logger *logger.Logger
ProjectName string
}
// RunDockerCompose runs docker compose with the given arguments and options and return stdout/stderr.
func RunDockerCompose(t testing.TestingT, options *Options, args ...string) string {
out, err := runDockerComposeE(t, false, options, args...)
if err != nil {
t.Fatal(err)
}
return out
}
// RunDockerComposeAndGetStdout runs docker compose with the given arguments and options and returns only stdout.
func RunDockerComposeAndGetStdOut(t testing.TestingT, options *Options, args ...string) string {
out, err := runDockerComposeE(t, true, options, args...)
require.NoError(t, err)
return out
}
// RunDockerComposeE runs docker compose with the given arguments and options and return stdout/stderr.
func RunDockerComposeE(t testing.TestingT, options *Options, args ...string) (string, error) {
return runDockerComposeE(t, false, options, args...)
}
func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args ...string) (string, error) {
var cmd shell.Command
projectName := options.ProjectName
if len(projectName) <= 0 {
projectName = strings.ToLower(t.Name())
}
dockerComposeVersionCmd := icmd.Command("docker", "compose", "version")
result := icmd.RunCmd(dockerComposeVersionCmd)
if options.EnableBuildKit {
if options.EnvVars == nil {
options.EnvVars = make(map[string]string)
}
options.EnvVars["DOCKER_BUILDKIT"] = "1"
options.EnvVars["COMPOSE_DOCKER_CLI_BUILD"] = "1"
}
if result.ExitCode == 0 {
cmd = shell.Command{
Command: "docker",
Args: append([]string{"compose", "--project-name", generateValidDockerComposeProjectName(projectName)}, args...),
WorkingDir: options.WorkingDir,
Env: options.EnvVars,
Logger: options.Logger,
}
} else {
cmd = shell.Command{
Command: "docker-compose",
// We append --project-name to ensure containers from multiple different tests using Docker Compose don't end
// up in the same project and end up conflicting with each other.
Args: append([]string{"--project-name", generateValidDockerComposeProjectName(projectName)}, args...),
WorkingDir: options.WorkingDir,
Env: options.EnvVars,
Logger: options.Logger,
}
}
if stdout {
return shell.RunCommandAndGetStdOut(t, cmd), nil
}
return shell.RunCommandAndGetOutputE(t, cmd)
}
// Note: docker-compose command doesn't like lower case or special characters, other than -.
func generateValidDockerComposeProjectName(str string) string {
lower_str := strings.ToLower(str)
return regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(lower_str, "-")
}