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 5a9e6b8b95281..233c3fd9eabd8 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java @@ -226,13 +226,14 @@ 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.allOf; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.startsWith; /** @@ -1733,23 +1734,13 @@ public static boolean indexExists(String index, Client client) { return getIndexResponse.getIndices().length > 0; } - public static void awaitIndexExists(String index) throws Exception { + public static void awaitIndexExists(String index) { 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( + public static void awaitIndexExists(String index, Client client) { + assertThat("wildcards not supported", index, allOf(not(Metadata.ALL), not(containsString("*")))); + safeGet( client.admin() .cluster() .prepareHealth(SAFE_AWAIT_TIMEOUT, index) diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java index fbe2e957fc73b..2d51303b1d939 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java @@ -6,7 +6,10 @@ */ package org.elasticsearch.xpack.monitoring; +import org.elasticsearch.action.support.IndicesOptions; +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.Index; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -22,7 +25,6 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThanOrEqualTo; -import static org.hamcrest.Matchers.instanceOf; @ClusterScope(scope = Scope.TEST, numDataNodes = 0, numClientNodes = 0) public class MultiNodesStatsTests extends MonitoringIntegTestCase { @@ -65,10 +67,7 @@ public void testMultipleNodes() throws Exception { nodes += n; final int nbNodes = nodes; - assertBusy(() -> { - assertThat(cluster().size(), equalTo(nbNodes)); - assertNoTimeout(clusterAdmin().prepareHealth(TEST_REQUEST_TIMEOUT).setWaitForNodes(Integer.toString(nbNodes)).get()); - }); + assertNoTimeout(safeGet(clusterAdmin().prepareHealth(TEST_REQUEST_TIMEOUT).setWaitForNodes(Integer.toString(nbNodes)).execute())); enableMonitoringCollection(); waitForMonitoringIndices(); @@ -83,11 +82,11 @@ public void testMultipleNodes() throws Exception { .addAggregation(AggregationBuilders.terms("nodes_ids").field("node_stats.node_id")), response -> { for (Aggregation aggregation : response.getAggregations()) { - assertThat(aggregation, instanceOf(StringTerms.class)); - assertThat(((StringTerms) aggregation).getBuckets().size(), equalTo(nbNodes)); + final var stringTerms = asInstanceOf(StringTerms.class, aggregation); + assertThat(stringTerms.getBuckets().size(), equalTo(nbNodes)); for (String nodeName : internalCluster().getNodeNames()) { - StringTerms.Bucket bucket = ((StringTerms) aggregation).getBucketByKey(getNodeId(nodeName)); + StringTerms.Bucket bucket = stringTerms.getBucketByKey(getNodeId(nodeName)); // At least 1 doc must exist per node, but it can be more than 1 // because the first node may have already collected many node stats documents // whereas the last node just started to collect node stats. @@ -98,4 +97,24 @@ public void testMultipleNodes() throws Exception { ); }); } + + private void waitForMonitoringIndices() throws Exception { + final var indexNameExpressionResolver = internalCluster().getCurrentMasterNodeInstance(IndexNameExpressionResolver.class); + final var indicesOptions = IndicesOptions.builder() + .wildcardOptions(IndicesOptions.WildcardOptions.builder().allowEmptyExpressions(true)) + .build(); + awaitClusterState(cs -> { + final var indices = indexNameExpressionResolver.concreteIndices(cs, indicesOptions, ".monitoring-es-*"); + if (indices.length == 0) { + return false; + } + for (Index index : indices) { + final var indexRoutingTable = cs.routingTable().index(index); + if (indexRoutingTable.allPrimaryShardsActive() == false) { + return false; + } + } + return true; + }); + } } 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 6c301032c1b94..eee7671c118b0 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 @@ -124,10 +124,6 @@ protected void deleteMonitoringIndices() { assertAcked(client().admin().indices().prepareDelete(ALL_MONITORING_INDICES)); } - protected void ensureMonitoringIndicesYellow() { - ensureYellowAndNoInitializingShards(".monitoring-es-*"); - } - protected List> monitoringWatches() { final ClusterService clusterService = clusterService(); @@ -150,11 +146,6 @@ protected void assertTemplateInstalled(String name) { assertTrue("failed to find a template matching [" + name + "]", found); } - protected void waitForMonitoringIndices() throws Exception { - awaitIndexExists(ALL_MONITORING_INDICES); - assertBusy(this::ensureMonitoringIndicesYellow); - } - protected void enableMonitoringCollection() { updateClusterSettings(Settings.builder().put(MonitoringService.ENABLED.getKey(), true)); }