|
57 | 57 | import org.opensearch.dataprepper.plugins.sink.opensearch.index.IndexConfiguration; |
58 | 58 | import org.opensearch.dataprepper.plugins.sink.opensearch.index.IndexConstants; |
59 | 59 | import org.opensearch.dataprepper.plugins.sink.opensearch.index.IndexType; |
| 60 | +import org.opensearch.dataprepper.plugins.sink.opensearch.index.TemplateType; |
60 | 61 |
|
61 | 62 | import javax.ws.rs.HttpMethod; |
62 | 63 | import java.io.BufferedReader; |
@@ -339,6 +340,71 @@ void testInstantiateSinkLogsDefaultLogSink() throws IOException { |
339 | 340 | } |
340 | 341 | } |
341 | 342 |
|
| 343 | + @Test |
| 344 | + @DisabledIf(value = "isES6", disabledReason = LOG_INGESTION_TEST_DISABLED_REASON) |
| 345 | + @Timeout(value = 50, unit = TimeUnit.SECONDS) |
| 346 | + void testInstantiateSinkLogsPlainWithTemplateTypeUsesIndexType() throws IOException { |
| 347 | + final OpenSearchSinkConfig openSearchSinkConfig = generateOpenSearchSinkConfig( |
| 348 | + IndexType.LOG_ANALYTICS_PLAIN.getValue(), |
| 349 | + null, |
| 350 | + TemplateType.INDEX_TEMPLATE.getTypeName(), |
| 351 | + null); |
| 352 | + OpenSearchSink sink = createObjectUnderTest(openSearchSinkConfig, true); |
| 353 | + |
| 354 | + final String indexAlias = IndexConstants.TYPE_TO_DEFAULT_ALIAS.get(IndexType.LOG_ANALYTICS_PLAIN); |
| 355 | + final String expectedIndexTemplateName = indexAlias + "-index-template"; |
| 356 | + |
| 357 | + // Verify composable index template is created |
| 358 | + Request getTemplateRequest = new Request(HttpMethod.GET, "/_index_template/" + expectedIndexTemplateName); |
| 359 | + Response getTemplateResponse = client.performRequest(getTemplateRequest); |
| 360 | + assertThat(getTemplateResponse.getStatusLine().getStatusCode(), equalTo(SC_OK)); |
| 361 | + String getTemplateResponseBody = EntityUtils.toString(getTemplateResponse.getEntity()); |
| 362 | + @SuppressWarnings("unchecked") final List<Map<String, Map<String, Object>>> indexTemplates = |
| 363 | + (List<Map<String, Map<String, Object>>>) createContentParser(XContentType.JSON.xContent(), getTemplateResponseBody) |
| 364 | + .map().get("index_templates"); |
| 365 | + assertThat(indexTemplates, notNullValue()); |
| 366 | + assertThat(indexTemplates.isEmpty(), equalTo(false)); |
| 367 | + @SuppressWarnings("unchecked") final List<String> indexPatterns = |
| 368 | + (List<String>) indexTemplates.get(0).get("index_template").get("index_patterns"); |
| 369 | + assertThat(indexPatterns, hasItem(indexAlias + "-*")); |
| 370 | + |
| 371 | + Request request = new Request(HttpMethod.HEAD, indexAlias); |
| 372 | + Response response = client.performRequest(request); |
| 373 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(SC_OK)); |
| 374 | + |
| 375 | + final String index = String.format("%s-000001", indexAlias); |
| 376 | + final Map<String, Object> mappings = getIndexMappings(index); |
| 377 | + assertThat(mappings, notNullValue()); |
| 378 | + assertThat((boolean) mappings.get("date_detection"), equalTo(false)); |
| 379 | + |
| 380 | + if (isOSBundle()) { |
| 381 | + await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> { |
| 382 | + assertThat(getIndexPolicyId(index), equalTo(IndexConstants.LOGS_ISM_POLICY)); |
| 383 | + } |
| 384 | + ); |
| 385 | + } |
| 386 | + |
| 387 | + // roll over initial index |
| 388 | + request = new Request(HttpMethod.POST, String.format("%s/_rollover", indexAlias)); |
| 389 | + request.setJsonEntity("{ \"conditions\" : { } }\n"); |
| 390 | + response = client.performRequest(request); |
| 391 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(SC_OK)); |
| 392 | + |
| 393 | + // Instantiate sink again |
| 394 | + sink = createObjectUnderTest(openSearchSinkConfig, true); |
| 395 | + // Make sure no new write index *-000001 is created under alias |
| 396 | + final String rolloverIndexName = String.format("%s-000002", indexAlias); |
| 397 | + request = new Request(HttpMethod.GET, rolloverIndexName + "/_alias"); |
| 398 | + response = client.performRequest(request); |
| 399 | + assertThat(checkIsWriteIndex(EntityUtils.toString(response.getEntity()), indexAlias, rolloverIndexName), equalTo(true)); |
| 400 | + |
| 401 | + if (isOSBundle()) { |
| 402 | + await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> |
| 403 | + assertThat(getIndexPolicyId(rolloverIndexName), equalTo(IndexConstants.LOGS_ISM_POLICY)) |
| 404 | + ); |
| 405 | + } |
| 406 | + } |
| 407 | + |
342 | 408 | @Test |
343 | 409 | @DisabledIf(value = "isES6", disabledReason = METRIC_INGESTION_TEST_DISABLED_REASON) |
344 | 410 | @Timeout(value = 50, unit = TimeUnit.SECONDS) |
|
0 commit comments