Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: do not read config but instead pass the hub prefix to the prependHub modifier #2047

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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