Skip to content

chore!: use testcontainers.Run function everywhere #3174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 102 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
9f0c6e0
chore: deprecate GenericContainer
mdelapenya Apr 22, 2025
c56241b
chore: migrate tests to Run
mdelapenya Apr 21, 2025
a37ffd3
docs: update quickstart
mdelapenya Apr 21, 2025
ab06444
chore: update more tests
mdelapenya Apr 21, 2025
fea6f17
chore: update more tests
mdelapenya Apr 21, 2025
7bce56b
chore(aerospike): use Run function
mdelapenya Apr 21, 2025
0413e94
chore(socat): use Run function
mdelapenya Apr 21, 2025
4818763
chore: update more tests
mdelapenya Apr 22, 2025
004f689
chore: update tests in the wait package
mdelapenya Apr 22, 2025
81c94d2
chore: use Run in the network package
mdelapenya Apr 22, 2025
913a9b9
chore: move test to the network package
mdelapenya Apr 22, 2025
803d504
chore: use Run in the core
mdelapenya Apr 29, 2025
c3b1cfb
chore(nginx): use Run in nginx example
mdelapenya Apr 29, 2025
8774193
chore(modulegen): use testcontainers.Run
mdelapenya Apr 29, 2025
eb41bd1
chore(azurite): use testcontainers.Run
mdelapenya Apr 29, 2025
dd81682
break(artemis): use testcontainers.Run
mdelapenya Apr 29, 2025
6e26784
break(eventhubs): use testcontainers.Run
mdelapenya May 21, 2025
1d39b41
break(arangodb): use testcontainers.Run
mdelapenya May 21, 2025
e92519c
break(servicebus): use testcontainers.Run
mdelapenya May 21, 2025
333d460
chore(cassandra): use testcontainers.Run
mdelapenya May 21, 2025
0fdd71e
chore(chroma): use testcontainers.Run
mdelapenya May 21, 2025
8692d52
break(clickhouse): use testcontainers.Run
mdelapenya May 21, 2025
079f5a4
chore(consul): use testcontainers.Run
mdelapenya May 21, 2025
780cd53
chore(couchbase): use testcontainers.Run
mdelapenya May 21, 2025
1b019aa
break(databend): use testcontainers.Run
mdelapenya May 21, 2025
085f537
chore(dind): use testcontainers.Run
mdelapenya May 21, 2025
1828bc1
break(dolt): use testcontainers.Run
mdelapenya May 21, 2025
894e6c4
break(dynamodb): use testcontainers.Run
mdelapenya May 21, 2025
a7d4b0e
chore(etcd): use testcontainers.Run
mdelapenya May 21, 2025
00e21b3
break(etcd): make options return errors
mdelapenya May 21, 2025
92a00b4
chore(gcloud): use testcontainers.Run in deprecated constructors
mdelapenya May 21, 2025
268980c
chore(bigquery): use testcontainers.Run
mdelapenya May 21, 2025
094ff73
chore(bigtable): use testcontainers.Run
mdelapenya May 21, 2025
9ebe5d7
chore(datastore): use testcontainers.Run
mdelapenya May 21, 2025
2ee45e1
chore(firestore): use testcontainers.Run
mdelapenya May 21, 2025
cb34d84
chore(pubsub): use testcontainers.Run
mdelapenya May 21, 2025
26da15d
chore(spanner): use testcontainers.Run
mdelapenya May 21, 2025
8ba39aa
chore(graafanalgtm): use testcontainers.Run
mdelapenya May 21, 2025
fc0d714
chore(inbucket): use testcontainers.Run
mdelapenya May 21, 2025
3e97ca8
chore(influxdb): use testcontainers.Run
mdelapenya May 21, 2025
e87695c
chore(k3s): use testcontainers.Run
mdelapenya May 21, 2025
b71d956
chore(k6): use testcontainers.Run
mdelapenya May 21, 2025
321b4ab
break(kafka): use testcontainers.Run
mdelapenya May 21, 2025
a57170f
chore(localstack): use testcontainers.Run
mdelapenya May 21, 2025
f409687
break(mariadb): use testcontainers.Run
mdelapenya May 21, 2025
4040ca8
break(meilisearch): use testcontainers.Run
mdelapenya May 21, 2025
00f7f1d
chore(memcached): use testcontainers.Run
mdelapenya May 21, 2025
1d2d462
chore(milvus): use testcontainers.Run
mdelapenya May 21, 2025
24015b8
break(minio): use testcontainers.Run
mdelapenya May 21, 2025
30e06e7
chore(mockserver): use testcontainers.Run
mdelapenya May 21, 2025
9d4b828
break(mongodb): use testcontainers.Run
mdelapenya May 21, 2025
e5a9efb
docs(dockermodelrunner): fix comment
mdelapenya May 21, 2025
95711be
break(mssql): use testcontainers.Run
mdelapenya May 21, 2025
c78a49a
docs(mssql): describe MSSQL issue with negative certificates
mdelapenya May 22, 2025
0e6bf99
chore(mssql): bump images to address the negative serial number of th…
mdelapenya May 22, 2025
9fdaa74
break(mysql): use testcontainers.Run
mdelapenya May 22, 2025
ff5d1a3
break(nats): use testcontainers.Run
mdelapenya May 22, 2025
dbeac76
chore(neo4j): use testcontainers.Run
mdelapenya May 22, 2025
c8b9272
chore(openfga): use testcontainers.Run
mdelapenya May 22, 2025
b081a42
break(openldap): use testcontainers.Run
mdelapenya May 22, 2025
af3dc12
chore(pinecone): use testcontainers.Run
mdelapenya May 22, 2025
a9510dd
break(postgres): use testcontainers.Run
mdelapenya May 22, 2025
9985c86
chore(pulsar): use testcontainers.Run
mdelapenya May 22, 2025
81964ac
chore(qdrant): use testcontainers.Run
mdelapenya May 22, 2025
ce1a280
break(rabbitmq): use testcontainers.Run
mdelapenya May 22, 2025
d6a799b
chore(rabbitmq): consistency in credentials as env vars
mdelapenya May 22, 2025
742c487
chore(redis): use testcontainers.Run
mdelapenya May 22, 2025
6a28907
break(redpanda): use testcontainers.Run
mdelapenya May 23, 2025
def23b1
chore(registry): use testcontainers.Run
mdelapenya May 23, 2025
dc4070c
chore(scylladb): use testcontainers.Run
mdelapenya May 23, 2025
4006eb0
fixup socat
mdelapenya May 23, 2025
843d5f4
chore(surrealdb): use testcontainers.Run
mdelapenya May 23, 2025
a4c7047
fixup socat
mdelapenya May 23, 2025
cfc3b1d
chore(toxiproxy): use testcontainers.Run
mdelapenya May 23, 2025
69dbd3e
chore(valkey): use testcontainers.Run
mdelapenya May 23, 2025
e613576
chore(vault): use testcontainers.Run
mdelapenya May 23, 2025
e1f17c2
chore(vearch): use testcontainers.Run
mdelapenya May 23, 2025
78e748a
chore(weaviate): use testcontainers.Run
mdelapenya May 23, 2025
881156c
break(yugabyte): use testcontainers.Run
mdelapenya May 23, 2025
e1131b7
chore(opensearch): use testcontainers.Run
mdelapenya May 23, 2025
bcede4a
docs: fix link
mdelapenya May 26, 2025
a437f0c
docs: fix snippet
mdelapenya May 26, 2025
070a5a8
docs: fix code snippet
mdelapenya May 26, 2025
e60087e
fix: initialise env vars map to avoid nil access
mdelapenya May 26, 2025
dce3b73
fix: update test for bridge network option
mdelapenya May 26, 2025
1e0b3b6
fix: proper azurite opts initialisation
mdelapenya May 26, 2025
cd47683
fix: wrong assertion change
mdelapenya May 26, 2025
65f48c5
chore: apply Run in reusable container tests
mdelapenya May 26, 2025
4cfdc96
fix: reuse error for container name in reuse
mdelapenya May 26, 2025
289abc1
chore: more deterministic container names
mdelapenya May 26, 2025
822cada
Merge branch 'main' into use-run-function
mdelapenya Jun 2, 2025
eb8149a
break(cockroachdb): use testcontainers.Run
mdelapenya Jun 2, 2025
857ba85
chore(elasticsearch): use testcontainers.Run
mdelapenya Jun 3, 2025
ac16bb5
chore(ollama): use testcontainers.Run
mdelapenya Jun 3, 2025
1e7017f
Merge branch 'main' into use-run-function
mdelapenya Jun 3, 2025
de88678
fix: lint
mdelapenya Jun 3, 2025
fe52983
Merge branch 'main' into use-run-function
mdelapenya Jun 3, 2025
a81e379
chore: merge labels must be thread-safe
mdelapenya Jun 3, 2025
facf916
fix: options tests
mdelapenya Jun 4, 2025
1dd29b6
Merge branch 'main' into use-run-function
mdelapenya Jun 10, 2025
b772f5c
Merge branch 'main' into use-run-function
mdelapenya Jun 17, 2025
deeb0cf
Merge branch 'main' into use-run-function
mdelapenya Jun 18, 2025
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
112 changes: 36 additions & 76 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func Test_BuildImageWithContexts(t *testing.T) {
ContextArchive: func() (io.ReadSeeker, error) {
return nil, nil
},
ExpectedError: "create container: you must specify either a build context or an image",
ExpectedError: "generic container: create container: you must specify either a build context or an image",
},
}

Expand All @@ -290,19 +290,15 @@ func Test_BuildImageWithContexts(t *testing.T) {
a, err := testCase.ContextArchive()
require.NoError(t, err)

req := testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
c, err := testcontainers.Run(
ctx, "",
testcontainers.WithDockerfile(testcontainers.FromDockerfile{
ContextArchive: a,
Context: testCase.ContextPath,
Dockerfile: testCase.Dockerfile,
},
WaitingFor: wait.ForLog(testCase.ExpectedEchoOutput).WithStartupTimeout(1 * time.Minute),
}

c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
}),
testcontainers.WithWaitStrategy(wait.ForLog(testCase.ExpectedEchoOutput).WithStartupTimeout(1*time.Minute)),
)
testcontainers.CleanupContainer(t, c)

if testCase.ExpectedError != "" {
Expand All @@ -322,17 +318,10 @@ func TestCustomLabelsImage(t *testing.T) {
)

ctx := context.Background()
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "alpine:latest",
Labels: map[string]string{myLabelName: myLabelValue},
},
}

ctr, err := testcontainers.GenericContainer(ctx, req)

ctr, err := testcontainers.Run(ctx, "alpine:latest", testcontainers.WithLabels(map[string]string{myLabelName: myLabelValue}))
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, ctr.Terminate(ctx)) })

ctrJSON, err := ctr.Inspect(ctx)
require.NoError(t, err)
Expand All @@ -348,22 +337,20 @@ func TestCustomLabelsBuildOptionsModifier(t *testing.T) {
)

ctx := context.Background()
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Context: "./testdata",
Dockerfile: "Dockerfile",
BuildOptionsModifier: func(opts *build.ImageBuildOptions) {
opts.Labels = map[string]string{
myBuildOptionLabel: myBuildOptionValue,
}
},
},
Labels: map[string]string{myLabelName: myLabelValue},
},
}

ctr, err := testcontainers.GenericContainer(ctx, req)
ctr, err := testcontainers.Run(
ctx, "",
testcontainers.WithDockerfile(testcontainers.FromDockerfile{
Context: "./testdata",
Dockerfile: "Dockerfile",
BuildOptionsModifier: func(opts *build.ImageBuildOptions) {
opts.Labels = map[string]string{
myBuildOptionLabel: myBuildOptionValue,
}
},
}),
testcontainers.WithLabels(map[string]string{myLabelName: myLabelValue}),
)
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)

Expand All @@ -376,17 +363,13 @@ 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 @@ -481,15 +464,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)
Expand All @@ -500,8 +475,7 @@ func TestImageSubstitutors(t *testing.T) {

// 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)
assert.Equal(t, test.expectedImage, ctr.Image)
})
}
}
Expand All @@ -515,15 +489,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 @@ -541,13 +511,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 @@ -560,11 +524,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
}
100 changes: 35 additions & 65 deletions docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,15 @@ func TestDockerImageAuth(t *testing.T) {

func TestBuildContainerFromDockerfile(t *testing.T) {
ctx := context.Background()
req := ContainerRequest{
FromDockerfile: FromDockerfile{
Context: "./testdata",
},
AlwaysPullImage: true, // make sure the authentication takes place
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForLog("Ready to accept connections"),
}

redisC, err := prepareRedisImage(ctx, req)
redisC, err := Run(ctx, "",
WithDockerfile(FromDockerfile{
Context: "./testdata",
}),
WithAlwaysPull(),
WithExposedPorts("6379/tcp"),
WithWaitStrategy(wait.ForLog("Ready to accept connections")),
)
CleanupContainer(t, redisC)
require.NoError(t, err)
}
Expand Down Expand Up @@ -201,21 +200,19 @@ func TestBuildContainerFromDockerfileWithDockerAuthConfig(t *testing.T) {

ctx := context.Background()

req := ContainerRequest{
FromDockerfile: FromDockerfile{
redisC, err := Run(ctx, "",
WithDockerfile(FromDockerfile{
Context: "./testdata",
Dockerfile: "auth.Dockerfile",
BuildArgs: map[string]*string{
"REGISTRY_HOST": &registryHost,
},
Repo: "localhost",
},
AlwaysPullImage: true, // make sure the authentication takes place
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForLog("Ready to accept connections"),
}

redisC, err := prepareRedisImage(ctx, req)
}),
WithAlwaysPull(),
WithExposedPorts("6379/tcp"),
WithWaitStrategy(wait.ForLog("Ready to accept connections")),
)
CleanupContainer(t, redisC)
require.NoError(t, err)
}
Expand All @@ -228,20 +225,18 @@ func TestBuildContainerFromDockerfileShouldFailWithWrongDockerAuthConfig(t *test

ctx := context.Background()

req := ContainerRequest{
FromDockerfile: FromDockerfile{
redisC, err := Run(ctx, "",
WithDockerfile(FromDockerfile{
Context: "./testdata",
Dockerfile: "auth.Dockerfile",
BuildArgs: map[string]*string{
"REGISTRY_HOST": &registryHost,
},
},
AlwaysPullImage: true, // make sure the authentication takes place
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForLog("Ready to accept connections"),
}

redisC, err := prepareRedisImage(ctx, req)
}),
WithAlwaysPull(),
WithExposedPorts("6379/tcp"),
WithWaitStrategy(wait.ForLog("Ready to accept connections")),
)
CleanupContainer(t, redisC)
require.Error(t, err)
}
Expand All @@ -253,17 +248,8 @@ func TestCreateContainerFromPrivateRegistry(t *testing.T) {
setAuthConfig(t, registryHost, "testuser", "testpassword")

ctx := context.Background()
req := ContainerRequest{
Image: registryHost + "/redis:5.0-alpine",
AlwaysPullImage: true, // make sure the authentication takes place
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForLog("Ready to accept connections"),
}

redisContainer, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
redisContainer, err := Run(ctx, registryHost+"/redis:5.0-alpine", WithAlwaysPull(), WithExposedPorts("6379/tcp"), WithWaitStrategy(wait.ForLog("Ready to accept connections")))
CleanupContainer(t, redisContainer)
require.NoError(t, err)
}
Expand All @@ -273,37 +259,31 @@ func prepareLocalRegistryWithAuth(t *testing.T) string {
ctx := context.Background()
wd, err := os.Getwd()
require.NoError(t, err)

// copyDirectoryToContainer {
req := ContainerRequest{
Image: "registry:2",
ExposedPorts: []string{"5000/tcp"},
Env: map[string]string{
registryC, err := Run(ctx, "registry:2",
WithAlwaysPull(),
WithEnv(map[string]string{
"REGISTRY_AUTH": "htpasswd",
"REGISTRY_AUTH_HTPASSWD_REALM": "Registry",
"REGISTRY_AUTH_HTPASSWD_PATH": "/auth/htpasswd",
"REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY": "/data",
},
Files: []ContainerFile{
{
}),
WithFiles(
ContainerFile{
HostFilePath: wd + "/testdata/auth",
ContainerFilePath: "/auth",
},
{
ContainerFile{
HostFilePath: wd + "/testdata/data",
ContainerFilePath: "/data",
},
},
WaitingFor: wait.ForHTTP("/").WithPort("5000/tcp"),
}
),
WithExposedPorts("5000/tcp"),
WithWaitStrategy(wait.ForHTTP("/").WithPort("5000/tcp")),
)
// }

genContainerReq := GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: req,
Started: true,
}

registryC, err := GenericContainer(ctx, genContainerReq)
CleanupContainer(t, registryC)
require.NoError(t, err)

Expand All @@ -321,16 +301,6 @@ func prepareLocalRegistryWithAuth(t *testing.T) string {
return addr
}

func prepareRedisImage(ctx context.Context, req ContainerRequest) (Container, error) {
genContainerReq := GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: req,
Started: true,
}

return GenericContainer(ctx, genContainerReq)
}

// setAuthConfig sets the DOCKER_AUTH_CONFIG environment variable with
// authentication for with the given host, username and password.
// It returns the base64 encoded credentials.
Expand Down
Loading
Loading