-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add support for initialization scripts #11331
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
base: main
Are you sure you want to change the base?
Add support for initialization scripts #11331
Conversation
Running initialization scripts in MongoDB causes the container to restart, which previously required manual WaitStrategy adjustment. This commit adds 'withInitScript(String)' to automatically handle the script copy and adjust the WaitStrategy to expect two startup log messages. Fixes testcontainers#3066
| * @param scriptPath the path to the init script file on the classpath | ||
| * @return this container instance | ||
| */ | ||
| public MongoDBContainer withInitScript(String scriptPath) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
org.testcontainers.containers.MongoDBContainer is deprecated. can you moved to org.testcontainers.mongodb.MongoDBContainer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved to org.testcontainers.mongodb.MongoDBContainer.
Also, For the tests, instead of creating a new file named MongoDBInitScriptTest.java, I added the test cases directly to MongoDBContainerTest.java.
6665ad7 to
67088da
Compare
67088da to
e81a2ac
Compare
|
does this work with ReplicaSet and Sharded mode? New MongoDBContainer implementation has |
|
I updated In these modes, the default entrypoint mechanism conflicts with the cluster initialization scripts or is ignored. To fix this, I modified the logic to isolate the init script from the default directory when running in Cluster mode, and instead execute it manually after the container has started. ReasonIn Cluster modes (ReplicaSet/Sharding), relying on the default
Changes
Fixes #3066 |
| .withEnv("LANG", "C.UTF-8") | ||
| .withEnv("LC_ALL", "C.UTF-8") | ||
| .withStartupTimeout(Duration.ofSeconds(30)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this required?
| try ( | ||
| MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10") | ||
| .withInitScript("init.js") | ||
| .withStartupTimeout(Duration.ofSeconds(30)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this required?
| .isEqualTo("{ } [ ] : ,"); | ||
|
|
||
| assertThat(doc.getString("description")) | ||
| .as("Japanese text should be preserved correctly") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove as
|
|
||
| assertThat(doc.getString("description")) | ||
| .as("Japanese text should be preserved correctly") | ||
| .isEqualTo("特殊記号を含むコレクションへの挿入テスト"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use plain english, please?
| @Test | ||
| void shouldExecuteInitScriptWithShardingConfiguredFirst() { | ||
| try (MongoDBContainer mongo = new MongoDBContainer("mongo:7.0.0").withSharding().withInitScript("init.js")) { | ||
| mongo.start(); | ||
| assertInitScriptExecuted(mongo); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove it. There is no difference with the previous test
| @Test | ||
| void shouldExecuteInitScriptWithReplicaSetConfiguredFirst() { | ||
| try (MongoDBContainer mongo = new MongoDBContainer("mongo:7.0.0").withReplicaSet().withInitScript("init.js")) { | ||
| mongo.start(); | ||
| assertInitScriptExecuted(mongo); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove it. There is no difference with the previous test
Fixes #3066
Description
This PR adds support for initialization scripts in
MongoDBContainer.When an initialization script is placed in
/docker-entrypoint-initdb.d/, the official MongoDB image restarts the container process after executing the script. This behavior previously causedWaitStrategytimeouts because the container would stop and start again.Changes
withInitScript(String scriptPath)method toMongoDBContainer./docker-entrypoint-initdb.d/init.js.WaitStrategyto expect two "waiting for connections" log messages, ensuring the container is fully ready after the restart.Verification
MongoDBInitScriptTestto verify that the container starts successfully with an init script.