From 5a503768eeda287bd1d3ec98203a6db8d65e37c9 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 17 Feb 2021 18:10:05 +0100 Subject: [PATCH] Manually trigger local exporter to open a bulk in some monitor tests. Change tests to use monitor bulk api on elected master node before verifying watcher index exists. Sometimes the monitor service on the elected master doesn't yet export monitor documents resulting in tests using the `ensureInitialLocalResources(...)` method to fail. Cluster alerts watcher are only installed when local exporter tries to resolve local bulk. Relates to #66586 --- ...sportMonitoringMigrateAlertsActionTests.java | 17 +++++++++++++++++ .../exporter/local/LocalExporterIntegTests.java | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java index fb7b8a62802f9..1a5099937752b 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java @@ -29,12 +29,16 @@ import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.http.MockRequest; import org.elasticsearch.test.http.MockResponse; import org.elasticsearch.test.http.MockWebServer; import org.elasticsearch.test.junit.annotations.TestLogging; import org.elasticsearch.xpack.core.XPackSettings; +import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkAction; +import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkRequest; +import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkResponse; import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsAction; import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsRequest; import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsResponse; @@ -47,6 +51,7 @@ import org.elasticsearch.xpack.monitoring.exporter.ClusterAlertsUtil; import org.elasticsearch.xpack.monitoring.exporter.http.HttpExporter; import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter; +import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporterIntegTests; import org.elasticsearch.xpack.monitoring.test.MonitoringIntegTestCase; import org.junit.After; import org.junit.Before; @@ -464,6 +469,18 @@ public void testRemoteAlertsRemoteDisallowsWatcher() throws Exception { } private void ensureInitialLocalResources() throws Exception { + // Should trigger setting up alert watches via LocalExporter#openBulk(...) and + // then eventually to LocalExporter#setupIfElectedMaster(...) + // Sometimes this last method doesn't install watches, because elected master node doesn't export monitor documents. + // and then these assertions here fail. + { + MonitoringBulkRequest request = new MonitoringBulkRequest(); + request.add(LocalExporterIntegTests.createMonitoringBulkDoc()); + String masterNode = internalCluster().getMasterName(); + MonitoringBulkResponse response = client(masterNode).execute(MonitoringBulkAction.INSTANCE, request).actionGet(); + assertThat(response.status(), equalTo(RestStatus.OK)); + } + waitForWatcherIndices(); assertBusy(() -> { assertThat(indexExists(".monitoring-*"), is(true)); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java index de183f30a0fa6..2eed872d6878d 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java @@ -301,7 +301,7 @@ private void checkMonitoringDocs() { } } - private static MonitoringBulkDoc createMonitoringBulkDoc() throws IOException { + public static MonitoringBulkDoc createMonitoringBulkDoc() throws IOException { final MonitoredSystem system = randomFrom(BEATS, KIBANA, LOGSTASH); final XContentType xContentType = randomFrom(XContentType.values()); final BytesReference source;