Skip to content

Commit f862c30

Browse files
Pass COMPUTERNAME env var to elasticsearch.bat (#45763)
* Pass COMPUTERNAME env var to elasticsearch.bat When we run bin/elasticsearch with bash, we get a $HOSTNAME builtin that contains the hostname of the machine the script is running on. When there's no provided nodename, Elasticsearch uses the HOSTNAME to create a nodename. On Windows, Powershell provides a $COMPUTERNAME variable for the same purpose. CMD.EXE provides the same thing, except it's called %COMPUTERNAME%. bin/elasticsearch.bat sets $HOSTNAME to the value of $COMPUTERNAME. However, when testclusters invokes bin/elasticsearch.bat, the COMPUTERNAME variable doesn't get passed in, leaving HOSTNAME null and breaking an integration test on Windows. This commit sets COMPUTERNAME in the environment so that our tests get the value that Elasticsearch would have when bin/elasticsearch.bat is invoked from the shell. * Add null check to protect in non-Windows case What good is it a developer to gain the whole Windows if they forfeit their Unix? The value that fixes things on Windows is null on Linux/Darwin, so let's null-check it. * Override system hostnames for testclusters Rather than relying on variable system behavior, let's just override HOSTNAME and COMPUTERNAME and test for correct values in the integration test that was originally failing. * Rename constants for clarity Since we are setting HOSTNAME and COMPUTERNAME regardless of whether the tests are running on Windows or Linux, we shouldn't imply that constants are only used in one case or the other.
1 parent 3f96863 commit f862c30

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

buildSrc/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchNode.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public class ElasticsearchNode implements TestClusterConfiguration {
107107
"is a pre-release version of Elasticsearch",
108108
"max virtual memory areas vm.max_map_count"
109109
);
110+
private static final String HOSTNAME_OVERRIDE = "LinuxDarwinHostname";
111+
private static final String COMPUTERNAME_OVERRIDE = "WindowsComputername";
110112

111113
private final String path;
112114
private final String name;
@@ -643,6 +645,10 @@ private Map<String, String> getESEnvironment() {
643645
// Windows requires this as it defaults to `c:\windows` despite ES_TMPDIR
644646
defaultEnv.put("TMP", tmpDir.toString());
645647

648+
// Override the system hostname variables for testing
649+
defaultEnv.put("HOSTNAME", HOSTNAME_OVERRIDE);
650+
defaultEnv.put("COMPUTERNAME", COMPUTERNAME_OVERRIDE);
651+
646652
Set<String> commonKeys = new HashSet<>(environment.keySet());
647653
commonKeys.retainAll(defaultEnv.keySet());
648654
if (commonKeys.isEmpty() == false) {

qa/unconfigured-node-name/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,4 @@ testClusters.integTest {
3030
integTest.runner {
3131
nonInputProperties.systemProperty 'tests.logfile',
3232
"${ -> testClusters.integTest.singleNode().getServerLog() }"
33-
// https://github.com/elastic/elasticsearch/issues/44656
34-
onlyIf { OS.WINDOWS.equals(OS.current()) == false }
3533
}

qa/unconfigured-node-name/src/test/java/org/elasticsearch/unconfigured_node_name/JsonLogsFormatAndParseIT.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,22 @@
3030
import java.security.AccessController;
3131
import java.security.PrivilegedAction;
3232

33-
import static org.hamcrest.Matchers.not;
33+
import static org.hamcrest.Matchers.equalTo;
3434

3535
public class JsonLogsFormatAndParseIT extends JsonLogsIntegTestCase {
36+
private static final String OS_NAME = System.getProperty("os.name");
37+
private static final boolean WINDOWS = OS_NAME.startsWith("Windows");
38+
39+
// These match the values defined in org.elasticsearch.gradle.testclusters.ElasticsearchNode
40+
private static final String COMPUTERNAME = "WindowsComputername";
41+
private static final String HOSTNAME = "LinuxDarwinHostname";
42+
3643
@Override
3744
protected Matcher<String> nodeNameMatcher() {
38-
return not("");
45+
if (WINDOWS) {
46+
return equalTo(COMPUTERNAME);
47+
}
48+
return equalTo(HOSTNAME);
3949
}
4050

4151
@Override

0 commit comments

Comments
 (0)