diff --git a/x-pack/plugin/apm-data/src/test/java/org/elasticsearch/xpack/apmdata/APMIndexTemplateRegistryTests.java b/x-pack/plugin/apm-data/src/test/java/org/elasticsearch/xpack/apmdata/APMIndexTemplateRegistryTests.java index 32e7c2225e19d..80df895418efc 100644 --- a/x-pack/plugin/apm-data/src/test/java/org/elasticsearch/xpack/apmdata/APMIndexTemplateRegistryTests.java +++ b/x-pack/plugin/apm-data/src/test/java/org/elasticsearch/xpack/apmdata/APMIndexTemplateRegistryTests.java @@ -23,6 +23,7 @@ import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.node.DiscoveryNodes; @@ -553,16 +554,15 @@ private ClusterState createClusterState( } IngestMetadata ingestMetadata = new IngestMetadata(ingestPipelineConfigurations); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .componentTemplates(componentTemplates) + .indexTemplates(composableTemplates) + .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) + .putCustom(IngestMetadata.TYPE, ingestMetadata) + .build(); return ClusterState.builder(new ClusterName("test")) - .metadata( - Metadata.builder() - .componentTemplates(componentTemplates) - .indexTemplates(composableTemplates) - .transientSettings(nodeSettings) - .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) - .putCustom(IngestMetadata.TYPE, ingestMetadata) - .build() - ) + // We need to ensure only one project is present in the cluster state to simplify the assertions in these tests. + .metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build()) .blocks(new ClusterBlocks.Builder().build()) .nodes(nodes) .build(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistryTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistryTests.java index 086a7b20745cc..d088fa445f66a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistryTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistryTests.java @@ -881,17 +881,6 @@ private ClusterState createClusterState( // ------------- functionality unit test -------- public void testFindRolloverTargetDataStreams() { - ClusterState state = ClusterState.EMPTY_STATE; - state = ClusterState.builder(state) - .metadata( - Metadata.builder(state.metadata()) - .put(DataStreamTestHelper.newInstance("ds1", Collections.singletonList(new Index(".ds-ds1-000001", "ds1i")))) - .put(DataStreamTestHelper.newInstance("ds2", Collections.singletonList(new Index(".ds-ds2-000001", "ds2i")))) - .put(DataStreamTestHelper.newInstance("ds3", Collections.singletonList(new Index(".ds-ds3-000001", "ds3i")))) - .put(DataStreamTestHelper.newInstance("ds4", Collections.singletonList(new Index(".ds-ds4-000001", "ds4i")))) - ) - .build(); - ComposableIndexTemplate it1 = ComposableIndexTemplate.builder() .indexPatterns(List.of("ds1*", "ds2*", "ds3*")) .priority(100L) @@ -910,16 +899,19 @@ public void testFindRolloverTargetDataStreams() { .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate()) .build(); - state = ClusterState.builder(state) - .metadata(Metadata.builder(state.metadata()).put("it1", it1).put("it2", it2).put("it5", it5)) + ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(DataStreamTestHelper.newInstance("ds1", Collections.singletonList(new Index(".ds-ds1-000001", "ds1i")))) + .put(DataStreamTestHelper.newInstance("ds2", Collections.singletonList(new Index(".ds-ds2-000001", "ds2i")))) + .put(DataStreamTestHelper.newInstance("ds3", Collections.singletonList(new Index(".ds-ds3-000001", "ds3i")))) + .put(DataStreamTestHelper.newInstance("ds4", Collections.singletonList(new Index(".ds-ds4-000001", "ds4i")))) + .put("it1", it1) + .put("it2", it2) + .put("it5", it5) .build(); - assertThat( - IndexTemplateRegistry.findRolloverTargetDataStreams(state.metadata().getProject(), "it1", it1), - containsInAnyOrder("ds1", "ds3") - ); - assertThat(IndexTemplateRegistry.findRolloverTargetDataStreams(state.metadata().getProject(), "it2", it2), contains("ds2")); - assertThat(IndexTemplateRegistry.findRolloverTargetDataStreams(state.metadata().getProject(), "it5", it5), empty()); + assertThat(IndexTemplateRegistry.findRolloverTargetDataStreams(project, "it1", it1), containsInAnyOrder("ds1", "ds3")); + assertThat(IndexTemplateRegistry.findRolloverTargetDataStreams(project, "it2", it2), contains("ds2")); + assertThat(IndexTemplateRegistry.findRolloverTargetDataStreams(project, "it5", it5), empty()); } // ------------- diff --git a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/analytics/AnalyticsTemplateRegistryTests.java b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/analytics/AnalyticsTemplateRegistryTests.java index 7d4b6290cc587..88d07da753d47 100644 --- a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/analytics/AnalyticsTemplateRegistryTests.java +++ b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/analytics/AnalyticsTemplateRegistryTests.java @@ -22,6 +22,7 @@ import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.node.DiscoveryNodes; @@ -29,7 +30,6 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.TriFunction; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.Strings; import org.elasticsearch.ingest.IngestMetadata; import org.elasticsearch.ingest.PipelineConfiguration; @@ -452,16 +452,15 @@ private ClusterState createClusterState( .collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePolicyMetadata(e.getValue(), Collections.emptyMap(), 1, 1))); IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata(existingILMMeta, OperationMode.RUNNING); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .indexTemplates(composableTemplates) + .componentTemplates(componentTemplates) + .putCustom(IngestMetadata.TYPE, ingestMetadata) + .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) + .build(); return ClusterState.builder(new ClusterName("test")) - .metadata( - Metadata.builder() - .indexTemplates(composableTemplates) - .componentTemplates(componentTemplates) - .transientSettings(Settings.EMPTY) - .putCustom(IngestMetadata.TYPE, ingestMetadata) - .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) - .build() - ) + // We need to ensure only one project is present in the cluster state to simplify the assertions in these tests. + .metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build()) .blocks(new ClusterBlocks.Builder().build()) .nodes(nodes) .build(); diff --git a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/connector/ConnectorTemplateRegistryTests.java b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/connector/ConnectorTemplateRegistryTests.java index f5aef4663b8cb..6cb33c37ccfae 100644 --- a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/connector/ConnectorTemplateRegistryTests.java +++ b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/connector/ConnectorTemplateRegistryTests.java @@ -22,6 +22,7 @@ import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.node.DiscoveryNodes; @@ -29,7 +30,6 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.TriFunction; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.Strings; import org.elasticsearch.ingest.IngestMetadata; import org.elasticsearch.ingest.PipelineConfiguration; @@ -478,16 +478,15 @@ private ClusterState createClusterState( .collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePolicyMetadata(e.getValue(), Collections.emptyMap(), 1, 1))); IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata(existingILMMeta, OperationMode.RUNNING); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .indexTemplates(composableTemplates) + .componentTemplates(componentTemplates) + .putCustom(IngestMetadata.TYPE, ingestMetadata) + .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) + .build(); return ClusterState.builder(new ClusterName("test")) - .metadata( - Metadata.builder() - .indexTemplates(composableTemplates) - .componentTemplates(componentTemplates) - .transientSettings(Settings.EMPTY) - .putCustom(IngestMetadata.TYPE, ingestMetadata) - .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) - .build() - ) + // We need to ensure only one project is present in the cluster state to simplify the assertions in these tests. + .metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build()) .blocks(new ClusterBlocks.Builder().build()) .nodes(nodes) .build(); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringTemplateRegistryTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringTemplateRegistryTests.java index 3e616797a9f82..21ec0de7aa3d2 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringTemplateRegistryTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringTemplateRegistryTests.java @@ -21,6 +21,7 @@ import org.elasticsearch.cluster.block.ClusterBlocks; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.node.DiscoveryNodes; @@ -383,7 +384,7 @@ private ClusterChangedEvent createClusterChangedEvent( Map existingPolicies, DiscoveryNodes nodes ) { - ClusterState cs = createClusterState(Settings.EMPTY, existingTemplates, existingPolicies, nodes); + ClusterState cs = createClusterState(existingTemplates, existingPolicies, nodes); ClusterChangedEvent realEvent = new ClusterChangedEvent( "created-from-test", cs, @@ -396,7 +397,6 @@ private ClusterChangedEvent createClusterChangedEvent( } private ClusterState createClusterState( - Settings nodeSettings, Map existingComposableTemplates, Map existingPolicies, DiscoveryNodes nodes @@ -413,14 +413,13 @@ private ClusterState createClusterState( .collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePolicyMetadata(e.getValue(), Collections.emptyMap(), 1, 1))); IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata(existingILMMeta, OperationMode.RUNNING); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .indexTemplates(composableTemplates) + .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) + .build(); return ClusterState.builder(new ClusterName("test")) - .metadata( - Metadata.builder() - .indexTemplates(composableTemplates) - .transientSettings(nodeSettings) - .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) - .build() - ) + // We need to ensure only one project is present in the cluster state to simplify the assertions in these tests. + .metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build()) .blocks(new ClusterBlocks.Builder().build()) .nodes(nodes) .build(); diff --git a/x-pack/plugin/slm/src/main/java/org/elasticsearch/xpack/slm/history/SnapshotLifecycleTemplateRegistry.java b/x-pack/plugin/slm/src/main/java/org/elasticsearch/xpack/slm/history/SnapshotLifecycleTemplateRegistry.java index 9757b0c55db76..6240d5f843550 100644 --- a/x-pack/plugin/slm/src/main/java/org/elasticsearch/xpack/slm/history/SnapshotLifecycleTemplateRegistry.java +++ b/x-pack/plugin/slm/src/main/java/org/elasticsearch/xpack/slm/history/SnapshotLifecycleTemplateRegistry.java @@ -8,13 +8,11 @@ package org.elasticsearch.xpack.slm.history; import org.elasticsearch.client.internal.Client; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; import org.elasticsearch.xpack.core.template.IndexTemplateConfig; import org.elasticsearch.xpack.core.template.IndexTemplateRegistry; @@ -23,9 +21,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; import static org.elasticsearch.xpack.core.ClientHelper.INDEX_LIFECYCLE_ORIGIN; import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.SLM_HISTORY_INDEX_ENABLED_SETTING; @@ -101,18 +96,4 @@ protected List getLifecycleConfigs() { protected String getOrigin() { return INDEX_LIFECYCLE_ORIGIN; // TODO use separate SLM origin? } - - public boolean validate(ClusterState state) { - boolean allTemplatesPresent = getComposableTemplateConfigs().keySet() - .stream() - .allMatch(name -> state.metadata().getProject().templatesV2().containsKey(name)); - - Optional> maybePolicies = Optional.ofNullable( - state.metadata().getProject().custom(IndexLifecycleMetadata.TYPE) - ).map(IndexLifecycleMetadata::getPolicies); - Set policyNames = getLifecyclePolicies().stream().map(LifecyclePolicy::getName).collect(Collectors.toSet()); - - boolean allPoliciesPresent = maybePolicies.map(policies -> policies.keySet().containsAll(policyNames)).orElse(false); - return allTemplatesPresent && allPoliciesPresent; - } } diff --git a/x-pack/plugin/slm/src/test/java/org/elasticsearch/xpack/slm/history/SnapshotLifecycleTemplateRegistryTests.java b/x-pack/plugin/slm/src/test/java/org/elasticsearch/xpack/slm/history/SnapshotLifecycleTemplateRegistryTests.java index c572f2adf9bca..de1e9f8323bcc 100644 --- a/x-pack/plugin/slm/src/test/java/org/elasticsearch/xpack/slm/history/SnapshotLifecycleTemplateRegistryTests.java +++ b/x-pack/plugin/slm/src/test/java/org/elasticsearch/xpack/slm/history/SnapshotLifecycleTemplateRegistryTests.java @@ -20,6 +20,7 @@ import org.elasticsearch.cluster.block.ClusterBlocks; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.node.DiscoveryNodes; @@ -303,23 +304,6 @@ public void testThatMissingMasterNodeDoesNothing() { registry.clusterChanged(event); } - public void testValidate() { - assertFalse(registry.validate(createClusterState(Settings.EMPTY, Collections.emptyMap(), Collections.emptyMap(), null))); - assertFalse( - registry.validate( - createClusterState(Settings.EMPTY, Collections.singletonMap(SLM_TEMPLATE_NAME, null), Collections.emptyMap(), null) - ) - ); - - Map policyMap = new HashMap<>(); - policyMap.put(SLM_POLICY_NAME, new LifecyclePolicy(SLM_POLICY_NAME, new HashMap<>())); - assertFalse(registry.validate(createClusterState(Settings.EMPTY, Collections.emptyMap(), policyMap, null))); - - assertTrue( - registry.validate(createClusterState(Settings.EMPTY, Collections.singletonMap(SLM_TEMPLATE_NAME, null), policyMap, null)) - ); - } - public void testTemplateNameIsVersioned() { assertThat(SLM_TEMPLATE_NAME, endsWith("-" + INDEX_TEMPLATE_VERSION)); } @@ -394,7 +378,7 @@ private ClusterChangedEvent createClusterChangedEvent( Map existingPolicies, DiscoveryNodes nodes ) { - ClusterState cs = createClusterState(Settings.EMPTY, existingTemplates, existingPolicies, nodes); + ClusterState cs = createClusterState(existingTemplates, existingPolicies, nodes); ClusterChangedEvent realEvent = new ClusterChangedEvent( "created-from-test", cs, @@ -407,7 +391,6 @@ private ClusterChangedEvent createClusterChangedEvent( } private ClusterState createClusterState( - Settings nodeSettings, Map existingTemplates, Map existingPolicies, DiscoveryNodes nodes @@ -426,14 +409,13 @@ private ClusterState createClusterState( .collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePolicyMetadata(e.getValue(), Collections.emptyMap(), 1, 1))); IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata(existingILMMeta, OperationMode.RUNNING); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .indexTemplates(indexTemplates) + .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) + .build(); return ClusterState.builder(new ClusterName("test")) - .metadata( - Metadata.builder() - .indexTemplates(indexTemplates) - .transientSettings(nodeSettings) - .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) - .build() - ) + // We need to ensure only one project is present in the cluster state to simplify the assertions in these tests. + .metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build()) .blocks(new ClusterBlocks.Builder().build()) .nodes(nodes) .build(); diff --git a/x-pack/plugin/stack/src/test/java/org/elasticsearch/xpack/stack/StackTemplateRegistryTests.java b/x-pack/plugin/stack/src/test/java/org/elasticsearch/xpack/stack/StackTemplateRegistryTests.java index 2c95afc5b8d9b..970ba67e25667 100644 --- a/x-pack/plugin/stack/src/test/java/org/elasticsearch/xpack/stack/StackTemplateRegistryTests.java +++ b/x-pack/plugin/stack/src/test/java/org/elasticsearch/xpack/stack/StackTemplateRegistryTests.java @@ -22,6 +22,7 @@ import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.node.DiscoveryNodes; @@ -607,7 +608,7 @@ private ClusterChangedEvent createClusterChangedEvent( DiscoveryNodes nodes, boolean addRegistryPipelines ) { - ClusterState cs = createClusterState(Settings.EMPTY, existingTemplates, existingPolicies, nodes, addRegistryPipelines); + ClusterState cs = createClusterState(existingTemplates, existingPolicies, nodes, addRegistryPipelines); ClusterChangedEvent realEvent = new ClusterChangedEvent( "created-from-test", cs, @@ -620,7 +621,6 @@ private ClusterChangedEvent createClusterChangedEvent( } private ClusterState createClusterState( - Settings nodeSettings, Map existingComponentTemplates, Map existingPolicies, DiscoveryNodes nodes, @@ -651,15 +651,14 @@ private ClusterState createClusterState( } IngestMetadata ingestMetadata = new IngestMetadata(ingestPipelines); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .componentTemplates(componentTemplates) + .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) + .putCustom(IngestMetadata.TYPE, ingestMetadata) + .build(); return ClusterState.builder(new ClusterName("test")) - .metadata( - Metadata.builder() - .componentTemplates(componentTemplates) - .transientSettings(nodeSettings) - .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) - .putCustom(IngestMetadata.TYPE, ingestMetadata) - .build() - ) + // We need to ensure only one project is present in the cluster state to simplify the assertions in these tests. + .metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build()) .blocks(new ClusterBlocks.Builder().build()) .nodes(nodes) .build(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java index c8a18e7fe9792..c36cfe1368794 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java @@ -19,7 +19,6 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.block.ClusterBlocks; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; -import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.metadata.ProjectId; import org.elasticsearch.cluster.metadata.ProjectMetadata; @@ -83,8 +82,6 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase { private ClusterService clusterService; private ThreadPool threadPool; private Client client; - @NotMultiProjectCapable(description = "Watcher is not available in serverless") - private final ProjectId projectId = ProjectId.DEFAULT; @SuppressWarnings("unchecked") @Before @@ -134,10 +131,7 @@ public void testThatNonExistingTemplatesAreAddedImmediately() { ); verify(client, times(1)).execute(same(TransportPutComposableIndexTemplateAction.TYPE), argumentCaptor.capture(), any()); - // now delete one template from the cluster state and lets retry - Map existingTemplates = new HashMap<>(); - existingTemplates.put(WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME, INDEX_TEMPLATE_VERSION); - ClusterChangedEvent newEvent = createClusterChangedEvent(existingTemplates, nodes); + ClusterChangedEvent newEvent = addTemplateToState(event); registry.clusterChanged(newEvent); argumentCaptor = ArgumentCaptor.forClass(TransportPutComposableIndexTemplateAction.Request.class); verify(client, times(1)).execute(same(TransportPutComposableIndexTemplateAction.TYPE), argumentCaptor.capture(), any()); @@ -160,7 +154,7 @@ public void testThatNonExistingTemplatesAreAddedEvenWithILMUsageDisabled() { client, xContentRegistry ); - ClusterChangedEvent event = createClusterChangedEvent(Settings.EMPTY, Collections.emptyMap(), Collections.emptyMap(), nodes); + ClusterChangedEvent event = createClusterChangedEvent(Collections.emptyMap(), Collections.emptyMap(), nodes); registry.clusterChanged(event); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass( TransportPutComposableIndexTemplateAction.Request.class @@ -168,9 +162,7 @@ public void testThatNonExistingTemplatesAreAddedEvenWithILMUsageDisabled() { verify(client, times(1)).execute(same(TransportPutComposableIndexTemplateAction.TYPE), argumentCaptor.capture(), any()); // now delete one template from the cluster state and lets retry - Map existingTemplates = new HashMap<>(); - existingTemplates.put(WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME, INDEX_TEMPLATE_VERSION); - ClusterChangedEvent newEvent = createClusterChangedEvent(existingTemplates, nodes); + ClusterChangedEvent newEvent = addTemplateToState(event); registry.clusterChanged(newEvent); ArgumentCaptor captor = ArgumentCaptor.forClass(PutIndexTemplateRequest.class); verify(client, times(1)).execute(same(TransportPutComposableIndexTemplateAction.TYPE), argumentCaptor.capture(), any()); @@ -212,7 +204,7 @@ public void testNoPolicyButILMDisabled() { client, xContentRegistry ); - ClusterChangedEvent event = createClusterChangedEvent(Settings.EMPTY, Collections.emptyMap(), Collections.emptyMap(), nodes); + ClusterChangedEvent event = createClusterChangedEvent(Collections.emptyMap(), Collections.emptyMap(), nodes); registry.clusterChanged(event); verify(client, times(0)).execute(eq(ILMActions.PUT), any(), any()); } @@ -295,7 +287,7 @@ public void testThatTemplatesAreNotAppliedOnSameVersionNodes() { DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("master").add(localNode).add(masterNode).build(); Map existingTemplates = new HashMap<>(); - existingTemplates.put(".watch-history-6", null); + existingTemplates.put(WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME, INDEX_TEMPLATE_VERSION); ClusterChangedEvent event = createClusterChangedEvent(existingTemplates, nodes); registry.clusterChanged(event); @@ -307,7 +299,7 @@ public void testThatMissingMasterNodeDoesNothing() { DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").add(localNode).build(); Map existingTemplates = new HashMap<>(); - existingTemplates.put(".watch-history-6", null); + existingTemplates.put(WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME, INDEX_TEMPLATE_VERSION); ClusterChangedEvent event = createClusterChangedEvent(existingTemplates, nodes); registry.clusterChanged(event); @@ -319,16 +311,14 @@ private ClusterChangedEvent createClusterChangedEvent(Map exist } private ClusterState createClusterState( - Settings nodeSettings, Map existingTemplates, Map existingPolicies, DiscoveryNodes nodes ) { - Map indexTemplates = new HashMap<>(); + Map indexTemplates = new HashMap<>(); for (Map.Entry template : existingTemplates.entrySet()) { - final IndexTemplateMetadata mockTemplate = mock(IndexTemplateMetadata.class); - when(mockTemplate.version()).thenReturn(template.getValue()); - when(mockTemplate.getVersion()).thenReturn(template.getValue()); + final ComposableIndexTemplate mockTemplate = mock(ComposableIndexTemplate.class); + when(mockTemplate.version()).thenReturn((long) template.getValue()); indexTemplates.put(template.getKey(), mockTemplate); } @@ -337,35 +327,24 @@ private ClusterState createClusterState( .collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePolicyMetadata(e.getValue(), Collections.emptyMap(), 1, 1))); IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata(existingILMMeta, OperationMode.RUNNING); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .indexTemplates(indexTemplates) + .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) + .build(); return ClusterState.builder(new ClusterName("test")) - .metadata( - Metadata.builder() - .transientSettings(nodeSettings) - .put( - ProjectMetadata.builder(projectId).templates(indexTemplates).putCustom(IndexLifecycleMetadata.TYPE, ilmMeta).build() - ) - .build() - ) + // We need to ensure only one project is present in the cluster state to simplify the assertions in these tests. + .metadata(Metadata.builder().projectMetadata(Map.of(project.id(), project)).build()) .blocks(new ClusterBlocks.Builder().build()) .nodes(nodes) .build(); } private ClusterChangedEvent createClusterChangedEvent( - Map existingTemplateNames, - Map existingPolicies, - DiscoveryNodes nodes - ) { - return createClusterChangedEvent(Settings.EMPTY, existingTemplateNames, existingPolicies, nodes); - } - - private ClusterChangedEvent createClusterChangedEvent( - Settings nodeSettings, Map existingTemplates, Map existingPolicies, DiscoveryNodes nodes ) { - ClusterState cs = createClusterState(nodeSettings, existingTemplates, existingPolicies, nodes); + ClusterState cs = createClusterState(existingTemplates, existingPolicies, nodes); ClusterChangedEvent realEvent = new ClusterChangedEvent( "created-from-test", cs, @@ -377,6 +356,16 @@ private ClusterChangedEvent createClusterChangedEvent( return event; } + private ClusterChangedEvent addTemplateToState(ClusterChangedEvent previousEvent) { + final ComposableIndexTemplate mockTemplate = mock(ComposableIndexTemplate.class); + when(mockTemplate.version()).thenReturn((long) INDEX_TEMPLATE_VERSION); + ProjectMetadata newProject = ProjectMetadata.builder(previousEvent.state().metadata().projects().values().iterator().next()) + .put(WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME, mockTemplate) + .build(); + ClusterState newState = ClusterState.builder(previousEvent.state()).putProjectMetadata(newProject).build(); + return new ClusterChangedEvent("created-from-test", newState, previousEvent.state()); + } + private ClusterState createClusterState(Map existingTemplates) { Metadata.Builder metadataBuilder = Metadata.builder(); HashMap templates = new HashMap<>(); @@ -386,6 +375,8 @@ private ClusterState createClusterState(Map existingTemplates) when(indexTemplate.indexPatterns()).thenReturn(Arrays.asList(generateRandomStringArray(10, 100, false, false))); templates.put(template.getKey(), indexTemplate); } + @NotMultiProjectCapable(description = "Watcher is not available in serverless") + final var projectId = ProjectId.DEFAULT; metadataBuilder.put(ProjectMetadata.builder(projectId).indexTemplates(templates)); return ClusterState.builder(new ClusterName("foo")).metadata(metadataBuilder.build()).build();