Skip to content

Commit

Permalink
Support restart on 5 containers with previously fixed ports (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
stscoundrel authored Jan 13, 2025
1 parent 5b39749 commit 742e839
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 20 deletions.
19 changes: 19 additions & 0 deletions packages/modules/cassandra/src/cassandra-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,23 @@ describe("Cassandra", () => {
await container.stop();
});
// }

it("should work with restarted container", async () => {
const container = await new CassandraContainer("cassandra:5.0.2").start();
await container.restart();

const client = new Client({
contactPoints: [container.getContactPoint()],
localDataCenter: container.getDatacenter(),
keyspace: "system",
});

await client.connect();

const result = await client.execute("SELECT release_version FROM system.local");
expect(result.rows[0].release_version).toBe("5.0.2");

await client.shutdown();
await container.stop();
});
});
5 changes: 1 addition & 4 deletions packages/modules/cassandra/src/cassandra-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ export class CassandraContainer extends GenericContainer {
}

export class StartedCassandraContainer extends AbstractStartedContainer {
private readonly port: number;

constructor(
startedTestContainer: StartedTestContainer,
private readonly dc: string,
Expand All @@ -60,11 +58,10 @@ export class StartedCassandraContainer extends AbstractStartedContainer {
private readonly password: string
) {
super(startedTestContainer);
this.port = startedTestContainer.getMappedPort(CASSANDRA_PORT);
}

public getPort(): number {
return this.port;
return this.startedTestContainer.getMappedPort(CASSANDRA_PORT);
}

public getDatacenter(): string {
Expand Down
12 changes: 12 additions & 0 deletions packages/modules/elasticsearch/src/elasticsearch-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ describe("ElasticsearchContainer", () => {
await container.stop();
});
// }

it("should work with restarted container", async () => {
const container = await new ElasticsearchContainer().start();
await container.restart();

const client = new Client({ node: container.getHttpUrl() });

await client.indices.create({ index: "people" });

expect((await client.indices.exists({ index: "people" })).statusCode).toBe(200);
await container.stop();
});
});
9 changes: 5 additions & 4 deletions packages/modules/elasticsearch/src/elasticsearch-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ export class ElasticsearchContainer extends GenericContainer {
}

export class StartedElasticsearchContainer extends AbstractStartedContainer {
private readonly httpPort: number;

constructor(override readonly startedTestContainer: StartedTestContainer) {
super(startedTestContainer);
this.httpPort = this.getMappedPort(ELASTIC_SEARCH_HTTP_PORT);
}

public getPort(): number {
return this.getMappedPort(ELASTIC_SEARCH_HTTP_PORT);
}

public getHttpUrl(): string {
return `http://${this.getHost()}:${this.httpPort}`;
return `http://${this.getHost()}:${this.getPort()}`;
}
}
19 changes: 19 additions & 0 deletions packages/modules/mariadb/src/mariadb-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,23 @@ describe("MariaDb", () => {
await container.stop();
});
// }

it("should work with restarted container", async () => {
const container = await new MariaDbContainer().start();
await container.restart();

const client = await mariadb.createConnection({
host: container.getHost(),
port: container.getPort(),
database: container.getDatabase(),
user: container.getUsername(),
password: container.getUserPassword(),
});

const rows = await client.query("SELECT 1 as res");
expect(rows).toEqual([{ res: 1 }]);

await client.end();
await container.stop();
});
});
5 changes: 1 addition & 4 deletions packages/modules/mariadb/src/mariadb-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ export class MariaDbContainer extends GenericContainer {
}

export class StartedMariaDbContainer extends AbstractStartedContainer {
private readonly port: number;

constructor(
startedTestContainer: StartedTestContainer,
private readonly database: string,
Expand All @@ -61,11 +59,10 @@ export class StartedMariaDbContainer extends AbstractStartedContainer {
private readonly rootPassword: string
) {
super(startedTestContainer);
this.port = startedTestContainer.getMappedPort(MARIADB_PORT);
}

public getPort(): number {
return this.port;
return this.startedTestContainer.getMappedPort(MARIADB_PORT);
}

public getDatabase(): string {
Expand Down
19 changes: 19 additions & 0 deletions packages/modules/mysql/src/mysql-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,23 @@ describe("MySqlContainer", () => {
await container.stop();
});
// }

it("should work with restarted container", async () => {
const container = await new MySqlContainer().start();
await container.restart();

const client = await createConnection({
host: container.getHost(),
port: container.getPort(),
database: container.getDatabase(),
user: container.getUsername(),
password: container.getUserPassword(),
});

const [rows] = await client.execute("SELECT 1 as res");
expect(rows).toEqual([{ res: 1 }]);

await client.end();
await container.stop();
});
});
5 changes: 1 addition & 4 deletions packages/modules/mysql/src/mysql-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ export class MySqlContainer extends GenericContainer {
}

export class StartedMySqlContainer extends AbstractStartedContainer {
private readonly port: number;

constructor(
startedTestContainer: StartedTestContainer,
private readonly database: string,
Expand All @@ -61,11 +59,10 @@ export class StartedMySqlContainer extends AbstractStartedContainer {
private readonly rootPassword: string
) {
super(startedTestContainer);
this.port = startedTestContainer.getMappedPort(3306);
}

public getPort(): number {
return this.port;
return this.startedTestContainer.getMappedPort(MYSQL_PORT);
}

public getDatabase(): string {
Expand Down
19 changes: 19 additions & 0 deletions packages/modules/scylladb/src/scylladb-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,23 @@ describe("ScyllaDB", () => {
await container.stop();
});
// }

it("should work with restarted container", async () => {
const container = await new ScyllaContainer("scylladb/scylla:6.2.0").start();
await container.restart();

const client = new Client({
contactPoints: [container.getContactPoint()],
localDataCenter: container.getDatacenter(),
keyspace: "system",
});

await client.connect();

const result = await client.execute("SELECT cql_version FROM system.local");
expect(result.rows[0].cql_version).toBe("3.3.1");

await client.shutdown();
await container.stop();
});
});
5 changes: 1 addition & 4 deletions packages/modules/scylladb/src/scylladb-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ export class ScyllaContainer extends GenericContainer {
}

export class StartedScyllaContainer extends AbstractStartedContainer {
private readonly port: number;

constructor(startedTestContainer: StartedTestContainer) {
super(startedTestContainer);
this.port = startedTestContainer.getMappedPort(SCYLLA_PORT);
}

public getPort(): number {
return this.port;
return this.startedTestContainer.getMappedPort(SCYLLA_PORT);
}

public getDatacenter(): string {
Expand Down

0 comments on commit 742e839

Please sign in to comment.