/* * 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.Transition 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 CloseActionIT : IndexStateManagementRestTestCase() { private val testIndexName = javaClass.simpleName.lowercase(Locale.ROOT) fun `test basic`() { val indexName = "${testIndexName}_index_1" val policyID = "${testIndexName}_testPolicyName_1" val actionConfig = CloseAction(0) val states = listOf( State("CloseState", 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) assertEquals("open", getIndexState(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) waitFor { assertEquals("close", getIndexState(indexName)) } } fun `test already closed index`() { val indexName = "${testIndexName}_index_2" val policyID = "${testIndexName}_testPolicyName_2" val actionConfig = CloseAction(0) val states = listOf( State("CloseState", 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) closeIndex(indexName) assertEquals("close", getIndexState(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) waitFor { assertEquals("close", getIndexState(indexName)) } } fun `test transitioning a closed index`() { val indexName = "${testIndexName}_index_3" val policyID = "${testIndexName}_testPolicyName_3" val actionConfig = CloseAction(0) val secondState = State("LastState", emptyList(), emptyList()) val firstState = State("CloseState", listOf(actionConfig), listOf(Transition(stateName = secondState.name, conditions = null))) val states = listOf(firstState, secondState) 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) assertEquals("open", getIndexState(indexName)) val managedIndexConfig = getExistingManagedIndexConfig(indexName) // Change the start time so the job will trigger in 2 seconds and init policy updateManagedIndexConfigStartTime(managedIndexConfig) waitFor { assertEquals(policyID, getExplainManagedIndexMetaData(indexName).policyID) } // Change the start time so the job will trigger in 2 seconds and close the index updateManagedIndexConfigStartTime(managedIndexConfig) waitFor { assertEquals("close", getIndexState(indexName)) } // Change the start time so the job will trigger in 2 seconds and attempt transitions updateManagedIndexConfigStartTime(managedIndexConfig) waitFor { assertEquals(secondState.name, getExplainManagedIndexMetaData(indexName).transitionTo) } // Change the start time so the job will trigger in 2 seconds and transition to next state (which should complete policy) updateManagedIndexConfigStartTime(managedIndexConfig) waitFor { assertEquals(true, getExplainManagedIndexMetaData(indexName).policyCompleted) } } }