Skip to content

Commit 78a2e85

Browse files
committed
fixes after review
1 parent e4db8df commit 78a2e85

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

package-lock.json

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

packages/modules/mongodb/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"build": "tsc --project tsconfig.build.json"
3030
},
3131
"devDependencies": {
32-
"mongoose": "8.15.0"
32+
"mongoose": "^8.16.4"
3333
},
3434
"dependencies": {
3535
"testcontainers": "^11.3.1",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ describe("MongodbContainer", { timeout: 240_000 }, () => {
6565
});
6666

6767
describe("MongodbContainer connect with credentials", { timeout: 240_000 }, () => {
68-
it.for([["mongo:4.0.1"], ["mongo:8.0"]])("should connect to %s with credentials", async ([image]) => {
69-
const mongodbContainer = await new MongoDBContainer(image)
68+
it("should connect to %s with credentials", async () => {
69+
const mongodbContainer = await new MongoDBContainer("mongo:8.0")
7070
.withUsername("mongo_user")
7171
.withPassword("mongo_password")
7272
.start();

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

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,29 @@ import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait
44
const MONGODB_PORT = 27017;
55

66
export class MongoDBContainer extends GenericContainer {
7-
private username = "";
8-
private password = "";
7+
private username: string | undefined;
8+
private password: string | undefined;
99

1010
constructor(image: string) {
1111
super(image);
12-
this.withExposedPorts(MONGODB_PORT).withStartupTimeout(120_000);
12+
this.withExposedPorts(MONGODB_PORT).withWaitStrategy(Wait.forHealthCheck()).withStartupTimeout(120_000);
1313
}
1414

1515
public withUsername(username: string): this {
16-
if (username === "") throw new Error("Username should not be empty.");
16+
if (!username) throw new Error("Username should not be empty.");
1717
this.username = username;
1818
return this;
1919
}
2020

2121
public withPassword(password: string): this {
22-
if (password === "") throw new Error("Password should not be empty.");
22+
if (!password) throw new Error("Password should not be empty.");
2323
this.password = password;
2424
return this;
2525
}
2626

2727
public override async start(): Promise<StartedMongoDBContainer> {
28-
const cmdArgs = ["--replSet", "rs0", "--bind_ip_all"];
29-
this.withHealthCheck({
30-
test: ["CMD-SHELL", this.buildMongoEvalCommand(this.initRsAndWait())],
31-
interval: 250,
32-
timeout: 60000,
33-
retries: 1000,
34-
}).withWaitStrategy(Wait.forHealthCheck());
28+
const cmdArgs = ["--replSet", "rs0"];
29+
if (!this.healthCheck) this.withWaitForRsHealthCheck();
3530
if (this.username && this.password) {
3631
cmdArgs.push("--keyFile", "/data/db/key.txt");
3732
this.withEnvironment({
@@ -52,26 +47,36 @@ export class MongoDBContainer extends GenericContainer {
5247
return new StartedMongoDBContainer(await super.start(), this.username, this.password);
5348
}
5449

50+
private withWaitForRsHealthCheck(): this {
51+
return this.withHealthCheck({
52+
test: [
53+
"CMD-SHELL",
54+
this.buildMongoEvalCommand(
55+
`'try { rs.initiate(); } catch (e){} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'`
56+
),
57+
],
58+
interval: 250,
59+
timeout: 60000,
60+
retries: 1000,
61+
});
62+
}
63+
5564
private buildMongoEvalCommand(command: string) {
5665
const useMongosh = satisfies(this.imageName.tag, ">=5.0.0");
5766
const args = [];
5867
if (useMongosh) args.push("mongosh");
5968
else args.push("mongo", "admin");
6069
if (this.username && this.password) args.push("-u", this.username, "-p", this.password);
61-
args.push("--host", "localhost", "--quiet", "--eval", command);
70+
args.push("--quiet", "--eval", command);
6271
return args.join(" ");
6372
}
64-
65-
private initRsAndWait() {
66-
return `'try { rs.initiate(); } catch (e){} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'`;
67-
}
6873
}
6974

7075
export class StartedMongoDBContainer extends AbstractStartedContainer {
71-
private readonly username: string = "";
72-
private readonly password: string = "";
76+
private readonly username: string | undefined;
77+
private readonly password: string | undefined;
7378

74-
constructor(startedTestContainer: StartedTestContainer, username: string, password: string) {
79+
constructor(startedTestContainer: StartedTestContainer, username: string | undefined, password: string | undefined) {
7580
super(startedTestContainer);
7681
this.username = username;
7782
this.password = password;

0 commit comments

Comments
 (0)