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 committed Jun 13, 2024
1 parent 467a3fb commit 9cf5e9f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
24 changes: 12 additions & 12 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions image_substitutors_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
14 changes: 14 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down

0 comments on commit 9cf5e9f

Please sign in to comment.