diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6e334a201..17ead6f3b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,3 +1,4 @@ +repos: - repo: https://github.com/gruntwork-io/pre-commit rev: v0.1.10 hooks: diff --git a/modules/docker/docker_compose.go b/modules/docker/docker_compose.go index d90ccff36..330309d1c 100644 --- a/modules/docker/docker_compose.go +++ b/modules/docker/docker_compose.go @@ -20,7 +20,8 @@ type Options struct { EnableBuildKit bool // Set a logger that should be used. See the logger package for more info. - Logger *logger.Logger + Logger *logger.Logger + ProjectName string } // RunDockerCompose runs docker compose with the given arguments and options and return stdout/stderr. @@ -47,8 +48,12 @@ func RunDockerComposeE(t testing.TestingT, options *Options, args ...string) (st func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args ...string) (string, error) { var cmd shell.Command - dockerComposeVersionCmd := icmd.Command("docker", "compose", "version") + 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 { @@ -63,7 +68,7 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args . if result.ExitCode == 0 { cmd = shell.Command{ Command: "docker", - Args: append([]string{"compose", "--project-name", generateValidDockerComposeProjectName(t.Name())}, args...), + Args: append([]string{"compose", "--project-name", generateValidDockerComposeProjectName(projectName)}, args...), WorkingDir: options.WorkingDir, Env: options.EnvVars, Logger: options.Logger, @@ -73,7 +78,7 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args . 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(t.Name())}, args...), + Args: append([]string{"--project-name", generateValidDockerComposeProjectName(projectName)}, args...), WorkingDir: options.WorkingDir, Env: options.EnvVars, Logger: options.Logger, @@ -83,6 +88,7 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args . if stdout { return shell.RunCommandAndGetStdOut(t, cmd), nil } + return shell.RunCommandAndGetOutputE(t, cmd) } diff --git a/modules/docker/docker_compose_test.go b/modules/docker/docker_compose_test.go index b9799a356..13b5c4702 100644 --- a/modules/docker/docker_compose_test.go +++ b/modules/docker/docker_compose_test.go @@ -25,3 +25,40 @@ func TestDockerComposeWithBuildKit(t *testing.T) { require.Contains(t, out, testToken) } + +func TestDockerComposeWithCustomProjectName(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + options *Options + expected string + }{ + { + name: "Testing ", + options: &Options{ + WorkingDir: "../../test/fixtures/docker-compose-with-custom-project-name", + }, + expected: "testdockercomposewithcustomprojectname", + }, + { + name: "Testing", + options: &Options{ + WorkingDir: "../../test/fixtures/docker-compose-with-custom-project-name", + ProjectName: "testingProjectName", + }, + expected: "testingprojectname", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Log(test.name) + + output := RunDockerCompose(t, test.options, "up", "-d") + defer RunDockerCompose(t, test.options, "down", "--remove-orphans", "--timeout", "2") + + require.Contains(t, output, test.expected) + }) + } +} diff --git a/test/fixtures/docker-compose-with-custom-project-name/docker-compose.yml b/test/fixtures/docker-compose-with-custom-project-name/docker-compose.yml new file mode 100644 index 000000000..8b8314856 --- /dev/null +++ b/test/fixtures/docker-compose-with-custom-project-name/docker-compose.yml @@ -0,0 +1,3 @@ +services: + test-docker-image: + image: busybox