Skip to content

Commit 5f3541f

Browse files
committed
migrate cleanup of ILM to ESRestTestCase
related #34530.
1 parent a83a464 commit 5f3541f

File tree

7 files changed

+76
-86
lines changed

7 files changed

+76
-86
lines changed

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ protected static RestClient adminClient() {
210210
/**
211211
* Returns whether to preserve the state of the cluster upon completion of this test. Defaults to false. If true, overrides the value of
212212
* {@link #preserveIndicesUponCompletion()}, {@link #preserveTemplatesUponCompletion()}, {@link #preserveReposUponCompletion()},
213-
* {@link #preserveSnapshotsUponCompletion()}, and {@link #preserveRollupJobsUponCompletion()}.
213+
* {@link #preserveSnapshotsUponCompletion()},{@link #preserveRollupJobsUponCompletion()},
214+
* and {@link #preserveILMPoliciesUponCompletion()}.
214215
*
215216
* @return true if the state of the cluster should be preserved
216217
*/
@@ -275,6 +276,15 @@ protected boolean preserveRollupJobsUponCompletion() {
275276
return false;
276277
}
277278

279+
/**
280+
* Returns whether to preserve ILM Policies of this test. Defaults to not
281+
* preserviing them. Only runs at all if xpack is installed on the cluster
282+
* being tested.
283+
*/
284+
protected boolean preserveILMPoliciesUponCompletion() {
285+
return false;
286+
}
287+
278288
private void wipeCluster() throws Exception {
279289
boolean hasXPack = hasXPack();
280290

@@ -329,6 +339,11 @@ private void wipeCluster() throws Exception {
329339
wipeRollupJobs();
330340
waitForPendingRollupTasks();
331341
}
342+
343+
if (hasXPack && false == preserveILMPoliciesUponCompletion()) {
344+
removePoliciesFromAllIndexes();
345+
deleteAllPolicies();
346+
}
332347
}
333348

334349
/**
@@ -441,6 +456,46 @@ private void waitForPendingRollupTasks() throws Exception {
441456
});
442457
}
443458

459+
private static void removePoliciesFromAllIndexes() throws IOException {
460+
Response response = adminClient().performRequest(new Request("GET", "/_all"));
461+
Map<String, Object> indexes = ESRestTestCase.entityAsMap(response);
462+
463+
if (indexes == null || indexes.isEmpty()) {
464+
return;
465+
}
466+
467+
for (String indexName : indexes.keySet()) {
468+
try {
469+
adminClient().performRequest(new Request("DELETE", indexName + "/_ilm/"));
470+
} catch (Exception e) {
471+
// ok
472+
}
473+
}
474+
}
475+
476+
private static void deleteAllPolicies() throws Exception {
477+
Map<String, Object> policies;
478+
479+
try {
480+
Response response = adminClient().performRequest(new Request("GET", "/_ilm"));
481+
policies = ESRestTestCase.entityAsMap(response);
482+
} catch (Exception e) {
483+
return;
484+
}
485+
486+
if (policies == null || policies.isEmpty()) {
487+
return;
488+
}
489+
490+
for (String policyName : policies.keySet()) {
491+
try {
492+
adminClient().performRequest(new Request("DELETE", "/_ilm/" + policyName));
493+
} catch (Exception e) {
494+
// ok
495+
}
496+
}
497+
}
498+
444499
/**
445500
* Logs a message if there are still running tasks. The reasoning is that any tasks still running are state the is trying to bleed into
446501
* other tests.

test/framework/src/main/java/org/elasticsearch/upgrades/AbstractFullClusterRestartTestCase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ protected boolean preserveClusterSettings() {
6666
protected boolean preserveRollupJobsUponCompletion() {
6767
return true;
6868
}
69+
70+
@Override
71+
protected boolean preserveILMPoliciesUponCompletion() {
72+
return true;
73+
}
6974
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ILMRestTestStateCleaner.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse;
2222
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
2323
import org.elasticsearch.test.rest.yaml.ObjectPath;
24-
import org.elasticsearch.xpack.core.indexlifecycle.ILMRestTestStateCleaner;
2524
import org.elasticsearch.xpack.core.ml.MlMetaIndex;
2625
import org.elasticsearch.xpack.core.ml.integration.MlRestTestStateCleaner;
2726
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex;
@@ -245,7 +244,6 @@ private void disableMonitoring() throws Exception {
245244
public void cleanup() throws Exception {
246245
disableMonitoring();
247246
clearMlState();
248-
clearILMState();
249247
if (isWaitForPendingTasks()) {
250248
// This waits for pending tasks to complete, so must go last (otherwise
251249
// it could be waiting for pending tasks while monitoring is still running).
@@ -265,20 +263,6 @@ private void clearMlState() throws Exception {
265263
}
266264
}
267265

268-
private void clearILMState() throws Exception {
269-
if (isILMTest()) {
270-
ILMRestTestStateCleaner.clearILMMetadata(adminClient());
271-
}
272-
}
273-
274-
/**
275-
276-
private void clearILMState() throws Exception {
277-
if (isILMTest()) {
278-
ILMRestTestStateCleaner.clearILMMetadata(adminClient());
279-
}
280-
}
281-
282266
/**
283267
* Executes an API call using the admin context, waiting for it to succeed.
284268
*/
@@ -339,12 +323,6 @@ protected boolean isMachineLearningTest() {
339323
return testName != null && (testName.contains("=ml/") || testName.contains("=ml\\"));
340324
}
341325

342-
protected boolean isILMTest() {
343-
String testName = getTestName();
344-
return testName != null && (testName.contains("=ilm/") || testName.contains("=ilm\\"))
345-
|| (testName.contains("/ilm/") || testName.contains("\\ilm\\"));
346-
}
347-
348326
/**
349327
* Should each test wait for pending tasks to finish after execution?
350328
* @return Wait for pending tasks

x-pack/qa/rolling-upgrade-basic/src/test/java/org/elasticsearch/upgrades/AbstractUpgradeTestCase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ protected boolean preserveRollupJobsUponCompletion() {
2929
return true;
3030
}
3131

32+
@Override
33+
protected boolean preserveILMPoliciesUponCompletion() {
34+
return true;
35+
}
36+
3237
enum CLUSTER_TYPE {
3338
OLD,
3439
MIXED,

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/AbstractUpgradeTestCase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ protected boolean preserveRollupJobsUponCompletion() {
4343
return true;
4444
}
4545

46+
@Override
47+
protected boolean preserveILMPoliciesUponCompletion() {
48+
return true;
49+
}
50+
4651
enum ClusterType {
4752
OLD,
4853
MIXED,

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/UpgradeClusterClientYamlTestSuiteIT.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ protected boolean preserveRollupJobsUponCompletion() {
4747
return true;
4848
}
4949

50+
@Override
51+
protected boolean preserveILMPoliciesUponCompletion() {
52+
return true;
53+
}
54+
5055
public UpgradeClusterClientYamlTestSuiteIT(ClientYamlTestCandidate testCandidate) {
5156
super(testCandidate);
5257
}

0 commit comments

Comments
 (0)