From 6ce07dedfa98002d8bdba0cb8f4f3e2ce57fa52b Mon Sep 17 00:00:00 2001 From: Aleix Penella Date: Tue, 4 Oct 2022 07:59:42 +0200 Subject: [PATCH 1/5] Include project name into docker-compose options --- modules/docker/docker_compose.go | 14 +++++++++----- modules/docker/inspect.go | 17 +++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/modules/docker/docker_compose.go b/modules/docker/docker_compose.go index aaba564a9..da015ea1a 100644 --- a/modules/docker/docker_compose.go +++ b/modules/docker/docker_compose.go @@ -15,7 +15,8 @@ type Options struct { WorkingDir string EnvVars map[string]string // 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. @@ -42,14 +43,17 @@ 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 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", projectName}, args...), WorkingDir: options.WorkingDir, Env: options.EnvVars, Logger: options.Logger, @@ -59,7 +63,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", projectName}, args...), WorkingDir: options.WorkingDir, Env: options.EnvVars, Logger: options.Logger, diff --git a/modules/docker/inspect.go b/modules/docker/inspect.go index c507aa745..6d058d1b3 100644 --- a/modules/docker/inspect.go +++ b/modules/docker/inspect.go @@ -187,14 +187,15 @@ func transformContainer(t *testing.T, container inspectOutput) (*ContainerInspec } // transformContainerPorts converts Docker's ports from the following json into a more testable format -// { -// "80/tcp": [ -// { -// "HostIp": "" -// "HostPort": "8080" -// } -// ] -// } +// +// { +// "80/tcp": [ +// { +// "HostIp": "" +// "HostPort": "8080" +// } +// ] +// } func transformContainerPorts(container inspectOutput) ([]Port, error) { var ports []Port From 8fcd2aab48e35a741583cc997bbe5afab0eacec7 Mon Sep 17 00:00:00 2001 From: Aleix Penella Date: Tue, 13 Dec 2022 08:08:23 +0100 Subject: [PATCH 2/5] pre-commit migrate-config --- .pre-commit-config.yaml | 1 + 1 file changed, 1 insertion(+) 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: From 6f719b084b98ca2a8efadb5a5839482ba77e1f96 Mon Sep 17 00:00:00 2001 From: Aleix Penella Date: Wed, 14 Dec 2022 07:20:54 +0100 Subject: [PATCH 3/5] testing docker compose project name --- modules/docker/docker_compose_test.go | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) 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) + }) + } +} From c316a70a888cc565e8ad4bae2972df6c517bde55 Mon Sep 17 00:00:00 2001 From: Aleix Penella Date: Wed, 14 Dec 2022 07:30:07 +0100 Subject: [PATCH 4/5] testing docker compose project name --- .../docker-compose.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 test/fixtures/docker-compose-with-custom-project-name/docker-compose.yml 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..89c00b364 --- /dev/null +++ b/test/fixtures/docker-compose-with-custom-project-name/docker-compose.yml @@ -0,0 +1,4 @@ +services: + test-docker-image: + image: busybox + # command: sh -c "while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; echo 'testing service'; } | nc -l -p 8080; done" From a17372cda2877e9fa33439bd617c3b959bc4f8f0 Mon Sep 17 00:00:00 2001 From: Aleix Penella Date: Wed, 14 Dec 2022 07:34:14 +0100 Subject: [PATCH 5/5] cleanup fixtures --- .../docker-compose-with-custom-project-name/docker-compose.yml | 1 - 1 file changed, 1 deletion(-) 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 index 89c00b364..8b8314856 100644 --- a/test/fixtures/docker-compose-with-custom-project-name/docker-compose.yml +++ b/test/fixtures/docker-compose-with-custom-project-name/docker-compose.yml @@ -1,4 +1,3 @@ services: test-docker-image: image: busybox - # command: sh -c "while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; echo 'testing service'; } | nc -l -p 8080; done"