Skip to content

Commit 5dc5293

Browse files
Simplify Compose naming to v2-only conventions (#1238)
1 parent 975665b commit 5dc5293

File tree

6 files changed

+32
-37
lines changed

6 files changed

+32
-37
lines changed

docs/features/compose.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ await environment.stop();
189189

190190
## Interacting with the containers
191191

192-
Interact with the containers in your compose environment as you would any other Generic Container. Note that the container name suffix has changed from `_` to `-` between docker-compose v1 and v2 respectively.
192+
Interact with the containers in your compose environment as you would any other Generic Container. Compose-managed container names use the `<service-name>-<index>` format.
193193

194194
```js
195195
const container = environment.getContainer("alpine-1");
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
version: "3.5"
22

33
services:
4-
service_1:
4+
service-a:
55
image: cristianrgreco/testcontainer:1.1.14
66
ports:
77
- 8080
8-
service_2:
8+
service-b:
99
image: cristianrgreco/testcontainer:1.1.14
1010
ports:
1111
- 8080

packages/testcontainers/src/container-runtime/clients/compose/parse-compose-container-name.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { parseComposeContainerName } from "./parse-compose-container-name";
22

33
describe("parseComposeContainerName", () => {
44
it("should remove project name label", () => {
5-
const name = "/project-name_container_1";
6-
const expected = "container_1";
5+
const name = "/project-name-container-1";
6+
const expected = "container-1";
77

88
expect(parseComposeContainerName("project-name", name)).toBe(expected);
99
});
@@ -16,8 +16,8 @@ describe("parseComposeContainerName", () => {
1616
});
1717

1818
it("should throw error if unable to resolve container name", () => {
19-
expect(() => parseComposeContainerName("project-name", "container_1")).toThrowError(
20-
`Unable to resolve container name for container name: "container_1", project name: "project-name"`
19+
expect(() => parseComposeContainerName("project-name", "container-1")).toThrowError(
20+
`Unable to resolve container name for container name: "container-1", project name: "project-name"`
2121
);
2222
});
2323
});

packages/testcontainers/src/container-runtime/clients/compose/parse-compose-container-name.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export function parseComposeContainerName(projectName: string, containerName: string): string {
2-
if (containerName.includes(projectName)) {
3-
return containerName.substring(`/${projectName}_`.length);
2+
if (containerName.startsWith(`/${projectName}-`)) {
3+
return containerName.substring(`/${projectName}-`.length);
44
} else if (containerName.startsWith("/")) {
55
return containerName.substring(1);
66
} else {

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { randomUuid } from "../common/uuid";
44
import { PullPolicy } from "../utils/pull-policy";
55
import {
66
checkEnvironmentContainerIsHealthy,
7-
composeContainerName,
87
getDockerEventStream,
98
getRunningContainerNames,
109
getVolumeNames,
@@ -24,7 +23,7 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
2423
await using startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose.yml").up();
2524

2625
await Promise.all(
27-
[await composeContainerName("container"), await composeContainerName("another_container")].map(
26+
["container-1", "another_container-1"].map(
2827
async (containerName) => await checkEnvironmentContainerIsHealthy(startedEnvironment, containerName)
2928
)
3029
);
@@ -43,7 +42,7 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
4342
it("should work with buildkit", async () => {
4443
const buildkitFixtures = path.resolve(fixtures, "docker-compose-with-buildkit");
4544
await using startedEnvironment = await new DockerComposeEnvironment(buildkitFixtures, "docker-compose.yml").up();
46-
await checkEnvironmentContainerIsHealthy(startedEnvironment, await composeContainerName("container"));
45+
await checkEnvironmentContainerIsHealthy(startedEnvironment, "container-1");
4746
});
4847
}
4948

@@ -63,12 +62,12 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
6362
it("should use pull policy for specific service", async () => {
6463
const env = new DockerComposeEnvironment(fixtures, "docker-compose-with-many-services.yml");
6564

66-
await using _ = await env.up(["service_2"]);
65+
await using _ = await env.up(["service-b"]);
6766

6867
{
6968
await using dockerEventStream = await getDockerEventStream();
7069
const dockerPullEventPromise = waitForDockerEvent(dockerEventStream.events, "pull");
71-
await using _ = await env.withPullPolicy(PullPolicy.alwaysPull()).up(["service_2"]);
70+
await using _ = await env.withPullPolicy(PullPolicy.alwaysPull()).up(["service-b"]);
7271
await dockerPullEventPromise;
7372
}
7473
});
@@ -80,7 +79,7 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
8079
"docker-compose.yml",
8180
"docker-compose-update.yml",
8281
]).up();
83-
await using container = startedEnvironment.getContainer(await composeContainerName("container"));
82+
await using container = startedEnvironment.getContainer("container-1");
8483

8584
const url = `http://${container.getHost()}:${container.getMappedPort(8080)}`;
8685
const response = await fetch(`${url}/env`);
@@ -94,17 +93,17 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
9493
.withDefaultWaitStrategy(Wait.forHealthCheck())
9594
.up();
9695

97-
await checkEnvironmentContainerIsHealthy(startedEnvironment, await composeContainerName("container"));
96+
await checkEnvironmentContainerIsHealthy(startedEnvironment, "container-1");
9897
});
9998

10099
it("should support log message wait strategy", async () => {
101100
await using startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose.yml")
102-
.withWaitStrategy(await composeContainerName("container"), Wait.forLogMessage("Listening on port 8080"))
103-
.withWaitStrategy(await composeContainerName("another_container"), Wait.forLogMessage("Listening on port 8080"))
101+
.withWaitStrategy("container-1", Wait.forLogMessage("Listening on port 8080"))
102+
.withWaitStrategy("another_container-1", Wait.forLogMessage("Listening on port 8080"))
104103
.up();
105104

106105
await Promise.all(
107-
[await composeContainerName("container"), await composeContainerName("another_container")].map(
106+
["container-1", "another_container-1"].map(
108107
async (containerName) => await checkEnvironmentContainerIsHealthy(startedEnvironment, containerName)
109108
)
110109
);
@@ -125,10 +124,10 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
125124

126125
it("should support health check wait strategy", async () => {
127126
await using startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose-with-healthcheck.yml")
128-
.withWaitStrategy(await composeContainerName("container"), Wait.forHealthCheck())
127+
.withWaitStrategy("container-1", Wait.forHealthCheck())
129128
.up();
130129

131-
await checkEnvironmentContainerIsHealthy(startedEnvironment, await composeContainerName("container"));
130+
await checkEnvironmentContainerIsHealthy(startedEnvironment, "container-1");
132131
});
133132

134133
it.sequential("should warn when no started containers match configured wait strategy names", async () => {
@@ -154,20 +153,20 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
154153
it("should support failing health check wait strategy", async () => {
155154
await expect(
156155
new DockerComposeEnvironment(fixtures, "docker-compose-with-healthcheck-unhealthy.yml")
157-
.withWaitStrategy(await composeContainerName("container"), Wait.forHealthCheck())
156+
.withWaitStrategy("container-1", Wait.forHealthCheck())
158157
.up()
159158
).rejects.toThrow(`Health check failed: unhealthy`);
160159
});
161160

162161
it("should stop the container when the health check wait strategy times out", async () => {
163162
await expect(
164163
new DockerComposeEnvironment(fixtures, "docker-compose-with-healthcheck-with-start-period.yml")
165-
.withWaitStrategy(await composeContainerName("container"), Wait.forHealthCheck())
164+
.withWaitStrategy("container-1", Wait.forHealthCheck())
166165
.withStartupTimeout(0)
167166
.up()
168167
).rejects.toThrow(`Health check not healthy after 0ms`);
169168

170-
expect(await getRunningContainerNames()).not.toContain("container_1");
169+
expect(await getRunningContainerNames()).not.toContain("container-1");
171170
});
172171

173172
it("should remove volumes when downing an environment", async () => {
@@ -189,7 +188,7 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
189188
.up();
190189

191190
await Promise.all(
192-
[await composeContainerName("container"), await composeContainerName("another_container")].map(
191+
["container-1", "another_container-1"].map(
193192
async (containerName) => await checkEnvironmentContainerIsHealthy(startedEnvironment, containerName)
194193
)
195194
);
@@ -200,7 +199,7 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
200199
.withEnvironment({ ENV_VAR: "ENV_VAR_VALUE" })
201200
.up();
202201

203-
await using container = startedEnvironment.getContainer(await composeContainerName("container"));
202+
await using container = startedEnvironment.getContainer("container-1");
204203
const response = await fetch(`http://${container.getHost()}:${container.getMappedPort(8080)}/env`);
205204
const responseBody = (await response.json()) as { [key: string]: string };
206205
expect(responseBody["ENV_VAR"]).toBe("ENV_VAR_VALUE");
@@ -218,11 +217,11 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
218217
await using startedEnvironment = await new DockerComposeEnvironment(
219218
fixtures,
220219
"docker-compose-with-many-services.yml"
221-
).up(["service_2"]);
220+
).up(["service-b"]);
222221

223-
await checkEnvironmentContainerIsHealthy(startedEnvironment, await composeContainerName("service_2"));
224-
expect(() => startedEnvironment.getContainer("service_1")).toThrow(
225-
`Cannot get container "service_1" as it is not running`
222+
await checkEnvironmentContainerIsHealthy(startedEnvironment, "service-b-1");
223+
expect(() => startedEnvironment.getContainer("service-a")).toThrow(
224+
`Cannot get container "service-a" as it is not running`
226225
);
227226
});
228227

@@ -244,7 +243,7 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
244243

245244
await using startedEnvironment = await new DockerComposeEnvironment(overrideFixtures, "docker-compose.yml").up();
246245

247-
await using container = startedEnvironment.getContainer(await composeContainerName("container"));
246+
await using container = startedEnvironment.getContainer("container-1");
248247
const response = await fetch(`http://${container.getHost()}:${container.getMappedPort(8080)}/env`);
249248
const responseBody = (await response.json()) as { [key: string]: string };
250249
expect(responseBody["ENV_VAR"]).toBe("default");
@@ -257,7 +256,7 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
257256
.withEnvironmentFile(".env.override")
258257
.up();
259258

260-
await using container = startedEnvironment.getContainer(await composeContainerName("container"));
259+
await using container = startedEnvironment.getContainer("container-1");
261260
const response = await fetch(`http://${container.getHost()}:${container.getMappedPort(8080)}/env`);
262261
const responseBody = (await response.json()) as { [key: string]: string };
263262
expect(responseBody["ENV_VAR"]).toBe("override");
@@ -269,7 +268,7 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
269268
.up();
270269

271270
await Promise.all(
272-
[await composeContainerName("container"), await composeContainerName("another_container")].map(
271+
["container-1", "another_container-1"].map(
273272
async (containerName) => await checkEnvironmentContainerIsHealthy(startedEnvironment, containerName)
274273
)
275274
);

packages/testcontainers/src/utils/test-helper.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ export const getVolumeNames = async (): Promise<string[]> => {
119119
return volumes.map((volume) => volume.Name);
120120
};
121121

122-
export const composeContainerName = async (serviceName: string, index = 1): Promise<string> => {
123-
return `${serviceName}-${index}`;
124-
};
125-
126122
export const waitForDockerEvent = async (eventStream: Readable, eventName: string, times = 1) => {
127123
let currentTimes = 0;
128124
let pendingData = "";

0 commit comments

Comments
 (0)