This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch 3.27.x in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit 3de02caf365cd24581e3ff99b393926f098418a4 Author: Gaƫlle Fournier <[email protected]> AuthorDate: Mon Feb 23 08:24:37 2026 +0100 fix: Add ES cluster health check before running test fixes #8321 Co-authored-by: Jiri Ondrusek <[email protected]> --- .../elasticsearch/it/ElasticsearchTest.java | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/integration-tests-jvm/elasticsearch/src/test/java/org/apache/camel/quarkus/component/elasticsearch/it/ElasticsearchTest.java b/integration-tests-jvm/elasticsearch/src/test/java/org/apache/camel/quarkus/component/elasticsearch/it/ElasticsearchTest.java index 59559b8e88..9d3f4c23eb 100644 --- a/integration-tests-jvm/elasticsearch/src/test/java/org/apache/camel/quarkus/component/elasticsearch/it/ElasticsearchTest.java +++ b/integration-tests-jvm/elasticsearch/src/test/java/org/apache/camel/quarkus/component/elasticsearch/it/ElasticsearchTest.java @@ -16,6 +16,13 @@ */ package org.apache.camel.quarkus.component.elasticsearch.it; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.Objects; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -24,7 +31,11 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.RestAssured; import io.restassured.http.ContentType; import org.awaitility.Awaitility; +import org.awaitility.core.ConditionTimeoutException; +import org.eclipse.microprofile.config.ConfigProvider; +import org.jboss.logging.Logger; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -33,6 +44,13 @@ import static org.hamcrest.Matchers.is; @QuarkusTest @QuarkusTestResource(ElasticsearchTestResource.class) class ElasticsearchTest { + private static final Logger LOG = Logger.getLogger(ElasticsearchTest.class); + + @BeforeEach + public void beforeEach() throws ConditionTimeoutException { + // Ensure the Elasticsearch cluster is ready before each test + waitClusterReady(); + } @AfterEach public void afterEach() { @@ -352,6 +370,69 @@ class ElasticsearchTest { }); } + /** + * Queries the Elasticsearch cluster health status and waits until it's green or yellow. + * Retries with Awaitility until the cluster is ready. + * + * @throws ConditionTimeoutException if the request fails after all retries + */ + private void waitClusterReady() throws ConditionTimeoutException { + String hostAddresses = ConfigProvider.getConfig().getValue("camel.component.elasticsearch.host-addresses", + String.class); + String username = ConfigProvider.getConfig().getValue("camel.component.elasticsearch.user", String.class); + String password = ConfigProvider.getConfig().getValue("camel.component.elasticsearch.password", String.class); + + Awaitility.await() + .pollInterval(500, TimeUnit.MILLISECONDS) + .atMost(10, TimeUnit.SECONDS) + .until(() -> { + try { + URL url = new URL(String.format("http://%s/_cluster/health", hostAddresses)); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + // Set up Basic Authentication + String auth = String.format("%s:%s", username, password); + String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8)); + connection.setRequestProperty("Authorization", "Basic " + encodedAuth); + connection.setRequestMethod("GET"); + connection.setConnectTimeout(5000); + connection.setReadTimeout(5000); + + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(connection.getInputStream()))) { + StringBuilder response = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + String healthJson = response.toString(); + + // Check if cluster status is green or yellow + if (healthJson.contains("\"status\":\"green\"") + || healthJson.contains("\"status\":\"yellow\"")) { + LOG.info("Cluster health is ready: " + healthJson); + return healthJson; + } else { + LOG.info("Cluster not ready yet, current status: " + + healthJson); + return null; + } + } + } else { + LOG.info("Cluster health check returned code: " + responseCode + + ", retrying..."); + return null; + } + } catch (Exception e) { + LOG.info("Failed to query cluster health: " + e.getMessage() + + ", retrying..."); + return null; + } + }, Objects::nonNull); + } + /** * This method returns array of component names used in test routes. * It can be handy e.g. for testing quarkus managed elasticsearch client.
