|
| 1 | +package io.ebean.test.containers; |
| 2 | + |
| 3 | +import com.mongodb.client.MongoClient; |
| 4 | +import com.mongodb.client.MongoClients; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.Properties; |
| 8 | + |
| 9 | +/** |
| 10 | + * MongoDB container. |
| 11 | + * |
| 12 | + * <pre>{@code |
| 13 | + * |
| 14 | + * MongoContainer container = MongoContainer.builder("8.0") |
| 15 | + * //.port(27017) |
| 16 | + * //.containerName("ut_mongodb") |
| 17 | + * //.username("test") |
| 18 | + * //.password("test") |
| 19 | + * //.dbName("test") |
| 20 | + * .build(); |
| 21 | + * |
| 22 | + * container.start(); |
| 23 | + * |
| 24 | + * // obtain connection string |
| 25 | + * String connectionString = container.connectionString(); |
| 26 | + * |
| 27 | + * // obtain a MongoClient (requires mongodb-driver-sync on the classpath) |
| 28 | + * MongoClient client = container.mongoClient(); |
| 29 | + * MongoDatabase db = client.getDatabase(container.dbName()); |
| 30 | + * |
| 31 | + * // container will be shutdown and removed via shutdown hook |
| 32 | + * // local devs: touch ~/.ebean/ignore-docker-shutdown |
| 33 | + * // to keep the container running for faster testing |
| 34 | + * |
| 35 | + * }</pre> |
| 36 | + * |
| 37 | + * <h3>Shutdown</h3> |
| 38 | + * <p> |
| 39 | + * By default, the container will be stopped and removed via shutdown hook. |
| 40 | + * |
| 41 | + * <h3>Local development</h3> |
| 42 | + * <p> |
| 43 | + * For local development, keep the container running between test runs: |
| 44 | + * |
| 45 | + * <pre> |
| 46 | + * touch ~/.ebean/ignore-docker-shutdown |
| 47 | + * </pre> |
| 48 | + */ |
| 49 | +public class MongoContainer extends BaseContainer<MongoContainer> { |
| 50 | + |
| 51 | + @Override |
| 52 | + public MongoContainer start() { |
| 53 | + startOrThrow(); |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Create a builder for MongoContainer. |
| 59 | + */ |
| 60 | + public static Builder builder(String version) { |
| 61 | + return new Builder(version); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * The MongoContainer builder. |
| 66 | + */ |
| 67 | + public static class Builder extends BaseBuilder<MongoContainer, Builder> { |
| 68 | + |
| 69 | + private String username = "test"; |
| 70 | + private String password = "test"; |
| 71 | + private String dbName = "test"; |
| 72 | + |
| 73 | + private Builder(String version) { |
| 74 | + super("mongo", 27017, 27017, version); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Set the MongoDB admin username. Defaults to "test". |
| 79 | + */ |
| 80 | + public Builder username(String username) { |
| 81 | + this.username = username; |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Set the MongoDB admin password. Defaults to "test". |
| 87 | + */ |
| 88 | + public Builder password(String password) { |
| 89 | + this.password = password; |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Set the initial database name. Defaults to "test". |
| 95 | + */ |
| 96 | + public Builder dbName(String dbName) { |
| 97 | + this.dbName = dbName; |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + protected void extraProperties(Properties properties) { |
| 103 | + username = prop(properties, "username", username); |
| 104 | + password = prop(properties, "password", password); |
| 105 | + dbName = prop(properties, "dbName", dbName); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public MongoContainer build() { |
| 110 | + return new MongoContainer(this); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public MongoContainer start() { |
| 115 | + return build().start(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private final String username; |
| 120 | + private final String password; |
| 121 | + private final String dbName; |
| 122 | + |
| 123 | + private MongoContainer(Builder builder) { |
| 124 | + super(builder); |
| 125 | + this.username = builder.username; |
| 126 | + this.password = builder.password; |
| 127 | + this.dbName = builder.dbName; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Return the database name. |
| 132 | + */ |
| 133 | + public String dbName() { |
| 134 | + return dbName; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Return the MongoDB connection string for this container. |
| 139 | + */ |
| 140 | + public String connectionString() { |
| 141 | + if (notEmpty(username) && notEmpty(password)) { |
| 142 | + return String.format("mongodb://%s:%s@%s:%d/%s", username, password, config.getHost(), config.getPort(), dbName); |
| 143 | + } |
| 144 | + return String.format("mongodb://%s:%d/%s", config.getHost(), config.getPort(), dbName); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Return a MongoClient connected to this container. |
| 149 | + * <p> |
| 150 | + * Requires {@code mongodb-driver-sync} on the classpath. |
| 151 | + */ |
| 152 | + public MongoClient mongoClient() { |
| 153 | + return MongoClients.create(connectionString()); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + boolean checkConnectivity() { |
| 158 | + // clearMatch discards the first "Waiting for connections" that appears during |
| 159 | + // the credential-setup phase (binds to 127.0.0.1 only) so we wait for the |
| 160 | + // second occurrence that binds to 0.0.0.0 and is actually reachable. |
| 161 | + // When no auth is configured the clearMatch never triggers, but the single |
| 162 | + // "Waiting for connections" still matches — so this is correct in both cases. |
| 163 | + return logsContain("Waiting for connections", "MongoDB init process complete"); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + protected ProcessBuilder runProcess() { |
| 168 | + List<String> args = dockerRun(); |
| 169 | + if (notEmpty(username)) { |
| 170 | + args.add("-e"); |
| 171 | + args.add("MONGO_INITDB_ROOT_USERNAME=" + username); |
| 172 | + } |
| 173 | + if (notEmpty(password)) { |
| 174 | + args.add("-e"); |
| 175 | + args.add("MONGO_INITDB_ROOT_PASSWORD=" + password); |
| 176 | + } |
| 177 | + if (notEmpty(dbName)) { |
| 178 | + args.add("-e"); |
| 179 | + args.add("MONGO_INITDB_DATABASE=" + dbName); |
| 180 | + } |
| 181 | + args.add(config.image()); |
| 182 | + return createProcessBuilder(args); |
| 183 | + } |
| 184 | +} |
0 commit comments