From 9cf5e9f2003ba80e345a30332bf8d7555ef37427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 13 Jun 2024 12:59:12 +0200 Subject: [PATCH] fix: prepend substitutors for built images (#2577) --- container.go | 24 ++++++++++++------------ image_substitutors_test.go | 37 +++++++++++++++++++++++++++++++++++++ request.go | 14 ++++++++++++++ 3 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 image_substitutors_test.go diff --git a/container.go b/container.go index 8b9be05f085..1f53e5e75a7 100644 --- a/container.go +++ b/container.go @@ -160,18 +160,6 @@ func newContainer(ctx context.Context, req Request) (*DockerContainer, error) { // always append the hub substitutor after the user-defined ones req.ImageSubstitutors = append(req.ImageSubstitutors, tcimage.NewPrependHubRegistry(tcConfig.HubImageNamePrefix)) - for _, is := range req.ImageSubstitutors { - modifiedTag, err := is.Substitute(imageName) - if err != nil { - return nil, fmt.Errorf("failed to substitute image %s with %s: %w", imageName, is.Description(), err) - } - - if modifiedTag != imageName { - req.Logger.Printf("✍🏼 Replacing image with %s. From: %s to %s\n", is.Description(), imageName, modifiedTag) - imageName = modifiedTag - } - } - cli, err := core.NewClient(ctx) if err != nil { return nil, fmt.Errorf("failed to get a Docker client: %w", err) @@ -186,6 +174,18 @@ func newContainer(ctx context.Context, req Request) (*DockerContainer, error) { return nil, err } } else { + for _, is := range req.ImageSubstitutors { + modifiedTag, err := is.Substitute(imageName) + if err != nil { + return nil, fmt.Errorf("failed to substitute image %s with %s: %w", imageName, is.Description(), err) + } + + if modifiedTag != imageName { + req.Logger.Printf("✍🏼 Replacing image with %s. From: %s to %s\n", is.Description(), imageName, modifiedTag) + imageName = modifiedTag + } + } + if req.ImagePlatform != "" { p, err := platforms.Parse(req.ImagePlatform) if err != nil { diff --git a/image_substitutors_test.go b/image_substitutors_test.go new file mode 100644 index 00000000000..b9c1e60ea12 --- /dev/null +++ b/image_substitutors_test.go @@ -0,0 +1,37 @@ +package testcontainers + +import ( + "context" + "testing" + + "github.com/testcontainers/testcontainers-go/image" +) + +func TestSubstituteBuiltImage(t *testing.T) { + req := Request{ + FromDockerfile: FromDockerfile{ + Context: "testdata", + Dockerfile: "echo.Dockerfile", + Tag: "my-image", + Repo: "my-repo", + }, + ImageSubstitutors: []image.Substitutor{image.NewPrependHubRegistry("my-registry")}, + Started: false, + } + + t.Run("should not use the properties prefix on built images", func(t *testing.T) { + c, err := New(context.Background(), req) + if err != nil { + t.Fatal(err) + } + + json, err := c.Inspect(context.Background()) + if err != nil { + t.Fatal(err) + } + + if json.Config.Image != "my-registry/my-repo:my-image" { + t.Errorf("expected my-registry/my-repo:my-image, got %s", json.Config.Image) + } + }) +} diff --git a/request.go b/request.go index 2aa5d0cb2e6..71431176c16 100644 --- a/request.go +++ b/request.go @@ -97,6 +97,20 @@ func (r *Request) BuildOptions() (types.ImageBuildOptions, error) { // make sure the first tag is the one defined in the Request tag := fmt.Sprintf("%s:%s", r.GetRepo(), r.GetTag()) + + // apply substitutors to the built image + for _, is := range r.ImageSubstitutors { + modifiedTag, err := is.Substitute(tag) + if err != nil { + return buildOptions, fmt.Errorf("failed to substitute image %s with %s: %w", tag, is.Description(), err) + } + + if modifiedTag != tag { + r.Logger.Printf("✍🏼 Replacing image with %s. From: %s to %s\n", is.Description(), tag, modifiedTag) + tag = modifiedTag + } + } + if len(buildOptions.Tags) > 0 { // prepend the tag buildOptions.Tags = append([]string{tag}, buildOptions.Tags...)