From 5074b721e108bcc8e0f82d6747dc176893bbde20 Mon Sep 17 00:00:00 2001 From: Drew Baugher <46505179+dbbaughe@users.noreply.github.com> Date: Fri, 3 Dec 2021 14:42:59 -0800 Subject: [PATCH] Successful deletes of an index still adds history document (#160) Signed-off-by: Drew Baugher <46505179+dbbaughe@users.noreply.github.com> --- .../ManagedIndexRunner.kt | 3 + .../action/DeleteActionIT.kt | 76 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/DeleteActionIT.kt diff --git a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt index 8cce26dee..8ff4d3888 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexRunner.kt @@ -360,6 +360,9 @@ object ManagedIndexRunner : } if (executedManagedIndexMetaData.isSuccessfulDelete) { + GlobalScope.launch(Dispatchers.IO + CoroutineName("ManagedIndexMetaData-AddHistory")) { + ismHistory.addManagedIndexMetaDataHistory(listOf(executedManagedIndexMetaData)) + } return } diff --git a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/DeleteActionIT.kt b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/DeleteActionIT.kt new file mode 100644 index 000000000..9233e3258 --- /dev/null +++ b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/DeleteActionIT.kt @@ -0,0 +1,76 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.indexmanagement.indexstatemanagement.action + +import org.opensearch.indexmanagement.indexstatemanagement.IndexStateManagementRestTestCase +import org.opensearch.indexmanagement.indexstatemanagement.model.Policy +import org.opensearch.indexmanagement.indexstatemanagement.model.State +import org.opensearch.indexmanagement.indexstatemanagement.model.action.DeleteActionConfig +import org.opensearch.indexmanagement.indexstatemanagement.randomErrorNotification +import org.opensearch.indexmanagement.waitFor +import java.time.Instant +import java.time.temporal.ChronoUnit +import java.util.Locale + +class DeleteActionIT : IndexStateManagementRestTestCase() { + + private val testIndexName = javaClass.simpleName.toLowerCase(Locale.ROOT) + + fun `test basic`() { + val indexName = "${testIndexName}_index_1" + val policyID = "${testIndexName}_testPolicyName_1" + val actionConfig = DeleteActionConfig(0) + val states = listOf( + State("DeleteState", listOf(actionConfig), listOf()) + ) + + val policy = Policy( + id = policyID, + description = "$testIndexName description", + schemaVersion = 1L, + lastUpdatedTime = Instant.now().truncatedTo(ChronoUnit.MILLIS), + errorNotification = randomErrorNotification(), + defaultState = states[0].name, + states = states + ) + createPolicy(policy, policyID) + createIndex(indexName, policyID) + + waitFor { assertIndexExists(indexName) } + + val managedIndexConfig = getExistingManagedIndexConfig(indexName) + // Change the start time so the job will trigger in 2 seconds. + updateManagedIndexConfigStartTime(managedIndexConfig) + + waitFor { assertEquals(policyID, getExplainManagedIndexMetaData(indexName).policyID) } + + // Need to wait two cycles. + // Change the start time so the job will trigger in 2 seconds. + updateManagedIndexConfigStartTime(managedIndexConfig) + + // confirm index does not exist anymore + waitFor { assertIndexDoesNotExist(indexName) } + + // confirm we added a history document that says we did a successful delete operation + waitFor { + val response = getHistorySearchResponse(indexName) + assertTrue( + response.hits.hits + .map { it.sourceAsMap } + .any { + val metadata = it["managed_index_meta_data"] as Map<*, *> + val index = metadata["index"] as String + val action = metadata["action"] as Map<*, *> + val actionName = action["name"] as String + val step = metadata["step"] as Map<*, *> + val stepName = step["name"] as String + val stepStatus = step["step_status"] as String + index == indexName && actionName == "delete" && stepName == "attempt_delete" && stepStatus == "completed" + } + ) + } + } +}