Skip to content

Commit

Permalink
Fix trailing slash on Image Prefix (testcontainers#2747)
Browse files Browse the repository at this point in the history
* Fix trailing slash on Image Prefix

* Apply Lint fix
  • Loading branch information
driverpt authored Aug 27, 2024
1 parent 5245aa8 commit 0d9e934
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
27 changes: 27 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2280,3 +2280,30 @@ 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)
}
15 changes: 13 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testcontainers
import (
"context"
"fmt"
"net/url"
"time"

"dario.cat/mergo"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0d9e934

Please sign in to comment.