@@ -27,13 +27,12 @@ export class MongoDBContainer extends GenericContainer {
2727 public override async start ( ) : Promise < StartedMongoDBContainer > {
2828 const cmdArgs = [ "--replSet" , "rs0" , "--bind_ip_all" ] ;
2929 this . withHealthCheck ( {
30- test : [ "CMD" , ...this . buildMongoEvalCommand ( this . buildMongoWaitCommand ( ) ) ] ,
31- startPeriod : 1_000 ,
32- timeout : 60_000 ,
33- retries : 10 ,
34- interval : 2_000 ,
30+ test : [ "CMD-SHELL" , this . buildMongoEvalCommand ( this . initRsAndWait ( ) ) ] ,
31+ interval : 250 ,
32+ timeout : 60000 ,
33+ retries : 1000 ,
3534 } ) . withWaitStrategy ( Wait . forHealthCheck ( ) ) ;
36- if ( this . authEnabled ( ) ) {
35+ if ( this . username && this . password ) {
3736 cmdArgs . push ( "--keyFile" , "/data/db/key.txt" ) ;
3837 this . withEnvironment ( {
3938 MONGO_INITDB_ROOT_USERNAME : this . username ,
@@ -58,16 +57,13 @@ export class MongoDBContainer extends GenericContainer {
5857 const args = [ ] ;
5958 if ( useMongosh ) args . push ( "mongosh" ) ;
6059 else args . push ( "mongo" , "admin" ) ;
61- args . push ( "-u" , this . username , "-p" , this . password , "--eval" , command ) ;
62- return args ;
60+ if ( this . username && this . password ) args . push ( "-u" , this . username , "-p" , this . password ) ;
61+ args . push ( "--host" , "localhost" , "--quiet" , "--eval" , command ) ;
62+ return args . join ( " " ) ;
6363 }
6464
65- private buildMongoWaitCommand ( ) {
66- return `rs.initiate(); while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(1000); } ` ;
67- }
68-
69- private authEnabled ( ) {
70- return this . username && this . password ;
65+ private initRsAndWait ( ) {
66+ return `'try { rs.initiate(); } catch (e){} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'` ;
7167 }
7268}
7369
0 commit comments