Skip to content
Open
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
52 changes: 52 additions & 0 deletions packages/modules/mongodb/src/mongodb-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,56 @@ describe("MongodbContainer", { timeout: 240_000 }, () => {
await mongodbContainer.stop();
});
// }

it("uses custom credentials (Mongo4, old commands)", async () => {
const mongodbContainer = await new MongoDBContainer("mongo:4.0.1")
.withUsername("CustomUserName")
.withPassword("CustomPassword")
.start();

const db = mongoose.createConnection(mongodbContainer.getConnectionString(), { directConnection: true });

const fooCollection = db.collection("foo");
const obj = { value: 1 };

const session = await db.startSession();
await session.withTransaction(async () => {
await fooCollection.insertOne(obj);
});

expect(
await fooCollection.findOne({
value: 1,
})
).toEqual(obj);

await mongoose.disconnect();
await mongodbContainer.stop();
});

it("uses custom credentials (Mongo8, new commands)", async () => {
const mongodbContainer = await new MongoDBContainer("mongo:8.0.8")
.withUsername("CustomUserName")
.withPassword("CustomPassword")
.start();

const db = mongoose.createConnection(mongodbContainer.getConnectionString(), { directConnection: true });

const fooCollection = db.collection("foo");
const obj = { value: 1 };

const session = await db.startSession();
await session.withTransaction(async () => {
await fooCollection.insertOne(obj);
});

expect(
await fooCollection.findOne({
value: 1,
})
).toEqual(obj);

await mongoose.disconnect();
await mongodbContainer.stop();
});
});
52 changes: 48 additions & 4 deletions packages/modules/mongodb/src/mongodb-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const MONGODB_PORT = 27017;

export class MongoDBContainer extends GenericContainer {
private username: string | null = null;
private password: string | null = null;

constructor(image = "mongo:4.0.1") {
super(image);
this.withExposedPorts(MONGODB_PORT)
Expand All @@ -11,16 +14,41 @@
.withStartupTimeout(120_000);
}

public withUsername(username: string): this {
this.username = username;
return this;
}

public withPassword(rootPassword: string): this {
this.password = rootPassword;
return this;
}

public override async start(): Promise<StartedMongoDBContainer> {
return new StartedMongoDBContainer(await super.start());
if (this.username && this.password) {
const containerKeyfilePath = "/tmp/mongo-keyfile";
this.withCommand([
"/bin/sh",
"-c",
`
openssl rand -base64 756 > ${containerKeyfilePath} &&
chmod 600 ${containerKeyfilePath} &&
chown mongodb:mongodb ${containerKeyfilePath} &&
exec mongod --replSet rs0 --keyFile ${containerKeyfilePath} --bind_ip_all
`,
]);
this.withEnvironment({ MONGO_INITDB_ROOT_USERNAME: this.username, MONGO_INITDB_ROOT_PASSWORD: this.password });
}

return new StartedMongoDBContainer(await super.start(), this.username, this.password);
}

protected override async containerStarted(startedTestContainer: StartedTestContainer): Promise<void> {
await this.initReplicaSet(startedTestContainer);
}

private async initReplicaSet(startedTestContainer: StartedTestContainer) {
await this.executeMongoEvalCommand(startedTestContainer, "rs.initiate();");
await this.executeMongoEvalCommand(startedTestContainer, `rs.initiate();`);
await this.executeMongoEvalCommand(startedTestContainer, this.buildMongoWaitCommand());
}

Expand All @@ -30,7 +58,15 @@
}

private buildMongoEvalCommand(command: string) {
return [this.getMongoCmdBasedOnImageTag(), "--eval", command];
const cmd = [this.getMongoCmdBasedOnImageTag()];

if (this.username && this.password) {
cmd.push("--username", this.username, "--password", this.password, "--authenticationDatabase", "admin");
}

cmd.push("--eval", command);

return cmd;
}

private getMongoCmdBasedOnImageTag() {
Expand All @@ -40,7 +76,7 @@
private checkMongoNodeExitCode(execResult: ExecResult) {
const { exitCode, output } = execResult;
if (execResult.exitCode !== 0) {
throw new Error(`Error running mongo command. Exit code ${exitCode}: ${output}`);

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 22.x) / Runner (ubuntu-22.04) / Node (22.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo8, new commands)

Error: Error running mongo command. Exit code 1: MongoServerError: Authentication failed. ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:92:30

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 22.x) / Runner (ubuntu-22.04) / Node (22.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo4, old commands)

Error: Error running mongo command. Exit code 1: MongoDB shell version v4.0.1 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 4.0.1 2025-04-21T12:12:20.581+0000 E QUERY [js] Error: Authentication failed. : DB.prototype._authOrThrow@src/mongo/shell/db.js:1679:20 @(auth):6:1 @(auth):1:2 exception: login failed ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:66:30

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 18.x) / Runner (ubuntu-22.04) / Node (18.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo8, new commands)

Error: Error running mongo command. Exit code 1: MongoServerError: Authentication failed. ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:92:30

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 18.x) / Runner (ubuntu-22.04) / Node (18.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo4, old commands)

Error: Error running mongo command. Exit code 1: MongoDB shell version v4.0.1 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 4.0.1 2025-04-21T12:12:24.196+0000 E QUERY [js] Error: Authentication failed. : DB.prototype._authOrThrow@src/mongo/shell/db.js:1679:20 @(auth):6:1 @(auth):1:2 exception: login failed ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:66:30

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 20.x) / Runner (ubuntu-22.04) / Node (20.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo8, new commands)

Error: Error running mongo command. Exit code 1: MongoServerError: Authentication failed. ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:92:30

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 20.x) / Runner (ubuntu-22.04) / Node (20.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo4, old commands)

Error: Error running mongo command. Exit code 1: MongoDB shell version v4.0.1 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 4.0.1 2025-04-21T12:12:25.334+0000 E QUERY [js] Error: Authentication failed. : DB.prototype._authOrThrow@src/mongo/shell/db.js:1679:20 @(auth):6:1 @(auth):1:2 exception: login failed ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:66:30

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 20.x) / Runner (ubuntu-22.04) / Node (20.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo8, new commands)

Error: Error running mongo command. Exit code 1: MongoServerError: Authentication failed. ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:92:30

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 20.x) / Runner (ubuntu-22.04) / Node (20.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo4, old commands)

Error: Error running mongo command. Exit code 1: MongoDB shell version v4.0.1 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 4.0.1 2025-04-21T12:27:47.039+0000 E QUERY [js] Error: Authentication failed. : DB.prototype._authOrThrow@src/mongo/shell/db.js:1679:20 @(auth):6:1 @(auth):1:2 exception: login failed ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:66:30

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 22.x) / Runner (ubuntu-22.04) / Node (22.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo8, new commands)

Error: Error running mongo command. Exit code 1: MongoServerError: Authentication failed. ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:92:30

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 22.x) / Runner (ubuntu-22.04) / Node (22.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo4, old commands)

Error: Error running mongo command. Exit code 1: MongoDB shell version v4.0.1 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 4.0.1 2025-04-21T12:27:57.023+0000 E QUERY [js] Error: Authentication failed. : DB.prototype._authOrThrow@src/mongo/shell/db.js:1679:20 @(auth):6:1 @(auth):1:2 exception: login failed ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:66:30

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 18.x) / Runner (ubuntu-22.04) / Node (18.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo8, new commands)

Error: Error running mongo command. Exit code 1: MongoServerError: Authentication failed. ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:92:30

Check failure on line 79 in packages/modules/mongodb/src/mongodb-container.ts

View workflow job for this annotation

GitHub Actions / Docker tests (mongodb, 18.x) / Runner (ubuntu-22.04) / Node (18.x) / Runtime (docker) / Workspace (mongodb)

packages/modules/mongodb/src/mongodb-container.test.ts > MongodbContainer > uses custom credentials (Mongo4, old commands)

Error: Error running mongo command. Exit code 1: MongoDB shell version v4.0.1 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 4.0.1 2025-04-21T12:27:54.685+0000 E QUERY [js] Error: Authentication failed. : DB.prototype._authOrThrow@src/mongo/shell/db.js:1679:20 @(auth):6:1 @(auth):1:2 exception: login failed ❯ MongoDBContainer.checkMongoNodeExitCode packages/modules/mongodb/src/mongodb-container.ts:79:13 ❯ MongoDBContainer.executeMongoEvalCommand packages/modules/mongodb/src/mongodb-container.ts:57:10 ❯ MongoDBContainer.initReplicaSet packages/modules/mongodb/src/mongodb-container.ts:51:5 ❯ MongoDBContainer.containerStarted packages/modules/mongodb/src/mongodb-container.ts:47:5 ❯ MongoDBContainer.startContainer packages/testcontainers/src/generic-container/generic-container.ts:238:7 ❯ MongoDBContainer.start packages/modules/mongodb/src/mongodb-container.ts:43:40 ❯ packages/modules/mongodb/src/mongodb-container.test.ts:66:30
}
}

Expand All @@ -58,11 +94,19 @@
}

export class StartedMongoDBContainer extends AbstractStartedContainer {
constructor(startedTestContainer: StartedTestContainer) {
constructor(
startedTestContainer: StartedTestContainer,
private readonly username: string | null,
private readonly password: string | null
) {
super(startedTestContainer);
}

public getConnectionString(): string {
if (this.username !== null && this.password !== null) {
return `mongodb://${this.username}:${this.password}@${this.getHost()}:${this.getMappedPort(MONGODB_PORT)}`;
}

return `mongodb://${this.getHost()}:${this.getMappedPort(MONGODB_PORT)}`;
}
}
Loading