Skip to content

Commit

Permalink
Merge branch 'main' into fix-log-consumer-race
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya authored Jun 14, 2024
2 parents dad7872 + 2681ae4 commit dd36de0
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 23 deletions.
18 changes: 18 additions & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,31 @@ 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...)
} else {
buildOptions.Tags = []string{tag}
}

if !c.ShouldKeepBuiltImage() {
buildOptions.Labels = core.DefaultLabels(core.SessionID())
}

return buildOptions, nil
}

Expand Down
24 changes: 12 additions & 12 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,18 +1007,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 @@ -1027,6 +1015,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
4 changes: 2 additions & 2 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,8 @@ func Test_BuildContainerFromDockerfileWithBuildLog(t *testing.T) {
os.Stdout = rescueStdout
temp := strings.Split(string(out), "\n")

if !regexp.MustCompile(`(?i)^Step\s*1/1\s*:\s*FROM docker.io/alpine$`).MatchString(temp[0]) {
t.Errorf("Expected stdout firstline to be %s. Got '%s'.", "Step 1/1 : FROM docker.io/alpine", temp[0])
if !regexp.MustCompile(`^Step\s*1/\d+\s*:\s*FROM docker.io/alpine$`).MatchString(temp[0]) {
t.Errorf("Expected stdout firstline to be %s. Got '%s'.", "Step 1/* : FROM docker.io/alpine", temp[0])
}
}

Expand Down
14 changes: 11 additions & 3 deletions docs/system_requirements/using_colima.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ default Current DOCKER_HOST based configuration unix:///var/run/docker.sock
If you're using an older version of Colima or have other applications that are
unaware of Docker context the following workaround is available:

1. Locate your Docker Socket, see: [Colima's FAQ - Docker Socket Location](https://github.com/abiosoft/colima/blob/main/docs/FAQ.md#docker-socket-location)
- Locate your Docker Socket, see: [Colima's FAQ - Docker Socket Location](https://github.com/abiosoft/colima/blob/main/docs/FAQ.md#docker-socket-location)

2. Set the `DOCKER_HOST` environment variable to match the located Docker Socket
- Create a symbolic link from the default Docker Socket to the expected location, and restart Colima with the `--network-address` flag.

```
sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock
colima stop
colima start --network-address
```

- Set the `DOCKER_HOST` environment variable to match the located Docker Socket

* Example: `export DOCKER_HOST="unix://${HOME}/.colima/default/docker.sock"`

3. As of testcontainers-go v0.14.0 set `TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE`
- As of testcontainers-go v0.14.0 set `TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE`
to `/var/run/docker.sock` as the default value refers to your `DOCKER_HOST`
environment variable.

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)
}
})
}
6 changes: 3 additions & 3 deletions modules/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func WithPassword(password string) testcontainers.CustomizeRequestOption {

// WithReplicaSet configures the container to run a single-node MongoDB replica set named "rs".
// It will wait until the replica set is ready.
func WithReplicaSet() testcontainers.CustomizeRequestOption {
func WithReplicaSet(replSetName string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Cmd = append(req.Cmd, "--replSet", "rs")
req.Cmd = append(req.Cmd, "--replSet", replSetName)
req.LifecycleHooks = append(req.LifecycleHooks, testcontainers.ContainerLifecycleHooks{
PostStarts: []testcontainers.ContainerHook{
func(ctx context.Context, c testcontainers.Container) error {
Expand All @@ -92,7 +92,7 @@ func WithReplicaSet() testcontainers.CustomizeRequestOption {
return err
}

cmd := eval("rs.initiate({ _id: 'rs', members: [ { _id: 0, host: '%s:27017' } ] })", ip)
cmd := eval("rs.initiate({ _id: '%s', members: [ { _id: 0, host: '%s:27017' } ] })", replSetName, ip)
return wait.ForExec(cmd).WaitUntilReady(ctx, c)
},
},
Expand Down
4 changes: 2 additions & 2 deletions modules/mongodb/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ func TestMongoDB(t *testing.T) {
name: "With Replica set and mongo:4",
opts: []testcontainers.ContainerCustomizer{
testcontainers.WithImage("mongo:4"),
mongodb.WithReplicaSet(),
mongodb.WithReplicaSet("rs"),
},
},
{
name: "With Replica set and mongo:6",
opts: []testcontainers.ContainerCustomizer{
testcontainers.WithImage("mongo:6"),
mongodb.WithReplicaSet(),
mongodb.WithReplicaSet("rs"),
},
},
}
Expand Down
12 changes: 12 additions & 0 deletions reaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ func TestContainerTerminationWithoutReaper(t *testing.T) {
}

func Test_NewReaper(t *testing.T) {
config.Reset() // reset the config using the internal method to avoid the sync.Once
tcConfig := config.Read()
if tcConfig.RyukDisabled {
t.Skip("Ryuk is disabled, skipping test")
}

type cases struct {
name string
req ContainerRequest
Expand Down Expand Up @@ -588,6 +594,12 @@ func TestReaper_reuseItFromOtherTestProgramUsingDocker(t *testing.T) {
// already running for the same session id by returning its container instance
// instead.
func TestReaper_ReuseRunning(t *testing.T) {
config.Reset() // reset the config using the internal method to avoid the sync.Once
tcConfig := config.Read()
if tcConfig.RyukDisabled {
t.Skip("Ryuk is disabled, skipping test")
}

const concurrency = 64

timeout, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
Expand Down
2 changes: 1 addition & 1 deletion scripts/check_environment.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

i=0
while [ $i -ne 10 ]
while [ $i -ne 30 ]
do
containers=$(docker ps --format "{{.ID}}" | wc -l)
if [ "$containers" -eq "0" ]; then
Expand Down

0 comments on commit dd36de0

Please sign in to comment.