-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests to help with code coverage and modified integ test
Signed-off-by: Ronnak Saxena <[email protected]>
- Loading branch information
1 parent
b8adc3e
commit eed3f84
Showing
6 changed files
with
267 additions
and
2 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
64 changes: 64 additions & 0 deletions
64
...rg/opensearch/indexmanagement/indexstatemanagement/step/AttemptCallForceMergeStepTests.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,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()) | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
.../org/opensearch/indexmanagement/indexstatemanagement/step/AttemptNotificationStepTests.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,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()) | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...tlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptRolloverStepTests.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,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()) | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...kotlin/org/opensearch/indexmanagement/indexstatemanagement/step/AttemptShrinkStepTests.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,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()) | ||
} | ||
} | ||
} |
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