Commit 0b5d39c0 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge pull request #5617 from Yogesh Lonkar

* gh-5617:
  Polish contribution
  Allow embedded Mongo's storage to be configured via the environment
parents f9a86363 436da1d5
......@@ -35,6 +35,7 @@ import de.flapdoodle.embed.mongo.config.IMongodConfig;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder;
import de.flapdoodle.embed.mongo.config.Storage;
import de.flapdoodle.embed.mongo.distribution.Feature;
import de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion;
import de.flapdoodle.embed.process.config.IRuntimeConfig;
......@@ -69,6 +70,7 @@ import org.springframework.util.Assert;
*
* @author Henryk Konsek
* @author Andy Wilkinson
* @author Yogesh Lonkar
* @since 1.3.0
*/
@Configuration
......@@ -125,6 +127,14 @@ public class EmbeddedMongoAutoConfiguration {
this.embeddedProperties.getFeatures());
MongodConfigBuilder builder = new MongodConfigBuilder()
.version(featureAwareVersion);
if (this.embeddedProperties.getStorage() != null) {
builder.replication(
new Storage(this.embeddedProperties.getStorage().getDatabaseDir(),
this.embeddedProperties.getStorage().getReplSetName(),
this.embeddedProperties.getStorage().getOplogSize() != null
? this.embeddedProperties.getStorage().getOplogSize()
: 0));
}
if (getPort() > 0) {
builder.net(new Net(getHost().getHostAddress(), getPort(),
Network.localhostIsIPv6()));
......
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -28,6 +28,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* Configuration properties for Embedded Mongo.
*
* @author Andy Wilkinson
* @author Yogesh Lonkar
* @since 1.3.0
*/
@ConfigurationProperties(prefix = "spring.mongodb.embedded")
......@@ -38,6 +39,8 @@ public class EmbeddedMongoProperties {
*/
private String version = "2.6.10";
private Storage storage;
/**
* Comma-separated list of features to enable.
*/
......@@ -60,4 +63,55 @@ public class EmbeddedMongoProperties {
this.features = features;
}
public Storage getStorage() {
return this.storage;
}
public void setStorage(Storage storage) {
this.storage = storage;
}
public static class Storage {
/**
* Maximum size of the oplog in megabytes.
*/
private Integer oplogSize;
/**
* Name of the replica set.
*/
private String replSetName;
/**
* Directory used for data storage.
*/
private String databaseDir;
public Integer getOplogSize() {
return this.oplogSize;
}
public void setOplogSize(Integer oplogSize) {
this.oplogSize = oplogSize;
}
public String getReplSetName() {
return this.replSetName;
}
public void setReplSetName(String replSetName) {
this.replSetName = replSetName;
}
public String getDatabaseDir() {
return this.databaseDir;
}
public void setDatabaseDir(String databaseDir) {
this.databaseDir = databaseDir;
}
}
}
......@@ -16,10 +16,13 @@
package org.springframework.boot.autoconfigure.mongo.embedded;
import java.io.File;
import java.net.UnknownHostException;
import com.mongodb.CommandResult;
import com.mongodb.MongoClient;
import de.flapdoodle.embed.mongo.config.IMongodConfig;
import de.flapdoodle.embed.mongo.config.Storage;
import de.flapdoodle.embed.mongo.distribution.Feature;
import org.junit.After;
import org.junit.Test;
......@@ -34,6 +37,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.SocketUtils;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -112,6 +116,63 @@ public class EmbeddedMongoAutoConfigurationTests {
}
}
@Test
public void defaultStorageConfiguration() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0");
this.context.register(EmbeddedMongoAutoConfiguration.class,
MongoClientConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
Storage replication = this.context.getBean(IMongodConfig.class).replication();
assertThat(replication.getOplogSize()).isEqualTo(0);
assertThat(replication.getDatabaseDir()).isNull();
assertThat(replication.getReplSetName()).isNull();
}
@Test
public void mongoWritesToCustomDatabaseDir() {
File customDatabaseDir = new File("target/custom-database-dir");
FileSystemUtils.deleteRecursively(customDatabaseDir);
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0",
"spring.mongodb.embedded.storage.databaseDir="
+ customDatabaseDir.getPath());
this.context.register(EmbeddedMongoAutoConfiguration.class,
MongoClientConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(customDatabaseDir).isDirectory();
assertThat(customDatabaseDir.listFiles()).isNotEmpty();
}
@Test
public void customOpLogSizeIsAppliedToConfiguration() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0",
"spring.mongodb.embedded.storage.oplogSize=10");
this.context.register(EmbeddedMongoAutoConfiguration.class,
MongoClientConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(IMongodConfig.class).replication().getOplogSize())
.isEqualTo(10);
}
@Test
public void customReplicaSetNameIsAppliedToConfiguration() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0",
"spring.mongodb.embedded.storage.replSetName=testing");
this.context.register(EmbeddedMongoAutoConfiguration.class,
MongoClientConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(
this.context.getBean(IMongodConfig.class).replication().getReplSetName())
.isEqualTo("testing");
}
private void assertVersionConfiguration(String configuredVersion,
String expectedVersion) {
this.context = new AnnotationConfigApplicationContext();
......
......@@ -730,6 +730,9 @@ content into your application; rather pick only the properties that you need.
# EMBEDDED MONGODB ({sc-spring-boot-autoconfigure}/mongo/embedded/EmbeddedMongoProperties.{sc-ext}[EmbeddedMongoProperties])
spring.mongodb.embedded.features=SYNC_DELAY # Comma-separated list of features to enable.
spring.mongodb.embedded.storage.databaseDir= # Directory used for data storage.
spring.mongodb.embedded.storage.oplogSize= # Maximum size of the oplog in megabytes.
spring.mongodb.embedded.storage.replSetName= # Name of the replica set.
spring.mongodb.embedded.version=2.6.10 # Version of Mongo to use.
# REDIS ({sc-spring-boot-autoconfigure}/redis/RedisProperties.{sc-ext}[RedisProperties])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment