Skip to content

Commit ffa8927

Browse files
authored
Make PhaseCacheManagementTests project-aware (#129047)
The functionality in `PhaseCacheManagement` was already project-aware, but these tests were still using deprecated methods.
1 parent f2e4201 commit ffa8927

File tree

1 file changed

+17
-45
lines changed

1 file changed

+17
-45
lines changed

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagementTests.java

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
package org.elasticsearch.xpack.core.ilm;
99

1010
import org.elasticsearch.client.internal.Client;
11-
import org.elasticsearch.cluster.ClusterState;
1211
import org.elasticsearch.cluster.metadata.IndexMetadata;
1312
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
14-
import org.elasticsearch.cluster.metadata.Metadata;
1513
import org.elasticsearch.cluster.metadata.ProjectMetadata;
16-
import org.elasticsearch.cluster.project.DefaultProjectResolver;
1714
import org.elasticsearch.common.Strings;
1815
import org.elasticsearch.common.xcontent.XContentHelper;
1916
import org.elasticsearch.core.TimeValue;
@@ -88,13 +85,11 @@ public void testRefreshPhaseJson() throws IOException {
8885
LifecyclePolicy newPolicy = new LifecyclePolicy("my-policy", phases);
8986
LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(newPolicy, Map.of(), 2L, 2L);
9087

91-
ClusterState existingState = ClusterState.builder(ClusterState.EMPTY_STATE)
92-
.metadata(Metadata.builder(Metadata.EMPTY_METADATA).put(meta, false).build())
93-
.build();
88+
ProjectMetadata existingProject = ProjectMetadata.builder(randomProjectIdOrDefault()).put(meta, false).build();
9489

95-
ClusterState changedState = refreshPhaseDefinition(existingState, indexName, policyMetadata);
90+
ProjectMetadata changedProject = PhaseCacheManagement.refreshPhaseDefinition(existingProject, indexName, policyMetadata);
9691

97-
IndexMetadata newIdxMeta = changedState.metadata().getProject().index(indexName);
92+
IndexMetadata newIdxMeta = changedProject.index(indexName);
9893
LifecycleExecutionState afterExState = newIdxMeta.getLifecycleExecutionState();
9994
Map<String, String> beforeState = new HashMap<>(exState.build().asMap());
10095
beforeState.remove("phase_definition");
@@ -495,15 +490,13 @@ public void testUpdateIndicesForPolicy() throws IOException {
495490

496491
assertTrue(isIndexPhaseDefinitionUpdatable(REGISTRY, client, meta, newPolicy, null));
497492

498-
ClusterState existingState = ClusterState.builder(ClusterState.EMPTY_STATE)
499-
.metadata(Metadata.builder(Metadata.EMPTY_METADATA).put(meta, false).build())
500-
.build();
493+
ProjectMetadata existingProject = ProjectMetadata.builder(randomProjectIdOrDefault()).put(meta, false).build();
501494

502495
logger.info("--> update for unchanged policy");
503-
ClusterState updatedState = updateIndicesForPolicy(existingState, REGISTRY, client, oldPolicy, policyMetadata, null);
496+
ProjectMetadata updatedProject = updateIndicesForPolicy(existingProject, REGISTRY, client, oldPolicy, policyMetadata, null);
504497

505498
// No change, because the policies were identical
506-
assertThat(updatedState, equalTo(existingState));
499+
assertThat(updatedProject, equalTo(existingProject));
507500

508501
actions = new HashMap<>();
509502
actions.put("rollover", new RolloverAction(null, null, null, 2L, null, null, null, null, null, null));
@@ -514,10 +507,10 @@ public void testUpdateIndicesForPolicy() throws IOException {
514507
policyMetadata = new LifecyclePolicyMetadata(newPolicy, Map.of(), 2L, 2L);
515508

516509
logger.info("--> update with changed policy, but not configured in settings");
517-
updatedState = updateIndicesForPolicy(existingState, REGISTRY, client, oldPolicy, policyMetadata, null);
510+
updatedProject = updateIndicesForPolicy(existingProject, REGISTRY, client, oldPolicy, policyMetadata, null);
518511

519512
// No change, because the index doesn't have a lifecycle.name setting for this policy
520-
assertThat(updatedState, equalTo(existingState));
513+
assertThat(updatedProject, equalTo(existingProject));
521514

522515
meta = IndexMetadata.builder(index)
523516
.settings(
@@ -528,14 +521,12 @@ public void testUpdateIndicesForPolicy() throws IOException {
528521
)
529522
.putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap())
530523
.build();
531-
existingState = ClusterState.builder(ClusterState.EMPTY_STATE)
532-
.metadata(Metadata.builder(Metadata.EMPTY_METADATA).put(meta, false).build())
533-
.build();
524+
existingProject = ProjectMetadata.builder(randomProjectIdOrDefault()).put(meta, false).build();
534525

535526
logger.info("--> update with changed policy and this index has the policy");
536-
updatedState = updateIndicesForPolicy(existingState, REGISTRY, client, oldPolicy, policyMetadata, null);
527+
updatedProject = updateIndicesForPolicy(existingProject, REGISTRY, client, oldPolicy, policyMetadata, null);
537528

538-
IndexMetadata newIdxMeta = updatedState.metadata().getProject().index(index);
529+
IndexMetadata newIdxMeta = updatedProject.index(index);
539530
LifecycleExecutionState afterExState = newIdxMeta.getLifecycleExecutionState();
540531
Map<String, String> beforeState = new HashMap<>(exState.asMap());
541532
beforeState.remove("phase_definition");
@@ -583,37 +574,18 @@ private static IndexMetadata.Builder mkMeta() {
583574
);
584575
}
585576

586-
static ClusterState updateIndicesForPolicy(
587-
final ClusterState clusterState,
577+
static ProjectMetadata updateIndicesForPolicy(
578+
ProjectMetadata project,
588579
final NamedXContentRegistry xContentRegistry,
589580
final Client client,
590581
final LifecyclePolicy oldPolicy,
591582
final LifecyclePolicyMetadata newPolicy,
592583
XPackLicenseState licenseState
593584
) {
594-
ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(clusterState);
595-
ProjectMetadata.Builder projectMetadataBuilder = ProjectMetadata.builder(projectMetadata);
596-
if (PhaseCacheManagement.updateIndicesForPolicy(
597-
projectMetadataBuilder,
598-
projectMetadata,
599-
xContentRegistry,
600-
client,
601-
oldPolicy,
602-
newPolicy,
603-
licenseState
604-
)) {
605-
return ClusterState.builder(clusterState).putProjectMetadata(projectMetadataBuilder).build();
585+
ProjectMetadata.Builder builder = ProjectMetadata.builder(project);
586+
if (PhaseCacheManagement.updateIndicesForPolicy(builder, project, xContentRegistry, client, oldPolicy, newPolicy, licenseState)) {
587+
return builder.build();
606588
}
607-
return clusterState;
608-
}
609-
610-
public static ClusterState refreshPhaseDefinition(
611-
final ClusterState clusterState,
612-
final String index,
613-
final LifecyclePolicyMetadata updatedPolicy
614-
) {
615-
ProjectMetadata projectMetadata = DefaultProjectResolver.INSTANCE.getProjectMetadata(clusterState);
616-
ProjectMetadata newProjectMetadata = PhaseCacheManagement.refreshPhaseDefinition(projectMetadata, index, updatedPolicy);
617-
return ClusterState.builder(clusterState).putProjectMetadata(newProjectMetadata).build();
589+
return project;
618590
}
619591
}

0 commit comments

Comments
 (0)