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:
parent
be2ba53fca
commit
115f983827
|
@ -602,7 +602,8 @@ public final class Settings implements ToXContent {
|
|||
|
||||
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() {
|
||||
@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.
|
||||
*
|
||||
|
|
|
@ -62,14 +62,18 @@ public class SettingsTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testReplacePropertiesPlaceholderByEnvironmentVariables() {
|
||||
final Map.Entry<String, String> 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() {
|
||||
|
|
Loading…
Reference in New Issue