github.com/mponton/terratest@v0.44.0/modules/docker/docker_compose.go (about) 1 package docker 2 3 import ( 4 "regexp" 5 "strings" 6 7 "github.com/mponton/terratest/modules/logger" 8 "github.com/mponton/terratest/modules/shell" 9 "github.com/mponton/terratest/modules/testing" 10 "github.com/stretchr/testify/require" 11 "gotest.tools/v3/icmd" 12 ) 13 14 // Options are Docker options. 15 type Options struct { 16 WorkingDir string 17 EnvVars map[string]string 18 19 // Whether ot not to enable buildkit. You can find more information about buildkit here https://docs.docker.com/build/buildkit/#getting-started. 20 EnableBuildKit bool 21 22 // Set a logger that should be used. See the logger package for more info. 23 Logger *logger.Logger 24 ProjectName string 25 } 26 27 // RunDockerCompose runs docker compose with the given arguments and options and return stdout/stderr. 28 func RunDockerCompose(t testing.TestingT, options *Options, args ...string) string { 29 out, err := runDockerComposeE(t, false, options, args...) 30 if err != nil { 31 t.Fatal(err) 32 } 33 return out 34 } 35 36 // RunDockerComposeAndGetStdout runs docker compose with the given arguments and options and returns only stdout. 37 func RunDockerComposeAndGetStdOut(t testing.TestingT, options *Options, args ...string) string { 38 out, err := runDockerComposeE(t, true, options, args...) 39 require.NoError(t, err) 40 return out 41 } 42 43 // RunDockerComposeE runs docker compose with the given arguments and options and return stdout/stderr. 44 func RunDockerComposeE(t testing.TestingT, options *Options, args ...string) (string, error) { 45 return runDockerComposeE(t, false, options, args...) 46 } 47 48 func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args ...string) (string, error) { 49 var cmd shell.Command 50 51 projectName := options.ProjectName 52 if len(projectName) <= 0 { 53 projectName = strings.ToLower(t.Name()) 54 } 55 56 dockerComposeVersionCmd := icmd.Command("docker", "compose", "version") 57 result := icmd.RunCmd(dockerComposeVersionCmd) 58 59 if options.EnableBuildKit { 60 if options.EnvVars == nil { 61 options.EnvVars = make(map[string]string) 62 } 63 64 options.EnvVars["DOCKER_BUILDKIT"] = "1" 65 options.EnvVars["COMPOSE_DOCKER_CLI_BUILD"] = "1" 66 } 67 68 if result.ExitCode == 0 { 69 cmd = shell.Command{ 70 Command: "docker", 71 Args: append([]string{"compose", "--project-name", generateValidDockerComposeProjectName(projectName)}, args...), 72 WorkingDir: options.WorkingDir, 73 Env: options.EnvVars, 74 Logger: options.Logger, 75 } 76 } else { 77 cmd = shell.Command{ 78 Command: "docker-compose", 79 // We append --project-name to ensure containers from multiple different tests using Docker Compose don't end 80 // up in the same project and end up conflicting with each other. 81 Args: append([]string{"--project-name", generateValidDockerComposeProjectName(projectName)}, args...), 82 WorkingDir: options.WorkingDir, 83 Env: options.EnvVars, 84 Logger: options.Logger, 85 } 86 } 87 88 if stdout { 89 return shell.RunCommandAndGetStdOut(t, cmd), nil 90 } 91 92 return shell.RunCommandAndGetOutputE(t, cmd) 93 } 94 95 // Note: docker-compose command doesn't like lower case or special characters, other than -. 96 func generateValidDockerComposeProjectName(str string) string { 97 lower_str := strings.ToLower(str) 98 return regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(lower_str, "-") 99 }