Expand config variables from system properties if not found as environment variables.

Allow '.' in config variable names: REDIS.HOST as well as REDIS_HOST

Signed-off-by: Jarrod Ribble <jarrod.ribble@gmail.com>
pull/3048/head
Jarrod Ribble 4 years ago
parent 9579ee735a
commit 4ee9e7a747

@ -104,11 +104,14 @@ public class ConfigSupport {
}
private String resolveEnvParams(String content) {
Pattern pattern = Pattern.compile("\\$\\{(\\w+(:-.+?)?)\\}");
Pattern pattern = Pattern.compile("\\$\\{([\\w\\.]+(:-.+?)?)\\}");
Matcher m = pattern.matcher(content);
while (m.find()) {
String[] parts = m.group(1).split(":-");
String v = System.getenv(parts[0]);
if (v == null) {
v = System.getProperty(parts[0]);
}
if (v != null) {
content = content.replace(m.group(), v);
} else if (parts.length == 2) {

@ -26,6 +26,14 @@ public class ConfigSupportTest {
assertEquals("redis://1.1.1.1", config.getAddress());
}
@Test
public void testParsingProperty() throws IOException {
mockHostProperty("1.1.1.1");
SingleServerConfig config = mkConfig("${REDIS_URI}");
assertEquals("redis://1.1.1.1", config.getAddress());
}
@Test
public void testParsingEnv2() throws IOException {
mockHostPortEnv("1.1.1.1", "6379");
@ -34,9 +42,18 @@ public class ConfigSupportTest {
assertEquals("redis://1.1.1.1:6379", config.getAddress());
}
@Test
public void testParsingProperty2() throws IOException {
mockHostPortProperty("1.1.1.1", "6379");
SingleServerConfig config = mkConfig("${REDIS_HOST}:${REDIS_PORT}");
assertEquals("redis://1.1.1.1:6379", config.getAddress());
}
@Test
public void testParsingEnv_envMissing() throws IOException {
mockHostEnv(null);
mockHostProperty(null);
final SingleServerConfig config = mkConfig("${REDIS_URI}");
assertEquals("redis://${REDIS_URI}", config.getAddress());
@ -50,6 +67,14 @@ public class ConfigSupportTest {
assertEquals("redis://11.0.0.1", config.getAddress());
}
@Test
public void testParsingDefault_propertyPresent() throws IOException {
mockHostProperty("11.0.0.1");
SingleServerConfig config = mkConfig("${REDIS_URI:-10.0.0.1}");
assertEquals("redis://11.0.0.1", config.getAddress());
}
@Test
public void testParsingDefault_envPresent2() throws IOException {
mockHostPortEnv("11.0.0.1", "1234");
@ -58,9 +83,18 @@ public class ConfigSupportTest {
assertEquals("redis://11.0.0.1:1234", config.getAddress());
}
@Test
public void testParsingDefault_propertyPresent2() throws IOException {
mockHostPortProperty("11.0.0.1", "1234");
SingleServerConfig config = mkConfig("${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:-6379}");
assertEquals("redis://11.0.0.1:1234", config.getAddress());
}
@Test
public void testParsingDefault_envMissing() throws IOException {
mockHostEnv(null);
mockHostProperty(null);
SingleServerConfig config = mkConfig("${REDIS_URI:-10.0.0.1}");
assertEquals("redis://10.0.0.1", config.getAddress());
@ -69,11 +103,28 @@ public class ConfigSupportTest {
@Test
public void testParsingDefault_envMissing2() throws IOException {
mockHostPortEnv(null, null);
mockHostPortProperty(null, null);
SingleServerConfig config = mkConfig("${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:-6379}");
assertEquals("redis://127.0.0.1:6379", config.getAddress());
}
@Test
public void testParsingDefaultPeriod_propertyPresent2() throws IOException {
mockHostPortPropertyPeriod("11.0.0.1", "1234");
SingleServerConfig config = mkConfig("${REDIS.HOST:-127.0.0.1}:${REDIS.PORT:-6379}");
assertEquals("redis://11.0.0.1:1234", config.getAddress());
}
@Test
public void testParsingDefaultPeriod_envMissing() throws IOException {
mockHostPortProperty(null, null);
SingleServerConfig config = mkConfig("${REDIS.HOST:-127.0.0.1}:${REDIS.PORT:-6379}");
assertEquals("redis://127.0.0.1:6379", config.getAddress());
}
private SingleServerConfig mkConfig(String authorityValue) throws IOException {
String config = "singleServerConfig:\n address: redis://" + authorityValue;
return new ConfigSupport().fromYAML(config, Config.class).getSingleServerConfig();
@ -88,6 +139,15 @@ public class ConfigSupportTest {
};
}
private void mockHostProperty(String value) {
new MockUp<System>() {
@Mock
String getProperty(String name) {
return value;
}
};
}
private void mockHostPortEnv(String host, String port) {
new MockUp<System>() {
@Mock
@ -104,4 +164,36 @@ public class ConfigSupportTest {
};
}
private void mockHostPortProperty(String host, String port) {
new MockUp<System>() {
@Mock
String getProperty(String name) {
switch (name) {
case "REDIS_HOST":
return host;
case "REDIS_PORT":
return port;
default:
return null;
}
}
};
}
private void mockHostPortPropertyPeriod(String host, String port) {
new MockUp<System>() {
@Mock
String getProperty(String name) {
switch (name) {
case "REDIS.HOST":
return host;
case "REDIS.PORT":
return port;
default:
return null;
}
}
};
}
}

Loading…
Cancel
Save