Skip to content

Commit

Permalink
fix: prepend substitutors for built images (#2577)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya authored Jun 13, 2024
1 parent 88dcc46 commit 5491ff5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
14 changes: 14 additions & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
24 changes: 12 additions & 12 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions image_substitutors_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testcontainers

import (
"context"
"testing"
)

Expand Down Expand Up @@ -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)
}
})
}

0 comments on commit 5491ff5

Please sign in to comment.