Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"printWidth": 120,
"trailingComma": "es5",
"plugins": ["prettier-plugin-organize-imports"]
}
"plugins": [
"prettier-plugin-organize-imports"
],
"endOfLine": "auto"
}
27 changes: 18 additions & 9 deletions packages/modules/mongodb/src/mongodb-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,36 @@ export class MongoDBContainer extends GenericContainer {

private withWaitForRsHealthCheck(): this {
return this.withHealthCheck({
test: [
"CMD-SHELL",
this.buildMongoEvalCommand(
`'try { rs.initiate(); } catch (e){} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'`
),
],
interval: 250,
test: ["CMD-SHELL", this.buildMongoEvalCommand(this.getRsInitCmd())],
interval: 5000,
timeout: 60000,
retries: 1000,
});
}

private buildMongoEvalCommand(command: string) {
const useMongosh = satisfies(this.imageName.tag, ">=5.0.0");
const args = [];
if (useMongosh) args.push("mongosh");
if (this.isV5OrLater()) args.push("mongosh");
else args.push("mongo", "admin");
if (this.username && this.password) args.push("-u", this.username, "-p", this.password);
args.push("--quiet", "--eval", command);
return args.join(" ");
}

private getRsInitCmd() {
if (this.isV5OrLater())
return `'try { rs.status(); } catch (e) { rs.initiate(); } while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'`;
else
return `'try { rs.initiate(); } catch (e) {} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'`;
}

private isV5OrLater() {
try {
return satisfies(this.imageName.tag, ">=5.0.0");
} catch {
return false;
}
}
}

export class StartedMongoDBContainer extends AbstractStartedContainer {
Expand Down
3 changes: 2 additions & 1 deletion packages/testcontainers/src/utils/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { GetEventsOptions, ImageInspectInfo } from "dockerode";
import { createServer, Server } from "http";
import { createSocket } from "node:dgram";
import fs from "node:fs";
import { EOL } from "node:os";
import path from "node:path";
import { Readable } from "stream";
import { Agent, request } from "undici";
Expand All @@ -14,7 +15,7 @@ import { StartedTestContainer } from "../test-container";
export const getImage = (dirname: string, index = 0): string => {
return fs
.readFileSync(path.resolve(dirname, "..", "Dockerfile"), "utf-8")
.split("\n")
.split(EOL)
[index].split(" ")[1];
};

Expand Down
Loading