diff --git a/.prettierrc b/.prettierrc index 57d20efae..68961b5be 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,8 @@ { "printWidth": 120, "trailingComma": "es5", - "plugins": ["prettier-plugin-organize-imports"] -} + "plugins": [ + "prettier-plugin-organize-imports" + ], + "endOfLine": "auto" +} \ No newline at end of file diff --git a/packages/modules/mongodb/src/mongodb-container.ts b/packages/modules/mongodb/src/mongodb-container.ts index 47db093ca..c3a93d517 100644 --- a/packages/modules/mongodb/src/mongodb-container.ts +++ b/packages/modules/mongodb/src/mongodb-container.ts @@ -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 { diff --git a/packages/testcontainers/src/utils/test-helper.ts b/packages/testcontainers/src/utils/test-helper.ts index 6fae65bb2..c574c00f9 100644 --- a/packages/testcontainers/src/utils/test-helper.ts +++ b/packages/testcontainers/src/utils/test-helper.ts @@ -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"; @@ -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]; };