diff --git a/build-tools/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchNode.java b/build-tools/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchNode.java index a30c551040612..3db000b1586f2 100644 --- a/build-tools/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchNode.java +++ b/build-tools/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchNode.java @@ -1409,6 +1409,12 @@ public boolean isProcessAlive() { return esProcess.isAlive(); } + @Internal + public long getPid() { + requireNonNull(esProcess, "Can't get pid of " + this + " as it's not started. Does the task have `useCluster` ?"); + return esProcess.pid(); + } + void waitForAllConditions() { waitForConditions(waitConditions, System.currentTimeMillis(), NODE_UP_TIMEOUT_UNIT.toMillis(NODE_UP_TIMEOUT) + // Installing plugins at config time and loading them when nods start requires additional time we need to diff --git a/test/external-modules/die-with-dignity/build.gradle b/test/external-modules/die-with-dignity/build.gradle index 9b93c30a7794e..7aa103edc7d21 100644 --- a/test/external-modules/die-with-dignity/build.gradle +++ b/test/external-modules/die-with-dignity/build.gradle @@ -17,6 +17,7 @@ tasks.named("javaRestTest").configure { systemProperty 'tests.security.manager', 'false' systemProperty 'tests.system_call_filter', 'false' nonInputProperties.systemProperty 'log', "${-> testClusters.javaRestTest.singleNode().getServerLog()}" + nonInputProperties.systemProperty 'es.pid', "${ -> testClusters.javaRestTest.singleNode().getPid() }" systemProperty 'runtime.java.home', BuildParams.runtimeJavaHome } diff --git a/test/external-modules/die-with-dignity/src/javaRestTest/java/org/elasticsearch/qa/die_with_dignity/DieWithDignityIT.java b/test/external-modules/die-with-dignity/src/javaRestTest/java/org/elasticsearch/qa/die_with_dignity/DieWithDignityIT.java index ad43bbd1e2e86..56ade9e53fb9e 100644 --- a/test/external-modules/die-with-dignity/src/javaRestTest/java/org/elasticsearch/qa/die_with_dignity/DieWithDignityIT.java +++ b/test/external-modules/die-with-dignity/src/javaRestTest/java/org/elasticsearch/qa/die_with_dignity/DieWithDignityIT.java @@ -8,7 +8,6 @@ package org.elasticsearch.qa.die_with_dignity; -import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.client.Request; import org.elasticsearch.core.PathUtils; import org.elasticsearch.test.rest.ESRestTestCase; @@ -22,45 +21,17 @@ import java.util.Iterator; import java.util.List; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.not; - -@LuceneTestCase.AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/77282") public class DieWithDignityIT extends ESRestTestCase { public void testDieWithDignity() throws Exception { // there should be an Elasticsearch process running with the die.with.dignity.test system property - { - final String jpsPath = PathUtils.get(System.getProperty("runtime.java.home"), "bin/jps").toString(); - final Process process = new ProcessBuilder().command(jpsPath, "-v").start(); - - boolean found = false; - try (InputStream is = process.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"))) { - String line; - while ((line = in.readLine()) != null) { - if (line.contains("-Ddie.with.dignity.test=true")) { - found = true; - break; - } - } - } - assertTrue(found); - } + String esPid = System.getProperty("es.pid"); + assertTrue(javaPidExists(esPid)); expectThrows(IOException.class, () -> client().performRequest(new Request("GET", "/_die_with_dignity"))); // the Elasticsearch process should die and disappear from the output of jps - assertBusy(() -> { - final String jpsPath = PathUtils.get(System.getProperty("runtime.java.home"), "bin/jps").toString(); - final Process process = new ProcessBuilder().command(jpsPath, "-v").start(); - - try (InputStream is = process.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"))) { - String line; - while ((line = in.readLine()) != null) { - assertThat(line, line, not(containsString("-Ddie.with.dignity.test=true"))); - } - } - }); + assertBusy(() -> { assertFalse(javaPidExists(esPid)); }); // parse the logs and ensure that Elasticsearch died with the expected cause final List lines = Files.readAllLines(PathUtils.get(System.getProperty("log"))); @@ -95,6 +66,22 @@ public void testDieWithDignity() throws Exception { } } + private boolean javaPidExists(String expectedPid) throws IOException { + final String jpsPath = PathUtils.get(System.getProperty("runtime.java.home"), "bin/jps").toString(); + final Process process = new ProcessBuilder().command(jpsPath, "-v").start(); + + try (InputStream is = process.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"))) { + String line; + while ((line = in.readLine()) != null) { + String foundPid = line.split(" ")[0]; + if (expectedPid.equals(foundPid)) { + return true; + } + } + } + return false; + } + private boolean containsAll(String line, String... subStrings) { for (String subString : subStrings) { if (line.matches(subString) == false) {