-
Notifications
You must be signed in to change notification settings - Fork 254
feat(embedded-mongodb): add support to launch mongo as a replicaset #2698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
b68cd43
38db2e2
8288d13
cf07395
75b7640
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,10 @@ public class MongodbProperties extends CommonContainerProperties { | |
| private String username; | ||
| private String password; | ||
| private String database = "test"; | ||
| private String[] checkCommand = new String[]{"mongosh", "admin", "--eval", "\"db['system.version'].find()\""}; | ||
| /** | ||
| * If provided, mongodb will be started as a replica set with the given name. Default: null (standalone mode). | ||
| */ | ||
| private String replicaSetName; | ||
|
|
||
| public MongodbProperties() { | ||
| this.setCapabilities(List.of(Capability.ALL)); | ||
|
|
@@ -36,6 +39,6 @@ public String getDefaultDockerImage() { | |
| // Please don`t remove this comment. | ||
| // renovate: datasource=docker | ||
| // https://hub.docker.com/_/mongo | ||
| return "mongodb/mongodb-community-server:8.0.10-ubuntu2204"; | ||
| return "mongodb/mongodb-community-server:8.2.2-ubuntu2204"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update also version in other places metron2@4d635ab
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not forget add it to readme |
||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.playtika.testcontainer.mongodb; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.SneakyThrows; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy; | ||
| import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; | ||
|
|
||
| @Slf4j | ||
| @AllArgsConstructor | ||
| public class MongodbWaitStrategy extends AbstractWaitStrategy { | ||
|
|
||
| private final MongodbProperties properties; | ||
|
|
||
| @Override | ||
| @SneakyThrows | ||
| protected void waitUntilReady() { | ||
| log.info("Waiting for mongodb to start"); | ||
| new LogMessageWaitStrategy().withRegEx(".*Waiting for connections.*").waitUntilReady(waitStrategyTarget); | ||
| if (properties.getReplicaSetName() != null) { | ||
ijusti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // The docker container will restart mongod and initialize the replicaset, so we just have to wait for that to finish now. | ||
| log.info("Waiting for mongodb to become primary."); | ||
|
|
||
| LogMessageWaitStrategy logMessageWaitStrategy = new LogMessageWaitStrategy().withRegEx(".*database writes are now permitted.*"); | ||
| logMessageWaitStrategy.waitUntilReady(waitStrategyTarget); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Mongo requires a keyfile for replicasets, but the docker image does not generate one. | ||
| # The image does let you run this arbitrary script after db initialization. | ||
|
|
||
| echo "testcontainers" > /data/configdb/mongod.keyfile | ||
| chmod 400 /data/configdb/mongod.keyfile |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
|
|
||
| storage: | ||
| dbPath: /data/db | ||
|
|
||
| processManagement: | ||
| timeZoneInfo: /usr/share/zoneinfo | ||
|
|
||
| net: | ||
| port: 27017 | ||
| bindIp: 0.0.0.0 | ||
|
|
||
| security: | ||
| keyFile: /data/configdb/mongod.keyfile | ||
| authorization: enabled | ||
|
|
||
| replication: | ||
| replSetName: ${replica-set-name} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package com.playtika.testcontainer.mongodb; | ||
|
|
||
|
|
||
| import lombok.Value; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.core.env.ConfigurableEnvironment; | ||
| import org.springframework.data.annotation.Id; | ||
| import org.springframework.data.mongodb.core.MongoTemplate; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @Slf4j | ||
| @SpringBootTest( | ||
| properties = { | ||
| "embedded.mongodb.username=root", | ||
| "embedded.mongodb.password=letmein", | ||
| "embedded.mongodb.replica-set-name=rs0", | ||
| "spring.data.mongodb.uri=mongodb://${embedded.mongodb.username}:${embedded.mongodb.password}@${embedded.mongodb.host}:${embedded.mongodb.port}/${embedded.mongodb.database}?replicaSet=${embedded.mongodb.replica-set-name}&directConnection=true&authSource=admin" | ||
| } | ||
| , classes = EmbeddedMongodbBootstrapReplicaSetConfigurationTest.TestConfiguration.class | ||
| ) | ||
| public class EmbeddedMongodbBootstrapReplicaSetConfigurationTest { | ||
|
|
||
| @Autowired | ||
| MongoTemplate mongoTemplate; | ||
|
|
||
| @Autowired | ||
| ConfigurableEnvironment environment; | ||
|
|
||
|
|
||
| @Test | ||
| public void shouldSaveAndGet() { | ||
| String someId = UUID.randomUUID().toString(); | ||
| Foo foo = new Foo(someId, "foo", Instant.parse("2019-09-26T07:57:12.801Z"), -42L); | ||
| mongoTemplate.save(foo); | ||
|
|
||
| assertThat(mongoTemplate.findById(someId, Foo.class)).isEqualTo(foo); | ||
| } | ||
|
|
||
| @Test | ||
| public void propertiesAreAvailable() { | ||
| assertThat(environment.getProperty("embedded.mongodb.port")).isNotEmpty(); | ||
| assertThat(environment.getProperty("embedded.mongodb.host")).isNotEmpty(); | ||
| assertThat(environment.getProperty("embedded.mongodb.username")).isNotEmpty(); | ||
| assertThat(environment.getProperty("embedded.mongodb.password")).isNotEmpty(); | ||
| assertThat(environment.getProperty("embedded.mongodb.database")).isNotEmpty(); | ||
| assertThat(environment.getProperty("embedded.mongodb.replica-set-name")).isNotEmpty(); | ||
|
Comment on lines
+50
to
+55
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not use environment. add fields with spring Value annotation
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was following the convention of the other test, do you want me to change both of them? I pretty much copied that one and added this line for the replicaset mode. |
||
| } | ||
|
|
||
| @Value | ||
| static class Foo { | ||
| @Id | ||
| String someId; | ||
| String someString; | ||
| Instant someTimestamp; | ||
| Long someNumber; | ||
| } | ||
|
|
||
| @EnableAutoConfiguration | ||
| @Configuration | ||
| static class TestConfiguration { | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.