Skip to content

Commit b94ce11

Browse files
committed
move selectWaitStrategy inside startContainer
1 parent f47af2e commit b94ce11

File tree

3 files changed

+56
-56
lines changed

3 files changed

+56
-56
lines changed

packages/testcontainers/src/generic-container/generic-container-wait-strategy.test.ts

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,45 @@ import { StartedGenericContainer } from "./started-generic-container";
77

88
const fixtures = path.resolve(__dirname, "..", "..", "fixtures", "docker");
99

10-
if (!process.env.CI_PODMAN) {
11-
describe("GenericContainer wait strategy", { timeout: 180_000 }, () => {
12-
it("should use Wait.forListeningPorts if healthcheck is not defined in DOCKERFILE", async () => {
13-
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
14-
.withExposedPorts(8080)
15-
.start();
16-
expect((container as StartedGenericContainer)["getWaitStrategy"]()).toBeInstanceOf(HostPortWaitStrategy);
17-
});
18-
it("should use Wait.forHealthCheck if withHealthCheck() explicitly called", async () => {
19-
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
20-
.withExposedPorts(8080)
21-
.withHealthCheck({
22-
test: ["CMD-SHELL", "echo 'started' && exit 0"],
23-
})
24-
.start();
25-
expect((container as StartedGenericContainer)["getWaitStrategy"]()).toBeInstanceOf(HealthCheckWaitStrategy);
26-
});
27-
it("should use Wait.forHealthCheck if healthcheck is defined in DOCKERFILE", async () => {
28-
const context = path.resolve(fixtures, "docker-with-health-check");
29-
const genericContainer = await GenericContainer.fromDockerfile(context).build();
30-
await using startedContainer = await genericContainer.start();
31-
expect((startedContainer as StartedGenericContainer)["getWaitStrategy"]()).toBeInstanceOf(
32-
HealthCheckWaitStrategy
33-
);
34-
});
35-
it("should use same WaitStrategy if it's explicitly defined in withWaitStrategy() even if image defines healthcheck", async () => {
36-
const context = path.resolve(fixtures, "docker-with-health-check");
37-
const genericContainer = await GenericContainer.fromDockerfile(context).build();
38-
await using container = await genericContainer
39-
.withExposedPorts(8080)
40-
.withWaitStrategy(Wait.forListeningPorts())
41-
.start();
42-
expect((container as StartedGenericContainer)["getWaitStrategy"]()).toBeInstanceOf(HostPortWaitStrategy);
43-
});
44-
it("should use same WaitStrategy if it's explicitly defined in withWaitStrategy() even if withHealthCheck() is called", async () => {
45-
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
46-
.withExposedPorts(8080)
47-
.withHealthCheck({
48-
test: ["CMD-SHELL", "echo 'started' && exit 0"],
49-
})
50-
.withWaitStrategy(Wait.forListeningPorts())
51-
.start();
52-
expect((container as StartedGenericContainer)["getWaitStrategy"]()).toBeInstanceOf(HostPortWaitStrategy);
53-
});
10+
describe("GenericContainer wait strategy", { timeout: 180_000 }, () => {
11+
it("should use Wait.forListeningPorts if healthcheck is not defined in DOCKERFILE", async () => {
12+
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
13+
.withExposedPorts(8080)
14+
.start();
15+
expect((container as StartedGenericContainer)["getWaitStrategy"]()).toBeInstanceOf(HostPortWaitStrategy);
5416
});
55-
}
17+
it("should use Wait.forHealthCheck if withHealthCheck() explicitly called", async () => {
18+
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
19+
.withExposedPorts(8080)
20+
.withHealthCheck({
21+
test: ["CMD-SHELL", "echo 'started' && exit 0"],
22+
})
23+
.start();
24+
expect((container as StartedGenericContainer)["getWaitStrategy"]()).toBeInstanceOf(HealthCheckWaitStrategy);
25+
});
26+
it("should use Wait.forHealthCheck if healthcheck is defined in DOCKERFILE", async () => {
27+
const context = path.resolve(fixtures, "docker-with-health-check");
28+
const genericContainer = await GenericContainer.fromDockerfile(context).build();
29+
await using startedContainer = await genericContainer.start();
30+
expect((startedContainer as StartedGenericContainer)["getWaitStrategy"]()).toBeInstanceOf(HealthCheckWaitStrategy);
31+
});
32+
it("should use same WaitStrategy if it's explicitly defined in withWaitStrategy() even if image defines healthcheck", async () => {
33+
const context = path.resolve(fixtures, "docker-with-health-check");
34+
const genericContainer = await GenericContainer.fromDockerfile(context).build();
35+
await using container = await genericContainer
36+
.withExposedPorts(8080)
37+
.withWaitStrategy(Wait.forListeningPorts())
38+
.start();
39+
expect((container as StartedGenericContainer)["getWaitStrategy"]()).toBeInstanceOf(HostPortWaitStrategy);
40+
});
41+
it("should use same WaitStrategy if it's explicitly defined in withWaitStrategy() even if withHealthCheck() is called", async () => {
42+
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
43+
.withExposedPorts(8080)
44+
.withHealthCheck({
45+
test: ["CMD-SHELL", "echo 'started' && exit 0"],
46+
})
47+
.withWaitStrategy(Wait.forListeningPorts())
48+
.start();
49+
expect((container as StartedGenericContainer)["getWaitStrategy"]()).toBeInstanceOf(HostPortWaitStrategy);
50+
});
51+
});

packages/testcontainers/src/generic-container/generic-container.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ export class GenericContainer implements TestContainer {
9494
await this.beforeContainerCreated();
9595
}
9696

97-
this.waitStrategy = await this.selectWaitStrategy(client);
98-
9997
if (!this.isHelperContainer() && PortForwarderInstance.isRunning()) {
10098
const portForwarder = await PortForwarderInstance.getInstance();
10199
this.hostConfig.ExtraHosts = [
@@ -119,13 +117,13 @@ export class GenericContainer implements TestContainer {
119117
return this.startContainer(client);
120118
}
121119

122-
private async selectWaitStrategy(client: ContainerRuntimeClient): Promise<WaitStrategy> {
120+
private async selectWaitStrategy(client: ContainerRuntimeClient, container: Container): Promise<WaitStrategy> {
123121
if (this.waitStrategy) return this.waitStrategy;
124122
if (this.healthCheck) {
125123
return Wait.forHealthCheck();
126124
}
127-
const imageInfo = await client.image.inspect(this.imageName);
128-
if (imageInfo.Config.Healthcheck?.Test) {
125+
const containerInfo = await client.container.inspect(container);
126+
if (containerInfo.Config.Healthcheck?.Test) {
129127
return Wait.forHealthCheck();
130128
}
131129
return Wait.forListeningPorts();
@@ -167,22 +165,26 @@ export class GenericContainer implements TestContainer {
167165
this.waitStrategy?.withStartupTimeout(this.startupTimeoutMs);
168166
}
169167

170-
await waitForContainer(client, container, this.waitStrategy, boundPorts);
168+
const waitStrategy = this.waitStrategy ?? Wait.forListeningPorts();
169+
170+
await waitForContainer(client, container, waitStrategy, boundPorts);
171171

172172
return new StartedGenericContainer(
173173
container,
174174
client.info.containerRuntime.host,
175175
inspectResult,
176176
boundPorts,
177177
inspectResult.Name,
178-
this.waitStrategy ?? Wait.forListeningPorts(),
178+
waitStrategy,
179179
this.autoRemove
180180
);
181181
}
182182

183183
private async startContainer(client: ContainerRuntimeClient): Promise<StartedTestContainer> {
184184
const container = await client.container.create({ ...this.createOpts, HostConfig: this.hostConfig });
185185

186+
this.waitStrategy = await this.selectWaitStrategy(client, container);
187+
186188
if (!this.isHelperContainer() && PortForwarderInstance.isRunning()) {
187189
await this.connectContainerToPortForwarder(client, container);
188190
}
@@ -239,15 +241,17 @@ export class GenericContainer implements TestContainer {
239241
await this.containerStarting(mappedInspectResult, false);
240242
}
241243

242-
await waitForContainer(client, container, this.waitStrategy, boundPorts);
244+
const waitStrategy = this.waitStrategy ?? Wait.forListeningPorts();
245+
246+
await waitForContainer(client, container, waitStrategy, boundPorts);
243247

244248
const startedContainer = new StartedGenericContainer(
245249
container,
246250
client.info.containerRuntime.host,
247251
inspectResult,
248252
boundPorts,
249253
inspectResult.Name,
250-
this.waitStrategy ?? Wait.forListeningPorts(),
254+
waitStrategy,
251255
this.autoRemove
252256
);
253257

packages/testcontainers/src/wait-strategies/wait-for-container.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import { WaitStrategy } from "./wait-strategy";
77
export const waitForContainer = async (
88
client: ContainerRuntimeClient,
99
container: Container,
10-
waitStrategy: WaitStrategy | undefined,
10+
waitStrategy: WaitStrategy,
1111
boundPorts: BoundPorts,
1212
startTime?: Date
1313
): Promise<void> => {
1414
log.debug(`Waiting for container to be ready...`, { containerId: container.id });
1515

1616
try {
17-
await waitStrategy?.waitUntilReady(container, boundPorts, startTime);
17+
await waitStrategy.waitUntilReady(container, boundPorts, startTime);
1818
log.info(`Container is ready`, { containerId: container.id });
1919
} catch (err) {
2020
log.error(`Container failed to be ready: ${err}`, { containerId: container.id });

0 commit comments

Comments
 (0)