Skip to content

Commit

Permalink
Add compatibility with MongoDB 6 (#5771)
Browse files Browse the repository at this point in the history
MongoDB 6 removed `mongo` shell and `mongosh` is recommended.
See [here](https://www.mongodb.com/docs/manual/release-notes/6.0-compatibility/#legacy-mongo-shell-removed)

Closes #5768
  • Loading branch information
eddumelendez committed Aug 26, 2022
1 parent a18cab6 commit bd22f1e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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 };
}

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();
}
}
}

0 comments on commit bd22f1e

Please sign in to comment.