Skip to content

Commit

Permalink
chore: do not read config but instead pass the hub prefix to the prep…
Browse files Browse the repository at this point in the history
…endHub modifier (#2047)
  • Loading branch information
mdelapenya authored Dec 22, 2023
1 parent 1bac302 commit c3a1834
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 35 deletions.
2 changes: 1 addition & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
}

// always append the hub substitutor after the user-defined ones
req.ImageSubstitutors = append(req.ImageSubstitutors, newPrependHubRegistry())
req.ImageSubstitutors = append(req.ImageSubstitutors, newPrependHubRegistry(tcConfig.HubImageNamePrefix))

for _, is := range req.ImageSubstitutors {
modifiedTag, err := is.Substitute(imageName)
Expand Down
36 changes: 6 additions & 30 deletions image_substitutors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@ package testcontainers

import (
"testing"

"github.com/testcontainers/testcontainers-go/internal/config"
)

func TestPrependHubRegistrySubstitutor(t *testing.T) {
resetConfigForTests := func() {
config.Reset()
}

t.Run("should prepend the hub registry to images from Docker Hub", func(t *testing.T) {
t.Run("plain image", func(t *testing.T) {
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "my-registry")
defer resetConfigForTests()

s := newPrependHubRegistry()
s := newPrependHubRegistry("my-registry")

img, err := s.Substitute("foo:latest")
if err != nil {
Expand All @@ -28,10 +19,7 @@ func TestPrependHubRegistrySubstitutor(t *testing.T) {
}
})
t.Run("image with user", func(t *testing.T) {
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "my-registry")
defer resetConfigForTests()

s := newPrependHubRegistry()
s := newPrependHubRegistry("my-registry")

img, err := s.Substitute("user/foo:latest")
if err != nil {
Expand All @@ -44,10 +32,7 @@ func TestPrependHubRegistrySubstitutor(t *testing.T) {
})

t.Run("image with organization and user", func(t *testing.T) {
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "my-registry")
defer resetConfigForTests()

s := newPrependHubRegistry()
s := newPrependHubRegistry("my-registry")

img, err := s.Substitute("org/user/foo:latest")
if err != nil {
Expand All @@ -62,10 +47,7 @@ func TestPrependHubRegistrySubstitutor(t *testing.T) {

t.Run("should not prepend the hub registry to the image name", func(t *testing.T) {
t.Run("non-hub image", func(t *testing.T) {
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "my-registry")
defer resetConfigForTests()

s := newPrependHubRegistry()
s := newPrependHubRegistry("my-registry")

img, err := s.Substitute("quay.io/foo:latest")
if err != nil {
Expand All @@ -78,10 +60,7 @@ func TestPrependHubRegistrySubstitutor(t *testing.T) {
})

t.Run("explicitly including docker.io", func(t *testing.T) {
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "my-registry")
defer resetConfigForTests()

s := newPrependHubRegistry()
s := newPrependHubRegistry("my-registry")

img, err := s.Substitute("docker.io/foo:latest")
if err != nil {
Expand All @@ -94,10 +73,7 @@ func TestPrependHubRegistrySubstitutor(t *testing.T) {
})

t.Run("explicitly including registry.hub.docker.com", func(t *testing.T) {
t.Setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "my-registry")
defer resetConfigForTests()

s := newPrependHubRegistry()
s := newPrependHubRegistry("my-registry")

img, err := s.Substitute("registry.hub.docker.com/foo:latest")
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/docker/docker/api/types/network"

tcexec "github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/internal/config"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down Expand Up @@ -86,9 +85,7 @@ type prependHubRegistry struct {
}

// newPrependHubRegistry creates a new prependHubRegistry
func newPrependHubRegistry() prependHubRegistry {
hubPrefix := config.Read().HubImageNamePrefix

func newPrependHubRegistry(hubPrefix string) prependHubRegistry {
return prependHubRegistry{
prefix: hubPrefix,
}
Expand Down

0 comments on commit c3a1834

Please sign in to comment.