@testcontainers/mongodb 10.0.0-beta.4
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.
- package/build/index.d.ts +1 -0
- package/build/index.js +7 -0
- package/build/index.js.map +1 -0
- package/build/mongodb-container.d.ts +16 -0
- package/build/mongodb-container.js +62 -0
- package/build/mongodb-container.js.map +1 -0
- package/package.json +21 -0
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MongoDBContainer, StartedMongoDBContainer } from "./mongodb-container";
|
package/build/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StartedMongoDBContainer = exports.MongoDBContainer = void 0;
|
|
4
|
+
var mongodb_container_1 = require("./mongodb-container");
|
|
5
|
+
Object.defineProperty(exports, "MongoDBContainer", { enumerable: true, get: function () { return mongodb_container_1.MongoDBContainer; } });
|
|
6
|
+
Object.defineProperty(exports, "StartedMongoDBContainer", { enumerable: true, get: function () { return mongodb_container_1.StartedMongoDBContainer; } });
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yDAAgF;AAAvE,qHAAA,gBAAgB,OAAA;AAAE,4HAAA,uBAAuB,OAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AbstractStartedContainer, GenericContainer, StartedTestContainer } from "@testcontainers/testcontainers";
|
|
2
|
+
export declare class MongoDBContainer extends GenericContainer {
|
|
3
|
+
constructor(image?: string);
|
|
4
|
+
start(): Promise<StartedMongoDBContainer>;
|
|
5
|
+
protected containerStarted(startedTestContainer: StartedTestContainer): Promise<void>;
|
|
6
|
+
private initReplicaSet;
|
|
7
|
+
private executeMongoEvalCommand;
|
|
8
|
+
private buildMongoEvalCommand;
|
|
9
|
+
private getMongoCmdBasedOnImageTag;
|
|
10
|
+
private checkMongoNodeExitCode;
|
|
11
|
+
private buildMongoWaitCommand;
|
|
12
|
+
}
|
|
13
|
+
export declare class StartedMongoDBContainer extends AbstractStartedContainer {
|
|
14
|
+
constructor(startedTestContainer: StartedTestContainer);
|
|
15
|
+
getConnectionString(): string;
|
|
16
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StartedMongoDBContainer = exports.MongoDBContainer = void 0;
|
|
4
|
+
const testcontainers_1 = require("@testcontainers/testcontainers");
|
|
5
|
+
const MONGODB_PORT = 27017;
|
|
6
|
+
class MongoDBContainer extends testcontainers_1.GenericContainer {
|
|
7
|
+
constructor(image = "mongo:4.0.1") {
|
|
8
|
+
super(image);
|
|
9
|
+
}
|
|
10
|
+
async start() {
|
|
11
|
+
this.withExposedPorts(MONGODB_PORT)
|
|
12
|
+
.withCommand(["--replSet", "rs0"])
|
|
13
|
+
.withWaitStrategy(testcontainers_1.Wait.forLogMessage(/.*waiting for connections.*/i))
|
|
14
|
+
.withStartupTimeout(120000);
|
|
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());
|
|
23
|
+
}
|
|
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];
|
|
30
|
+
}
|
|
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}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
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++;
|
|
48
|
+
}
|
|
49
|
+
`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.MongoDBContainer = MongoDBContainer;
|
|
53
|
+
class StartedMongoDBContainer extends testcontainers_1.AbstractStartedContainer {
|
|
54
|
+
constructor(startedTestContainer) {
|
|
55
|
+
super(startedTestContainer);
|
|
56
|
+
}
|
|
57
|
+
getConnectionString() {
|
|
58
|
+
return `mongodb://${this.getHost()}:${this.getMappedPort(MONGODB_PORT)}`;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.StartedMongoDBContainer = StartedMongoDBContainer;
|
|
62
|
+
//# sourceMappingURL=mongodb-container.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongodb-container.js","sourceRoot":"","sources":["../src/mongodb-container.ts"],"names":[],"mappings":";;;AAAA,mEAMwC;AAExC,MAAM,YAAY,GAAG,KAAK,CAAC;AAE3B,MAAa,gBAAiB,SAAQ,iCAAgB;IACpD,YAAY,KAAK,GAAG,aAAa;QAC/B,KAAK,CAAC,KAAK,CAAC,CAAC;IACf,CAAC;IAEe,K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UAAU,CAAC;QACxC,IAAI,UAAU,CAAC,QAAQ,KAAK,CAAC,EAAE;YAC7B,MAAM,IAAI,KAAK,CAAC,0CAA0C,QAAQ,KAAK,MAAM,EAAE,CAAC,CAAC;SAClF;IACH,CAAC;IAEO,qBAAqB;QAC3B,OAAO;;;;;;;;KAQN,CAAC;IACJ,CAAC;CACF;AArDD,4CAqDC;AAED,MAAa,uBAAwB,SAAQ,yCAAwB;IACnE,YAAY,oBAA0C;QACpD,KAAK,CAAC,oBAAoB,CAAC,CAAC;IAC9B,CAAC;IAEM,mBAAmB;QACxB,OAAO,aAAa,IAAI,CAAC,OAAO,EAAE,IAAI,IAAI,CAAC,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;IAC3E,CAAC;CACF;AARD,0DAQC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@testcontainers/mongodb",
|
|
3
|
+
"version": "10.0.0-beta.4",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "build/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"build"
|
|
8
|
+
],
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc --project tsconfig.build.json"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"mongoose": "^7.4.0"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@testcontainers/testcontainers": "*"
|
|
20
|
+
}
|
|
21
|
+
}
|