github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/docker/docker_compose.go (about) 1 package docker 2 3 import ( 4 "github.com/gruntwork-io/terratest/modules/logger" 5 "github.com/gruntwork-io/terratest/modules/shell" 6 "github.com/gruntwork-io/terratest/modules/testing" 7 "github.com/stretchr/testify/require" 8 ) 9 10 // Options are Docker options. 11 type Options struct { 12 WorkingDir string 13 EnvVars map[string]string 14 // Set a logger that should be used. See the logger package for more info. 15 Logger *logger.Logger 16 } 17 18 // RunDockerCompose runs docker-compose with the given arguments and options and return stdout/stderr. 19 func RunDockerCompose(t testing.TestingT, options *Options, args ...string) string { 20 out, err := runDockerComposeE(t, false, options, args...) 21 if err != nil { 22 t.Fatal(err) 23 } 24 return out 25 } 26 27 // RunDockerComposeAndGetStdout runs docker-compose with the given arguments and options and returns only stdout. 28 func RunDockerComposeAndGetStdOut(t testing.TestingT, options *Options, args ...string) string { 29 out, err := runDockerComposeE(t, true, options, args...) 30 require.NoError(t, err) 31 return out 32 } 33 34 // RunDockerComposeE runs docker-compose with the given arguments and options and return stdout/stderr. 35 func RunDockerComposeE(t testing.TestingT, options *Options, args ...string) (string, error) { 36 return runDockerComposeE(t, false, options, args...) 37 } 38 39 func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args ...string) (string, error) { 40 cmd := shell.Command{ 41 Command: "docker-compose", 42 // We append --project-name to ensure containers from multiple different tests using Docker Compose don't end 43 // up in the same project and end up conflicting with each other. 44 Args: append([]string{"--project-name", t.Name()}, args...), 45 WorkingDir: options.WorkingDir, 46 Env: options.EnvVars, 47 Logger: options.Logger, 48 } 49 50 if stdout { 51 return shell.RunCommandAndGetStdOut(t, cmd), nil 52 } 53 return shell.RunCommandAndGetOutputE(t, cmd) 54 }