Skip to content

Commit

Permalink
added unit tests to help with code coverage and modified integ test
Browse files Browse the repository at this point in the history
Signed-off-by: Ronnak Saxena <[email protected]>
  • Loading branch information
ronnaksaxena committed Jul 27, 2023
1 parent b8adc3e commit eed3f84
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ActionListener<CreateSnapshotResponse>>(1)
if (createSnapshotRequest != null) listener.onResponse(createSnapshotRequest)
else listener.onFailure(exception)
}.whenever(this.mock).createSnapshot(any(), any())
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ActionListener<CreateSnapshotResponse>>(1)
if (createSnapshotRequest != null) listener.onResponse(createSnapshotRequest)
else listener.onFailure(exception)
}.whenever(this.mock).createSnapshot(any(), any())
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ActionListener<CreateSnapshotResponse>>(1)
if (createSnapshotRequest != null) listener.onResponse(createSnapshotRequest)
else listener.onFailure(exception)
}.whenever(this.mock).createSnapshot(any(), any())
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ActionListener<CreateSnapshotResponse>>(1)
if (createSnapshotRequest != null) listener.onResponse(createSnapshotRequest)
else listener.onFailure(exception)
}.whenever(this.mock).createSnapshot(any(), any())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit eed3f84

Please sign in to comment.