@@ -4,34 +4,29 @@ import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait
44const MONGODB_PORT = 27017 ;
55
66export class MongoDBContainer extends GenericContainer {
7- private username = "" ;
8- private password = "" ;
7+ private username : string | undefined ;
8+ private password : string | undefined ;
99
1010 constructor ( image : string ) {
1111 super ( image ) ;
12- this . withExposedPorts ( MONGODB_PORT ) . withStartupTimeout ( 120_000 ) ;
12+ this . withExposedPorts ( MONGODB_PORT ) . withWaitStrategy ( Wait . forHealthCheck ( ) ) . withStartupTimeout ( 120_000 ) ;
1313 }
1414
1515 public withUsername ( username : string ) : this {
16- if ( username === "" ) throw new Error ( "Username should not be empty." ) ;
16+ if ( ! username ) throw new Error ( "Username should not be empty." ) ;
1717 this . username = username ;
1818 return this ;
1919 }
2020
2121 public withPassword ( password : string ) : this {
22- if ( password === "" ) throw new Error ( "Password should not be empty." ) ;
22+ if ( ! password ) throw new Error ( "Password should not be empty." ) ;
2323 this . password = password ;
2424 return this ;
2525 }
2626
2727 public override async start ( ) : Promise < StartedMongoDBContainer > {
28- const cmdArgs = [ "--replSet" , "rs0" , "--bind_ip_all" ] ;
29- this . withHealthCheck ( {
30- test : [ "CMD-SHELL" , this . buildMongoEvalCommand ( this . initRsAndWait ( ) ) ] ,
31- interval : 250 ,
32- timeout : 60000 ,
33- retries : 1000 ,
34- } ) . withWaitStrategy ( Wait . forHealthCheck ( ) ) ;
28+ const cmdArgs = [ "--replSet" , "rs0" ] ;
29+ if ( ! this . healthCheck ) this . withWaitForRsHealthCheck ( ) ;
3530 if ( this . username && this . password ) {
3631 cmdArgs . push ( "--keyFile" , "/data/db/key.txt" ) ;
3732 this . withEnvironment ( {
@@ -52,26 +47,36 @@ export class MongoDBContainer extends GenericContainer {
5247 return new StartedMongoDBContainer ( await super . start ( ) , this . username , this . password ) ;
5348 }
5449
50+ private withWaitForRsHealthCheck ( ) : this {
51+ return this . withHealthCheck ( {
52+ test : [
53+ "CMD-SHELL" ,
54+ this . buildMongoEvalCommand (
55+ `'try { rs.initiate(); } catch (e){} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'`
56+ ) ,
57+ ] ,
58+ interval : 250 ,
59+ timeout : 60000 ,
60+ retries : 1000 ,
61+ } ) ;
62+ }
63+
5564 private buildMongoEvalCommand ( command : string ) {
5665 const useMongosh = satisfies ( this . imageName . tag , ">=5.0.0" ) ;
5766 const args = [ ] ;
5867 if ( useMongosh ) args . push ( "mongosh" ) ;
5968 else args . push ( "mongo" , "admin" ) ;
6069 if ( this . username && this . password ) args . push ( "-u" , this . username , "-p" , this . password ) ;
61- args . push ( "--host" , "localhost" , "-- quiet", "--eval" , command ) ;
70+ args . push ( "--quiet" , "--eval" , command ) ;
6271 return args . join ( " " ) ;
6372 }
64-
65- private initRsAndWait ( ) {
66- return `'try { rs.initiate(); } catch (e){} while (db.runCommand({isMaster: 1}).ismaster==false) { sleep(100); }'` ;
67- }
6873}
6974
7075export class StartedMongoDBContainer extends AbstractStartedContainer {
71- private readonly username : string = "" ;
72- private readonly password : string = "" ;
76+ private readonly username : string | undefined ;
77+ private readonly password : string | undefined ;
7378
74- constructor ( startedTestContainer : StartedTestContainer , username : string , password : string ) {
79+ constructor ( startedTestContainer : StartedTestContainer , username : string | undefined , password : string | undefined ) {
7580 super ( startedTestContainer ) ;
7681 this . username = username ;
7782 this . password = password ;
0 commit comments