|
@@ -12,6 +12,7 @@ import org.elasticsearch.action.admin.indices.rollover.RolloverConfiguration;
|
|
|
import org.elasticsearch.cluster.metadata.DataStreamGlobalRetention;
|
|
|
import org.elasticsearch.cluster.metadata.DataStreamLifecycle;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
+import org.elasticsearch.common.bytes.BytesReference;
|
|
|
import org.elasticsearch.common.xcontent.XContentHelper;
|
|
|
import org.elasticsearch.core.TimeValue;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
@@ -20,18 +21,80 @@ import org.elasticsearch.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.xcontent.XContentType;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
-import static org.elasticsearch.rest.RestRequest.PATH_RESTRICTED;
|
|
|
+import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
+import static org.hamcrest.Matchers.nullValue;
|
|
|
|
|
|
public class GetDataStreamLifecycleActionTests extends ESTestCase {
|
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public void testDefaultLifecycleResponseToXContent() throws Exception {
|
|
|
+ boolean isInternalDataStream = randomBoolean();
|
|
|
+ GetDataStreamLifecycleAction.Response.DataStreamLifecycle dataStreamLifecycle = createDataStreamLifecycle(
|
|
|
+ DataStreamLifecycle.DEFAULT,
|
|
|
+ isInternalDataStream
|
|
|
+ );
|
|
|
+ GetDataStreamLifecycleAction.Response response = new GetDataStreamLifecycleAction.Response(List.of(dataStreamLifecycle));
|
|
|
+ try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
|
|
|
+ builder.humanReadable(true);
|
|
|
+ response.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> {
|
|
|
+ try {
|
|
|
+ xcontent.toXContent(builder, EMPTY_PARAMS);
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
+ fail(e.getMessage());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ Map<String, Object> resultMap = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
|
|
|
+ assertThat(resultMap.get("global_retention"), equalTo(Map.of()));
|
|
|
+ assertThat(resultMap.containsKey("data_streams"), equalTo(true));
|
|
|
+ List<Map<String, Object>> dataStreams = (List<Map<String, Object>>) resultMap.get("data_streams");
|
|
|
+ Map<String, Object> firstDataStream = dataStreams.get(0);
|
|
|
+ assertThat(firstDataStream.containsKey("lifecycle"), equalTo(true));
|
|
|
+ Map<String, Object> lifecycleResult = (Map<String, Object>) firstDataStream.get("lifecycle");
|
|
|
+ assertThat(lifecycleResult.get("enabled"), equalTo(true));
|
|
|
+ assertThat(lifecycleResult.get("data_retention"), nullValue());
|
|
|
+ assertThat(lifecycleResult.get("effective_retention"), nullValue());
|
|
|
+ assertThat(lifecycleResult.get("retention_determined_by"), nullValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public void testGlobalRetentionToXContent() {
|
|
|
+ TimeValue globalDefaultRetention = TimeValue.timeValueDays(10);
|
|
|
+ TimeValue globalMaxRetention = TimeValue.timeValueDays(50);
|
|
|
+ DataStreamGlobalRetention globalRetention = new DataStreamGlobalRetention(globalDefaultRetention, globalMaxRetention);
|
|
|
+ GetDataStreamLifecycleAction.Response response = new GetDataStreamLifecycleAction.Response(List.of(), null, globalRetention);
|
|
|
+ try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
|
|
|
+ builder.humanReadable(true);
|
|
|
+ response.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> {
|
|
|
+ try {
|
|
|
+ xcontent.toXContent(builder, EMPTY_PARAMS);
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
+ fail(e.getMessage());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ Map<String, Object> resultMap = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
|
|
|
+ assertThat(resultMap.containsKey("global_retention"), equalTo(true));
|
|
|
+ Map<String, String> globalRetentionMap = (Map<String, String>) resultMap.get("global_retention");
|
|
|
+ assertThat(globalRetentionMap.get("max_retention"), equalTo(globalMaxRetention.getStringRep()));
|
|
|
+ assertThat(globalRetentionMap.get("default_retention"), equalTo(globalDefaultRetention.getStringRep()));
|
|
|
+ assertThat(resultMap.containsKey("data_streams"), equalTo(true));
|
|
|
+ } catch (Exception e) {
|
|
|
+ fail(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public void testDataStreamLifecycleToXContent() throws Exception {
|
|
|
TimeValue configuredRetention = TimeValue.timeValueDays(100);
|
|
|
TimeValue globalDefaultRetention = TimeValue.timeValueDays(10);
|
|
|
TimeValue globalMaxRetention = TimeValue.timeValueDays(50);
|
|
|
+ DataStreamGlobalRetention globalRetention = new DataStreamGlobalRetention(globalDefaultRetention, globalMaxRetention);
|
|
|
DataStreamLifecycle lifecycle = new DataStreamLifecycle(new DataStreamLifecycle.Retention(configuredRetention), null, null);
|
|
|
{
|
|
|
boolean isInternalDataStream = true;
|
|
@@ -39,7 +102,7 @@ public class GetDataStreamLifecycleActionTests extends ESTestCase {
|
|
|
lifecycle,
|
|
|
isInternalDataStream
|
|
|
);
|
|
|
- Map<String, Object> resultMap = getXContentMap(explainIndexDataStreamLifecycle, globalDefaultRetention, globalMaxRetention);
|
|
|
+ Map<String, Object> resultMap = getXContentMap(explainIndexDataStreamLifecycle, globalRetention);
|
|
|
Map<String, Object> lifecycleResult = (Map<String, Object>) resultMap.get("lifecycle");
|
|
|
assertThat(lifecycleResult.get("data_retention"), equalTo(configuredRetention.getStringRep()));
|
|
|
assertThat(lifecycleResult.get("effective_retention"), equalTo(configuredRetention.getStringRep()));
|
|
@@ -51,7 +114,7 @@ public class GetDataStreamLifecycleActionTests extends ESTestCase {
|
|
|
lifecycle,
|
|
|
isInternalDataStream
|
|
|
);
|
|
|
- Map<String, Object> resultMap = getXContentMap(explainIndexDataStreamLifecycle, globalDefaultRetention, globalMaxRetention);
|
|
|
+ Map<String, Object> resultMap = getXContentMap(explainIndexDataStreamLifecycle, globalRetention);
|
|
|
Map<String, Object> lifecycleResult = (Map<String, Object>) resultMap.get("lifecycle");
|
|
|
assertThat(lifecycleResult.get("data_retention"), equalTo(configuredRetention.getStringRep()));
|
|
|
assertThat(lifecycleResult.get("effective_retention"), equalTo(globalMaxRetention.getStringRep()));
|
|
@@ -71,14 +134,11 @@ public class GetDataStreamLifecycleActionTests extends ESTestCase {
|
|
|
*/
|
|
|
private Map<String, Object> getXContentMap(
|
|
|
GetDataStreamLifecycleAction.Response.DataStreamLifecycle dataStreamLifecycle,
|
|
|
- TimeValue globalDefaultRetention,
|
|
|
- TimeValue globalMaxRetention
|
|
|
+ DataStreamGlobalRetention globalRetention
|
|
|
) throws IOException {
|
|
|
try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
|
|
|
- ToXContent.Params params = new ToXContent.MapParams(Map.of(PATH_RESTRICTED, "serverless"));
|
|
|
RolloverConfiguration rolloverConfiguration = null;
|
|
|
- DataStreamGlobalRetention globalRetention = new DataStreamGlobalRetention(globalDefaultRetention, globalMaxRetention);
|
|
|
- dataStreamLifecycle.toXContent(builder, params, rolloverConfiguration, globalRetention);
|
|
|
+ dataStreamLifecycle.toXContent(builder, ToXContent.EMPTY_PARAMS, rolloverConfiguration, globalRetention);
|
|
|
String serialized = Strings.toString(builder);
|
|
|
return XContentHelper.convertToMap(XContentType.JSON.xContent(), serialized, randomBoolean());
|
|
|
}
|