Skip to content

Commit c2d3ce4

Browse files
Add missing case to container name resolution
1 parent 4f89387 commit c2d3ce4

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { resolveDockerComposeContainerName } from "./docker-compose-container-name-resolver";
2+
3+
describe("resolveDockerComposeContainerName", () => {
4+
it("should remove docker-compose label", () => {
5+
const name = "docker-compose_container_1";
6+
const expected = "container_1";
7+
8+
expect(resolveDockerComposeContainerName(name)).toBe(expected);
9+
});
10+
11+
it("should remove prefix", () => {
12+
const name = "123_docker-compose_container_1";
13+
const expected = "container_1";
14+
15+
expect(resolveDockerComposeContainerName(name)).toBe(expected);
16+
});
17+
18+
it("should remove suffix", () => {
19+
const name = "docker-compose_container_1_123";
20+
const expected = "container_1";
21+
22+
expect(resolveDockerComposeContainerName(name)).toBe(expected);
23+
});
24+
25+
it("should throw error if unable to resolve container name", () => {
26+
expect(() => resolveDockerComposeContainerName("unknown")).toThrowError(
27+
`Unable to resolve container name for: "unknown"`
28+
);
29+
});
30+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const resolveDockerComposeContainerName = (containerName: string): string => {
2+
const matches = containerName.match(/^.*docker-compose_(.+?_[0-9]+)/);
3+
if (!matches) {
4+
throw new Error(`Unable to resolve container name for: "${containerName}"`);
5+
}
6+
return matches[1];
7+
};

src/docker-compose-environment.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Container } from "./container";
66
import { ContainerState } from "./container-state";
77
import { DockerClient } from "./docker-client";
88
import { DockerodeClientFactory } from "./docker-client-factory";
9+
import { resolveDockerComposeContainerName } from "./docker-compose-container-name-resolver";
910
import { StartedGenericContainer } from "./generic-container";
1011
import log from "./logger";
1112
import { HostPortCheck, InternalPortCheck } from "./port-check";
@@ -46,7 +47,7 @@ export class DockerComposeEnvironment {
4647
const startedGenericContainers = (await Promise.all(
4748
startedContainers.map(async startedContainer => {
4849
const container = await this.dockerClient.getContainer(startedContainer.Id);
49-
const containerName = this.getContainerName(startedContainer);
50+
const containerName = resolveDockerComposeContainerName(startedContainer.Names[0]);
5051

5152
(await container.logs())
5253
.on("data", data => log.trace(`${containerName}: ${data}`))
@@ -99,15 +100,6 @@ export class DockerComposeEnvironment {
99100
return boundPorts;
100101
}
101102

102-
private getContainerName(container: Dockerode.ContainerInfo): string {
103-
const containerName = container.Names[0];
104-
const matches = containerName.match(/^.*docker-compose_(.*$)/);
105-
if (!matches) {
106-
throw new Error(`Unable to compute container name for: "${containerName}"`);
107-
}
108-
return matches[1];
109-
}
110-
111103
private async waitForContainer(
112104
container: Container,
113105
containerName: string,

0 commit comments

Comments
 (0)