From e99709c9a70dfe365f0f0d0aa6c1b8807343e1cd Mon Sep 17 00:00:00 2001 From: Gordon Brown Date: Thu, 24 Oct 2019 13:48:45 -0600 Subject: [PATCH 1/3] Wrap ResponseException in AssertionError in test When checking for the existence of a document in the ILM/CCR integration tests, `assertDocumentExists` makes an HTTP request and checks the response code. However, if the repsonse code is not successful, the call will throw a `ResponseException`. `assertDocumentExists` is often called inside an `assertBusy`, and wrapping the `ResponseException` in an `AssertionError` will allow the `assertBusy` to retry. In particular, this fixes an issue with `testCCRUnfollowDuringSnapshot` where the index in question may still be closed when the document is requested. --- .../xpack/ilm/CCRIndexLifecycleIT.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java b/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java index 844235ba1e214..1d9cf85dbd240 100644 --- a/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java +++ b/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java @@ -7,11 +7,12 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; +import org.apache.http.util.EntityUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; +import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.RestClient; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; @@ -108,7 +109,6 @@ public void testBasicCCRAndILMIntegration() throws Exception { } } - @LuceneTestCase.AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/48461") public void testCCRUnfollowDuringSnapshot() throws Exception { String indexName = "unfollow-test-index"; if ("leader".equals(targetCluster)) { @@ -746,10 +746,19 @@ private static Object getIndexSetting(RestClient client, String index, String se return settings.get(setting); } - private static void assertDocumentExists(RestClient client, String index, String id) throws IOException { + private void assertDocumentExists(RestClient client, String index, String id) throws IOException { Request request = new Request("HEAD", "/" + index + "/_doc/" + id); - Response response = client.performRequest(request); - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); + Response response; + try { + response = client.performRequest(request); + if (response.getStatusLine().getStatusCode() != 200) { + logger.error(EntityUtils.toString(response.getEntity())); + fail("HTTP response code expected to be [200] but was [" + response.getStatusLine().getStatusCode() + "]"); + } + } catch (ResponseException ex) { + logger.error(EntityUtils.toString(ex.getResponse().getEntity())); + fail("HTTP response code expected to be [200] but was [" + ex.getResponse().getStatusLine().getStatusCode() + "]"); + } } private void createNewSingletonPolicy(String policyName, String phaseName, LifecycleAction action, TimeValue after) throws IOException { From 1c0cab4522517135d010224e2200bdcb47d96626 Mon Sep 17 00:00:00 2001 From: Gordon Brown Date: Thu, 24 Oct 2019 14:00:24 -0600 Subject: [PATCH 2/3] Include exception in log --- .../java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java b/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java index 1d9cf85dbd240..672ffb423d974 100644 --- a/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java +++ b/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java @@ -756,7 +756,7 @@ private void assertDocumentExists(RestClient client, String index, String id) th fail("HTTP response code expected to be [200] but was [" + response.getStatusLine().getStatusCode() + "]"); } } catch (ResponseException ex) { - logger.error(EntityUtils.toString(ex.getResponse().getEntity())); + logger.error(EntityUtils.toString(ex.getResponse().getEntity()), ex); fail("HTTP response code expected to be [200] but was [" + ex.getResponse().getStatusLine().getStatusCode() + "]"); } } From 4e7c1931cfff2ebaff3fb350ac9c84587b6e1891 Mon Sep 17 00:00:00 2001 From: Gordon Brown Date: Thu, 24 Oct 2019 14:54:54 -0600 Subject: [PATCH 3/3] Null checks, use GET instead of HEAD for more info --- .../xpack/ilm/CCRIndexLifecycleIT.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java b/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java index 672ffb423d974..4dfdc507350b5 100644 --- a/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java +++ b/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java @@ -747,16 +747,24 @@ private static Object getIndexSetting(RestClient client, String index, String se } private void assertDocumentExists(RestClient client, String index, String id) throws IOException { - Request request = new Request("HEAD", "/" + index + "/_doc/" + id); + Request request = new Request("GET", "/" + index + "/_doc/" + id); Response response; try { response = client.performRequest(request); if (response.getStatusLine().getStatusCode() != 200) { - logger.error(EntityUtils.toString(response.getEntity())); + if (response.getEntity() != null) { + logger.error(EntityUtils.toString(response.getEntity())); + } else { + logger.error("response body was null"); + } fail("HTTP response code expected to be [200] but was [" + response.getStatusLine().getStatusCode() + "]"); } } catch (ResponseException ex) { - logger.error(EntityUtils.toString(ex.getResponse().getEntity()), ex); + if (ex.getResponse().getEntity() != null) { + logger.error(EntityUtils.toString(ex.getResponse().getEntity()), ex); + } else { + logger.error("response body was null"); + } fail("HTTP response code expected to be [200] but was [" + ex.getResponse().getStatusLine().getStatusCode() + "]"); } }