@testcontainers/mongodb 11.3.2 → 11.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,17 @@
1
1
  import { AbstractStartedContainer, GenericContainer, StartedTestContainer } from "testcontainers";
2
2
  export declare class MongoDBContainer extends GenericContainer {
3
+ private username;
4
+ private password;
3
5
  constructor(image: string);
6
+ withUsername(username: string): this;
7
+ withPassword(password: string): this;
4
8
  start(): Promise<StartedMongoDBContainer>;
5
- protected containerStarted(startedTestContainer: StartedTestContainer): Promise<void>;
6
- private initReplicaSet;
7
- private executeMongoEvalCommand;
9
+ private withWaitForRsHealthCheck;
8
10
  private buildMongoEvalCommand;
9
- private getMongoCmdBasedOnImageTag;
10
- private checkMongoNodeExitCode;
11
- private buildMongoWaitCommand;
12
11
  }
13
12
  export declare class StartedMongoDBContainer extends AbstractStartedContainer {
14
- constructor(startedTestContainer: StartedTestContainer);
13
+ private readonly username;
14
+ private readonly password;
15
+ constructor(startedTestContainer: StartedTestContainer, username: string | undefined, password: string | undefined);
15
16
  getConnectionString(): string;
16
17
  }
@@ -1,60 +1,88 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StartedMongoDBContainer = exports.MongoDBContainer = void 0;
4
+ const compare_versions_1 = require("compare-versions");
4
5
  const testcontainers_1 = require("testcontainers");
5
6
  const MONGODB_PORT = 27017;
6
7
  class MongoDBContainer extends testcontainers_1.GenericContainer {
8
+ username;
9
+ password;
7
10
  constructor(image) {
8
11
  super(image);
9
- this.withExposedPorts(MONGODB_PORT)
10
- .withCommand(["--replSet", "rs0"])
11
- .withWaitStrategy(testcontainers_1.Wait.forLogMessage(/.*waiting for connections.*/i))
12
- .withStartupTimeout(120_000);
12
+ this.withExposedPorts(MONGODB_PORT).withWaitStrategy(testcontainers_1.Wait.forHealthCheck()).withStartupTimeout(120_000);
13
13
  }
14
- async start() {
15
- return new StartedMongoDBContainer(await super.start());
16
- }
17
- async containerStarted(startedTestContainer) {
18
- await this.initReplicaSet(startedTestContainer);
19
- }
20
- async initReplicaSet(startedTestContainer) {
21
- await this.executeMongoEvalCommand(startedTestContainer, "rs.initiate();");
22
- await this.executeMongoEvalCommand(startedTestContainer, this.buildMongoWaitCommand());
14
+ withUsername(username) {
15
+ if (!username)
16
+ throw new Error("Username should not be empty.");
17
+ this.username = username;
18
+ return this;
23
19
  }
24
- async executeMongoEvalCommand(startedTestContainer, command) {
25
- const execResult = await startedTestContainer.exec(this.buildMongoEvalCommand(command));
26
- this.checkMongoNodeExitCode(execResult);
27
- }
28
- buildMongoEvalCommand(command) {
29
- return [this.getMongoCmdBasedOnImageTag(), "--eval", command];
20
+ withPassword(password) {
21
+ if (!password)
22
+ throw new Error("Password should not be empty.");
23
+ this.password = password;
24
+ return this;
30
25
  }
31
- getMongoCmdBasedOnImageTag() {
32
- return parseInt(this.imageName.tag[0]) >= 5 ? "mongosh" : "mongo";
33
- }
34
- checkMongoNodeExitCode(execResult) {
35
- const { exitCode, output } = execResult;
36
- if (execResult.exitCode !== 0) {
37
- throw new Error(`Error running mongo command. Exit code ${exitCode}: ${output}`);
26
+ async start() {
27
+ const cmdArgs = ["--replSet", "rs0"];
28
+ if (!this.healthCheck)
29
+ this.withWaitForRsHealthCheck();
30
+ if (this.username && this.password) {
31
+ cmdArgs.push("--keyFile", "/data/db/key.txt");
32
+ this.withEnvironment({
33
+ MONGO_INITDB_ROOT_USERNAME: this.username,
34
+ MONGO_INITDB_ROOT_PASSWORD: this.password,
35
+ })
36
+ .withCopyContentToContainer([
37
+ {
38
+ content: "1111111111",
39
+ mode: 0o400,
40
+ target: "/data/db/key.txt",
41
+ },
42
+ ])
43
+ .withCommand(cmdArgs);
38
44
  }
45
+ else {
46
+ this.withCommand(cmdArgs);
47
+ }
48
+ return new StartedMongoDBContainer(await super.start(), this.username, this.password);
39
49
  }
40
- buildMongoWaitCommand() {
41
- return `
42
- var attempt = 0;
43
- while(db.runCommand({isMaster: 1}).ismaster==false) {
44
- if (attempt > 60) {
45
- quit(1);
46
- }
47
- print(attempt); sleep(100); attempt++;
50
+ withWaitForRsHealthCheck() {
51
+ return this.withHealthCheck({
52
+ test: [
53
+ "CMD-SHELL",
54
+ this.buildMongoEvalCommand(`'try { rs.initiate(); } catch (e){} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'`),
55
+ ],
56
+ interval: 250,
57
+ timeout: 60000,
58
+ retries: 1000,
59
+ });
48
60
  }
49
- `;
61
+ buildMongoEvalCommand(command) {
62
+ const useMongosh = (0, compare_versions_1.satisfies)(this.imageName.tag, ">=5.0.0");
63
+ const args = [];
64
+ if (useMongosh)
65
+ args.push("mongosh");
66
+ else
67
+ args.push("mongo", "admin");
68
+ if (this.username && this.password)
69
+ args.push("-u", this.username, "-p", this.password);
70
+ args.push("--quiet", "--eval", command);
71
+ return args.join(" ");
50
72
  }
51
73
  }
52
74
  exports.MongoDBContainer = MongoDBContainer;
53
75
  class StartedMongoDBContainer extends testcontainers_1.AbstractStartedContainer {
54
- constructor(startedTestContainer) {
76
+ username;
77
+ password;
78
+ constructor(startedTestContainer, username, password) {
55
79
  super(startedTestContainer);
80
+ this.username = username;
81
+ this.password = password;
56
82
  }
57
83
  getConnectionString() {
84
+ if (this.username && this.password)
85
+ return `mongodb://${this.username}:${this.password}@${this.getHost()}:${this.getMappedPort(MONGODB_PORT)}?authSource=admin`;
58
86
  return `mongodb://${this.getHost()}:${this.getMappedPort(MONGODB_PORT)}`;
59
87
  }
60
88
  }
@@ -1 +1 @@
1
- {"version":3,"file":"mongodb-container.js","sourceRoot":"","sources":["../src/mongodb-container.ts"],"names":[],"mappings":";;;AAAA,mDAAoH;AAEpH,MAAM,YAAY,GAAG,KAAK,CAAC;AAE3B,MAAa,gBAAiB,SAAQ,iCAAgB;IACpD,YAAY,KAAa;QACvB,KAAK,CAAC,KAAK,CAAC,CAAC;QACb,IAAI,CAAC,gBAAgB,CAAC,YAAY,CA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
  "mongoose": "^8.16.4"
33
33
  },
34
34
  "dependencies": {
35
- "testcontainers": "^11.3.2"
35
+ "compare-versions": "^6.1.1",
36
+ "testcontainers": "^11.5.0"
36
37
  }
37
38
  }