From 1b3dc31fb744b89a6f97415e78239bc1b944084d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 12 Jun 2024 12:34:10 +0200 Subject: [PATCH] fix: prepend substitutors for built images --- container.go | 14 ++++++++++++++ docker.go | 24 ++++++++++++------------ image_substitutors_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/container.go b/container.go index 99af536cd6..108f5df3f1 100644 --- a/container.go +++ b/container.go @@ -375,6 +375,20 @@ func (c *ContainerRequest) BuildOptions() (types.ImageBuildOptions, error) { // make sure the first tag is the one defined in the ContainerRequest tag := fmt.Sprintf("%s:%s", c.GetRepo(), c.GetTag()) + + // apply substitutors to the built image + for _, is := range c.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 { + 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...) diff --git a/docker.go b/docker.go index e3999a7f62..e593218e40 100644 --- a/docker.go +++ b/docker.go @@ -1023,18 +1023,6 @@ 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(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 { - p.Logger.Printf("✍🏼 Replacing image with %s. From: %s to %s\n", is.Description(), imageName, modifiedTag) - imageName = modifiedTag - } - } - var platform *specs.Platform if req.ShouldBuildImage() { @@ -1043,6 +1031,18 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque 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 { + 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 index 64e3b95d2d..dd6189255d 100644 --- a/image_substitutors_test.go +++ b/image_substitutors_test.go @@ -1,6 +1,7 @@ package testcontainers import ( + "context" "testing" ) @@ -86,3 +87,34 @@ func TestPrependHubRegistrySubstitutor(t *testing.T) { }) }) } + +func TestSubstituteBuiltImage(t *testing.T) { + req := GenericContainerRequest{ + ContainerRequest: ContainerRequest{ + FromDockerfile: FromDockerfile{ + Context: "testdata", + Dockerfile: "echo.Dockerfile", + Tag: "my-image", + Repo: "my-repo", + }, + ImageSubstitutors: []ImageSubstitutor{newPrependHubRegistry("my-registry")}, + }, + Started: false, + } + + t.Run("should not use the properties prefix on built images", func(t *testing.T) { + c, err := GenericContainer(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) + } + }) +}