Skip to content

Commit

Permalink
UT deleting
Browse files Browse the repository at this point in the history
Signed-off-by: bowenlan-amzn <[email protected]>
  • Loading branch information
bowenlan-amzn committed May 23, 2022
1 parent 8805116 commit d6a547b
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object DeletingState : State {
val res: AcknowledgedResponse
val snapshotToDelete: List<SMMetadata.SnapshotInfo>

val snapshotInfos = try {
val getSnapshots = try {
client.getSnapshots(
smJobIdToPolicyName(job.id) + "*",
job.snapshotConfig["repository"] as String
Expand All @@ -43,7 +43,7 @@ object DeletingState : State {
return SMResult.Retry(WorkflowType.DELETION)
}

snapshotToDelete = findSnapshotsToDelete(snapshotInfos, job.deletion.condition)
snapshotToDelete = findSnapshotsToDelete(getSnapshots, job.deletion.condition)
log.info("sm dev: Going to delete: ${snapshotToDelete.map { it.name }}")

if (snapshotToDelete.isNotEmpty()) {
Expand All @@ -60,7 +60,6 @@ object DeletingState : State {
}

val metadataToSave = SMMetadata.Builder(metadata)
.currentState(SMState.DELETING)
if (snapshotToDelete.isNotEmpty())
metadataToSave.deletion(
startTime = now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ enum class SMState(val instance: State) {
}

enum class WorkflowType {
CREATION, // CREATE_CONDITION_MET, CREATING
DELETION, // DELETE_CONDITION_MET, DELETING
CREATION, // CREATE_CONDITION_MET, CREATING, part of FINISHED
DELETION, // DELETE_CONDITION_MET, DELETING, part of FINISHED
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.indexmanagement.snapshotmanagement

import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doAnswer
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import org.junit.Before
import org.opensearch.action.ActionListener
import org.opensearch.action.ActionResponse
import org.opensearch.action.index.IndexResponse
import org.opensearch.client.AdminClient
import org.opensearch.client.Client
import org.opensearch.client.ClusterAdminClient
import org.opensearch.test.OpenSearchTestCase

open class SnapshotManagementClientMockTests : OpenSearchTestCase() {

val client: Client = mock()
private val adminClient: AdminClient = mock()
private val clusterAdminClient: ClusterAdminClient = mock()

@Before
fun setupClient() {
doAnswer {
val listener = it.getArgument<ActionListener<IndexResponse>>(1)
listener.onResponse(mockIndexResponse())
}.whenever(client).index(any(), any())
}

fun mockCreateSnapshotCall(
response: ActionResponse? = null,
exception: Exception? = null
) {
assertTrue(
"Must provide either a response or an exception.",
(response != null).xor(exception != null)
)
whenever(client.admin()).thenReturn(adminClient)
whenever(adminClient.cluster()).thenReturn(clusterAdminClient)
doAnswer {
val listener = it.getArgument<ActionListener<ActionResponse>>(1)
if (response != null) listener.onResponse(response)
else listener.onFailure(exception)
}.whenever(clusterAdminClient).createSnapshot(any(), any())
}

fun mockDeleteSnapshotCall(
response: ActionResponse? = null,
exception: Exception? = null
) {
assertTrue(
"Must provide either a response or an exception.",
(response != null).xor(exception != null)
)
whenever(client.admin()).thenReturn(adminClient)
whenever(adminClient.cluster()).thenReturn(clusterAdminClient)
doAnswer {
val listener = it.getArgument<ActionListener<ActionResponse>>(1)
if (response != null) listener.onResponse(response)
else listener.onFailure(exception)
}.whenever(clusterAdminClient).deleteSnapshot(any(), any())
}

fun mockGetSnapshotsCall(
response: ActionResponse? = null,
exception: Exception? = null
) {
assertTrue(
"Must provide either a response or an exception.",
(response != null).xor(exception != null)
)
whenever(client.admin()).thenReturn(adminClient)
whenever(adminClient.cluster()).thenReturn(clusterAdminClient)
doAnswer {
val listener = it.getArgument<ActionListener<ActionResponse>>(1)
if (response != null) listener.onResponse(response)
else listener.onFailure(exception)
}.whenever(clusterAdminClient).getSnapshots(any(), any())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.opensearch.rest.RestStatus
import org.opensearch.snapshots.SnapshotId
import org.opensearch.snapshots.SnapshotInfo
import org.opensearch.test.OpenSearchTestCase.randomAlphaOfLength
import org.opensearch.test.OpenSearchTestCase.randomIntBetween
import org.opensearch.test.OpenSearchTestCase.randomNonNegativeLong
import java.time.Instant
import java.time.Instant.now
Expand All @@ -36,6 +37,8 @@ fun randomSMMetadata(
policySeqNo: Long = randomNonNegativeLong(),
policyPrimaryTerm: Long = randomNonNegativeLong(),
startedCreation: SMMetadata.SnapshotInfo? = null,
startedDeletion: List<SMMetadata.SnapshotInfo>? = null,
deleteStartedTime: Instant? = null,
): SMMetadata {
return SMMetadata(
policySeqNo = policySeqNo,
Expand All @@ -50,7 +53,9 @@ fun randomSMMetadata(
deletion = SMMetadata.Deletion(
trigger = SMMetadata.Trigger(
time = nextDeletionTime
)
),
started = startedDeletion,
startedTime = deleteStartedTime,
),
)
}
Expand All @@ -63,7 +68,7 @@ fun randomSMPolicy(
creationTimeout: TimeValue? = null,
deletionSchedule: CronSchedule = randomCronSchedule(),
deletionTimeout: TimeValue? = null,
deletionMaxCount: Int = 5,
deletionMaxCount: Int = randomIntBetween(5, 10),
deletionMaxAge: TimeValue? = null,
deletionMinCount: Int? = null,
snapshotConfig: Map<String, Any> = mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@

package org.opensearch.indexmanagement.snapshotmanagement.engine.states

import com.nhaarman.mockitokotlin2.mock
import kotlinx.coroutines.runBlocking
import org.opensearch.client.Client
import org.opensearch.indexmanagement.snapshotmanagement.SnapshotManagementClientMockTests
import org.opensearch.indexmanagement.snapshotmanagement.engine.statemachine.SMStateMachine
import org.opensearch.indexmanagement.snapshotmanagement.randomSMMetadata
import org.opensearch.indexmanagement.snapshotmanagement.randomSMPolicy
import org.opensearch.indexmanagement.snapshotmanagement.randomSMSnapshotInfo
import org.opensearch.test.OpenSearchTestCase
import java.time.Instant.now

class CreateConditionMetStateTests : OpenSearchTestCase() {

private val client: Client = mock()
class CreateConditionMetStateTests : SnapshotManagementClientMockTests() {

fun `test next creation time met`() = runBlocking {
val metadata = randomSMMetadata(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,16 @@

package org.opensearch.indexmanagement.snapshotmanagement.engine.states

import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doAnswer
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.opensearch.action.ActionListener
import org.opensearch.action.ActionResponse
import org.opensearch.action.index.IndexResponse
import org.opensearch.client.AdminClient
import org.opensearch.client.Client
import org.opensearch.client.ClusterAdminClient
import org.opensearch.indexmanagement.snapshotmanagement.SnapshotManagementClientMockTests
import org.opensearch.indexmanagement.snapshotmanagement.engine.statemachine.SMStateMachine
import org.opensearch.indexmanagement.snapshotmanagement.randomSMMetadata
import org.opensearch.indexmanagement.snapshotmanagement.randomSMPolicy
import org.opensearch.indexmanagement.snapshotmanagement.mockCreateSnapshotResponse
import org.opensearch.indexmanagement.snapshotmanagement.mockGetSnapshotResponse
import org.opensearch.indexmanagement.snapshotmanagement.mockIndexResponse
import org.opensearch.test.OpenSearchTestCase
import java.time.Instant.now

class CreatingStateTests : OpenSearchTestCase() {

private val client: Client = mock()
private val adminClient: AdminClient = mock()
private val clusterAdminClient: ClusterAdminClient = mock()

@Before
fun setupClient() {
doAnswer {
val listener = it.getArgument<ActionListener<IndexResponse>>(1)
listener.onResponse(mockIndexResponse())
}.whenever(client).index(any(), any())
}
class CreatingStateTests : SnapshotManagementClientMockTests() {

fun `test create snapshot succeed`() = runBlocking {
mockGetSnapshotsCall(response = mockGetSnapshotResponse(0))
Expand Down Expand Up @@ -69,6 +45,8 @@ class CreatingStateTests : OpenSearchTestCase() {

val result = SMState.CREATING.instance.execute(context)
assertTrue("Execution result should be Failure.", result is SMResult.Failure)
result as SMResult.Failure
assertTrue("Create snapshot exception should notify user.", result.notifiable)
}

fun `test snapshot already created in previous schedule`() = runBlocking {
Expand All @@ -87,38 +65,4 @@ class CreatingStateTests : OpenSearchTestCase() {
result as SMResult.Next
assertEquals("Started create snapshot name is $snapshotName.", snapshotName, result.metadataToSave.creation.started!!.name)
}

private fun mockCreateSnapshotCall(
response: ActionResponse? = null,
exception: Exception? = null
) {
assertTrue(
"Must provide either a response or an exception.",
(response != null).xor(exception != null)
)
whenever(client.admin()).thenReturn(adminClient)
whenever(adminClient.cluster()).thenReturn(clusterAdminClient)
doAnswer {
val listener = it.getArgument<ActionListener<ActionResponse>>(1)
if (response != null) listener.onResponse(response)
else listener.onFailure(exception)
}.whenever(clusterAdminClient).createSnapshot(any(), any())
}

private fun mockGetSnapshotsCall(
response: ActionResponse? = null,
exception: Exception? = null
) {
assertTrue(
"Must provide either a response or an exception.",
(response != null).xor(exception != null)
)
whenever(client.admin()).thenReturn(adminClient)
whenever(adminClient.cluster()).thenReturn(clusterAdminClient)
doAnswer {
val listener = it.getArgument<ActionListener<ActionResponse>>(1)
if (response != null) listener.onResponse(response)
else listener.onFailure(exception)
}.whenever(clusterAdminClient).getSnapshots(any(), any())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,55 @@

package org.opensearch.indexmanagement.snapshotmanagement.engine.states

class DeleteConditionMetStateTests
import kotlinx.coroutines.runBlocking
import org.opensearch.indexmanagement.randomInstant
import org.opensearch.indexmanagement.snapshotmanagement.SnapshotManagementClientMockTests
import org.opensearch.indexmanagement.snapshotmanagement.engine.statemachine.SMStateMachine
import org.opensearch.indexmanagement.snapshotmanagement.randomSMMetadata
import org.opensearch.indexmanagement.snapshotmanagement.randomSMPolicy
import org.opensearch.indexmanagement.snapshotmanagement.randomSMSnapshotInfo
import java.time.Instant

class DeleteConditionMetStateTests : SnapshotManagementClientMockTests() {

fun `test next deletion time met`() = runBlocking {
val metadata = randomSMMetadata(
currentState = SMState.START,
nextDeletionTime = Instant.now().minusSeconds(60),
)
val job = randomSMPolicy()
val context = SMStateMachine(client, job, metadata)

val result = SMState.DELETE_CONDITION_MET.instance.execute(context)
assertTrue("Execution result should be Next.", result is SMResult.Next)
result as SMResult.Next
assertNotEquals("Next execution time should be updated.", metadata.deletion.trigger.time, result.metadataToSave.deletion.trigger.time)
}

fun `test next deletion time has not met`() = runBlocking {
val metadata = randomSMMetadata(
currentState = SMState.START,
nextDeletionTime = Instant.now().plusSeconds(60),
)
val job = randomSMPolicy()
val context = SMStateMachine(client, job, metadata)

val result = SMState.DELETE_CONDITION_MET.instance.execute(context)
assertTrue("Execution result should be Stay.", result is SMResult.Stay)
result as SMResult.Stay
assertNull("Next execution time should not be updated.", result.metadataToSave)
}

fun `test already started snapshot deletion`() = runBlocking {
val metadata = randomSMMetadata(
currentState = SMState.START,
startedDeletion = listOf(randomSMSnapshotInfo()),
deleteStartedTime = randomInstant(),
)
val job = randomSMPolicy()
val context = SMStateMachine(client, job, metadata)

val result = SMState.DELETE_CONDITION_MET.instance.execute(context)
assertTrue("Execution result should be Retry.", result is SMResult.Stay)
}
}
Loading

0 comments on commit d6a547b

Please sign in to comment.