From 785aedebade8e4c0736f7a5f65373e0c449691d9 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 1 Jul 2019 17:29:38 +0200 Subject: [PATCH] Add restart node enrich tests. (#43579) This test verifies that enrich policies still exist after a full cluster restart. If EnrichPolicy is not registered as named xcontent in EnrichPlugin class then this test fails. --- .../xpack/enrich/EnrichMultiNodeIT.java | 8 +-- .../xpack/enrich/EnrichRestartIT.java | 72 +++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichRestartIT.java diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichMultiNodeIT.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichMultiNodeIT.java index c1a6675690d..2df911f35ba 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichMultiNodeIT.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichMultiNodeIT.java @@ -46,11 +46,11 @@ import static org.hamcrest.Matchers.notNullValue; @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0) public class EnrichMultiNodeIT extends ESIntegTestCase { - private static final String POLICY_NAME = "my-policy"; + static final String POLICY_NAME = "my-policy"; private static final String PIPELINE_NAME = "my-pipeline"; - private static final String SOURCE_INDEX_NAME = "users"; - private static final String KEY_FIELD = "email"; - private static final String[] DECORATE_FIELDS = new String[]{"address", "city", "country"}; + static final String SOURCE_INDEX_NAME = "users"; + static final String KEY_FIELD = "email"; + static final String[] DECORATE_FIELDS = new String[]{"address", "city", "country"}; @Override protected Collection> nodePlugins() { diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichRestartIT.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichRestartIT.java new file mode 100644 index 00000000000..c0dad54da87 --- /dev/null +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichRestartIT.java @@ -0,0 +1,72 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.enrich; + +import org.elasticsearch.index.reindex.ReindexPlugin; +import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.test.ESIntegTestCase; +import org.elasticsearch.xpack.core.enrich.EnrichPolicy; +import org.elasticsearch.xpack.core.enrich.action.ListEnrichPolicyAction; +import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Optional; + +import static org.elasticsearch.xpack.enrich.EnrichMultiNodeIT.DECORATE_FIELDS; +import static org.elasticsearch.xpack.enrich.EnrichMultiNodeIT.KEY_FIELD; +import static org.elasticsearch.xpack.enrich.EnrichMultiNodeIT.POLICY_NAME; +import static org.elasticsearch.xpack.enrich.EnrichMultiNodeIT.SOURCE_INDEX_NAME; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0) +public class EnrichRestartIT extends ESIntegTestCase { + + @Override + protected Collection> nodePlugins() { + return Arrays.asList(EnrichPlugin.class, ReindexPlugin.class); + } + + @Override + protected Collection> transportClientPlugins() { + return nodePlugins(); + } + + public void testRestart() throws Exception { + final int numPolicies = randomIntBetween(2, 4); + internalCluster().startNode(); + + EnrichPolicy enrichPolicy = new EnrichPolicy(EnrichPolicy.EXACT_MATCH_TYPE, null, + Collections.singletonList(SOURCE_INDEX_NAME), KEY_FIELD, Arrays.asList(DECORATE_FIELDS)); + for (int i = 0; i < numPolicies; i++) { + String policyName = POLICY_NAME + i; + PutEnrichPolicyAction.Request request = new PutEnrichPolicyAction.Request(policyName, enrichPolicy); + client().execute(PutEnrichPolicyAction.INSTANCE, request).actionGet(); + } + + verifyPolicies(numPolicies, enrichPolicy); + // After full restart the policies should still exist: + internalCluster().fullRestart(); + verifyPolicies(numPolicies, enrichPolicy); + } + + private static void verifyPolicies(int numPolicies, EnrichPolicy enrichPolicy) { + ListEnrichPolicyAction.Response response = + client().execute(ListEnrichPolicyAction.INSTANCE, new ListEnrichPolicyAction.Request()).actionGet(); + assertThat(response.getPolicies().size(), equalTo(numPolicies)); + for (int i = 0; i < numPolicies; i++) { + String policyName = POLICY_NAME + i; + Optional result = response.getPolicies().stream() + .filter(namedPolicy -> namedPolicy.getName().equals(policyName)) + .findFirst(); + assertThat(result.isPresent(), is(true)); + assertThat(result.get().getPolicy(), equalTo(enrichPolicy)); + } + } + +}