Skip to content

Commit

Permalink
Merge pull request #1187 from apenella/docker-compose-projectname
Browse files Browse the repository at this point in the history
Include project name into docker-compose options
  • Loading branch information
denis256 committed Jan 14, 2023
2 parents 599561b + a17372c commit 6264798
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
repos:
- repo: https://github.com/gruntwork-io/pre-commit
rev: v0.1.10
hooks:
Expand Down
14 changes: 10 additions & 4 deletions modules/docker/docker_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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)
}

Expand Down
37 changes: 37 additions & 0 deletions modules/docker/docker_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
test-docker-image:
image: busybox

0 comments on commit 6264798

Please sign in to comment.