|
| 1 | +package org.testcontainers.mongodb; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 4 | +import lombok.NonNull; |
| 5 | +import lombok.SneakyThrows; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.testcontainers.containers.GenericContainer; |
| 8 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 9 | +import org.testcontainers.utility.DockerImageName; |
| 10 | +import org.testcontainers.utility.MountableFile; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | + |
| 14 | +/** |
| 15 | + * Testcontainers implementation for MongoDB. |
| 16 | + * <p> |
| 17 | + * Supported images: {@code mongo}, {@code mongodb/mongodb-community-server}, {@code mongodb/mongodb-enterprise-server} |
| 18 | + * <p> |
| 19 | + * Exposed ports: 27017 |
| 20 | + */ |
| 21 | +@Slf4j |
| 22 | +public class MongoDBContainer extends GenericContainer<MongoDBContainer> { |
| 23 | + |
| 24 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("mongo"); |
| 25 | + |
| 26 | + private static final DockerImageName COMMUNITY_SERVER_IMAGE = DockerImageName.parse( |
| 27 | + "mongodb/mongodb-community-server" |
| 28 | + ); |
| 29 | + |
| 30 | + private static final DockerImageName ENTERPRISE_SERVER_IMAGE = DockerImageName.parse( |
| 31 | + "mongodb/mongodb-enterprise-server" |
| 32 | + ); |
| 33 | + |
| 34 | + private static final int MONGODB_INTERNAL_PORT = 27017; |
| 35 | + |
| 36 | + private static final int CONTAINER_EXIT_CODE_OK = 0; |
| 37 | + |
| 38 | + private static final int AWAIT_INIT_REPLICA_SET_ATTEMPTS = 60; |
| 39 | + |
| 40 | + private static final String MONGODB_DATABASE_NAME_DEFAULT = "test"; |
| 41 | + |
| 42 | + private static final String STARTER_SCRIPT = "/testcontainers_start.sh"; |
| 43 | + |
| 44 | + private boolean shardingEnabled; |
| 45 | + |
| 46 | + private boolean rsEnabled; |
| 47 | + |
| 48 | + public MongoDBContainer(@NonNull String dockerImageName) { |
| 49 | + this(DockerImageName.parse(dockerImageName)); |
| 50 | + } |
| 51 | + |
| 52 | + public MongoDBContainer(DockerImageName dockerImageName) { |
| 53 | + super(dockerImageName); |
| 54 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME, COMMUNITY_SERVER_IMAGE, ENTERPRISE_SERVER_IMAGE); |
| 55 | + |
| 56 | + withExposedPorts(MONGODB_INTERNAL_PORT); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void containerIsStarting(InspectContainerResponse containerInfo) { |
| 61 | + if (this.shardingEnabled) { |
| 62 | + copyFileToContainer(MountableFile.forClasspathResource("/sharding.sh", 0777), STARTER_SCRIPT); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) { |
| 68 | + if (this.rsEnabled) { |
| 69 | + initReplicaSet(reused); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private String[] buildMongoEvalCommand(String command) { |
| 74 | + return new String[] { |
| 75 | + "sh", |
| 76 | + "-c", |
| 77 | + "mongosh mongo --eval \"" + command + "\" || mongo --eval \"" + command + "\"", |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + private void checkMongoNodeExitCode(ExecResult execResult) { |
| 82 | + if (execResult.getExitCode() != CONTAINER_EXIT_CODE_OK) { |
| 83 | + String errorMessage = String.format("An error occurred: %s", execResult.getStdout()); |
| 84 | + log.error(errorMessage); |
| 85 | + throw new ReplicaSetInitializationException(errorMessage); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private String buildMongoWaitCommand() { |
| 90 | + return String.format( |
| 91 | + "var attempt = 0; " + |
| 92 | + "while" + |
| 93 | + "(%s) " + |
| 94 | + "{ " + |
| 95 | + "if (attempt > %d) {quit(1);} " + |
| 96 | + "print('%s ' + attempt); sleep(100); attempt++; " + |
| 97 | + " }", |
| 98 | + "db.runCommand( { isMaster: 1 } ).ismaster==false", |
| 99 | + AWAIT_INIT_REPLICA_SET_ATTEMPTS, |
| 100 | + "An attempt to await for a single node replica set initialization:" |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + private void checkMongoNodeExitCodeAfterWaiting(ExecResult execResultWaitForMaster) { |
| 105 | + if (execResultWaitForMaster.getExitCode() != CONTAINER_EXIT_CODE_OK) { |
| 106 | + String errorMessage = String.format( |
| 107 | + "A single node replica set was not initialized in a set timeout: %d attempts", |
| 108 | + AWAIT_INIT_REPLICA_SET_ATTEMPTS |
| 109 | + ); |
| 110 | + log.error(errorMessage); |
| 111 | + throw new ReplicaSetInitializationException(errorMessage); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @SneakyThrows(value = { IOException.class, InterruptedException.class }) |
| 116 | + private void initReplicaSet(boolean reused) { |
| 117 | + if (reused && isReplicationSetAlreadyInitialized()) { |
| 118 | + log.debug("Replica set already initialized."); |
| 119 | + } else { |
| 120 | + log.debug("Initializing a single node node replica set..."); |
| 121 | + ExecResult execResultInitRs = execInContainer(buildMongoEvalCommand("rs.initiate();")); |
| 122 | + log.debug(execResultInitRs.getStdout()); |
| 123 | + checkMongoNodeExitCode(execResultInitRs); |
| 124 | + |
| 125 | + log.debug( |
| 126 | + "Awaiting for a single node replica set initialization up to {} attempts", |
| 127 | + AWAIT_INIT_REPLICA_SET_ATTEMPTS |
| 128 | + ); |
| 129 | + ExecResult execResultWaitForMaster = execInContainer(buildMongoEvalCommand(buildMongoWaitCommand())); |
| 130 | + log.debug(execResultWaitForMaster.getStdout()); |
| 131 | + |
| 132 | + checkMongoNodeExitCodeAfterWaiting(execResultWaitForMaster); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public static class ReplicaSetInitializationException extends RuntimeException { |
| 137 | + |
| 138 | + ReplicaSetInitializationException(String errorMessage) { |
| 139 | + super(errorMessage); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @SneakyThrows |
| 144 | + private boolean isReplicationSetAlreadyInitialized() { |
| 145 | + // since we are creating a replica set with one node, this node must be primary (state = 1) |
| 146 | + ExecResult execCheckRsInit = execInContainer( |
| 147 | + buildMongoEvalCommand("if(db.adminCommand({replSetGetStatus: 1})['myState'] != 1) quit(900)") |
| 148 | + ); |
| 149 | + return execCheckRsInit.getExitCode() == CONTAINER_EXIT_CODE_OK; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Enables replica set on the cluster |
| 154 | + * |
| 155 | + * @return this |
| 156 | + */ |
| 157 | + public MongoDBContainer withReplicaSet() { |
| 158 | + this.rsEnabled = true; |
| 159 | + withCommand("--replSet", "docker-rs"); |
| 160 | + waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", 1)); |
| 161 | + return this; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Enables sharding on the cluster |
| 166 | + * |
| 167 | + * @return this |
| 168 | + */ |
| 169 | + public MongoDBContainer withSharding() { |
| 170 | + this.shardingEnabled = true; |
| 171 | + withCommand("-c", "while [ ! -f " + STARTER_SCRIPT + " ]; do sleep 0.1; done; " + STARTER_SCRIPT); |
| 172 | + waitingFor(Wait.forLogMessage("(?i).*mongos ready.*", 1)); |
| 173 | + withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint("sh")); |
| 174 | + return this; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Gets a connection string url, unlike {@link #getReplicaSetUrl} this does not point to a |
| 179 | + * database |
| 180 | + * @return a connection url pointing to a mongodb instance |
| 181 | + */ |
| 182 | + public String getConnectionString() { |
| 183 | + return String.format("mongodb://%s:%d", getHost(), getMappedPort(MONGODB_INTERNAL_PORT)); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Gets a replica set url for the default {@value #MONGODB_DATABASE_NAME_DEFAULT} database. |
| 188 | + * |
| 189 | + * @return a replica set url. |
| 190 | + */ |
| 191 | + public String getReplicaSetUrl() { |
| 192 | + return getReplicaSetUrl(MONGODB_DATABASE_NAME_DEFAULT); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Gets a replica set url for a provided <code>databaseName</code>. |
| 197 | + * |
| 198 | + * @param databaseName a database name. |
| 199 | + * @return a replica set url. |
| 200 | + */ |
| 201 | + public String getReplicaSetUrl(String databaseName) { |
| 202 | + if (!isRunning()) { |
| 203 | + throw new IllegalStateException("MongoDBContainer should be started first"); |
| 204 | + } |
| 205 | + return getConnectionString() + "/" + databaseName; |
| 206 | + } |
| 207 | +} |
0 commit comments