From 7f553f391fa562ad2f88a70b245547d0dd76aab1 Mon Sep 17 00:00:00 2001 From: Nhat Nguyen Date: Tue, 5 Dec 2017 21:25:53 -0500 Subject: [PATCH] TEST: fix index template without index patterns Relates https://github.com/elastic/elasticsearch/pull/27662 Original commit: elastic/x-pack-elasticsearch@7f2766695f4941341cc1490963b1a286a8c2b69e --- .../SecurityLifecycleServiceTests.java | 5 ++- .../watcher/WatcherLifeCycleServiceTests.java | 45 +++++++++++-------- .../WatcherIndexTemplateRegistryTests.java | 3 +- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityLifecycleServiceTests.java b/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityLifecycleServiceTests.java index 597396f58f2..b9e92457163 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityLifecycleServiceTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/security/SecurityLifecycleServiceTests.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.security; import java.io.IOException; +import java.util.Arrays; import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; @@ -220,8 +221,8 @@ public class SecurityLifecycleServiceTests extends ESTestCase { IndexLifecycleManager.TEMPLATE_VERSION_PATTERN); PutIndexTemplateRequest request = new PutIndexTemplateRequest(); request.source(template, XContentType.JSON); - IndexTemplateMetaData.Builder templateBuilder = - IndexTemplateMetaData.builder(templateName); + IndexTemplateMetaData.Builder templateBuilder = IndexTemplateMetaData.builder(templateName) + .patterns(Arrays.asList(generateRandomStringArray(10, 100, false, false))); for (Map.Entry entry : request.mappings().entrySet()) { templateBuilder.putMapping(entry.getKey(), entry.getValue()); } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleServiceTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleServiceTests.java index 284b5ac2d4a..4ac894ffd4b 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleServiceTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleServiceTests.java @@ -37,7 +37,10 @@ import org.mockito.stubbing.Answer; import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.concurrent.ExecutorService; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import static java.util.Arrays.asList; import static org.elasticsearch.cluster.routing.ShardRoutingState.RELOCATING; @@ -87,9 +90,9 @@ public class WatcherLifeCycleServiceTests extends ESTestCase { IndexRoutingTable watchRoutingTable = IndexRoutingTable.builder(new Index(Watch.INDEX, "foo")).build(); ClusterState clusterState = ClusterState.builder(new ClusterName("my-cluster")) .metaData(MetaData.builder() - .put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME)) - .put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME)) - .put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME)) + .put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())) + .put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME).patterns(randomIndexPatterns())) + .put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME).patterns(randomIndexPatterns())) .build()) .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1"))) .routingTable(RoutingTable.builder().add(watchRoutingTable).build()) @@ -124,9 +127,9 @@ public class WatcherLifeCycleServiceTests extends ESTestCase { .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1"))) .routingTable(RoutingTable.builder().add(watchRoutingTable).build()) .metaData(MetaData.builder() - .put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME)) - .put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME)) - .put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME)) + .put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())) + .put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME).patterns(randomIndexPatterns())) + .put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME).patterns(randomIndexPatterns())) .build()) .build(); @@ -153,9 +156,9 @@ public class WatcherLifeCycleServiceTests extends ESTestCase { clusterState = ClusterState.builder(new ClusterName("my-cluster")) .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1"))) .metaData(MetaData.builder() - .put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME)) - .put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME)) - .put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME)) + .put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())) + .put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME).patterns(randomIndexPatterns())) + .put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME).patterns(randomIndexPatterns())) .build()) .build(); when(watcherService.state()).thenReturn(WatcherState.STARTED); @@ -166,9 +169,9 @@ public class WatcherLifeCycleServiceTests extends ESTestCase { ClusterState previousClusterState = ClusterState.builder(new ClusterName("my-cluster")) .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1"))) .metaData(MetaData.builder() - .put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME)) - .put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME)) - .put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME)) + .put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())) + .put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME).patterns(randomIndexPatterns())) + .put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME).patterns(randomIndexPatterns())) .build()) .build(); when(watcherService.validate(clusterState)).thenReturn(true); @@ -440,9 +443,9 @@ public class WatcherLifeCycleServiceTests extends ESTestCase { ClusterState state = ClusterState.builder(new ClusterName("my-cluster")) .nodes(nodes) .metaData(MetaData.builder() - .put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME)) - .put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME)) - .put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME)) + .put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())) + .put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME).patterns(randomIndexPatterns())) + .put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME).patterns(randomIndexPatterns())) .build()) .build(); when(watcherService.validate(eq(state))).thenReturn(true); @@ -461,17 +464,17 @@ public class WatcherLifeCycleServiceTests extends ESTestCase { MetaData.Builder metaDataBuilder = MetaData.builder(); boolean isHistoryTemplateAdded = randomBoolean(); if (isHistoryTemplateAdded) { - metaDataBuilder.put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME)); + metaDataBuilder.put(IndexTemplateMetaData.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())); } boolean isTriggeredTemplateAdded = randomBoolean(); if (isTriggeredTemplateAdded) { - metaDataBuilder.put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME)); + metaDataBuilder.put(IndexTemplateMetaData.builder(TRIGGERED_TEMPLATE_NAME).patterns(randomIndexPatterns())); } boolean isWatchesTemplateAdded = randomBoolean(); if (isWatchesTemplateAdded) { // ensure not all templates are added, otherwise life cycle service would start if ((isHistoryTemplateAdded || isTriggeredTemplateAdded) == false) { - metaDataBuilder.put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME)); + metaDataBuilder.put(IndexTemplateMetaData.builder(WATCHES_TEMPLATE_NAME).patterns(randomIndexPatterns())); } } ClusterState state = ClusterState.builder(new ClusterName("my-cluster")).nodes(nodes).metaData(metaDataBuilder).build(); @@ -482,6 +485,12 @@ public class WatcherLifeCycleServiceTests extends ESTestCase { verify(watcherService, times(0)).start(any(ClusterState.class)); } + private List randomIndexPatterns() { + return IntStream.range(0, between(1, 10)) + .mapToObj(n -> randomAlphaOfLengthBetween(1, 100)) + .collect(Collectors.toList()); + } + private static DiscoveryNode newNode(String nodeName) { return newNode(nodeName, Version.CURRENT); } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java index 21134223103..8832aca8290 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java @@ -154,7 +154,8 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase { private ClusterState createClusterState(String ... existingTemplates) { MetaData.Builder metaDataBuilder = MetaData.builder(); for (String templateName : existingTemplates) { - metaDataBuilder.put(IndexTemplateMetaData.builder(templateName)); + metaDataBuilder.put(IndexTemplateMetaData.builder(templateName) + .patterns(Arrays.asList(generateRandomStringArray(10, 100, false, false)))); } return ClusterState.builder(new ClusterName("foo")).metaData(metaDataBuilder.build()).build();