diff --git a/container_test.go b/container_test.go index d60812ffaf..55da19e445 100644 --- a/container_test.go +++ b/container_test.go @@ -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" @@ -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) { @@ -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") @@ -468,15 +462,7 @@ 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) @@ -484,11 +470,7 @@ func TestImageSubstitutors(t *testing.T) { } 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) }) } } @@ -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) @@ -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) @@ -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 } diff --git a/examples/nginx/nginx.go b/examples/nginx/nginx.go index 78ab6291a2..02ca2a3d84 100644 --- a/examples/nginx/nginx.go +++ b/examples/nginx/nginx.go @@ -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 } diff --git a/options_test.go b/options_test.go index 8e58946f68..149b4aa0c4 100644 --- a/options_test.go +++ b/options_test.go @@ -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 @@ -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) @@ -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) diff --git a/options_unit_test.go b/options_unit_test.go new file mode 100644 index 0000000000..f6d5807bbb --- /dev/null +++ b/options_unit_test.go @@ -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) +}