Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 15 additions & 47 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/docker/docker/api/types/build"
"github.com/docker/docker/api/types/container"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -325,7 +324,7 @@ func TestCustomLabelsImage(t *testing.T) {

ctrJSON, err := ctr.Inspect(ctx)
require.NoError(t, err)
assert.Equal(t, myLabelValue, ctrJSON.Config.Labels[myLabelName])
require.Equal(t, myLabelValue, ctrJSON.Config.Labels[myLabelName])
}

func TestCustomLabelsBuildOptionsModifier(t *testing.T) {
Expand Down Expand Up @@ -363,17 +362,12 @@ func TestCustomLabelsBuildOptionsModifier(t *testing.T) {
func Test_GetLogsFromFailedContainer(t *testing.T) {
ctx := context.Background()
// directDockerHubReference {
req := testcontainers.ContainerRequest{
Image: "alpine",
Cmd: []string{"echo", "-n", "I was not expecting this"},
WaitingFor: wait.ForLog("I was expecting this").WithStartupTimeout(5 * time.Second),
}
c, err := testcontainers.Run(
ctx, "alpine",
testcontainers.WithCmd("echo", "-n", "I was not expecting this"),
testcontainers.WithWaitStrategy(wait.ForLog("I was expecting this").WithStartupTimeout(5*time.Second)),
)
// }

c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
testcontainers.CleanupContainer(t, c)
require.ErrorContains(t, err, "container exited with code 0")

Expand Down Expand Up @@ -468,27 +462,15 @@ func TestImageSubstitutors(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: test.image,
ImageSubstitutors: test.substitutors,
}

ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
ctr, err := testcontainers.Run(ctx, test.image, testcontainers.WithImageSubstitutors(test.substitutors...))
testcontainers.CleanupContainer(t, ctr)
if test.expectedError != nil {
require.ErrorIs(t, err, test.expectedError)
return
}

require.NoError(t, err)

// enforce the concrete type, as GenericContainer returns an interface,
// which will be changed in future implementations of the library
dockerContainer := ctr.(*testcontainers.DockerContainer)
assert.Equal(t, test.expectedImage, dockerContainer.Image)
require.Equal(t, test.expectedImage, ctr.Image)
})
}
}
Expand All @@ -502,15 +484,11 @@ func TestShouldStartContainersInParallel(t *testing.T) {
t.Run(fmt.Sprintf("iteration_%d", i), func(t *testing.T) {
t.Parallel()

req := testcontainers.ContainerRequest{
Image: nginxAlpineImage,
ExposedPorts: []string{nginxDefaultPort},
WaitingFor: wait.ForHTTP("/").WithStartupTimeout(10 * time.Second),
}
ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
ctr, err := testcontainers.Run(
ctx, nginxAlpineImage,
testcontainers.WithExposedPorts(nginxDefaultPort),
testcontainers.WithWaitStrategy(wait.ForHTTP("/").WithStartupTimeout(10*time.Second)),
)
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)

Expand All @@ -528,13 +506,7 @@ func ExampleGenericContainer_withSubstitutors() {
ctx := context.Background()

// applyImageSubstitutors {
ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "alpine:latest",
ImageSubstitutors: []testcontainers.ImageSubstitutor{dockerImageSubstitutor{}},
},
Started: true,
})
ctr, err := testcontainers.Run(ctx, "alpine:latest", testcontainers.WithImageSubstitutors(dockerImageSubstitutor{}))
defer func() {
if err := testcontainers.TerminateContainer(ctr); err != nil {
log.Printf("failed to terminate container: %s", err)
Expand All @@ -547,11 +519,7 @@ func ExampleGenericContainer_withSubstitutors() {
return
}

// enforce the concrete type, as GenericContainer returns an interface,
// which will be changed in future implementations of the library
dockerContainer := ctr.(*testcontainers.DockerContainer)

fmt.Println(dockerContainer.Image)
fmt.Println(ctr.Image)

// Output: registry.hub.docker.com/library/alpine:latest
}
20 changes: 8 additions & 12 deletions examples/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@ type nginxContainer struct {
}

func startContainer(ctx context.Context) (*nginxContainer, error) {
req := testcontainers.ContainerRequest{
Image: "nginx",
ExposedPorts: []string{"80/tcp"},
WaitingFor: wait.ForHTTP("/").WithStartupTimeout(10 * time.Second),
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
ctr, err := testcontainers.Run(
ctx, "nginx",
testcontainers.WithExposedPorts("80/tcp"),
testcontainers.WithWaitStrategy(wait.ForHTTP("/").WithStartupTimeout(10*time.Second)),
)
var nginxC *nginxContainer
if container != nil {
nginxC = &nginxContainer{Container: container}
if ctr != nil {
nginxC = &nginxContainer{Container: ctr}
}
if err != nil {
return nginxC, err
}

endpoint, err := container.PortEndpoint(ctx, "80", "http")
endpoint, err := ctr.PortEndpoint(ctx, "80", "http")
if err != nil {
return nginxC, err
}
Expand Down
53 changes: 11 additions & 42 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,14 @@ func (lc *msgsLogConsumer) Accept(l testcontainers.Log) {
}

func TestWithLogConsumers(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "mysql:8.0.36",
WaitingFor: wait.ForLog("port: 3306 MySQL Community Server - GPL"),
},
Started: true,
}

lc := &msgsLogConsumer{}

err := testcontainers.WithLogConsumers(lc)(&req)
require.NoError(t, err)

ctx := context.Background()
c, err := testcontainers.GenericContainer(ctx, req)
c, err := testcontainers.Run(
ctx, "mysql:8.0.36",
testcontainers.WithWaitStrategy(wait.ForLog("port: 3306 MySQL Community Server - GPL")),
testcontainers.WithLogConsumers(lc),
)
testcontainers.CleanupContainer(t, c)
// we expect an error because the MySQL environment variables are not set
// but this is expected because we just want to test the log consumer
Expand Down Expand Up @@ -138,23 +131,13 @@ func TestWithLogConsumerConfig(t *testing.T) {
}

func TestWithStartupCommand(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "alpine",
Entrypoint: []string{"tail", "-f", "/dev/null"},
},
Started: true,
}

testExec := testcontainers.NewRawCommand([]string{"touch", ".testcontainers"}, exec.WithWorkingDir("/tmp"))

err := testcontainers.WithStartupCommand(testExec)(&req)
require.NoError(t, err)

require.Len(t, req.LifecycleHooks, 1)
require.Len(t, req.LifecycleHooks[0].PostStarts, 1)

c, err := testcontainers.GenericContainer(context.Background(), req)
c, err := testcontainers.Run(
context.Background(), "alpine",
testcontainers.WithEntrypoint("tail", "-f", "/dev/null"),
testcontainers.WithStartupCommand(testExec),
)
testcontainers.CleanupContainer(t, c)
require.NoError(t, err)

Expand All @@ -167,23 +150,9 @@ func TestWithStartupCommand(t *testing.T) {
}

func TestWithAfterReadyCommand(t *testing.T) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "alpine",
Entrypoint: []string{"tail", "-f", "/dev/null"},
},
Started: true,
}

testExec := testcontainers.NewRawCommand([]string{"touch", "/tmp/.testcontainers"})

err := testcontainers.WithAfterReadyCommand(testExec)(&req)
require.NoError(t, err)

require.Len(t, req.LifecycleHooks, 1)
require.Len(t, req.LifecycleHooks[0].PostReadies, 1)

c, err := testcontainers.GenericContainer(context.Background(), req)
c, err := testcontainers.Run(context.Background(), "alpine", testcontainers.WithEntrypoint("tail", "-f", "/dev/null"), testcontainers.WithAfterReadyCommand(testExec))
testcontainers.CleanupContainer(t, c)
require.NoError(t, err)

Expand Down
37 changes: 37 additions & 0 deletions options_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package testcontainers

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go/exec"
)

func TestWithStartupCommand_unit(t *testing.T) {
req := GenericContainerRequest{
ContainerRequest: ContainerRequest{},
}

testExec := NewRawCommand([]string{"touch", ".testcontainers"}, exec.WithWorkingDir("/tmp"))

err := WithStartupCommand(testExec)(&req)
require.NoError(t, err)

require.Len(t, req.LifecycleHooks, 1)
require.Len(t, req.LifecycleHooks[0].PostStarts, 1)
}

func TestWithAfterReadyCommand_unit(t *testing.T) {
req := GenericContainerRequest{
ContainerRequest: ContainerRequest{},
}

testExec := NewRawCommand([]string{"touch", "/tmp/.testcontainers"})

err := WithAfterReadyCommand(testExec)(&req)
require.NoError(t, err)

require.Len(t, req.LifecycleHooks, 1)
require.Len(t, req.LifecycleHooks[0].PostReadies, 1)
}
Loading