Skip to content
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

Add compatibility with MongoDB 6 #5771

Merged
merged 1 commit into from Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,6 +5,7 @@
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.ComparableVersion;
import org.testcontainers.utility.DockerImageName;

import java.io.IOException;
Expand All @@ -29,6 +30,8 @@ public class MongoDBContainer extends GenericContainer<MongoDBContainer> {

private static final String MONGODB_DATABASE_NAME_DEFAULT = "test";

private final boolean isAtLeastVersion6;

/**
* @deprecated use {@link MongoDBContainer(DockerImageName)} instead
*/
Expand All @@ -45,6 +48,8 @@ public MongoDBContainer(final DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

this.isAtLeastVersion6 = new ComparableVersion(dockerImageName.getVersionPart()).isGreaterThanOrEqualTo("6.0");

withExposedPorts(MONGODB_INTERNAL_PORT);
withCommand("--replSet", "docker-rs");
waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", 1));
Expand Down Expand Up @@ -87,7 +92,8 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
}

private String[] buildMongoEvalCommand(final String command) {
return new String[] { "mongo", "--eval", command };
String cmd = this.isAtLeastVersion6 ? "mongosh" : "mongo";
return new String[] { cmd, "--eval", command };
Comment on lines +95 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddumelendez WDYT about not using ComparableVersion but doing "mongosh || mongo"-style command, so that the shell will "fallback" automatically?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a good idea, let's do a follow-up PR for this @eddumelendez and keep this in mind as a pattern when possible 👍

}

private void checkMongoNodeExitCode(final Container.ExecResult execResult) {
Expand Down
Expand Up @@ -98,4 +98,11 @@ public void shouldTestDatabaseName() {
assertThat(mongoDBContainer.getReplicaSetUrl(databaseName)).endsWith(databaseName);
}
}

@Test
public void supportsMongoDB_6() {
try (final MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:6.0.1")) {
mongoDBContainer.start();
}
}
}