From 115f983827c0c29652bd444c072b658b76651317 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 20 May 2016 17:11:37 -0400 Subject: [PATCH] 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 --- .../elasticsearch/common/settings/Settings.java | 10 ++++++++-- .../common/settings/SettingsTests.java | 16 ++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/settings/Settings.java b/core/src/main/java/org/elasticsearch/common/settings/Settings.java index 7a335aa1f32..2ceafc376c5 100644 --- a/core/src/main/java/org/elasticsearch/common/settings/Settings.java +++ b/core/src/main/java/org/elasticsearch/common/settings/Settings.java @@ -602,7 +602,8 @@ public final class Settings implements ToXContent { private final Map 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() { @Override public String resolvePlaceholder(String placeholderName) { - final String value = System.getenv(placeholderName); + final String value = getenv(placeholderName); if (value != null) { return value; } @@ -1000,6 +1001,11 @@ public final class Settings implements ToXContent { 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. * diff --git a/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java b/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java index fe7dcda3b25..5b3a698d328 100644 --- a/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java +++ b/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java @@ -62,14 +62,18 @@ public class SettingsTests extends ESTestCase { } public void testReplacePropertiesPlaceholderByEnvironmentVariables() { - final Map.Entry entry = randomSubsetOf(1, System.getenv().entrySet()).get(0); - assertNotNull(entry.getValue()); - - final Settings implicitEnvSettings = Settings.builder() - .put("setting1", "${" + entry.getKey() + "}") + final String hostname = randomAsciiOfLength(16); + final Settings.Builder builder = new Settings.Builder() { + @Override + protected String getenv(String placeholderName) { + return "HOSTNAME".equals(placeholderName) ? hostname : null; + } + }; + final Settings implicitEnvSettings = builder + .put("setting1", "${HOSTNAME}") .replacePropertyPlaceholders() .build(); - assertThat(implicitEnvSettings.get("setting1"), equalTo(entry.getValue())); + assertThat(implicitEnvSettings.get("setting1"), equalTo(hostname)); } public void testReplacePropertiesPlaceholderIgnoresPrompt() {