From eed3f84dbe7195c6a7cda16ee2083fef57ab1e39 Mon Sep 17 00:00:00 2001 From: Ronnak Saxena Date: Wed, 26 Jul 2023 17:10:26 -0700 Subject: [PATCH] added unit tests to help with code coverage and modified integ test Signed-off-by: Ronnak Saxena --- .../action/RolloverActionIT.kt | 5 +- .../step/AttemptCallForceMergeStepTests.kt | 64 ++++++++++++++++++ .../step/AttemptNotificationStepTests.kt | 64 ++++++++++++++++++ .../step/AttemptRolloverStepTests.kt | 62 ++++++++++++++++++ .../step/AttemptShrinkStepTests.kt | 65 +++++++++++++++++++ .../step/AttemptSnapshotStepTests.kt | 9 +++ 6 files changed, 267 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptCallForceMergeStepTests.kt create mode 100644 src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptNotificationStepTests.kt create mode 100644 src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptRolloverStepTests.kt create mode 100644 src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptShrinkStepTests.kt diff --git a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/RolloverActionIT.kt b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/RolloverActionIT.kt index 05ba0806d..97324fc9e 100644 --- a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/RolloverActionIT.kt +++ b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/action/RolloverActionIT.kt @@ -688,8 +688,9 @@ class RolloverActionIT : IndexStateManagementRestTestCase() { // Execute again to see if the metadata will update updateManagedIndexConfigStartTime(managedIndexConfig) waitFor { - val stepStatus = getExplainManagedIndexMetaData(firstIndex).stepMetaData?.stepStatus - assertEquals("rollover step did not continue executing after detecting the transient failure.", Step.StepStatus.COMPLETED, stepStatus) + val stepMetaData = getExplainManagedIndexMetaData(firstIndex).stepMetaData + assertEquals("executed the wrong step: ${stepMetaData?.name} instead of attempt rollover", "attempt_rollover", stepMetaData?.name) + assertEquals("rollover step did not continue executing after detecting the transient failure.", Step.StepStatus.COMPLETED, stepMetaData?.stepStatus) } } } diff --git a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptCallForceMergeStepTests.kt b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptCallForceMergeStepTests.kt new file mode 100644 index 000000000..6ba77521a --- /dev/null +++ b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptCallForceMergeStepTests.kt @@ -0,0 +1,64 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.indexmanagement.indexstatemanagement.step + +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 org.junit.Assert +import org.opensearch.action.ActionListener +import org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse +import org.opensearch.client.AdminClient +import org.opensearch.client.Client +import org.opensearch.client.ClusterAdminClient +import org.opensearch.cluster.service.ClusterService +import org.opensearch.common.settings.Settings +import org.opensearch.indexmanagement.indexstatemanagement.randomForceMergeActionConfig +import org.opensearch.indexmanagement.indexstatemanagement.step.forcemerge.AttemptCallForceMergeStep +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionMetaData +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionProperties +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ManagedIndexMetaData +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.StepContext +import org.opensearch.jobscheduler.spi.utils.LockService +import org.opensearch.script.ScriptService +import org.opensearch.test.OpenSearchTestCase + +class AttemptCallForceMergeStepTests { + private val clusterService: ClusterService = mock() + private val scriptService: ScriptService = mock() + private val settings: Settings = Settings.EMPTY + private val forceMergeAction = randomForceMergeActionConfig() + private val metadata = ManagedIndexMetaData( + "test", "indexUuid", "policy_id", null, null, null, null, null, null, null, + ActionMetaData(AttemptCallForceMergeStep.name, 1, 0, false, 0, null, ActionProperties()), null, null, null + ) + private val lockService: LockService = LockService(mock(), clusterService) + + suspend fun `test detect transient failure`() { + // TODO adjust this test after implementing isTransientFailure method + val step = AttemptCallForceMergeStep(forceMergeAction) + val client = getClient(getAdminClient(getClusterAdminClient(null, null))) + val stepContext = StepContext(metadata, clusterService, client, null, null, scriptService, settings, lockService) + Assert.assertEquals("Cannot detect transient failure", false, step.isTransientFailure(client, stepContext, metadata)) + } + private fun getClient(adminClient: AdminClient): Client = mock { on { admin() } doReturn adminClient } + private fun getAdminClient(clusterAdminClient: ClusterAdminClient): AdminClient = mock { on { cluster() } doReturn clusterAdminClient } + private fun getClusterAdminClient(createSnapshotRequest: CreateSnapshotResponse?, exception: Exception?): ClusterAdminClient { + OpenSearchTestCase.assertTrue( + "Must provide one and only one response or exception", + (createSnapshotRequest != null).xor(exception != null) + ) + return mock { + doAnswer { invocationOnMock -> + val listener = invocationOnMock.getArgument>(1) + if (createSnapshotRequest != null) listener.onResponse(createSnapshotRequest) + else listener.onFailure(exception) + }.whenever(this.mock).createSnapshot(any(), any()) + } + } +} diff --git a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptNotificationStepTests.kt b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptNotificationStepTests.kt new file mode 100644 index 000000000..a3c97e39a --- /dev/null +++ b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptNotificationStepTests.kt @@ -0,0 +1,64 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.indexmanagement.indexstatemanagement.step + +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 org.junit.Assert +import org.opensearch.action.ActionListener +import org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse +import org.opensearch.client.AdminClient +import org.opensearch.client.Client +import org.opensearch.client.ClusterAdminClient +import org.opensearch.cluster.service.ClusterService +import org.opensearch.common.settings.Settings +import org.opensearch.indexmanagement.indexstatemanagement.randomNotificationActionConfig +import org.opensearch.indexmanagement.indexstatemanagement.step.notification.AttemptNotificationStep +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionMetaData +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionProperties +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ManagedIndexMetaData +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.StepContext +import org.opensearch.jobscheduler.spi.utils.LockService +import org.opensearch.script.ScriptService +import org.opensearch.test.OpenSearchTestCase + +class AttemptNotificationStepTests { + private val clusterService: ClusterService = mock() + private val scriptService: ScriptService = mock() + private val settings: Settings = Settings.EMPTY + private val notificationAction = randomNotificationActionConfig() + private val metadata = ManagedIndexMetaData( + "test", "indexUuid", "policy_id", null, null, null, null, null, null, null, + ActionMetaData(AttemptNotificationStep.name, 1, 0, false, 0, null, ActionProperties()), null, null, null + ) + private val lockService: LockService = LockService(mock(), clusterService) + + suspend fun `test detect transient failure`() { + // TODO adjust this test after implementing isTransientFailure method + val step = AttemptNotificationStep(notificationAction) + val client = getClient(getAdminClient(getClusterAdminClient(null, null))) + val stepContext = StepContext(metadata, clusterService, client, null, null, scriptService, settings, lockService) + Assert.assertEquals("Cannot detect transient failure", false, step.isTransientFailure(client, stepContext, metadata)) + } + private fun getClient(adminClient: AdminClient): Client = mock { on { admin() } doReturn adminClient } + private fun getAdminClient(clusterAdminClient: ClusterAdminClient): AdminClient = mock { on { cluster() } doReturn clusterAdminClient } + private fun getClusterAdminClient(createSnapshotRequest: CreateSnapshotResponse?, exception: Exception?): ClusterAdminClient { + OpenSearchTestCase.assertTrue( + "Must provide one and only one response or exception", + (createSnapshotRequest != null).xor(exception != null) + ) + return mock { + doAnswer { invocationOnMock -> + val listener = invocationOnMock.getArgument>(1) + if (createSnapshotRequest != null) listener.onResponse(createSnapshotRequest) + else listener.onFailure(exception) + }.whenever(this.mock).createSnapshot(any(), any()) + } + } +} diff --git a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptRolloverStepTests.kt b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptRolloverStepTests.kt new file mode 100644 index 000000000..63fbb9e9a --- /dev/null +++ b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptRolloverStepTests.kt @@ -0,0 +1,62 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.indexmanagement.indexstatemanagement.step + +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 org.junit.Assert +import org.opensearch.action.ActionListener +import org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse +import org.opensearch.client.AdminClient +import org.opensearch.client.Client +import org.opensearch.client.ClusterAdminClient +import org.opensearch.cluster.service.ClusterService +import org.opensearch.common.settings.Settings +import org.opensearch.indexmanagement.indexstatemanagement.randomRolloverActionConfig +import org.opensearch.indexmanagement.indexstatemanagement.step.rollover.AttemptRolloverStep +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionMetaData +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionProperties +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ManagedIndexMetaData +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.StepContext +import org.opensearch.jobscheduler.spi.utils.LockService +import org.opensearch.script.ScriptService +import org.opensearch.test.OpenSearchTestCase + +class AttemptRolloverStepTests : OpenSearchTestCase() { + private val clusterService: ClusterService = mock() + private val scriptService: ScriptService = mock() + private val settings: Settings = Settings.EMPTY + private val rolloverAction = randomRolloverActionConfig() + private val metadata = ManagedIndexMetaData( + "test", "indexUuid", "policy_id", null, null, null, null, null, null, null, + ActionMetaData(AttemptRolloverStep.name, 1, 0, false, 0, null, ActionProperties()), null, null, null + ) + private val lockService: LockService = LockService(mock(), clusterService) + + suspend fun `test detect transient failure`() { + // TODO adjust this test after implementing isTransientFailure method + val step = AttemptRolloverStep(rolloverAction) + val client = getClient(getAdminClient(getClusterAdminClient(null, null))) + val stepContext = StepContext(metadata, clusterService, client, null, null, scriptService, settings, lockService) + // Rollover has not happened yet, should not be able to find an alias attached to index + Assert.assertEquals("Incorrectly found alias attached to index", false, step.isTransientFailure(client, stepContext, metadata)) + } + private fun getClient(adminClient: AdminClient): Client = mock { on { admin() } doReturn adminClient } + private fun getAdminClient(clusterAdminClient: ClusterAdminClient): AdminClient = mock { on { cluster() } doReturn clusterAdminClient } + private fun getClusterAdminClient(createSnapshotRequest: CreateSnapshotResponse?, exception: Exception?): ClusterAdminClient { + assertTrue("Must provide one and only one response or exception", (createSnapshotRequest != null).xor(exception != null)) + return mock { + doAnswer { invocationOnMock -> + val listener = invocationOnMock.getArgument>(1) + if (createSnapshotRequest != null) listener.onResponse(createSnapshotRequest) + else listener.onFailure(exception) + }.whenever(this.mock).createSnapshot(any(), any()) + } + } +} diff --git a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptShrinkStepTests.kt b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptShrinkStepTests.kt new file mode 100644 index 000000000..d97d41d50 --- /dev/null +++ b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptShrinkStepTests.kt @@ -0,0 +1,65 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.indexmanagement.indexstatemanagement.step + +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 org.junit.Assert +import org.opensearch.action.ActionListener +import org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse +import org.opensearch.client.AdminClient +import org.opensearch.client.Client +import org.opensearch.client.ClusterAdminClient +import org.opensearch.cluster.service.ClusterService +import org.opensearch.common.settings.Settings +import org.opensearch.indexmanagement.indexstatemanagement.randomShrinkAction +import org.opensearch.indexmanagement.indexstatemanagement.step.notification.AttemptNotificationStep +import org.opensearch.indexmanagement.indexstatemanagement.step.shrink.AttemptShrinkStep +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionMetaData +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionProperties +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ManagedIndexMetaData +import org.opensearch.indexmanagement.spi.indexstatemanagement.model.StepContext +import org.opensearch.jobscheduler.spi.utils.LockService +import org.opensearch.script.ScriptService +import org.opensearch.test.OpenSearchTestCase + +class AttemptShrinkStepTests { + private val clusterService: ClusterService = mock() + private val scriptService: ScriptService = mock() + private val settings: Settings = Settings.EMPTY + private val shrinkAction = randomShrinkAction() + private val metadata = ManagedIndexMetaData( + "test", "indexUuid", "policy_id", null, null, null, null, null, null, null, + ActionMetaData(AttemptNotificationStep.name, 1, 0, false, 0, null, ActionProperties()), null, null, null + ) + private val lockService: LockService = LockService(mock(), clusterService) + + suspend fun `test detect transient failure`() { + // TODO adjust this test after implementing isTransientFailure method + val step = AttemptShrinkStep(shrinkAction) + val client = getClient(getAdminClient(getClusterAdminClient(null, null))) + val stepContext = StepContext(metadata, clusterService, client, null, null, scriptService, settings, lockService) + Assert.assertEquals("Cannot detect transient failure", false, step.isTransientFailure(client, stepContext, metadata)) + } + private fun getClient(adminClient: AdminClient): Client = mock { on { admin() } doReturn adminClient } + private fun getAdminClient(clusterAdminClient: ClusterAdminClient): AdminClient = mock { on { cluster() } doReturn clusterAdminClient } + private fun getClusterAdminClient(createSnapshotRequest: CreateSnapshotResponse?, exception: Exception?): ClusterAdminClient { + OpenSearchTestCase.assertTrue( + "Must provide one and only one response or exception", + (createSnapshotRequest != null).xor(exception != null) + ) + return mock { + doAnswer { invocationOnMock -> + val listener = invocationOnMock.getArgument>(1) + if (createSnapshotRequest != null) listener.onResponse(createSnapshotRequest) + else listener.onFailure(exception) + }.whenever(this.mock).createSnapshot(any(), any()) + } + } +} diff --git a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptSnapshotStepTests.kt b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptSnapshotStepTests.kt index 74d07b449..59cb909d4 100644 --- a/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptSnapshotStepTests.kt +++ b/src/test/kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptSnapshotStepTests.kt @@ -12,6 +12,7 @@ import com.nhaarman.mockitokotlin2.eq import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.whenever import kotlinx.coroutines.runBlocking +import org.junit.Assert import org.junit.Before import org.opensearch.action.ActionListener import org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse @@ -137,6 +138,14 @@ class AttemptSnapshotStepTests : OpenSearchTestCase() { } } + suspend fun `test detect transient failure`() { + // TODO adjust this test after implementing isTransientFailure method + val step = AttemptSnapshotStep(snapshotAction) + val client = getClient(getAdminClient(getClusterAdminClient(null, null))) + val stepContext = StepContext(metadata, clusterService, client, null, null, scriptService, settings, lockService) + Assert.assertEquals("Cannot detect transient failure", false, step.isTransientFailure(client, stepContext, metadata)) + } + private fun getClient(adminClient: AdminClient): Client = mock { on { admin() } doReturn adminClient } private fun getAdminClient(clusterAdminClient: ClusterAdminClient): AdminClient = mock { on { cluster() } doReturn clusterAdminClient } private fun getClusterAdminClient(createSnapshotRequest: CreateSnapshotResponse?, exception: Exception?): ClusterAdminClient {