diff --git a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java
index 10e0d997bf272..5615e53ddcc29 100644
--- a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java
+++ b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java
@@ -748,7 +748,7 @@ public void testDeletionOfFailingToRecoverIndexShouldStopRestore() throws Except
logger.info("--> wait for the index to appear");
// that would mean that recovery process started and failing
- safeGet(clusterAdmin().prepareHealth(SAFE_AWAIT_TIMEOUT, "test-idx").execute());
+ awaitIndexExists("test-idx");
logger.info("--> delete index");
cluster().wipeIndices("test-idx");
diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java
index 5c033558daaee..5a9e6b8b95281 100644
--- a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java
+++ b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java
@@ -226,6 +226,8 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoTimeout;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
@@ -1731,6 +1733,31 @@ public static boolean indexExists(String index, Client client) {
return getIndexResponse.getIndices().length > 0;
}
+ public static void awaitIndexExists(String index) throws Exception {
+ awaitIndexExists(index, client());
+ }
+
+ public static void awaitIndexExists(String index, Client client) throws Exception {
+ if (Regex.isSimpleMatchPattern(index) || Metadata.ALL.equals(index)) {
+ assertBusy(() -> {
+ final var response = clusterHealthWithIndex(index, client);
+ assertThat(response.getIndices(), not(anEmptyMap()));
+ });
+ } else {
+ clusterHealthWithIndex(index, client);
+ }
+ }
+
+ private static ClusterHealthResponse clusterHealthWithIndex(String index, Client client) {
+ return safeGet(
+ client.admin()
+ .cluster()
+ .prepareHealth(SAFE_AWAIT_TIMEOUT, index)
+ .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED)
+ .execute()
+ );
+ }
+
/**
* Syntactic sugar for enabling allocation for indices
*/
diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java
index c37bb006d180d..6cbe5128929c6 100644
--- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java
+++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java
@@ -118,7 +118,7 @@ public void testAutoFollow() throws Exception {
createLeaderIndex("metrics-201901", leaderIndexSettings);
createLeaderIndex("logs-201901", leaderIndexSettings);
- assertLongBusy(() -> { assertTrue(ESIntegTestCase.indexExists("copy-logs-201901", followerClient())); });
+ ESIntegTestCase.awaitIndexExists("copy-logs-201901", followerClient());
createLeaderIndex("transactions-201901", leaderIndexSettings);
assertLongBusy(() -> {
AutoFollowStats autoFollowStats = getAutoFollowStats();
diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java
index ea2c30fac00be..ab8ed46b9bc04 100644
--- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java
+++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java
@@ -173,7 +173,7 @@ public void onFailure(Exception e) {
// the shrink index generated in the first attempt must've been deleted!
assertBusy(() -> assertFalse(indexExists(firstAttemptShrinkIndexName[0])));
- assertBusy(() -> assertTrue(indexExists(secondCycleShrinkIndexName[0])), 30, TimeUnit.SECONDS);
+ awaitIndexExists(secondCycleShrinkIndexName[0]);
// at this point, the second shrink attempt was executed and the manged index is looping into the `shrunk-shards-allocated` step as
// waiting for the huge numbers of replicas for the shrunk index to allocate. this will never happen, so let's unblock this
diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java
index 24bff8bbb2915..aa8b2d69dbae3 100644
--- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java
+++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java
@@ -179,7 +179,7 @@ public void testSingleNodeCluster() throws Exception {
ClusterState clusterState = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
RoutingNode routingNodeEntry1 = clusterState.getRoutingNodes().node(node1);
assertThat(routingNodeEntry1.numberOfShardsWithState(STARTED), equalTo(1));
- assertBusy(() -> { assertTrue(indexExists("test")); });
+ awaitIndexExists("test");
IndexLifecycleService indexLifecycleService = internalCluster().getInstance(IndexLifecycleService.class, server_1);
assertThat(indexLifecycleService.getScheduler().jobCount(), equalTo(1));
assertNotNull(indexLifecycleService.getScheduledJob());
@@ -420,7 +420,7 @@ public void testMasterDedicatedDataDedicated() throws Exception {
RoutingNode routingNodeEntry1 = clusterState.getRoutingNodes().node(node2);
assertThat(routingNodeEntry1.numberOfShardsWithState(STARTED), equalTo(1));
- assertBusy(() -> assertTrue(indexExists("test")));
+ awaitIndexExists("test");
assertBusy(() -> {
LifecycleExecutionState lifecycleState = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT)
.get()
diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeDataFrameAnalyticsIntegTestCase.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeDataFrameAnalyticsIntegTestCase.java
index 432215ce5c3f9..d4a577393b7fd 100644
--- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeDataFrameAnalyticsIntegTestCase.java
+++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeDataFrameAnalyticsIntegTestCase.java
@@ -367,7 +367,7 @@ protected static void assertThatAuditMessagesMatch(String configId, String... ex
// Make sure we wrote to the audit
// Since calls to write the AbstractAuditor are sent and forgot (async) we could have returned from the start,
// finished the job (as this is a very short analytics job), all without the audit being fully written.
- assertBusy(() -> assertTrue(indexExists(NotificationsIndex.NOTIFICATIONS_INDEX)));
+ awaitIndexExists(NotificationsIndex.NOTIFICATIONS_INDEX);
@SuppressWarnings("unchecked")
Matcher[] itemMatchers = Arrays.stream(expectedAuditMessagePrefixes).map(Matchers::startsWith).toArray(Matcher[]::new);
diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java
index 92e18178a667b..6c301032c1b94 100644
--- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java
+++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java
@@ -9,7 +9,6 @@
import org.elasticsearch.analysis.common.CommonAnalysisPlugin;
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
import org.elasticsearch.cluster.service.ClusterService;
-import org.elasticsearch.common.Strings;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.CountDown;
@@ -33,7 +32,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
-import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
@@ -157,17 +155,6 @@ protected void waitForMonitoringIndices() throws Exception {
assertBusy(this::ensureMonitoringIndicesYellow);
}
- protected void awaitIndexExists(final String index) throws Exception {
- assertBusy(() -> assertIndicesExists(index), 30, TimeUnit.SECONDS);
- }
-
- private void assertIndicesExists(String... indices) {
- logger.trace("checking if index exists [{}]", Strings.arrayToCommaDelimitedString(indices));
- for (String index : indices) {
- assertThat(indexExists(index), is(true));
- }
- }
-
protected void enableMonitoringCollection() {
updateClusterSettings(Settings.builder().put(MonitoringService.ENABLED.getKey(), true));
}