Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix project-name so it doesn't contain special characters for docker-… #1203

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions modules/docker/docker_compose.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package docker

import (
"regexp"
"strings"

"github.com/gruntwork-io/terratest/modules/logger"
Expand Down Expand Up @@ -62,7 +63,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", strings.ToLower(t.Name())}, args...),
Args: append([]string{"compose", "--project-name", generateValidDockerComposeProjectName(t.Name())}, args...),
WorkingDir: options.WorkingDir,
Env: options.EnvVars,
Logger: options.Logger,
Expand All @@ -72,7 +73,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", strings.ToLower(t.Name())}, args...),
Args: append([]string{"--project-name", generateValidDockerComposeProjectName(t.Name())}, args...),
WorkingDir: options.WorkingDir,
Env: options.EnvVars,
Logger: options.Logger,
Expand All @@ -84,3 +85,9 @@ func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args .
}
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, "-")
}