Skip to content

Commit

Permalink
Fix trailing slash on Image Prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
driverpt committed Aug 26, 2024
1 parent 3682745 commit a70b292
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
28 changes: 28 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

Check failure on line 2298 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest) / ./ubuntu-latest/1.22.x

File is not `gofumpt`-ed (gofumpt)

Check failure on line 2298 in docker_test.go

View workflow job for this annotation

GitHub Actions / test (1.x, ubuntu-latest) / ./ubuntu-latest/1.x

File is not `gofumpt`-ed (gofumpt)

Check failure on line 2298 in docker_test.go

View workflow job for this annotation

GitHub Actions / Test with reaper off (1.x) / ./ubuntu-latest/1.x

File is not `gofumpt`-ed (gofumpt)

Check failure on line 2298 in docker_test.go

View workflow job for this annotation

GitHub Actions / Test with Rootless Docker (1.22.x, ubuntu-latest) / ./ubuntu-latest/1.22.x

File is not `gofumpt`-ed (gofumpt)

Check failure on line 2298 in docker_test.go

View workflow job for this annotation

GitHub Actions / Test with Rootless Docker (1.x, ubuntu-latest) / ./ubuntu-latest/1.x

File is not `gofumpt`-ed (gofumpt)

Check failure on line 2298 in docker_test.go

View workflow job for this annotation

GitHub Actions / Test with reaper off (1.22.x) / ./ubuntu-latest/1.22.x

File is not `gofumpt`-ed (gofumpt)
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 a70b292

Please sign in to comment.