Skip to content

Commit f6327ef

Browse files
Allow user healthcheck override
1 parent fa8d71b commit f6327ef

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

packages/modules/postgresql/src/postgresql-container.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Client } from "pg";
22
import { PostgreSqlContainer } from "./postgresql-container";
3+
import { Wait } from "testcontainers";
34

45
describe("PostgreSqlContainer", () => {
56
jest.setTimeout(180_000);
@@ -103,4 +104,15 @@ describe("PostgreSqlContainer", () => {
103104
await client.end();
104105
await container.stop();
105106
});
107+
108+
it("should allow custom healthcheck", async () => {
109+
const container = new PostgreSqlContainer().withHealthCheck({
110+
test: ["CMD-SHELL", "exit 1"],
111+
interval: 100,
112+
retries: 0,
113+
timeout: 0,
114+
});
115+
116+
await expect(() => container.start()).rejects.toThrow();
117+
});
106118
});

packages/modules/postgresql/src/postgresql-container.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export class PostgreSqlContainer extends GenericContainer {
99

1010
constructor(image = "postgres:13.3-alpine") {
1111
super(image);
12-
this.withExposedPorts(POSTGRES_PORT).withStartupTimeout(120_000);
12+
this.withExposedPorts(POSTGRES_PORT);
13+
this.withWaitStrategy(Wait.forHealthCheck());
14+
this.withStartupTimeout(120_000);
1315
}
1416

1517
public withDatabase(database: string): this {
@@ -33,13 +35,14 @@ export class PostgreSqlContainer extends GenericContainer {
3335
POSTGRES_USER: this.username,
3436
POSTGRES_PASSWORD: this.password,
3537
});
36-
this.withHealthCheck({
37-
test: ["CMD-SHELL", `PGPASSWORD=${this.password} psql -U ${this.username} -d ${this.database} -c 'SELECT 1;'`],
38-
interval: 250,
39-
timeout: 1000,
40-
retries: 1000,
41-
});
42-
this.withWaitStrategy(Wait.forHealthCheck());
38+
if (!this.healthCheck) {
39+
this.withHealthCheck({
40+
test: ["CMD-SHELL", `PGPASSWORD=${this.password} psql -U ${this.username} -d ${this.database} -c 'SELECT 1;'`],
41+
interval: 250,
42+
timeout: 1000,
43+
retries: 1000,
44+
});
45+
}
4346
return new StartedPostgreSqlContainer(await super.start(), this.database, this.username, this.password);
4447
}
4548
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class GenericContainer implements TestContainer {
5757
protected filesToCopy: FileToCopy[] = [];
5858
protected directoriesToCopy: DirectoryToCopy[] = [];
5959
protected contentsToCopy: ContentToCopy[] = [];
60+
protected healthCheck?: HealthCheck;
6061

6162
constructor(image: string) {
6263
this.imageName = ImageName.fromString(image);
@@ -387,13 +388,15 @@ export class GenericContainer implements TestContainer {
387388
public withHealthCheck(healthCheck: HealthCheck): this {
388389
const toNanos = (duration: number): number => duration * 1e6;
389390

391+
this.healthCheck = healthCheck;
390392
this.createOpts.Healthcheck = {
391393
Test: healthCheck.test,
392394
Interval: healthCheck.interval ? toNanos(healthCheck.interval) : 0,
393395
Timeout: healthCheck.timeout ? toNanos(healthCheck.timeout) : 0,
394396
Retries: healthCheck.retries || 0,
395397
StartPeriod: healthCheck.startPeriod ? toNanos(healthCheck.startPeriod) : 0,
396398
};
399+
397400
return this;
398401
}
399402

0 commit comments

Comments
 (0)