Skip to content

Commit b06098a

Browse files
committed
trying add ilm tests
1 parent bc54710 commit b06098a

File tree

2 files changed

+147
-15
lines changed

2 files changed

+147
-15
lines changed

x-pack/plugin/migrate/src/internalClusterTest/java/org/elasticsearch/xpack/migrate/action/CopyIndexMetadataTransportActionIT.java

Lines changed: 141 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,30 @@
2121
import org.elasticsearch.action.support.IndicesOptions;
2222
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
2323
import org.elasticsearch.cluster.metadata.IndexMetadata;
24+
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
2425
import org.elasticsearch.cluster.metadata.Template;
2526
import org.elasticsearch.common.compress.CompressedXContent;
27+
import org.elasticsearch.common.settings.Settings;
28+
import org.elasticsearch.core.TimeValue;
2629
import org.elasticsearch.datastreams.DataStreamsPlugin;
27-
import org.elasticsearch.ingest.common.IngestCommonPlugin;
2830
import org.elasticsearch.plugins.Plugin;
2931
import org.elasticsearch.reindex.ReindexPlugin;
3032
import org.elasticsearch.test.ESIntegTestCase;
3133
import org.elasticsearch.test.transport.MockTransportService;
3234
import org.elasticsearch.xcontent.json.JsonXContent;
35+
//import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin;
36+
import org.elasticsearch.xpack.core.ilm.LifecyclePolicy;
37+
import org.elasticsearch.xpack.core.ilm.Phase;
38+
import org.elasticsearch.xpack.core.ilm.StartILMRequest;
39+
import org.elasticsearch.xpack.core.ilm.action.ILMActions;
40+
import org.elasticsearch.xpack.core.ilm.action.PutLifecycleRequest;
41+
//import org.elasticsearch.xpack.ilm.IndexLifecycle;
3342
import org.elasticsearch.xpack.migrate.MigratePlugin;
3443

3544
import java.util.Collection;
3645
import java.util.List;
3746
import java.util.Locale;
47+
import java.util.Map;
3848
import java.util.concurrent.TimeUnit;
3949

4050
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
@@ -44,11 +54,11 @@ public class CopyIndexMetadataTransportActionIT extends ESIntegTestCase {
4454
@Override
4555
protected Collection<Class<? extends Plugin>> nodePlugins() {
4656
return List.of(
57+
// LocalStateCompositeXPackPlugin.class,
4758
MigratePlugin.class,
4859
ReindexPlugin.class,
4960
MockTransportService.TestPlugin.class,
50-
DataStreamsPlugin.class,
51-
IngestCommonPlugin.class
61+
DataStreamsPlugin.class
5262
);
5363
}
5464

@@ -84,9 +94,105 @@ public void testCreationDate() throws Exception {
8494
assertEquals(sourceDate, destDate);
8595
}
8696

97+
public void testILMState() throws Exception {
98+
99+
Map<String, Phase> phases = Map.of(
100+
"warm",
101+
new Phase(
102+
"warm",
103+
TimeValue.ZERO,
104+
Map.of("rollover", new org.elasticsearch.xpack.core.ilm.RolloverAction(null, null, null, 1L, null, null, null, null, null, null))
105+
)
106+
);
107+
108+
var policyName = "my-policy";
109+
LifecyclePolicy policy = new LifecyclePolicy(policyName, phases);
110+
PutLifecycleRequest putLifecycleRequest = new PutLifecycleRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, policy);
111+
assertAcked(client().execute(ILMActions.PUT, putLifecycleRequest).actionGet());
112+
113+
var dataStream = createDataStream(policyName);
114+
115+
assertAcked(safeGet(client().execute(ILMActions.START, new StartILMRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT))));
116+
117+
// rollover a few times
118+
createDocument(dataStream);
119+
createDocument(dataStream);
120+
createDocument(dataStream);
121+
122+
var writeIndex = safeGet(
123+
indicesAdmin().execute(
124+
GetDataStreamAction.INSTANCE,
125+
new GetDataStreamAction.Request(TEST_REQUEST_TIMEOUT, new String[] { dataStream })
126+
)
127+
).getDataStreams().get(0).getDataStream().getWriteIndex().getName();
128+
129+
var getIndexResponse = indicesAdmin().getIndex(new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(dataStream)).get();
130+
for (var backingIndex : getIndexResponse.indices()) {
131+
132+
var destIndex = randomAlphaOfLength(20).toLowerCase(Locale.ROOT);
133+
indicesAdmin().create(new CreateIndexRequest(destIndex)).get();
134+
135+
var metadataBefore = safeGet(
136+
clusterAdmin().state(new ClusterStateRequest(TEST_REQUEST_TIMEOUT).indices(backingIndex, destIndex))
137+
).getState().metadata();
138+
IndexMetadata source = metadataBefore.index(backingIndex);
139+
IndexMetadata destBefore = metadataBefore.index(destIndex);
140+
141+
assertNotEquals(
142+
source.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY),
143+
destBefore.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY)
144+
);
145+
146+
// sanity check not equal before the copy
147+
if (backingIndex.equals(writeIndex)) {
148+
assertTrue(source.getRolloverInfos().isEmpty());
149+
assertTrue(destBefore.getRolloverInfos().isEmpty());
150+
} else {
151+
assertNotEquals(source.getRolloverInfos(), destBefore.getRolloverInfos());
152+
}
153+
154+
// copy over the metadata
155+
copyMetadata(backingIndex, destIndex);
156+
157+
var metadataAfter = safeGet(clusterAdmin().state(new ClusterStateRequest(TEST_REQUEST_TIMEOUT).indices(destIndex)))
158+
.getState()
159+
.metadata();
160+
IndexMetadata destAfter = metadataAfter.index(destIndex);
161+
162+
// now rollover info should be equal
163+
assertEquals(source.getRolloverInfos(), destAfter.getRolloverInfos());
164+
165+
Map<String, String> customData = source.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY);
166+
//
167+
assertEquals(
168+
source.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY),
169+
destAfter.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY)
170+
);
171+
172+
}
173+
}
174+
175+
176+
177+
87178
public void testRolloverInfos() throws Exception {
88179

89-
var dataStream = createDataStream();
180+
var dataStream = createDataStream(null);
181+
Map<String, Phase> phases = Map.of(
182+
"warm",
183+
new Phase(
184+
"warm",
185+
TimeValue.ZERO,
186+
Map.of("rollover", new org.elasticsearch.xpack.core.ilm.RolloverAction(null, null, null, 1L, null, null, null, null, null, null))
187+
)
188+
);
189+
190+
var policyName = "my-policy";
191+
LifecyclePolicy policy = new LifecyclePolicy(policyName, phases);
192+
PutLifecycleRequest putLifecycleRequest = new PutLifecycleRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, policy);
193+
assertAcked(client().execute(ILMActions.PUT, putLifecycleRequest).actionGet());
194+
195+
90196

91197
// rollover a few times
92198
createDocument(dataStream);
@@ -115,6 +221,11 @@ public void testRolloverInfos() throws Exception {
115221
IndexMetadata source = metadataBefore.index(backingIndex);
116222
IndexMetadata destBefore = metadataBefore.index(destIndex);
117223

224+
// assertNotEquals(
225+
// source.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY),
226+
// destBefore.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY)
227+
// );
228+
118229
// sanity check not equal before the copy
119230
if (backingIndex.equals(writeIndex)) {
120231
assertTrue(source.getRolloverInfos().isEmpty());
@@ -133,15 +244,37 @@ public void testRolloverInfos() throws Exception {
133244

134245
// now rollover info should be equal
135246
assertEquals(source.getRolloverInfos(), destAfter.getRolloverInfos());
247+
248+
Map<String, String> customData = source.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY);
249+
//
250+
assertEquals(
251+
source.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY),
252+
destAfter.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY)
253+
);
254+
136255
}
137256
}
138257

139-
private String createDataStream() throws Exception {
258+
private String createDataStream(String ilmPolicy) throws Exception {
140259
String dataStreamName = randomAlphaOfLength(10).toLowerCase(Locale.getDefault());
141260

142-
Template idxTemplate = new Template(null, new CompressedXContent("""
143-
{"properties":{"@timestamp":{"type":"date"},"data":{"type":"keyword"}}}
144-
"""), null);
261+
Settings settings = ilmPolicy != null ?
262+
Settings.builder().put(IndexMetadata.LIFECYCLE_NAME, ilmPolicy).build()
263+
: null;
264+
265+
String mapping = """
266+
{
267+
"properties": {
268+
"@timestamp": {
269+
"type":"date"
270+
},
271+
"data":{
272+
"type":"keyword"
273+
}
274+
}
275+
}
276+
""";
277+
Template idxTemplate = new Template(settings, new CompressedXContent(mapping), null);
145278

146279
ComposableIndexTemplate template = ComposableIndexTemplate.builder()
147280
.indexPatterns(List.of(dataStreamName + "*"))

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,12 @@ private void compareIndexMetadata(
217217
Map<String, Object> upgradedIndexMetadata = upgradedIndexEntry.getValue();
218218
compareSettings(oldIndexMetadata, upgradedIndexMetadata);
219219
assertThat("Mappings did not match", upgradedIndexMetadata.get("mappings"), equalTo(oldIndexMetadata.get("mappings")));
220-
// TODO: Uncomment the following two checks once we are correctly copying this state over:
221-
// assertThat("ILM states did not match", upgradedIndexMetadata.get("ilm"), equalTo(oldIndexMetadata.get("ilm")));
222-
// assertThat(
223-
// "Rollover info did not match",
224-
// upgradedIndexMetadata.get("rollover_info"),
225-
// equalTo(oldIndexMetadata.get("rollover_info"))
226-
// );
220+
assertThat("ILM states did not match", upgradedIndexMetadata.get("ilm"), equalTo(oldIndexMetadata.get("ilm")));
221+
assertThat(
222+
"Rollover info did not match",
223+
upgradedIndexMetadata.get("rollover_info"),
224+
equalTo(oldIndexMetadata.get("rollover_info"))
225+
);
227226
assertThat(upgradedIndexMetadata.get("system"), equalTo(oldIndexMetadata.get("system")));
228227
}
229228
}

0 commit comments

Comments
 (0)