Skip to content

Commit bf7c3ea

Browse files
Fix PostgreSQL container restart
1 parent 3b48cc0 commit bf7c3ea

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,24 @@ describe("PostgreSqlContainer", () => {
8383
await container.stop();
8484
});
8585
// }
86+
87+
it("should work with restarted container", async () => {
88+
const container = await new PostgreSqlContainer().start();
89+
await container.restart();
90+
91+
const client = new Client({
92+
host: container.getHost(),
93+
port: container.getPort(),
94+
database: container.getDatabase(),
95+
user: container.getUsername(),
96+
password: container.getPassword(),
97+
});
98+
await client.connect();
99+
100+
const result = await client.query("SELECT 1");
101+
expect(result.rows[0]).toEqual({ "?column?": 1 });
102+
103+
await client.end();
104+
await container.stop();
105+
});
86106
});

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ export class PostgreSqlContainer extends GenericContainer {
1010
constructor(image = "postgres:13.3-alpine") {
1111
super(image);
1212
this.withExposedPorts(POSTGRES_PORT)
13-
.withWaitStrategy(Wait.forLogMessage(/.*database system is ready to accept connections.*/, 2))
13+
.withHealthCheck({
14+
test: ["CMD-SHELL", "pg_isready -U postgres"],
15+
interval: 10000,
16+
timeout: 5000,
17+
retries: 3,
18+
})
19+
.withWaitStrategy(Wait.forHealthCheck())
1420
.withStartupTimeout(120_000);
1521
}
1622

@@ -40,20 +46,17 @@ export class PostgreSqlContainer extends GenericContainer {
4046
}
4147

4248
export class StartedPostgreSqlContainer extends AbstractStartedContainer {
43-
private readonly port: number;
44-
4549
constructor(
4650
startedTestContainer: StartedTestContainer,
4751
private readonly database: string,
4852
private readonly username: string,
4953
private readonly password: string
5054
) {
5155
super(startedTestContainer);
52-
this.port = startedTestContainer.getMappedPort(POSTGRES_PORT);
5356
}
5457

5558
public getPort(): number {
56-
return this.port;
59+
return super.getMappedPort(POSTGRES_PORT);
5760
}
5861

5962
public getDatabase(): string {

packages/testcontainers/src/container-runtime/clients/container/docker-container-client.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ export class DockerContainerClient implements ContainerClient {
120120

121121
async inspect(container: Dockerode.Container): Promise<ContainerInspectInfo> {
122122
try {
123-
log.debug(`Inspecting container...`, { containerId: container.id });
124123
const inspectInfo = await container.inspect();
125-
log.debug(`Inspected container`, { containerId: container.id });
126124
return inspectInfo;
127125
} catch (err) {
128126
log.error(`Failed to inspect container: ${err}`, { containerId: container.id });

0 commit comments

Comments
 (0)