11import { AbstractStartedContainer , ExecResult , GenericContainer , StartedTestContainer , Wait } from "testcontainers" ;
2- import createKeyfile from "./keyfile" ;
32
43const MONGODB_PORT = 27017 ;
54
65export class MongoDBContainer extends GenericContainer {
76 private username : string | null = null ;
87 private password : string | null = null ;
9- private keyfilePath : string | null = null ;
108
119 constructor ( image = "mongo:4.0.1" ) {
1210 super ( image ) ;
@@ -28,34 +26,32 @@ export class MongoDBContainer extends GenericContainer {
2826
2927 public override async start ( ) : Promise < StartedMongoDBContainer > {
3028 if ( this . username && this . password ) {
31- this . keyfilePath = createKeyfile ( ) ;
32- const containerKeyfilePath = "/etc/mongo-keyfile" ;
33- this . withBindMounts ( [
34- {
35- source : this . keyfilePath ,
36- target : containerKeyfilePath ,
37- mode : "ro" ,
38- } ,
29+ const containerKeyfilePath = "/tmp/mongo-keyfile" ;
30+ this . withCommand ( [
31+ "/bin/sh" ,
32+ "-c" ,
33+ `
34+ openssl rand -base64 756 > ${ containerKeyfilePath } &&
35+ chmod 600 ${ containerKeyfilePath } &&
36+ chown mongodb:mongodb ${ containerKeyfilePath } &&
37+ exec mongod --replSet rs0 --keyFile ${ containerKeyfilePath } --bind_ip_all
38+ ` ,
3939 ] ) ;
40- this . withCommand ( [ "--replSet" , "rs0" , "--keyFile" , containerKeyfilePath , "--bind_ip_all" ] ) ;
4140 this . withEnvironment ( { MONGO_INITDB_ROOT_USERNAME : this . username , MONGO_INITDB_ROOT_PASSWORD : this . password } ) ;
4241 }
4342
4443 return new StartedMongoDBContainer ( await super . start ( ) , this . username , this . password ) ;
4544 }
4645
4746 protected override async containerStarted ( startedTestContainer : StartedTestContainer ) : Promise < void > {
48- if ( this . username && this . password ) {
49- await this . waitForRootUser ( startedTestContainer ) ;
50- }
47+ // if (this.username && this.password) {
48+ // await this.waitForRootUser(startedTestContainer);
49+ // }
5150 await this . initReplicaSet ( startedTestContainer ) ;
5251 }
5352
5453 private async initReplicaSet ( startedTestContainer : StartedTestContainer ) {
55- await this . executeMongoEvalCommand (
56- startedTestContainer ,
57- "rs.initiate({ _id: 'rs0', members: [ { _id: 0, host: '127.0.0.1:27017' } ] });"
58- ) ;
54+ await this . executeMongoEvalCommand ( startedTestContainer , `rs.initiate();` ) ;
5955 await this . executeMongoEvalCommand ( startedTestContainer , this . buildMongoWaitCommand ( ) ) ;
6056 }
6157
@@ -68,17 +64,7 @@ export class MongoDBContainer extends GenericContainer {
6864 const cmd = [ this . getMongoCmdBasedOnImageTag ( ) ] ;
6965
7066 if ( this . username && this . password ) {
71- cmd . push (
72- "admin" ,
73- "--port" ,
74- MONGODB_PORT . toString ( ) ,
75- "-u" ,
76- this . username ,
77- "-p" ,
78- this . password ,
79- "--authenticationDatabase" ,
80- "admin"
81- ) ;
67+ cmd . push ( "--username" , this . username , "--password" , this . password , "--authenticationDatabase" , "admin" ) ;
8268 }
8369
8470 cmd . push ( "--eval" , command ) ;
@@ -97,19 +83,23 @@ export class MongoDBContainer extends GenericContainer {
9783 }
9884 }
9985
100- private async waitForRootUser ( startedTestContainer : StartedTestContainer ) {
101- const checkCommand = this . buildMongoEvalCommand ( "db.runCommand({ connectionStatus: 1 })" ) ;
102-
103- for ( let i = 0 ; i < 30 ; i ++ ) {
104- const result = await startedTestContainer . exec ( checkCommand ) ;
105- if ( result . exitCode === 0 && result . output . includes ( `${ this . username } ` ) ) {
106- return ;
107- }
108- await new Promise ( ( res ) => setTimeout ( res , 1000 ) ) ;
109- }
110-
111- throw new Error ( "Root user not ready after 30s" ) ;
112- }
86+ // private async waitForRootUser(startedTestContainer: StartedTestContainer) {
87+ // const checkCommand = this.buildMongoEvalCommand("db.runCommand({ connectionStatus: 1 })");
88+
89+ // for (let i = 0; i < 30; i++) {
90+ // const result = await startedTestContainer.exec(checkCommand);
91+ // if (
92+ // result.exitCode === 0 &&
93+ // result.output.includes(`${this.username}`) &&
94+ // !result.output.includes(`UserNotFound`)
95+ // ) {
96+ // return;
97+ // }
98+ // await new Promise((res) => setTimeout(res, 1000));
99+ // }
100+
101+ // throw new Error("Root user not ready after 30s");
102+ // }
113103
114104 private buildMongoWaitCommand ( ) {
115105 return `
0 commit comments