Fix env. var placeholder test so it's reproducible

This commit modifies the settings test for environment variables
placeholders so that it is reproducible. The underlying issue is that
the set of environment variables from system to system can vary, and
this means that's we can never be sure that a failing test will be
reproducible. This commit simplifies this test to not rely on external
forces that could influence reproducibility.

Relates #18501
This commit is contained in:
Jason Tedor 2016-05-20 17:11:37 -04:00
parent be2ba53fca
commit 115f983827
2 changed files with 18 additions and 8 deletions

View File

@ -602,7 +602,8 @@ public final class Settings implements ToXContent {
private final Map<String, String> map = new LinkedHashMap<>(); private final Map<String, String> map = new LinkedHashMap<>();
private Builder() { // visible for testing
Builder() {
} }
@ -964,7 +965,7 @@ public final class Settings implements ToXContent {
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new PropertyPlaceholder.PlaceholderResolver() { PropertyPlaceholder.PlaceholderResolver placeholderResolver = new PropertyPlaceholder.PlaceholderResolver() {
@Override @Override
public String resolvePlaceholder(String placeholderName) { public String resolvePlaceholder(String placeholderName) {
final String value = System.getenv(placeholderName); final String value = getenv(placeholderName);
if (value != null) { if (value != null) {
return value; return value;
} }
@ -1000,6 +1001,11 @@ public final class Settings implements ToXContent {
return this; return this;
} }
// visible for testing
String getenv(String placeholderName) {
return System.getenv(placeholderName);
}
/** /**
* Checks that all settings in the builder start with the specified prefix. * Checks that all settings in the builder start with the specified prefix.
* *

View File

@ -62,14 +62,18 @@ public class SettingsTests extends ESTestCase {
} }
public void testReplacePropertiesPlaceholderByEnvironmentVariables() { public void testReplacePropertiesPlaceholderByEnvironmentVariables() {
final Map.Entry<String, String> entry = randomSubsetOf(1, System.getenv().entrySet()).get(0); final String hostname = randomAsciiOfLength(16);
assertNotNull(entry.getValue()); final Settings.Builder builder = new Settings.Builder() {
@Override
final Settings implicitEnvSettings = Settings.builder() protected String getenv(String placeholderName) {
.put("setting1", "${" + entry.getKey() + "}") return "HOSTNAME".equals(placeholderName) ? hostname : null;
}
};
final Settings implicitEnvSettings = builder
.put("setting1", "${HOSTNAME}")
.replacePropertyPlaceholders() .replacePropertyPlaceholders()
.build(); .build();
assertThat(implicitEnvSettings.get("setting1"), equalTo(entry.getValue())); assertThat(implicitEnvSettings.get("setting1"), equalTo(hostname));
} }
public void testReplacePropertiesPlaceholderIgnoresPrompt() { public void testReplacePropertiesPlaceholderIgnoresPrompt() {