From a70b292c9cbe4c7a98cc91796158814b18db9a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Duarte?= Date: Mon, 26 Aug 2024 08:48:26 +0000 Subject: [PATCH 1/2] Fix trailing slash on Image Prefix --- docker_test.go | 28 ++++++++++++++++++++++++++++ options.go | 15 +++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/docker_test.go b/docker_test.go index f532d8650f..d8fce131d4 100644 --- a/docker_test.go +++ b/docker_test.go @@ -2280,3 +2280,31 @@ func TestDockerProvider_attemptToPullImage_retries(t *testing.T) { }) } } + +func TestCustomPrefixTrailingSlashIsProperlyRemovedIfPresent(t *testing.T) { + hubPrefixWithTrailingSlash := "public.ecr.aws/" + dockerImage := "amazonlinux/amazonlinux:2023" + + ctx := context.Background() + req := ContainerRequest{ + Image: dockerImage, + ImageSubstitutors: []ImageSubstitutor{newPrependHubRegistry(hubPrefixWithTrailingSlash)}, + } + + c, err := GenericContainer(ctx, GenericContainerRequest{ + ContainerRequest: req, + Started: true, + }) + + if err != nil { + t.Fatal(err) + } + defer func() { + terminateContainerOnEnd(t, ctx, c) + }() + + // enforce the concrete type, as GenericContainer returns an interface, + // which will be changed in future implementations of the library + dockerContainer := c.(*DockerContainer) + assert.Equal(t, fmt.Sprintf("%s%s", hubPrefixWithTrailingSlash, dockerImage), dockerContainer.Image) +} diff --git a/options.go b/options.go index 6b5dcb1293..2849b15667 100644 --- a/options.go +++ b/options.go @@ -3,6 +3,7 @@ package testcontainers import ( "context" "fmt" + "net/url" "time" "dario.cat/mergo" @@ -155,7 +156,12 @@ func (c CustomHubSubstitutor) Substitute(image string) (string, error) { } } - return fmt.Sprintf("%s/%s", c.hub, image), nil + result, err := url.JoinPath(c.hub, image) + if err != nil { + return "", err + } + + return result, nil } // prependHubRegistry represents a way to prepend a custom Hub registry to the image name, @@ -198,7 +204,12 @@ func (p prependHubRegistry) Substitute(image string) (string, error) { } } - return fmt.Sprintf("%s/%s", p.prefix, image), nil + result, err := url.JoinPath(p.prefix, image) + if err != nil { + return "", err + } + + return result, nil } // WithImageSubstitutors sets the image substitutors for a container From 5be5853d99927cec632cee11fb3cb53788b184d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Duarte?= Date: Mon, 26 Aug 2024 12:02:33 +0000 Subject: [PATCH 2/2] Apply Lint fix --- docker_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/docker_test.go b/docker_test.go index d8fce131d4..402d944dce 100644 --- a/docker_test.go +++ b/docker_test.go @@ -2295,7 +2295,6 @@ func TestCustomPrefixTrailingSlashIsProperlyRemovedIfPresent(t *testing.T) { ContainerRequest: req, Started: true, }) - if err != nil { t.Fatal(err) }