|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.ilm.action; |
9 | 9 |
|
10 | | -import org.elasticsearch.ElasticsearchException; |
11 | 10 | import org.elasticsearch.cluster.metadata.IndexMetadata; |
12 | 11 | import org.elasticsearch.cluster.metadata.LifecycleExecutionState; |
13 | 12 | import org.elasticsearch.cluster.metadata.ProjectMetadata; |
14 | | -import org.elasticsearch.common.Strings; |
15 | 13 | import org.elasticsearch.index.IndexVersion; |
16 | 14 | import org.elasticsearch.test.ESTestCase; |
17 | 15 | import org.elasticsearch.xcontent.NamedXContentRegistry; |
18 | 16 | import org.elasticsearch.xcontent.ParseField; |
19 | | -import org.elasticsearch.xcontent.ToXContent; |
20 | | -import org.elasticsearch.xcontent.XContentFactory; |
21 | | -import org.elasticsearch.xcontent.XContentParser; |
22 | | -import org.elasticsearch.xcontent.XContentParserConfiguration; |
23 | | -import org.elasticsearch.xcontent.XContentType; |
24 | 17 | import org.elasticsearch.xpack.core.ilm.ErrorStep; |
25 | 18 | import org.elasticsearch.xpack.core.ilm.IndexLifecycleExplainResponse; |
26 | 19 | import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; |
|
38 | 31 |
|
39 | 32 | import static org.elasticsearch.cluster.metadata.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; |
40 | 33 | import static org.elasticsearch.xpack.ilm.action.TransportExplainLifecycleAction.getIndexLifecycleExplainResponse; |
41 | | -import static org.hamcrest.Matchers.equalTo; |
42 | 34 | import static org.hamcrest.Matchers.is; |
43 | 35 | import static org.hamcrest.Matchers.notNullValue; |
44 | 36 | import static org.hamcrest.Matchers.nullValue; |
@@ -225,84 +217,6 @@ public void testGetIndexLifecycleExplainResponse_rolloverOnlyIfHasDocuments_adds |
225 | 217 | assertThat(rolloverAction.getConditions().getMinDocs(), is(1L)); |
226 | 218 | } |
227 | 219 |
|
228 | | - public void testPreviousStepInfoTruncationDoesNotBreakExplainJson() throws Exception { |
229 | | - final String policyName = "policy"; |
230 | | - final String indexName = "index"; |
231 | | - |
232 | | - final String longReasonMessage = "a".repeat(LifecycleExecutionState.MAXIMUM_STEP_INFO_STRING_LENGTH); |
233 | | - final String errorJsonWhichWillBeTruncated = Strings.toString((builder, params) -> { |
234 | | - ElasticsearchException.generateThrowableXContent( |
235 | | - builder, |
236 | | - ToXContent.EMPTY_PARAMS, |
237 | | - new IllegalArgumentException(longReasonMessage) |
238 | | - ); |
239 | | - return builder; |
240 | | - }); |
241 | | - |
242 | | - final LifecycleExecutionState stateWithTruncatedStepInfo = LifecycleExecutionState.builder() |
243 | | - .setPhase("hot") |
244 | | - .setAction("rollover") |
245 | | - .setStep("some_step") |
246 | | - .setPhaseDefinition(PHASE_DEFINITION) |
247 | | - .setStepInfo(errorJsonWhichWillBeTruncated) |
248 | | - .build(); |
249 | | - |
250 | | - // Simulate transition to next step where previous_step_info is copied |
251 | | - final LifecycleExecutionState stateWithPreviousStepInfo = LifecycleExecutionState.builder(stateWithTruncatedStepInfo) |
252 | | - .setPreviousStepInfo(stateWithTruncatedStepInfo.stepInfo()) |
253 | | - .setStepInfo(null) |
254 | | - .build(); |
255 | | - |
256 | | - final IndexMetadata indexMetadata = IndexMetadata.builder(indexName) |
257 | | - .settings(settings(IndexVersion.current()).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) |
258 | | - .numberOfShards(1) |
259 | | - .numberOfReplicas(0) |
260 | | - .putCustom(ILM_CUSTOM_METADATA_KEY, stateWithPreviousStepInfo.asMap()) |
261 | | - .build(); |
262 | | - |
263 | | - final ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) |
264 | | - .put(indexMetadata, true) |
265 | | - .putCustom( |
266 | | - IndexLifecycleMetadata.TYPE, |
267 | | - new IndexLifecycleMetadata( |
268 | | - Map.of(policyName, LifecyclePolicyMetadataTests.createRandomPolicyMetadata(policyName)), |
269 | | - randomFrom(OperationMode.values()) |
270 | | - ) |
271 | | - ) |
272 | | - .build(); |
273 | | - |
274 | | - final IndexLifecycleExplainResponse response = TransportExplainLifecycleAction.getIndexLifecycleExplainResponse( |
275 | | - indexName, |
276 | | - project, |
277 | | - false, |
278 | | - true, |
279 | | - REGISTRY, |
280 | | - randomBoolean() |
281 | | - ); |
282 | | - |
283 | | - final String serialized = Strings.toString(response); |
284 | | - // test we produce valid JSON |
285 | | - try ( |
286 | | - XContentParser p = XContentFactory.xContent(XContentType.JSON) |
287 | | - .createParser(XContentParserConfiguration.EMPTY.withRegistry(REGISTRY), serialized) |
288 | | - ) { |
289 | | - final IndexLifecycleExplainResponse deserialized = IndexLifecycleExplainResponse.PARSER.apply(p, null); |
290 | | - assertThat(deserialized.toString(), equalTo(response.toString())); |
291 | | - final String actualPreviousStepInfo = deserialized.getPreviousStepInfo().utf8ToString(); |
292 | | - |
293 | | - final String expectedPreviousStepInfoFormat = """ |
294 | | - {"type":"illegal_argument_exception","reason":"%s"}"""; |
295 | | - final int jsonBaseLength = Strings.format(expectedPreviousStepInfoFormat, "").length(); |
296 | | - final String expectedReason = Strings.format( |
297 | | - "%s... (%d chars truncated)", |
298 | | - "a".repeat(LifecycleExecutionState.MAXIMUM_STEP_INFO_STRING_LENGTH - jsonBaseLength), |
299 | | - jsonBaseLength |
300 | | - ); |
301 | | - final String expectedPreviousStepInfo = Strings.format(expectedPreviousStepInfoFormat, expectedReason); |
302 | | - assertEquals(actualPreviousStepInfo, expectedPreviousStepInfo); |
303 | | - } |
304 | | - } |
305 | | - |
306 | 220 | private static IndexLifecycleMetadata createIndexLifecycleMetadata() { |
307 | 221 | return new IndexLifecycleMetadata( |
308 | 222 | Map.of(POLICY_NAME, LifecyclePolicyMetadataTests.createRandomPolicyMetadata(POLICY_NAME)), |
|
0 commit comments