Skip to content

Commit 9f0e4cd

Browse files
authored
chore: use Run in more tests (#3309)
1 parent 038b8a0 commit 9f0e4cd

File tree

4 files changed

+71
-101
lines changed

4 files changed

+71
-101
lines changed

container_test.go

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/docker/docker/api/types/build"
1414
"github.com/docker/docker/api/types/container"
15-
"github.com/stretchr/testify/assert"
1615
"github.com/stretchr/testify/require"
1716

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

326325
ctrJSON, err := ctr.Inspect(ctx)
327326
require.NoError(t, err)
328-
assert.Equal(t, myLabelValue, ctrJSON.Config.Labels[myLabelName])
327+
require.Equal(t, myLabelValue, ctrJSON.Config.Labels[myLabelName])
329328
}
330329

331330
func TestCustomLabelsBuildOptionsModifier(t *testing.T) {
@@ -363,17 +362,12 @@ func TestCustomLabelsBuildOptionsModifier(t *testing.T) {
363362
func Test_GetLogsFromFailedContainer(t *testing.T) {
364363
ctx := context.Background()
365364
// directDockerHubReference {
366-
req := testcontainers.ContainerRequest{
367-
Image: "alpine",
368-
Cmd: []string{"echo", "-n", "I was not expecting this"},
369-
WaitingFor: wait.ForLog("I was expecting this").WithStartupTimeout(5 * time.Second),
370-
}
365+
c, err := testcontainers.Run(
366+
ctx, "alpine",
367+
testcontainers.WithCmd("echo", "-n", "I was not expecting this"),
368+
testcontainers.WithWaitStrategy(wait.ForLog("I was expecting this").WithStartupTimeout(5*time.Second)),
369+
)
371370
// }
372-
373-
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
374-
ContainerRequest: req,
375-
Started: true,
376-
})
377371
testcontainers.CleanupContainer(t, c)
378372
require.ErrorContains(t, err, "container exited with code 0")
379373

@@ -468,27 +462,15 @@ func TestImageSubstitutors(t *testing.T) {
468462
for _, test := range tests {
469463
t.Run(test.name, func(t *testing.T) {
470464
ctx := context.Background()
471-
req := testcontainers.ContainerRequest{
472-
Image: test.image,
473-
ImageSubstitutors: test.substitutors,
474-
}
475-
476-
ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
477-
ContainerRequest: req,
478-
Started: true,
479-
})
465+
ctr, err := testcontainers.Run(ctx, test.image, testcontainers.WithImageSubstitutors(test.substitutors...))
480466
testcontainers.CleanupContainer(t, ctr)
481467
if test.expectedError != nil {
482468
require.ErrorIs(t, err, test.expectedError)
483469
return
484470
}
485471

486472
require.NoError(t, err)
487-
488-
// enforce the concrete type, as GenericContainer returns an interface,
489-
// which will be changed in future implementations of the library
490-
dockerContainer := ctr.(*testcontainers.DockerContainer)
491-
assert.Equal(t, test.expectedImage, dockerContainer.Image)
473+
require.Equal(t, test.expectedImage, ctr.Image)
492474
})
493475
}
494476
}
@@ -502,15 +484,11 @@ func TestShouldStartContainersInParallel(t *testing.T) {
502484
t.Run(fmt.Sprintf("iteration_%d", i), func(t *testing.T) {
503485
t.Parallel()
504486

505-
req := testcontainers.ContainerRequest{
506-
Image: nginxAlpineImage,
507-
ExposedPorts: []string{nginxDefaultPort},
508-
WaitingFor: wait.ForHTTP("/").WithStartupTimeout(10 * time.Second),
509-
}
510-
ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
511-
ContainerRequest: req,
512-
Started: true,
513-
})
487+
ctr, err := testcontainers.Run(
488+
ctx, nginxAlpineImage,
489+
testcontainers.WithExposedPorts(nginxDefaultPort),
490+
testcontainers.WithWaitStrategy(wait.ForHTTP("/").WithStartupTimeout(10*time.Second)),
491+
)
514492
testcontainers.CleanupContainer(t, ctr)
515493
require.NoError(t, err)
516494

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

530508
// applyImageSubstitutors {
531-
ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
532-
ContainerRequest: testcontainers.ContainerRequest{
533-
Image: "alpine:latest",
534-
ImageSubstitutors: []testcontainers.ImageSubstitutor{dockerImageSubstitutor{}},
535-
},
536-
Started: true,
537-
})
509+
ctr, err := testcontainers.Run(ctx, "alpine:latest", testcontainers.WithImageSubstitutors(dockerImageSubstitutor{}))
538510
defer func() {
539511
if err := testcontainers.TerminateContainer(ctr); err != nil {
540512
log.Printf("failed to terminate container: %s", err)
@@ -547,11 +519,7 @@ func ExampleGenericContainer_withSubstitutors() {
547519
return
548520
}
549521

550-
// enforce the concrete type, as GenericContainer returns an interface,
551-
// which will be changed in future implementations of the library
552-
dockerContainer := ctr.(*testcontainers.DockerContainer)
553-
554-
fmt.Println(dockerContainer.Image)
522+
fmt.Println(ctr.Image)
555523

556524
// Output: registry.hub.docker.com/library/alpine:latest
557525
}

examples/nginx/nginx.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@ type nginxContainer struct {
1414
}
1515

1616
func startContainer(ctx context.Context) (*nginxContainer, error) {
17-
req := testcontainers.ContainerRequest{
18-
Image: "nginx",
19-
ExposedPorts: []string{"80/tcp"},
20-
WaitingFor: wait.ForHTTP("/").WithStartupTimeout(10 * time.Second),
21-
}
22-
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
23-
ContainerRequest: req,
24-
Started: true,
25-
})
17+
ctr, err := testcontainers.Run(
18+
ctx, "nginx",
19+
testcontainers.WithExposedPorts("80/tcp"),
20+
testcontainers.WithWaitStrategy(wait.ForHTTP("/").WithStartupTimeout(10*time.Second)),
21+
)
2622
var nginxC *nginxContainer
27-
if container != nil {
28-
nginxC = &nginxContainer{Container: container}
23+
if ctr != nil {
24+
nginxC = &nginxContainer{Container: ctr}
2925
}
3026
if err != nil {
3127
return nginxC, err
3228
}
3329

34-
endpoint, err := container.PortEndpoint(ctx, "80", "http")
30+
endpoint, err := ctr.PortEndpoint(ctx, "80", "http")
3531
if err != nil {
3632
return nginxC, err
3733
}

options_test.go

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,14 @@ func (lc *msgsLogConsumer) Accept(l testcontainers.Log) {
7878
}
7979

8080
func TestWithLogConsumers(t *testing.T) {
81-
req := testcontainers.GenericContainerRequest{
82-
ContainerRequest: testcontainers.ContainerRequest{
83-
Image: "mysql:8.0.36",
84-
WaitingFor: wait.ForLog("port: 3306 MySQL Community Server - GPL"),
85-
},
86-
Started: true,
87-
}
88-
8981
lc := &msgsLogConsumer{}
9082

91-
err := testcontainers.WithLogConsumers(lc)(&req)
92-
require.NoError(t, err)
93-
9483
ctx := context.Background()
95-
c, err := testcontainers.GenericContainer(ctx, req)
84+
c, err := testcontainers.Run(
85+
ctx, "mysql:8.0.36",
86+
testcontainers.WithWaitStrategy(wait.ForLog("port: 3306 MySQL Community Server - GPL")),
87+
testcontainers.WithLogConsumers(lc),
88+
)
9689
testcontainers.CleanupContainer(t, c)
9790
// we expect an error because the MySQL environment variables are not set
9891
// but this is expected because we just want to test the log consumer
@@ -138,23 +131,13 @@ func TestWithLogConsumerConfig(t *testing.T) {
138131
}
139132

140133
func TestWithStartupCommand(t *testing.T) {
141-
req := testcontainers.GenericContainerRequest{
142-
ContainerRequest: testcontainers.ContainerRequest{
143-
Image: "alpine",
144-
Entrypoint: []string{"tail", "-f", "/dev/null"},
145-
},
146-
Started: true,
147-
}
148-
149134
testExec := testcontainers.NewRawCommand([]string{"touch", ".testcontainers"}, exec.WithWorkingDir("/tmp"))
150135

151-
err := testcontainers.WithStartupCommand(testExec)(&req)
152-
require.NoError(t, err)
153-
154-
require.Len(t, req.LifecycleHooks, 1)
155-
require.Len(t, req.LifecycleHooks[0].PostStarts, 1)
156-
157-
c, err := testcontainers.GenericContainer(context.Background(), req)
136+
c, err := testcontainers.Run(
137+
context.Background(), "alpine",
138+
testcontainers.WithEntrypoint("tail", "-f", "/dev/null"),
139+
testcontainers.WithStartupCommand(testExec),
140+
)
158141
testcontainers.CleanupContainer(t, c)
159142
require.NoError(t, err)
160143

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

169152
func TestWithAfterReadyCommand(t *testing.T) {
170-
req := testcontainers.GenericContainerRequest{
171-
ContainerRequest: testcontainers.ContainerRequest{
172-
Image: "alpine",
173-
Entrypoint: []string{"tail", "-f", "/dev/null"},
174-
},
175-
Started: true,
176-
}
177-
178153
testExec := testcontainers.NewRawCommand([]string{"touch", "/tmp/.testcontainers"})
179154

180-
err := testcontainers.WithAfterReadyCommand(testExec)(&req)
181-
require.NoError(t, err)
182-
183-
require.Len(t, req.LifecycleHooks, 1)
184-
require.Len(t, req.LifecycleHooks[0].PostReadies, 1)
185-
186-
c, err := testcontainers.GenericContainer(context.Background(), req)
155+
c, err := testcontainers.Run(context.Background(), "alpine", testcontainers.WithEntrypoint("tail", "-f", "/dev/null"), testcontainers.WithAfterReadyCommand(testExec))
187156
testcontainers.CleanupContainer(t, c)
188157
require.NoError(t, err)
189158

options_unit_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package testcontainers
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/testcontainers/testcontainers-go/exec"
9+
)
10+
11+
func TestWithStartupCommand_unit(t *testing.T) {
12+
req := GenericContainerRequest{
13+
ContainerRequest: ContainerRequest{},
14+
}
15+
16+
testExec := NewRawCommand([]string{"touch", ".testcontainers"}, exec.WithWorkingDir("/tmp"))
17+
18+
err := WithStartupCommand(testExec)(&req)
19+
require.NoError(t, err)
20+
21+
require.Len(t, req.LifecycleHooks, 1)
22+
require.Len(t, req.LifecycleHooks[0].PostStarts, 1)
23+
}
24+
25+
func TestWithAfterReadyCommand_unit(t *testing.T) {
26+
req := GenericContainerRequest{
27+
ContainerRequest: ContainerRequest{},
28+
}
29+
30+
testExec := NewRawCommand([]string{"touch", "/tmp/.testcontainers"})
31+
32+
err := WithAfterReadyCommand(testExec)(&req)
33+
require.NoError(t, err)
34+
35+
require.Len(t, req.LifecycleHooks, 1)
36+
require.Len(t, req.LifecycleHooks[0].PostReadies, 1)
37+
}

0 commit comments

Comments
 (0)