Skip to content

Commit d5ae058

Browse files
Warn on unmatched compose wait strategy names
1 parent 1eec889 commit d5ae058

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

docs/features/compose.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ Provide a list of service names to only start those services:
2929

3030
```js
3131
const environment = await new DockerComposeEnvironment(composeFilePath, composeFile)
32-
.up(["redis-1", "postgres-1"]);
32+
.up(["redis", "postgres"]);
3333
```
3434

3535
### With wait strategy
3636

37+
`withWaitStrategy` expects **container names**, not service names. With Docker Compose v2, the default container name for the first replica is usually `<service>-1`.
38+
3739
```js
3840
const environment = await new DockerComposeEnvironment(composeFilePath, composeFile)
3941
.withWaitStrategy("redis-1", Wait.forLogMessage("Ready to accept connections"))
4042
.withWaitStrategy("postgres-1", Wait.forHealthCheck())
41-
.up();
43+
.up(["redis", "postgres"]);
4244
```
4345

4446
### With a default wait strategy

packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path";
2-
import { RandomUuid } from "../common";
2+
import { log, RandomUuid } from "../common";
33
import { randomUuid } from "../common/uuid";
44
import { PullPolicy } from "../utils/pull-policy";
55
import {
@@ -131,6 +131,23 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
131131
await checkEnvironmentContainerIsHealthy(startedEnvironment, await composeContainerName("container"));
132132
});
133133

134+
it("should warn when no started containers match configured wait strategy names", async () => {
135+
const warnSpy = vi.spyOn(log, "warn");
136+
137+
await using startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose.yml")
138+
.withWaitStrategy("container", Wait.forLogMessage("Listening on port 8080"))
139+
.up(["container"]);
140+
141+
await checkEnvironmentContainerIsHealthy(startedEnvironment, await composeContainerName("container"));
142+
143+
const warningMessages = warnSpy.mock.calls.map(([message]) => message);
144+
expect(
145+
warningMessages.some((warningMessage) =>
146+
warningMessage.includes(`No containers were started for the configured wait strategy names: "container"`)
147+
)
148+
).toBe(true);
149+
});
150+
134151
it("should support failing health check wait strategy", async () => {
135152
await expect(
136153
new DockerComposeEnvironment(fixtures, "docker-compose-with-healthcheck-unhealthy.yml")

packages/testcontainers/src/docker-compose-environment/docker-compose-environment.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ export class DockerComposeEnvironment {
150150
);
151151
log.info(`Started containers "${startedContainerNames.join('", "')}"`);
152152

153+
const startedContainerNameSet = new Set(
154+
startedContainers.map((startedContainer) =>
155+
parseComposeContainerName(this.projectName, startedContainer.Names[0])
156+
)
157+
);
158+
this.warnForUnusedWaitStrategies(startedContainerNameSet);
159+
153160
const startedGenericContainers = (
154161
await Promise.all(
155162
startedContainers.map(async (startedContainer) => {
@@ -207,4 +214,15 @@ export class DockerComposeEnvironment {
207214
environment: this.environment,
208215
});
209216
}
217+
218+
private warnForUnusedWaitStrategies(startedContainerNames: Set<string>): void {
219+
const unusedWaitStrategyContainerNames = Object.keys(this.waitStrategy).filter(
220+
(configuredContainerName) => !startedContainerNames.has(configuredContainerName)
221+
);
222+
if (unusedWaitStrategyContainerNames.length > 0) {
223+
log.warn(
224+
`No containers were started for the configured wait strategy names: "${unusedWaitStrategyContainerNames.join('", "')}". Wait strategies are matched against container names (for example "redis-1"), not service names.`
225+
);
226+
}
227+
}
210228
}

0 commit comments

Comments
 (0)