Skip to content

Commit f33ac4b

Browse files
authored
Fix tests in IndexLifecycleServiceTests (elastic#129449) (elastic#129542)
These two tests were ineffective because the result of the countdown latch was ignored. Correctly asserting the output of the latch showed that the test was failing, which was due to missing test setup - the policies were not defined in the registry and the lifecycle runner didn't execute the cluster state steps due to a missing task queue. Finally, some test objects were constructed incorrectly likely due to typos. (cherry picked from commit d384e2f)
1 parent 18b267f commit f33ac4b

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@
2828
import org.elasticsearch.common.settings.ClusterSettings;
2929
import org.elasticsearch.common.settings.Settings;
3030
import org.elasticsearch.common.transport.TransportAddress;
31+
import org.elasticsearch.common.util.set.Sets;
3132
import org.elasticsearch.core.TimeValue;
3233
import org.elasticsearch.index.Index;
3334
import org.elasticsearch.index.IndexSettings;
3435
import org.elasticsearch.index.IndexVersion;
36+
import org.elasticsearch.test.ClusterServiceUtils;
3537
import org.elasticsearch.test.ESTestCase;
3638
import org.elasticsearch.test.NodeRoles;
39+
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
3740
import org.elasticsearch.threadpool.TestThreadPool;
3841
import org.elasticsearch.threadpool.ThreadPool;
3942
import org.elasticsearch.xpack.core.ilm.CheckShrinkReadyStep;
@@ -348,7 +351,6 @@ public void testExceptionStillProcessesOtherIndicesOnMaster() {
348351
doTestExceptionStillProcessesOtherIndices(true);
349352
}
350353

351-
@SuppressWarnings("unchecked")
352354
public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) {
353355
String policy1 = randomAlphaOfLengthBetween(1, 20);
354356
Step.StepKey i1currentStepKey = randomStepKey();
@@ -377,7 +379,7 @@ public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) {
377379
}
378380
MockAction mockAction = new MockAction(List.of(i2mockStep));
379381
Phase i2phase = new Phase("phase", TimeValue.ZERO, Map.of("action", mockAction));
380-
LifecyclePolicy i2policy = newTestLifecyclePolicy(policy1, Map.of(i2phase.getName(), i1phase));
382+
LifecyclePolicy i2policy = newTestLifecyclePolicy(policy2, Map.of(i2phase.getName(), i2phase));
381383
Index index2 = new Index(
382384
randomValueOtherThan(index1.getName(), () -> randomAlphaOfLengthBetween(1, 20)),
383385
randomAlphaOfLengthBetween(1, 20)
@@ -403,8 +405,8 @@ public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) {
403405
((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep).setException(
404406
failStep1 ? new IllegalArgumentException("forcing a failure for index 1") : null
405407
);
406-
((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep).setLatch(stepLatch);
407-
((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep).setException(
408+
((IndexLifecycleRunnerTests.MockClusterStateActionStep) i2mockStep).setLatch(stepLatch);
409+
((IndexLifecycleRunnerTests.MockClusterStateActionStep) i2mockStep).setException(
408410
failStep1 ? null : new IllegalArgumentException("forcing a failure for index 2")
409411
);
410412
}
@@ -420,7 +422,7 @@ public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) {
420422
.numberOfReplicas(randomIntBetween(0, 5))
421423
.build();
422424
IndexMetadata i2indexMetadata = IndexMetadata.builder(index2.getName())
423-
.settings(settings(IndexVersion.current()).put(LifecycleSettings.LIFECYCLE_NAME, policy1))
425+
.settings(settings(IndexVersion.current()).put(LifecycleSettings.LIFECYCLE_NAME, policy2))
424426
.putCustom(ILM_CUSTOM_METADATA_KEY, i2lifecycleState.build().asMap())
425427
.numberOfShards(randomIntBetween(1, 5))
426428
.numberOfReplicas(randomIntBetween(0, 5))
@@ -433,23 +435,46 @@ public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) {
433435
.persistentSettings(settings(IndexVersion.current()).build())
434436
.build();
435437

438+
Settings settings = Settings.builder().put(LifecycleSettings.LIFECYCLE_POLL_INTERVAL, "1s").build();
439+
var clusterSettings = new ClusterSettings(
440+
settings,
441+
Sets.union(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS, Set.of(LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING))
442+
);
443+
ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool, clusterSettings);
444+
DiscoveryNode node = clusterService.localNode();
436445
ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT)
437446
.metadata(metadata)
438-
.nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build())
447+
.nodes(DiscoveryNodes.builder().add(node).masterNodeId(node.getId()).localNodeId(node.getId()))
439448
.build();
449+
ClusterServiceUtils.setState(clusterService, currentState);
450+
451+
indexLifecycleService = new IndexLifecycleService(
452+
Settings.EMPTY,
453+
mock(Client.class),
454+
clusterService,
455+
threadPool,
456+
systemUTC(),
457+
() -> now,
458+
null,
459+
null,
460+
null
461+
);
440462

463+
ClusterChangedEvent event = new ClusterChangedEvent("_source", currentState, ClusterState.EMPTY_STATE);
464+
indexLifecycleService.applyClusterState(event);
441465
if (useOnMaster) {
442-
when(clusterService.state()).thenReturn(currentState);
443466
indexLifecycleService.onMaster(currentState);
444467
} else {
445-
indexLifecycleService.triggerPolicies(currentState, randomBoolean());
468+
// TODO: this relies on the master task queue
469+
indexLifecycleService.triggerPolicies(currentState, true);
446470
}
447471
try {
448-
stepLatch.await(5, TimeUnit.SECONDS);
472+
ElasticsearchAssertions.awaitLatch(stepLatch, 5, TimeUnit.SECONDS);
449473
} catch (InterruptedException e) {
450474
logger.error("failure while waiting for step execution", e);
451475
fail("both steps should have been executed, even with an exception");
452476
}
477+
clusterService.close();
453478
}
454479

455480
public void testClusterChangedWaitsForTheStateToBeRecovered() {

0 commit comments

Comments
 (0)