This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...n/com/amazon/opendistroforelasticsearch/indexstatemanagement/action/TransitionActionIT.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.amazon.opendistroforelasticsearch.indexstatemanagement.action | ||
|
||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.IndexStateManagementRestTestCase | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.Conditions | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.Policy | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.State | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.Transition | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.randomErrorNotification | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.step.transition.AttemptTransitionStep | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.waitFor | ||
import java.time.Instant | ||
import java.time.temporal.ChronoUnit | ||
import java.util.Locale | ||
|
||
class TransitionActionIT : IndexStateManagementRestTestCase() { | ||
|
||
private val testIndexName = javaClass.simpleName.toLowerCase(Locale.ROOT) | ||
|
||
fun `test doc count condition`() { | ||
val indexName = "${testIndexName}_index_1" | ||
val policyID = "${testIndexName}_testPolicyName_1" | ||
val secondStateName = "second" | ||
val states = listOf( | ||
State("first", listOf(), listOf(Transition(secondStateName, Conditions(docCount = 5L)))), | ||
State(secondStateName, listOf(), 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) | ||
|
||
val managedIndexConfig = getExistingManagedIndexConfig(indexName) | ||
|
||
// Initializing the policy/metadata | ||
updateManagedIndexConfigStartTime(managedIndexConfig) | ||
|
||
waitFor { assertEquals(policyID, getExplainManagedIndexMetaData(indexName).policyID) } | ||
|
||
// Evaluating transition conditions for first time | ||
updateManagedIndexConfigStartTime(managedIndexConfig) | ||
|
||
// Should not have evaluated to true | ||
waitFor { assertEquals(AttemptTransitionStep.getEvaluatingMessage(indexName), getExplainManagedIndexMetaData(indexName).info?.get("message")) } | ||
|
||
// Add 6 documents (>5) | ||
insertSampleData(indexName, 6) | ||
|
||
// Evaluating transition conditions for second time | ||
updateManagedIndexConfigStartTime(managedIndexConfig) | ||
|
||
// Should have evaluated to true | ||
waitFor { assertEquals(AttemptTransitionStep.getSuccessMessage(indexName, secondStateName), getExplainManagedIndexMetaData(indexName).info?.get("message")) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...amazon/opendistroforelasticsearch/indexstatemanagement/step/AttemptTransitionStepTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.amazon.opendistroforelasticsearch.indexstatemanagement.step | ||
|
||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.Conditions | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.ManagedIndexMetaData | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.Transition | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.model.action.TransitionsActionConfig | ||
import com.amazon.opendistroforelasticsearch.indexstatemanagement.step.transition.AttemptTransitionStep | ||
import com.nhaarman.mockitokotlin2.any | ||
import com.nhaarman.mockitokotlin2.doAnswer | ||
import com.nhaarman.mockitokotlin2.doReturn | ||
import com.nhaarman.mockitokotlin2.mock | ||
import com.nhaarman.mockitokotlin2.whenever | ||
import kotlinx.coroutines.runBlocking | ||
import org.elasticsearch.action.ActionListener | ||
import org.elasticsearch.action.admin.indices.stats.CommonStats | ||
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse | ||
import org.elasticsearch.client.AdminClient | ||
import org.elasticsearch.client.Client | ||
import org.elasticsearch.client.IndicesAdminClient | ||
import org.elasticsearch.cluster.ClusterState | ||
import org.elasticsearch.cluster.metadata.IndexMetadata | ||
import org.elasticsearch.cluster.metadata.Metadata | ||
import org.elasticsearch.cluster.service.ClusterService | ||
import org.elasticsearch.index.shard.DocsStats | ||
import org.elasticsearch.rest.RestStatus | ||
import org.elasticsearch.test.ESTestCase | ||
import org.elasticsearch.transport.RemoteTransportException | ||
|
||
class AttemptTransitionStepTests : ESTestCase() { | ||
|
||
private val indexMetadata: IndexMetadata = mock() | ||
private val metadata: Metadata = mock { on { index(any<String>()) } doReturn indexMetadata } | ||
private val clusterState: ClusterState = mock { on { metadata() } doReturn metadata } | ||
private val clusterService: ClusterService = mock { on { state() } doReturn clusterState } | ||
|
||
private val docsStats: DocsStats = mock() | ||
private val primaries: CommonStats = mock { on { getDocs() } doReturn docsStats } | ||
private val statsResponse: IndicesStatsResponse = mock { on { primaries } doReturn primaries } | ||
|
||
fun `test stats response not OK`() { | ||
whenever(indexMetadata.creationDate).doReturn(5L) | ||
whenever(statsResponse.status).doReturn(RestStatus.INTERNAL_SERVER_ERROR) | ||
whenever(statsResponse.shardFailures).doReturn(IndicesStatsResponse.EMPTY) | ||
whenever(docsStats.count).doReturn(6L) | ||
whenever(docsStats.totalSizeInBytes).doReturn(2) | ||
val client = getClient(getAdminClient(getIndicesAdminClient(statsResponse, null))) | ||
|
||
runBlocking { | ||
val config = TransitionsActionConfig(listOf(Transition("some_state", Conditions(docCount = 5L)))) | ||
val managedIndexMetaData = ManagedIndexMetaData("test", "indexUuid", "policy_id", null, null, null, null, null, null, null, null, null, null) | ||
val step = AttemptTransitionStep(clusterService, client, config, managedIndexMetaData) | ||
step.execute() | ||
val updatedManagedIndexMetaData = step.getUpdatedManagedIndexMetaData(managedIndexMetaData) | ||
assertEquals("Step status is not FAILED", Step.StepStatus.FAILED, updatedManagedIndexMetaData.stepMetaData?.stepStatus) | ||
assertEquals("Did not get correct failed message", AttemptTransitionStep.getFailedStatsMessage("test"), updatedManagedIndexMetaData.info!!["message"]) | ||
} | ||
} | ||
|
||
fun `test transitions fails on exception`() { | ||
whenever(indexMetadata.creationDate).doReturn(5L) | ||
val exception = IllegalArgumentException("example") | ||
val client = getClient(getAdminClient(getIndicesAdminClient(null, exception))) | ||
|
||
runBlocking { | ||
val config = TransitionsActionConfig(listOf(Transition("some_state", Conditions(docCount = 5L)))) | ||
val managedIndexMetaData = ManagedIndexMetaData("test", "indexUuid", "policy_id", null, null, null, null, null, null, null, null, null, null) | ||
val step = AttemptTransitionStep(clusterService, client, config, managedIndexMetaData) | ||
step.execute() | ||
val updatedManagedIndexMetaData = step.getUpdatedManagedIndexMetaData(managedIndexMetaData) | ||
assertEquals("Step status is not FAILED", Step.StepStatus.FAILED, updatedManagedIndexMetaData.stepMetaData?.stepStatus) | ||
assertEquals("Did not get cause from nested exception", "example", updatedManagedIndexMetaData.info!!["cause"]) | ||
} | ||
} | ||
|
||
fun `test transitions remote transport exception`() { | ||
whenever(indexMetadata.creationDate).doReturn(5L) | ||
val exception = RemoteTransportException("rte", IllegalArgumentException("nested")) | ||
val client = getClient(getAdminClient(getIndicesAdminClient(null, exception))) | ||
|
||
runBlocking { | ||
val config = TransitionsActionConfig(listOf(Transition("some_state", Conditions(docCount = 5L)))) | ||
val managedIndexMetaData = ManagedIndexMetaData("test", "indexUuid", "policy_id", null, null, null, null, null, null, null, null, null, null) | ||
val step = AttemptTransitionStep(clusterService, client, config, managedIndexMetaData) | ||
step.execute() | ||
val updatedManagedIndexMetaData = step.getUpdatedManagedIndexMetaData(managedIndexMetaData) | ||
assertEquals("Step status is not FAILED", Step.StepStatus.FAILED, updatedManagedIndexMetaData.stepMetaData?.stepStatus) | ||
assertEquals("Did not get cause from nested exception", "nested", updatedManagedIndexMetaData.info!!["cause"]) | ||
} | ||
} | ||
|
||
private fun getClient(adminClient: AdminClient): Client = mock { on { admin() } doReturn adminClient } | ||
private fun getAdminClient(indicesAdminClient: IndicesAdminClient): AdminClient = mock { on { indices() } doReturn indicesAdminClient } | ||
private fun getIndicesAdminClient(statsResponse: IndicesStatsResponse?, exception: Exception?): IndicesAdminClient { | ||
assertTrue("Must provide one and only one response or exception", (statsResponse != null).xor(exception != null)) | ||
return mock { | ||
doAnswer { invocationOnMock -> | ||
val listener = invocationOnMock.getArgument<ActionListener<IndicesStatsResponse>>(1) | ||
if (statsResponse != null) listener.onResponse(statsResponse) | ||
else listener.onFailure(exception) | ||
}.whenever(this.mock).stats(any(), any()) | ||
} | ||
} | ||
} |