1010import org .springframework .beans .factory .annotation .Qualifier ;
1111import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
1212import org .springframework .boot .autoconfigure .condition .ConditionalOnExpression ;
13- import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
1413import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
1514import org .springframework .boot .context .properties .EnableConfigurationProperties ;
1615import org .springframework .context .annotation .Bean ;
1716import org .springframework .context .annotation .Configuration ;
1817import org .springframework .core .env .ConfigurableEnvironment ;
1918import org .springframework .core .env .MapPropertySource ;
19+ import org .springframework .core .io .ClassPathResource ;
20+ import org .testcontainers .containers .BindMode ;
2021import org .testcontainers .containers .GenericContainer ;
2122import org .testcontainers .containers .Network ;
2223import org .testcontainers .containers .ToxiproxyContainer ;
2324import org .testcontainers .containers .wait .strategy .LogMessageWaitStrategy ;
25+ import org .testcontainers .images .builder .Transferable ;
26+ import org .testcontainers .shaded .org .apache .commons .lang3 .StringUtils ;
2427
28+ import java .io .IOException ;
29+ import java .nio .charset .Charset ;
2530import java .util .LinkedHashMap ;
2631import java .util .Optional ;
2732
@@ -44,10 +49,10 @@ public class EmbeddedMongodbBootstrapConfiguration {
4449 @ Bean
4550 @ ConditionalOnToxiProxyEnabled (module = "mongodb" )
4651 ToxiproxyClientProxy mongodbContainerProxy (ToxiproxyClient toxiproxyClient ,
47- ToxiproxyContainer toxiproxyContainer ,
48- @ Qualifier (BEAN_NAME_EMBEDDED_MONGODB ) GenericContainer <?> mongodb ,
49- MongodbProperties properties ,
50- ConfigurableEnvironment environment ) {
52+ ToxiproxyContainer toxiproxyContainer ,
53+ @ Qualifier (BEAN_NAME_EMBEDDED_MONGODB ) GenericContainer <?> mongodb ,
54+ MongodbProperties properties ,
55+ ConfigurableEnvironment environment ) {
5156 ToxiproxyClientProxy proxy = ToxiproxyHelper .createProxy (
5257 toxiproxyClient ,
5358 toxiproxyContainer ,
@@ -64,16 +69,36 @@ ToxiproxyClientProxy mongodbContainerProxy(ToxiproxyClient toxiproxyClient,
6469 @ Bean (value = BEAN_NAME_EMBEDDED_MONGODB , destroyMethod = "stop" )
6570 public GenericContainer <?> mongodb (ConfigurableEnvironment environment ,
6671 MongodbProperties properties ,
67- MongodbStatusCheck mongodbStatusCheck ,
68- Optional <Network > network ) {
69- GenericContainer <?> mongodb =
70- new GenericContainer <>(ContainerUtils .getDockerImageName (properties ))
71- .withEnv ("MONGO_INITDB_ROOT_USERNAME" , properties .getUsername ())
72- .withEnv ("MONGO_INITDB_ROOT_PASSWORD" , properties .getPassword ())
73- .withEnv ("MONGO_INITDB_DATABASE" , properties .getDatabase ())
74- .withExposedPorts (properties .getPort ())
75- .waitingFor (new LogMessageWaitStrategy ().withRegEx (".*mongod startup complete.*" ))
76- .withNetworkAliases (MONGODB_NETWORK_ALIAS );
72+ Optional <Network > network ) throws IOException , InterruptedException {
73+
74+ GenericContainer <?> mongodb ;
75+ if (properties .getReplicaSetName () == null ) {
76+ mongodb = new GenericContainer <>(ContainerUtils .getDockerImageName (properties ))
77+ .withEnv ("MONGO_INITDB_ROOT_USERNAME" , properties .getUsername ())
78+ .withEnv ("MONGO_INITDB_ROOT_PASSWORD" , properties .getPassword ())
79+ .withEnv ("MONGO_INITDB_DATABASE" , properties .getDatabase ())
80+ .withExposedPorts (properties .getPort ())
81+ .waitingFor (new LogMessageWaitStrategy ().withRegEx (".*mongod startup complete.*" ))
82+ .withNetworkAliases (MONGODB_NETWORK_ALIAS );
83+ } else {
84+ mongodb = new GenericContainer <>(ContainerUtils .getDockerImageName (properties ))
85+ .withCommand ("-f" , "/etc/mongod.conf" )
86+ .withClasspathResourceMapping ("/mongod/gen-keyfile.sh" , "/docker-entrypoint-initdb.d/gen-keyfile.sh" , BindMode .READ_ONLY )
87+ .withCopyToContainer (
88+ Transferable .of (
89+ new ClassPathResource ("/mongod/mongod.conf" )
90+ .getContentAsString (Charset .defaultCharset ())
91+ .replace ("${replica-set-name}" , properties .getReplicaSetName ())
92+ )
93+ , "/etc/mongod.conf" )
94+ .withEnv ("MONGO_INITDB_ROOT_USERNAME" , properties .getUsername ())
95+ .withEnv ("MONGO_INITDB_ROOT_PASSWORD" , properties .getPassword ())
96+ .withEnv ("MONGO_INITDB_DATABASE" , properties .getDatabase ())
97+ .withEnv ("MONGO_INITDB_REPL_SET_HOST" , properties .getHost ())
98+ .withExposedPorts (properties .getPort ())
99+ .waitingFor (new MongodbWaitStrategy (properties ))
100+ .withNetworkAliases (MONGODB_NETWORK_ALIAS );
101+ }
77102
78103 network .ifPresent (mongodb ::withNetwork );
79104
@@ -82,12 +107,6 @@ public GenericContainer<?> mongodb(ConfigurableEnvironment environment,
82107 return mongodb ;
83108 }
84109
85- @ Bean
86- @ ConditionalOnMissingBean
87- MongodbStatusCheck mongodbStartupCheckStrategy (MongodbProperties properties ) {
88- return new MongodbStatusCheck (properties );
89- }
90-
91110 private void registerMongodbEnvironment (GenericContainer <?> mongodb , ConfigurableEnvironment environment , MongodbProperties properties ) {
92111 Integer mappedPort = mongodb .getMappedPort (properties .getPort ());
93112 String host = mongodb .getHost ();
@@ -100,8 +119,13 @@ private void registerMongodbEnvironment(GenericContainer<?> mongodb, Configurabl
100119 map .put ("embedded.mongodb.database" , properties .getDatabase ());
101120 map .put ("embedded.mongodb.networkAlias" , MONGODB_NETWORK_ALIAS );
102121 map .put ("embedded.mongodb.internalPort" , properties .getPort ());
122+ if (StringUtils .isNotBlank (properties .getReplicaSetName ())) {
123+ map .put ("embedded.mongodb.replica-set-name" , properties .getReplicaSetName ());
124+ }
103125
104- log .info ("Started mongodb. Connection Details: {}, Connection URI: mongodb://{}:{}/{}" , map , host , mappedPort , properties .getDatabase ());
126+ log .info ("Started mongodb. Connection Details: {}, Connection URI: mongodb://{}:{}/{}{}" , map , host , mappedPort , properties .getDatabase (), Optional .ofNullable (
127+ properties .getReplicaSetName ()).map ("?directConnection=true&authSource=admin&rs=" ::concat ).orElse ("" )
128+ );
105129
106130 MapPropertySource propertySource = new MapPropertySource ("embeddedMongoInfo" , map );
107131 environment .getPropertySources ().addFirst (propertySource );
0 commit comments