Skip to content

Commit 92c6f68

Browse files
Move default container configurations to constructor to allow for overriding (#712)
1 parent 0aeaa42 commit 92c6f68

File tree

16 files changed

+83
-104
lines changed

16 files changed

+83
-104
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const USERNAME = "root";
66
export class ArangoDBContainer extends GenericContainer {
77
constructor(image = "arangodb:3.10.0", private password = "test") {
88
super(image);
9+
this.withExposedPorts(ARANGODB_PORT).withWaitStrategy(Wait.forLogMessage("Have fun!")).withStartupTimeout(120_000);
910
}
1011

1112
public withPassword(password: string): this {
@@ -14,11 +15,7 @@ export class ArangoDBContainer extends GenericContainer {
1415
}
1516

1617
public override async start(): Promise<StartedArangoContainer> {
17-
this.withExposedPorts(...(this.hasExposedPorts ? this.exposedPorts : [ARANGODB_PORT]))
18-
.withWaitStrategy(Wait.forLogMessage("Have fun!"))
19-
.withEnvironment({ ARANGO_ROOT_PASSWORD: this.password })
20-
.withStartupTimeout(120_000);
21-
18+
this.withEnvironment({ ARANGO_ROOT_PASSWORD: this.password });
2219
return new StartedArangoContainer(await super.start(), this.password);
2320
}
2421
}

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { IntervalRetry } from "testcontainers/src/common";
1717

1818
export class CouchbaseContainer extends GenericContainer {
1919
private static readonly DEFAULT_IMAGE_NAME = "couchbase/server";
20-
2120
private static readonly DEFAULT_TAG = "6.5.1";
2221

2322
private username = "Administrator";
@@ -38,6 +37,9 @@ export class CouchbaseContainer extends GenericContainer {
3837

3938
constructor(image = `${CouchbaseContainer.DEFAULT_IMAGE_NAME}:${CouchbaseContainer.DEFAULT_TAG}`) {
4039
super(image);
40+
this.withExposedPorts(...this.getPortsToExpose()).withWaitStrategy(
41+
Wait.forLogMessage("Starting Couchbase Server -- Web UI available at http://<ip>:8091")
42+
);
4143
}
4244

4345
withCredentials(username: string, password: string) {
@@ -562,13 +564,7 @@ export class CouchbaseContainer extends GenericContainer {
562564
}
563565

564566
public override async start(): Promise<StartedCouchbaseContainer> {
565-
const startingMessage = "Starting Couchbase Server -- Web UI available at http://<ip>:8091";
566-
this.withExposedPorts(...(this.hasExposedPorts ? this.exposedPorts : this.getPortsToExpose())).withWaitStrategy(
567-
Wait.forLogMessage(startingMessage)
568-
);
569-
const startedTestContainer = await super.start();
570-
571-
return new StartedCouchbaseContainer(startedTestContainer, this.username, this.password);
567+
return new StartedCouchbaseContainer(await super.start(), this.username, this.password);
572568
}
573569
}
574570

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ const ELASTIC_SEARCH_HTTP_PORT = 9200;
55
export class ElasticsearchContainer extends GenericContainer {
66
constructor(image = "elasticsearch:7.17.7") {
77
super(image);
8-
}
9-
10-
public override async start(): Promise<StartedElasticsearchContainer> {
11-
this.withExposedPorts(...(this.hasExposedPorts ? this.exposedPorts : [ELASTIC_SEARCH_HTTP_PORT]))
8+
this.withExposedPorts(ELASTIC_SEARCH_HTTP_PORT)
129
.withEnvironment({ "discovery.type": "single-node" })
1310
.withCopyContentToContainer([
1411
{
@@ -17,7 +14,9 @@ export class ElasticsearchContainer extends GenericContainer {
1714
},
1815
])
1916
.withStartupTimeout(120_000);
17+
}
2018

19+
public override async start(): Promise<StartedElasticsearchContainer> {
2120
return new StartedElasticsearchContainer(await super.start());
2221
}
2322
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const MQTT_PORT = 1883;
88
export class HiveMQContainer extends GenericContainer {
99
constructor(image = "hivemq/hivemq-ce:2023.5") {
1010
super(image);
11-
12-
this.withExposedPorts(...(this.hasExposedPorts ? this.exposedPorts : [MQTT_PORT]))
11+
this.withExposedPorts(MQTT_PORT)
1312
.withWaitStrategy(Wait.forLogMessage(START_LOG_MESSAGE_REGEX))
1413
.withTmpFs({
1514
[path.join(HIVEMQ_BASE_PATH, "log")]: "rw",

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export const LOCALSTACK_PORT = 4566;
55
export class LocalstackContainer extends GenericContainer {
66
constructor(image = "localstack/localstack:2.2.0") {
77
super(image);
8+
this.resolveHostname();
9+
this.withExposedPorts(LOCALSTACK_PORT).withWaitStrategy(Wait.forLogMessage("Ready", 1)).withStartupTimeout(120_000);
810
}
911

1012
private resolveHostname(): void {
@@ -23,13 +25,6 @@ export class LocalstackContainer extends GenericContainer {
2325
log.info(`${envVar} environment variable set to ${this.environment[envVar]} (${hostnameExternalReason})"`);
2426
}
2527

26-
protected override async beforeContainerCreated(): Promise<void> {
27-
this.resolveHostname();
28-
this.withExposedPorts(...(this.hasExposedPorts ? this.exposedPorts : [LOCALSTACK_PORT]))
29-
.withWaitStrategy(Wait.forLogMessage("Ready", 1))
30-
.withStartupTimeout(120_000);
31-
}
32-
3328
public override async start(): Promise<StartedLocalStackContainer> {
3429
return new StartedLocalStackContainer(await super.start());
3530
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const MONGODB_PORT = 27017;
55
export class MongoDBContainer extends GenericContainer {
66
constructor(image = "mongo:4.0.1") {
77
super(image);
8-
}
9-
10-
public override async start(): Promise<StartedMongoDBContainer> {
118
this.withExposedPorts(MONGODB_PORT)
129
.withCommand(["--replSet", "rs0"])
1310
.withWaitStrategy(Wait.forLogMessage(/.*waiting for connections.*/i))
1411
.withStartupTimeout(120_000);
12+
}
13+
14+
public override async start(): Promise<StartedMongoDBContainer> {
1515
return new StartedMongoDBContainer(await super.start());
1616
}
1717

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers";
22

33
const MSSQL_PORT = 1433;
4+
45
export class MSSQLServerContainer extends GenericContainer {
56
private database = "master";
67
private username = "sa";
@@ -10,6 +11,7 @@ export class MSSQLServerContainer extends GenericContainer {
1011

1112
constructor(image = "mcr.microsoft.com/mssql/server:2022-latest") {
1213
super(image);
14+
this.withExposedPorts(MSSQL_PORT).withWaitStrategy(Wait.forLogMessage(this.message, 1)).withStartupTimeout(120_000);
1315
}
1416

1517
public acceptLicense(): this {
@@ -33,15 +35,11 @@ export class MSSQLServerContainer extends GenericContainer {
3335
}
3436

3537
public override async start(): Promise<StartedMSSQLServerContainer> {
36-
this.withExposedPorts(...(this.hasExposedPorts ? this.exposedPorts : [MSSQL_PORT]))
37-
.withEnvironment({
38-
ACCEPT_EULA: this.acceptEula,
39-
MSSQL_SA_PASSWORD: this.password,
40-
MSSQL_TCP_PORT: String(MSSQL_PORT),
41-
})
42-
.withWaitStrategy(Wait.forLogMessage(this.message, 1))
43-
.withStartupTimeout(120_000);
44-
38+
this.withEnvironment({
39+
ACCEPT_EULA: this.acceptEula,
40+
MSSQL_SA_PASSWORD: this.password,
41+
MSSQL_TCP_PORT: String(MSSQL_PORT),
42+
});
4543
return new StartedMSSQLServerContainer(await super.start(), this.database, this.username, this.password);
4644
}
4745
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class MySqlContainer extends GenericContainer {
1010

1111
constructor(image = "mysql:8.0.31") {
1212
super(image);
13+
this.withExposedPorts(MYSQL_PORT).withStartupTimeout(120_000);
1314
}
1415

1516
public withDatabase(database: string): this {
@@ -33,15 +34,12 @@ export class MySqlContainer extends GenericContainer {
3334
}
3435

3536
public override async start(): Promise<StartedMySqlContainer> {
36-
this.withExposedPorts(...(this.hasExposedPorts ? this.exposedPorts : [MYSQL_PORT]))
37-
.withEnvironment({
38-
MYSQL_DATABASE: this.database,
39-
MYSQL_ROOT_PASSWORD: this.rootPassword,
40-
MYSQL_USER: this.username,
41-
MYSQL_PASSWORD: this.userPassword,
42-
})
43-
.withStartupTimeout(120_000);
44-
37+
this.withEnvironment({
38+
MYSQL_DATABASE: this.database,
39+
MYSQL_ROOT_PASSWORD: this.rootPassword,
40+
MYSQL_USER: this.username,
41+
MYSQL_PASSWORD: this.userPassword,
42+
});
4543
return new StartedMySqlContainer(
4644
await super.start(),
4745
this.database,

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,29 @@ const HTTP_MANAGEMENT_PORT = 8222;
77
const USER_ARGUMENT_KEY = "--user";
88
const PASS_ARGUMENT_KEY = "--pass";
99

10+
function buildCmdsFromArgs(args: { [p: string]: string }): string[] {
11+
const result: string[] = [];
12+
result.push("nats-server");
13+
14+
for (const argsKey in args) {
15+
result.push(argsKey);
16+
result.push(args[argsKey]);
17+
}
18+
return result;
19+
}
20+
1021
export class NatsContainer extends GenericContainer {
1122
private args: { [name: string]: string } = {};
1223

1324
constructor(image = "nats:2.8.4-alpine") {
1425
super(image);
26+
1527
this.args[USER_ARGUMENT_KEY] = "test";
1628
this.args[PASS_ARGUMENT_KEY] = "test";
29+
30+
this.withExposedPorts(CLIENT_PORT, ROUTING_PORT_FOR_CLUSTERING, HTTP_MANAGEMENT_PORT)
31+
.withWaitStrategy(Wait.forLogMessage(/.*Server is ready.*/))
32+
.withStartupTimeout(120_000);
1733
}
1834

1935
public withUsername(user: string): this {
@@ -45,24 +61,7 @@ export class NatsContainer extends GenericContainer {
4561
}
4662

4763
public override async start(): Promise<StartedNatsContainer> {
48-
function buildCmdsFromArgs(args: { [p: string]: string }): string[] {
49-
const result: string[] = [];
50-
result.push("nats-server");
51-
52-
for (const argsKey in args) {
53-
result.push(argsKey);
54-
result.push(args[argsKey]);
55-
}
56-
return result;
57-
}
58-
59-
this.withCommand(buildCmdsFromArgs(this.args))
60-
.withExposedPorts(
61-
...(this.hasExposedPorts ? this.exposedPorts : [CLIENT_PORT, ROUTING_PORT_FOR_CLUSTERING, HTTP_MANAGEMENT_PORT])
62-
)
63-
.withWaitStrategy(Wait.forLogMessage(/.*Server is ready.*/))
64-
.withStartupTimeout(120_000);
65-
64+
this.withCommand(buildCmdsFromArgs(this.args));
6665
return new StartedNatsContainer(await super.start(), this.getUser(), this.getPass());
6766
}
6867

0 commit comments

Comments
 (0)