From 2d92539162f5a0ab42bc133997228aaf665210a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Fri, 22 Dec 2023 12:08:49 +0100 Subject: [PATCH] chore: do not read config but instead pass the hub prefix to the prependHub modifier --- docker.go | 2 +- image_substitutors_test.go | 36 ++++++------------------------------ options.go | 5 +---- 3 files changed, 8 insertions(+), 35 deletions(-) diff --git a/docker.go b/docker.go index 3148dfe6cf..252d94ec7c 100644 --- a/docker.go +++ b/docker.go @@ -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) diff --git a/image_substitutors_test.go b/image_substitutors_test.go index e73c4718f8..64e3b95d2d 100644 --- a/image_substitutors_test.go +++ b/image_substitutors_test.go @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/options.go b/options.go index 28833f200b..8df728f203 100644 --- a/options.go +++ b/options.go @@ -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" ) @@ -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, }