Skip to content

Commit 742e839

Browse files
authored
Support restart on 5 containers with previously fixed ports (#895)
1 parent 5b39749 commit 742e839

File tree

10 files changed

+97
-20
lines changed

10 files changed

+97
-20
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,23 @@ describe("Cassandra", () => {
109109
await container.stop();
110110
});
111111
// }
112+
113+
it("should work with restarted container", async () => {
114+
const container = await new CassandraContainer("cassandra:5.0.2").start();
115+
await container.restart();
116+
117+
const client = new Client({
118+
contactPoints: [container.getContactPoint()],
119+
localDataCenter: container.getDatacenter(),
120+
keyspace: "system",
121+
});
122+
123+
await client.connect();
124+
125+
const result = await client.execute("SELECT release_version FROM system.local");
126+
expect(result.rows[0].release_version).toBe("5.0.2");
127+
128+
await client.shutdown();
129+
await container.stop();
130+
});
112131
});

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ export class CassandraContainer extends GenericContainer {
5050
}
5151

5252
export class StartedCassandraContainer extends AbstractStartedContainer {
53-
private readonly port: number;
54-
5553
constructor(
5654
startedTestContainer: StartedTestContainer,
5755
private readonly dc: string,
@@ -60,11 +58,10 @@ export class StartedCassandraContainer extends AbstractStartedContainer {
6058
private readonly password: string
6159
) {
6260
super(startedTestContainer);
63-
this.port = startedTestContainer.getMappedPort(CASSANDRA_PORT);
6461
}
6562

6663
public getPort(): number {
67-
return this.port;
64+
return this.startedTestContainer.getMappedPort(CASSANDRA_PORT);
6865
}
6966

7067
public getDatacenter(): string {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,16 @@ describe("ElasticsearchContainer", () => {
3535
await container.stop();
3636
});
3737
// }
38+
39+
it("should work with restarted container", async () => {
40+
const container = await new ElasticsearchContainer().start();
41+
await container.restart();
42+
43+
const client = new Client({ node: container.getHttpUrl() });
44+
45+
await client.indices.create({ index: "people" });
46+
47+
expect((await client.indices.exists({ index: "people" })).statusCode).toBe(200);
48+
await container.stop();
49+
});
3850
});

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ export class ElasticsearchContainer extends GenericContainer {
2222
}
2323

2424
export class StartedElasticsearchContainer extends AbstractStartedContainer {
25-
private readonly httpPort: number;
26-
2725
constructor(override readonly startedTestContainer: StartedTestContainer) {
2826
super(startedTestContainer);
29-
this.httpPort = this.getMappedPort(ELASTIC_SEARCH_HTTP_PORT);
27+
}
28+
29+
public getPort(): number {
30+
return this.getMappedPort(ELASTIC_SEARCH_HTTP_PORT);
3031
}
3132

3233
public getHttpUrl(): string {
33-
return `http://${this.getHost()}:${this.httpPort}`;
34+
return `http://${this.getHost()}:${this.getPort()}`;
3435
}
3536
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,23 @@ describe("MariaDb", () => {
125125
await container.stop();
126126
});
127127
// }
128+
129+
it("should work with restarted container", async () => {
130+
const container = await new MariaDbContainer().start();
131+
await container.restart();
132+
133+
const client = await mariadb.createConnection({
134+
host: container.getHost(),
135+
port: container.getPort(),
136+
database: container.getDatabase(),
137+
user: container.getUsername(),
138+
password: container.getUserPassword(),
139+
});
140+
141+
const rows = await client.query("SELECT 1 as res");
142+
expect(rows).toEqual([{ res: 1 }]);
143+
144+
await client.end();
145+
await container.stop();
146+
});
128147
});

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ export class MariaDbContainer extends GenericContainer {
5151
}
5252

5353
export class StartedMariaDbContainer extends AbstractStartedContainer {
54-
private readonly port: number;
55-
5654
constructor(
5755
startedTestContainer: StartedTestContainer,
5856
private readonly database: string,
@@ -61,11 +59,10 @@ export class StartedMariaDbContainer extends AbstractStartedContainer {
6159
private readonly rootPassword: string
6260
) {
6361
super(startedTestContainer);
64-
this.port = startedTestContainer.getMappedPort(MARIADB_PORT);
6562
}
6663

6764
public getPort(): number {
68-
return this.port;
65+
return this.startedTestContainer.getMappedPort(MARIADB_PORT);
6966
}
7067

7168
public getDatabase(): string {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,23 @@ describe("MySqlContainer", () => {
114114
await container.stop();
115115
});
116116
// }
117+
118+
it("should work with restarted container", async () => {
119+
const container = await new MySqlContainer().start();
120+
await container.restart();
121+
122+
const client = await createConnection({
123+
host: container.getHost(),
124+
port: container.getPort(),
125+
database: container.getDatabase(),
126+
user: container.getUsername(),
127+
password: container.getUserPassword(),
128+
});
129+
130+
const [rows] = await client.execute("SELECT 1 as res");
131+
expect(rows).toEqual([{ res: 1 }]);
132+
133+
await client.end();
134+
await container.stop();
135+
});
117136
});

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ export class MySqlContainer extends GenericContainer {
5151
}
5252

5353
export class StartedMySqlContainer extends AbstractStartedContainer {
54-
private readonly port: number;
55-
5654
constructor(
5755
startedTestContainer: StartedTestContainer,
5856
private readonly database: string,
@@ -61,11 +59,10 @@ export class StartedMySqlContainer extends AbstractStartedContainer {
6159
private readonly rootPassword: string
6260
) {
6361
super(startedTestContainer);
64-
this.port = startedTestContainer.getMappedPort(3306);
6562
}
6663

6764
public getPort(): number {
68-
return this.port;
65+
return this.startedTestContainer.getMappedPort(MYSQL_PORT);
6966
}
7067

7168
public getDatabase(): string {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,23 @@ describe("ScyllaDB", () => {
6464
await container.stop();
6565
});
6666
// }
67+
68+
it("should work with restarted container", async () => {
69+
const container = await new ScyllaContainer("scylladb/scylla:6.2.0").start();
70+
await container.restart();
71+
72+
const client = new Client({
73+
contactPoints: [container.getContactPoint()],
74+
localDataCenter: container.getDatacenter(),
75+
keyspace: "system",
76+
});
77+
78+
await client.connect();
79+
80+
const result = await client.execute("SELECT cql_version FROM system.local");
81+
expect(result.rows[0].cql_version).toBe("3.3.1");
82+
83+
await client.shutdown();
84+
await container.stop();
85+
});
6786
});

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ export class ScyllaContainer extends GenericContainer {
2121
}
2222

2323
export class StartedScyllaContainer extends AbstractStartedContainer {
24-
private readonly port: number;
25-
2624
constructor(startedTestContainer: StartedTestContainer) {
2725
super(startedTestContainer);
28-
this.port = startedTestContainer.getMappedPort(SCYLLA_PORT);
2926
}
3027

3128
public getPort(): number {
32-
return this.port;
29+
return this.startedTestContainer.getMappedPort(SCYLLA_PORT);
3330
}
3431

3532
public getDatacenter(): string {

0 commit comments

Comments
 (0)