From 63e722040a5f20ab6c58b5ffc7dd19443ef1bc35 Mon Sep 17 00:00:00 2001 From: jaymode Date: Fri, 28 Feb 2020 09:45:00 -0700 Subject: [PATCH 1/4] Make watch history indices hidden This commit updates the template used for watch history indices with hidden index setting so that new indices will be created as hidden. Additionally, some failing tests were encountered where a search would find the documents in the history index but a subsequent search would fail to find the documents. This is most likely due to different refresh times between the primary and replica shards. The failures were resolved by using an assertsBusy to retrieve the documents. Relates #50251 --- .../metadata/MetaDataCreateIndexService.java | 1 - .../WatcherIndexTemplateRegistryField.java | 3 ++- .../main/resources/watch-history-no-ilm.json | 1 + .../src/main/resources/watch-history.json | 1 + .../actions/email/EmailAttachmentTests.java | 6 +++-- .../webhook/WebhookHttpsIntegrationTests.java | 12 +++++++-- .../history/HistoryActionConditionTests.java | 25 +++++++++++++------ .../HistoryTemplateEmailMappingsTests.java | 25 +++++++++++++------ ...storyTemplateIndexActionMappingsTests.java | 16 ++++++++---- .../test/integration/WatchMetadataTests.java | 6 +++-- 10 files changed, 67 insertions(+), 29 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java index 9bac4976be212..5d9d71ba06823 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java @@ -118,7 +118,6 @@ public class MetaDataCreateIndexService { * These index patterns will be converted to hidden indices, at which point they should be removed from this list. */ private static final CharacterRunAutomaton DOT_INDICES_EXCLUSIONS = new CharacterRunAutomaton(Regex.simpleMatchToAutomaton( - ".watch-history-*", ".data-frame-notifications-*", ".transform-notifications-*" )); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherIndexTemplateRegistryField.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherIndexTemplateRegistryField.java index 9eaf0237ffdc1..2c3442956d113 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherIndexTemplateRegistryField.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherIndexTemplateRegistryField.java @@ -15,8 +15,9 @@ public final class WatcherIndexTemplateRegistryField { // version 8: fix slack attachment property not to be dynamic, causing field type issues // version 9: add a user field defining which user executed the watch // version 10: add support for foreach path in actions + // version 11: watch history indices are hidden // Note: if you change this, also inform the kibana team around the watcher-ui - public static final int INDEX_TEMPLATE_VERSION = 10; + public static final int INDEX_TEMPLATE_VERSION = 11; public static final String HISTORY_TEMPLATE_NAME = ".watch-history-" + INDEX_TEMPLATE_VERSION; public static final String HISTORY_TEMPLATE_NAME_NO_ILM = ".watch-history-no-ilm-" + INDEX_TEMPLATE_VERSION; public static final String TRIGGERED_TEMPLATE_NAME = ".triggered_watches"; diff --git a/x-pack/plugin/core/src/main/resources/watch-history-no-ilm.json b/x-pack/plugin/core/src/main/resources/watch-history-no-ilm.json index 5b3186a9a6c12..7b5910508392d 100644 --- a/x-pack/plugin/core/src/main/resources/watch-history-no-ilm.json +++ b/x-pack/plugin/core/src/main/resources/watch-history-no-ilm.json @@ -5,6 +5,7 @@ "index.number_of_shards": 1, "index.number_of_replicas": 0, "index.auto_expand_replicas": "0-1", + "index.hidden": true, "index.format": 6 }, "mappings": { diff --git a/x-pack/plugin/core/src/main/resources/watch-history.json b/x-pack/plugin/core/src/main/resources/watch-history.json index d35ddd9afd24c..109ca95d75190 100644 --- a/x-pack/plugin/core/src/main/resources/watch-history.json +++ b/x-pack/plugin/core/src/main/resources/watch-history.json @@ -6,6 +6,7 @@ "index.number_of_replicas": 0, "index.auto_expand_replicas": "0-1", "index.lifecycle.name": "watch-history-ilm-policy", + "index.hidden": true, "index.format": 6 }, "mappings": { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java index bbd3d8ec4dd72..2915e85ebd260 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java @@ -187,10 +187,12 @@ public void testThatEmailAttachmentsAreSent() throws Exception { timeWarp().trigger("_test_id"); refresh(); - SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*") + assertBusy(() -> { + SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*") .setQuery(QueryBuilders.termQuery("watch_id", "_test_id")) .execute().actionGet(); - assertHitCount(searchResponse, 1); + assertHitCount(searchResponse, 1); + }); if (!latch.await(5, TimeUnit.SECONDS)) { fail("waited too long for email to be received"); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java index 1deced67b3d23..b67f30148944c 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.watcher.actions.webhook; import com.sun.net.httpserver.HttpsServer; +import org.apache.lucene.util.SetOnce; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.bootstrap.JavaVersion; import org.elasticsearch.common.settings.Settings; @@ -34,6 +35,7 @@ import java.security.PrivilegedAction; import java.util.List; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder; import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput; @@ -99,10 +101,16 @@ public void testHttps() throws Exception { assertThat(webServer.requests().get(0).getUri().getPath(), equalTo("/test/_id")); assertThat(webServer.requests().get(0).getBody(), equalTo("{key=value}")); - SearchResponse response = + final SetOnce responseSetOnce = new SetOnce<>(); + assertBusy(() -> { + SearchResponse response = searchWatchRecords(b -> b.setQuery(QueryBuilders.termQuery(WatchRecord.STATE.getPreferredName(), "executed"))); + assertNoFailures(response); + assertHitCount(response, 1L); + responseSetOnce.set(response); + }); + final SearchResponse response = responseSetOnce.get(); - assertNoFailures(response); XContentSource source = xContentSource(response.getHits().getAt(0).getSourceRef()); String body = source.getValue("result.actions.0.webhook.response.body"); assertThat(body, notNullValue()); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java index 01fe96c480da0..4c6a1bb66ddb7 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.watcher.history; +import org.apache.lucene.util.SetOnce; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; @@ -104,11 +105,15 @@ public void testActionConditionWithHardFailures() throws Exception { assertWatchWithMinimumActionsCount(id, ExecutionState.EXECUTED, 1); - // only one action should have failed via condition - final SearchResponse response = searchHistory(SearchSourceBuilder.searchSource().query(termQuery("watch_id", id))); - assertThat(response.getHits().getTotalHits().value, is(1L)); + final SetOnce responseSetOnce = new SetOnce<>(); + assertBusy(() -> { + final SearchResponse response = searchHistory(SearchSourceBuilder.searchSource().query(termQuery("watch_id", id))); + assertThat(response.getHits().getTotalHits().value, is(1L)); + responseSetOnce.set(response); + }); - final SearchHit hit = response.getHits().getAt(0); + // only one action should have failed via condition + final SearchHit hit = responseSetOnce.get().getHits().getAt(0); final List actions = getActionsFromHit(hit.getSourceAsMap()); for (int i = 0; i < actionConditionsWithFailure.size(); ++i) { @@ -199,11 +204,15 @@ public void testActionCondition() throws Exception { assertWatchWithMinimumActionsCount(id, ExecutionState.EXECUTED, 1); - // all actions should be successful - final SearchResponse response = searchHistory(SearchSourceBuilder.searchSource().query(termQuery("watch_id", id))); - assertThat(response.getHits().getTotalHits().value, is(1L)); + final SetOnce responseSetOnce = new SetOnce<>(); + assertBusy(() -> { + final SearchResponse response = searchHistory(SearchSourceBuilder.searchSource().query(termQuery("watch_id", id))); + assertThat(response.getHits().getTotalHits().value, is(1L)); + responseSetOnce.set(response); + }); - final SearchHit hit = response.getHits().getAt(0); + // all actions should be successful + final SearchHit hit = responseSetOnce.get().getHits().getAt(0); final List actions = getActionsFromHit(hit.getSourceAsMap()); for (int i = 0; i < actionConditions.size(); ++i) { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java index 9bbddf9e0aee6..aceb3644bd525 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java @@ -20,6 +20,8 @@ import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase; import org.junit.After; +import java.util.concurrent.atomic.AtomicReference; + import static org.elasticsearch.search.aggregations.AggregationBuilders.terms; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.emailAction; @@ -88,14 +90,21 @@ public void testEmailFields() throws Exception { // the action should fail as no email server is available assertWatchWithMinimumActionsCount("_id", ExecutionState.EXECUTED, 1); - SearchResponse response = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*").setSource(searchSource() - .aggregation(terms("from").field("result.actions.email.message.from")) - .aggregation(terms("to").field("result.actions.email.message.to")) - .aggregation(terms("cc").field("result.actions.email.message.cc")) - .aggregation(terms("bcc").field("result.actions.email.message.bcc")) - .aggregation(terms("reply_to").field("result.actions.email.message.reply_to"))) - .get(); - + final AtomicReference responseRef = new AtomicReference<>(); + assertBusy(() -> { + SearchResponse response = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*").setSource(searchSource() + .aggregation(terms("from").field("result.actions.email.message.from")) + .aggregation(terms("to").field("result.actions.email.message.to")) + .aggregation(terms("cc").field("result.actions.email.message.cc")) + .aggregation(terms("bcc").field("result.actions.email.message.bcc")) + .aggregation(terms("reply_to").field("result.actions.email.message.reply_to"))) + .get(); + assertThat(response, notNullValue()); + assertThat(response.getHits().getTotalHits().value, is(1L)); + responseRef.set(response); + }); + + final SearchResponse response = responseRef.get(); assertThat(response, notNullValue()); assertThat(response.getHits().getTotalHits().value, is(1L)); Aggregations aggs = response.getAggregations(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java index b72545e9e032f..3445a0de45b21 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.watcher.history; +import org.apache.lucene.util.SetOnce; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.search.aggregations.Aggregations; @@ -47,12 +48,17 @@ public void testIndexActionFields() throws Exception { flush(); refresh(); - SearchResponse response = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*").setSource(searchSource() - .aggregation(terms("index_action_indices").field("result.actions.index.response.index"))) - .get(); + final SetOnce responseReference = new SetOnce<>(); + assertBusy(() -> { + SearchResponse response = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*").setSource(searchSource() + .aggregation(terms("index_action_indices").field("result.actions.index.response.index"))) + .get(); + assertThat(response, notNullValue()); + assertThat(response.getHits().getTotalHits().value, is(1L)); + responseReference.set(response); + }); - assertThat(response, notNullValue()); - assertThat(response.getHits().getTotalHits().value, is(1L)); + final SearchResponse response = responseReference.get(); Aggregations aggs = response.getAggregations(); assertThat(aggs, notNullValue()); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java index a192c9315cd46..7367e3b058bce 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java @@ -61,10 +61,12 @@ public void testWatchMetadata() throws Exception { timeWarp().trigger("_name"); refresh(); - SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*") + assertBusy(() -> { + SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*") .setQuery(termQuery("metadata.foo", "bar")) .get(); - assertThat(searchResponse.getHits().getTotalHits().value, greaterThan(0L)); + assertThat(searchResponse.getHits().getTotalHits().value, greaterThan(0L)); + }); } public void testWatchMetadataAvailableAtExecution() throws Exception { From 65e1b3b3ffd1821210f290fa83459254954bf3f7 Mon Sep 17 00:00:00 2001 From: jaymode Date: Fri, 28 Feb 2020 10:12:44 -0700 Subject: [PATCH 2/4] remove watch history from MetaDataCreateIndexServiceTests --- .../cluster/metadata/MetaDataCreateIndexServiceTests.java | 1 - 1 file changed, 1 deletion(-) diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java index 7d0d065f611ff..1ad07109b8d07 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java @@ -617,7 +617,6 @@ public void testValidateDotIndex() { public void testIndexNameExclusionsList() { // this test case should be removed when DOT_INDICES_EXCLUSIONS is empty List excludedNames = Arrays.asList( - ".watch-history-" + randomAlphaOfLength(5).toLowerCase(Locale.ROOT), ".data-frame-notifications-" + randomAlphaOfLength(5).toLowerCase(Locale.ROOT), ".transform-notifications-" + randomAlphaOfLength(5).toLowerCase(Locale.ROOT) ); From a42703ad6aa7833e83ff01ec52b2f9d6c2f362b8 Mon Sep 17 00:00:00 2001 From: jaymode Date: Fri, 28 Feb 2020 12:52:41 -0700 Subject: [PATCH 3/4] fix refresh --- .../action/support/IndicesOptions.java | 3 +++ .../elasticsearch/test/ESIntegTestCase.java | 3 ++- .../actions/email/EmailAttachmentTests.java | 10 +++----- .../webhook/WebhookHttpsIntegrationTests.java | 14 +++-------- .../history/HistoryActionConditionTests.java | 25 ++++++------------- .../HistoryTemplateEmailMappingsTests.java | 25 ++++++------------- ...storyTemplateIndexActionMappingsTests.java | 16 ++++-------- .../test/integration/WatchMetadataTests.java | 10 +++----- 8 files changed, 37 insertions(+), 69 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/support/IndicesOptions.java b/server/src/main/java/org/elasticsearch/action/support/IndicesOptions.java index 5438eb2122ea0..3cdc83102983a 100644 --- a/server/src/main/java/org/elasticsearch/action/support/IndicesOptions.java +++ b/server/src/main/java/org/elasticsearch/action/support/IndicesOptions.java @@ -126,6 +126,9 @@ public enum Option { new IndicesOptions(EnumSet.of(Option.ALLOW_NO_INDICES), EnumSet.of(WildcardStates.OPEN, WildcardStates.CLOSED)); public static final IndicesOptions STRICT_EXPAND_OPEN_FORBID_CLOSED = new IndicesOptions(EnumSet.of(Option.ALLOW_NO_INDICES, Option.FORBID_CLOSED_INDICES), EnumSet.of(WildcardStates.OPEN)); + public static final IndicesOptions STRICT_EXPAND_OPEN_HIDDEN_FORBID_CLOSED = + new IndicesOptions(EnumSet.of(Option.ALLOW_NO_INDICES, Option.FORBID_CLOSED_INDICES), + EnumSet.of(WildcardStates.OPEN, WildcardStates.HIDDEN)); public static final IndicesOptions STRICT_EXPAND_OPEN_FORBID_CLOSED_IGNORE_THROTTLED = new IndicesOptions(EnumSet.of(Option.ALLOW_NO_INDICES, Option.FORBID_CLOSED_INDICES, Option.IGNORE_THROTTLED), EnumSet.of(WildcardStates.OPEN)); 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 24ab6803e6e7c..3116792c502d6 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java @@ -1146,7 +1146,8 @@ protected final IndexResponse index(String index, String id, String source) { protected final RefreshResponse refresh(String... indices) { waitForRelocation(); // TODO RANDOMIZE with flush? - RefreshResponse actionGet = client().admin().indices().prepareRefresh(indices).execute().actionGet(); + RefreshResponse actionGet = client().admin().indices().prepareRefresh(indices) + .setIndicesOptions(IndicesOptions.STRICT_EXPAND_OPEN_HIDDEN_FORBID_CLOSED).execute().actionGet(); assertNoFailures(actionGet); return actionGet; } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java index 2915e85ebd260..62c989cf3a859 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java @@ -187,12 +187,10 @@ public void testThatEmailAttachmentsAreSent() throws Exception { timeWarp().trigger("_test_id"); refresh(); - assertBusy(() -> { - SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*") - .setQuery(QueryBuilders.termQuery("watch_id", "_test_id")) - .execute().actionGet(); - assertHitCount(searchResponse, 1); - }); + SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*") + .setQuery(QueryBuilders.termQuery("watch_id", "_test_id")) + .execute().actionGet(); + assertHitCount(searchResponse, 1); if (!latch.await(5, TimeUnit.SECONDS)) { fail("waited too long for email to be received"); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java index b67f30148944c..c252111456ba1 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.watcher.actions.webhook; import com.sun.net.httpserver.HttpsServer; -import org.apache.lucene.util.SetOnce; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.bootstrap.JavaVersion; import org.elasticsearch.common.settings.Settings; @@ -35,7 +34,6 @@ import java.security.PrivilegedAction; import java.util.List; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder; import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput; @@ -101,16 +99,10 @@ public void testHttps() throws Exception { assertThat(webServer.requests().get(0).getUri().getPath(), equalTo("/test/_id")); assertThat(webServer.requests().get(0).getBody(), equalTo("{key=value}")); - final SetOnce responseSetOnce = new SetOnce<>(); - assertBusy(() -> { - SearchResponse response = - searchWatchRecords(b -> b.setQuery(QueryBuilders.termQuery(WatchRecord.STATE.getPreferredName(), "executed"))); - assertNoFailures(response); - assertHitCount(response, 1L); - responseSetOnce.set(response); - }); - final SearchResponse response = responseSetOnce.get(); + SearchResponse response = + searchWatchRecords(b -> b.setQuery(QueryBuilders.termQuery(WatchRecord.STATE.getPreferredName(), "executed"))); + assertNoFailures(response); XContentSource source = xContentSource(response.getHits().getAt(0).getSourceRef()); String body = source.getValue("result.actions.0.webhook.response.body"); assertThat(body, notNullValue()); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java index 4c6a1bb66ddb7..01fe96c480da0 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.xpack.watcher.history; -import org.apache.lucene.util.SetOnce; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; @@ -105,15 +104,11 @@ public void testActionConditionWithHardFailures() throws Exception { assertWatchWithMinimumActionsCount(id, ExecutionState.EXECUTED, 1); - final SetOnce responseSetOnce = new SetOnce<>(); - assertBusy(() -> { - final SearchResponse response = searchHistory(SearchSourceBuilder.searchSource().query(termQuery("watch_id", id))); - assertThat(response.getHits().getTotalHits().value, is(1L)); - responseSetOnce.set(response); - }); - // only one action should have failed via condition - final SearchHit hit = responseSetOnce.get().getHits().getAt(0); + final SearchResponse response = searchHistory(SearchSourceBuilder.searchSource().query(termQuery("watch_id", id))); + assertThat(response.getHits().getTotalHits().value, is(1L)); + + final SearchHit hit = response.getHits().getAt(0); final List actions = getActionsFromHit(hit.getSourceAsMap()); for (int i = 0; i < actionConditionsWithFailure.size(); ++i) { @@ -204,15 +199,11 @@ public void testActionCondition() throws Exception { assertWatchWithMinimumActionsCount(id, ExecutionState.EXECUTED, 1); - final SetOnce responseSetOnce = new SetOnce<>(); - assertBusy(() -> { - final SearchResponse response = searchHistory(SearchSourceBuilder.searchSource().query(termQuery("watch_id", id))); - assertThat(response.getHits().getTotalHits().value, is(1L)); - responseSetOnce.set(response); - }); - // all actions should be successful - final SearchHit hit = responseSetOnce.get().getHits().getAt(0); + final SearchResponse response = searchHistory(SearchSourceBuilder.searchSource().query(termQuery("watch_id", id))); + assertThat(response.getHits().getTotalHits().value, is(1L)); + + final SearchHit hit = response.getHits().getAt(0); final List actions = getActionsFromHit(hit.getSourceAsMap()); for (int i = 0; i < actionConditions.size(); ++i) { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java index aceb3644bd525..9f3896e6b0511 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java @@ -20,8 +20,6 @@ import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase; import org.junit.After; -import java.util.concurrent.atomic.AtomicReference; - import static org.elasticsearch.search.aggregations.AggregationBuilders.terms; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.emailAction; @@ -90,21 +88,14 @@ public void testEmailFields() throws Exception { // the action should fail as no email server is available assertWatchWithMinimumActionsCount("_id", ExecutionState.EXECUTED, 1); - final AtomicReference responseRef = new AtomicReference<>(); - assertBusy(() -> { - SearchResponse response = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*").setSource(searchSource() - .aggregation(terms("from").field("result.actions.email.message.from")) - .aggregation(terms("to").field("result.actions.email.message.to")) - .aggregation(terms("cc").field("result.actions.email.message.cc")) - .aggregation(terms("bcc").field("result.actions.email.message.bcc")) - .aggregation(terms("reply_to").field("result.actions.email.message.reply_to"))) - .get(); - assertThat(response, notNullValue()); - assertThat(response.getHits().getTotalHits().value, is(1L)); - responseRef.set(response); - }); - - final SearchResponse response = responseRef.get(); + SearchResponse response = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*").setSource(searchSource() + .aggregation(terms("from").field("result.actions.email.message.from")) + .aggregation(terms("to").field("result.actions.email.message.to")) + .aggregation(terms("cc").field("result.actions.email.message.cc")) + .aggregation(terms("bcc").field("result.actions.email.message.bcc")) + .aggregation(terms("reply_to").field("result.actions.email.message.reply_to"))) + .get(); + assertThat(response, notNullValue()); assertThat(response.getHits().getTotalHits().value, is(1L)); Aggregations aggs = response.getAggregations(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java index 3445a0de45b21..b72545e9e032f 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.xpack.watcher.history; -import org.apache.lucene.util.SetOnce; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.search.aggregations.Aggregations; @@ -48,17 +47,12 @@ public void testIndexActionFields() throws Exception { flush(); refresh(); - final SetOnce responseReference = new SetOnce<>(); - assertBusy(() -> { - SearchResponse response = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*").setSource(searchSource() - .aggregation(terms("index_action_indices").field("result.actions.index.response.index"))) - .get(); - assertThat(response, notNullValue()); - assertThat(response.getHits().getTotalHits().value, is(1L)); - responseReference.set(response); - }); + SearchResponse response = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*").setSource(searchSource() + .aggregation(terms("index_action_indices").field("result.actions.index.response.index"))) + .get(); - final SearchResponse response = responseReference.get(); + assertThat(response, notNullValue()); + assertThat(response.getHits().getTotalHits().value, is(1L)); Aggregations aggs = response.getAggregations(); assertThat(aggs, notNullValue()); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java index 7367e3b058bce..e77e48025a5b9 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java @@ -61,12 +61,10 @@ public void testWatchMetadata() throws Exception { timeWarp().trigger("_name"); refresh(); - assertBusy(() -> { - SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*") - .setQuery(termQuery("metadata.foo", "bar")) - .get(); - assertThat(searchResponse.getHits().getTotalHits().value, greaterThan(0L)); - }); + SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*") + .setQuery(termQuery("metadata.foo", "bar")) + .get(); + assertThat(searchResponse.getHits().getTotalHits().value, greaterThan(0L)); } public void testWatchMetadataAvailableAtExecution() throws Exception { From c8ef68e54bc138260afdf156ab4b5f197bc72f26 Mon Sep 17 00:00:00 2001 From: jaymode Date: Fri, 28 Feb 2020 12:54:18 -0700 Subject: [PATCH 4/4] formatting --- .../watcher/actions/email/EmailAttachmentTests.java | 4 ++-- .../webhook/WebhookHttpsIntegrationTests.java | 2 +- .../history/HistoryTemplateEmailMappingsTests.java | 12 ++++++------ .../watcher/test/integration/WatchMetadataTests.java | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java index 62c989cf3a859..bbd3d8ec4dd72 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java @@ -188,8 +188,8 @@ public void testThatEmailAttachmentsAreSent() throws Exception { refresh(); SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*") - .setQuery(QueryBuilders.termQuery("watch_id", "_test_id")) - .execute().actionGet(); + .setQuery(QueryBuilders.termQuery("watch_id", "_test_id")) + .execute().actionGet(); assertHitCount(searchResponse, 1); if (!latch.await(5, TimeUnit.SECONDS)) { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java index c252111456ba1..1deced67b3d23 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java @@ -100,7 +100,7 @@ public void testHttps() throws Exception { assertThat(webServer.requests().get(0).getBody(), equalTo("{key=value}")); SearchResponse response = - searchWatchRecords(b -> b.setQuery(QueryBuilders.termQuery(WatchRecord.STATE.getPreferredName(), "executed"))); + searchWatchRecords(b -> b.setQuery(QueryBuilders.termQuery(WatchRecord.STATE.getPreferredName(), "executed"))); assertNoFailures(response); XContentSource source = xContentSource(response.getHits().getAt(0).getSourceRef()); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java index 9f3896e6b0511..9bbddf9e0aee6 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java @@ -89,12 +89,12 @@ public void testEmailFields() throws Exception { assertWatchWithMinimumActionsCount("_id", ExecutionState.EXECUTED, 1); SearchResponse response = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*").setSource(searchSource() - .aggregation(terms("from").field("result.actions.email.message.from")) - .aggregation(terms("to").field("result.actions.email.message.to")) - .aggregation(terms("cc").field("result.actions.email.message.cc")) - .aggregation(terms("bcc").field("result.actions.email.message.bcc")) - .aggregation(terms("reply_to").field("result.actions.email.message.reply_to"))) - .get(); + .aggregation(terms("from").field("result.actions.email.message.from")) + .aggregation(terms("to").field("result.actions.email.message.to")) + .aggregation(terms("cc").field("result.actions.email.message.cc")) + .aggregation(terms("bcc").field("result.actions.email.message.bcc")) + .aggregation(terms("reply_to").field("result.actions.email.message.reply_to"))) + .get(); assertThat(response, notNullValue()); assertThat(response.getHits().getTotalHits().value, is(1L)); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java index e77e48025a5b9..a192c9315cd46 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java @@ -62,8 +62,8 @@ public void testWatchMetadata() throws Exception { refresh(); SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX_WITH_TEMPLATE + "*") - .setQuery(termQuery("metadata.foo", "bar")) - .get(); + .setQuery(termQuery("metadata.foo", "bar")) + .get(); assertThat(searchResponse.getHits().getTotalHits().value, greaterThan(0L)); }