Skip to content

Commit

Permalink
Issue651 tests (opensearch-project#1046)
Browse files Browse the repository at this point in the history
* Implemented filtering on the ISM eplain API

Signed-off-by: Joshua Au <[email protected]>

* Fixed tests for ExplainRequest

Signed-off-by: Joshua Au <[email protected]>

* Added filtering on query and metadata map

Signed-off-by: Joshua Au <[email protected]>

* Filtered on indexNames in metadata

Signed-off-by: Joshua Au <[email protected]>

* Fixed github workflow check errors

Signed-off-by: Joshua Au <[email protected]>

* Removed debugging comments

Signed-off-by: Joshua Au <[email protected]>

* Updated code styling to make more clear

Signed-off-by: Joshua Au <[email protected]>

* Refactored code to match suggestions

Signed-off-by: Joshua Au <[email protected]>

* Added test case for the ExplainFilter.byMetaData and parse methods

Signed-off-by: Joshua Au <[email protected]>

* Started implementation of explain filter IT

Signed-off-by: Joshua Au <[email protected]>

* Implemented test explain filter method

Signed-off-by: Joshua Au <[email protected]>

* Implemented explain filter test on failure

Signed-off-by: Joshua Au <[email protected]>

* Cleaned up log statements

Signed-off-by: Joshua Au <[email protected]>

* Added explain filter test for success

Signed-off-by: Joshua Au <[email protected]>

* Fixed lint errors

Signed-off-by: Joshua Au <[email protected]>

* Removed policy from index to fix flaky tests

Signed-off-by: Joshua Au <[email protected]>

---------

Signed-off-by: Joshua Au <[email protected]>
Signed-off-by: bowenlan-amzn <[email protected]>
Co-authored-by: bowenlan-amzn <[email protected]>
  • Loading branch information
Joshua152 and bowenlan-amzn committed Dec 19, 2023
1 parent 6e787fa commit e47aff8
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ package org.opensearch.indexmanagement.indexstatemanagement.model
import org.opensearch.core.common.io.stream.StreamInput
import org.opensearch.core.common.io.stream.StreamOutput
import org.opensearch.core.common.io.stream.Writeable
import org.opensearch.core.xcontent.ToXContent
import org.opensearch.core.xcontent.ToXContentObject
import org.opensearch.core.xcontent.XContentBuilder
import org.opensearch.core.xcontent.XContentParser
import org.opensearch.core.xcontent.XContentParser.Token
import org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken
Expand All @@ -22,7 +25,7 @@ data class ExplainFilter(
val state: String? = null,
val actionType: String? = null,
val failed: Boolean? = null
) : Writeable {
) : ToXContentObject, Writeable {

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
Expand All @@ -32,6 +35,19 @@ data class ExplainFilter(
failed = sin.readOptionalBoolean()
)

override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
builder.startObject()
builder.startObject(FILTER_FIELD)

if (policyID != null) builder.field(POLICY_ID_FIELD, policyID)
if (state != null) builder.field(STATE_FIELD, state)
if (actionType != null) builder.field(ACTION_FIELD, actionType)
if (failed != null) builder.field(FAILED_FIELD, failed)

builder.endObject()
return builder.endObject()
}

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeOptionalString(policyID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ import org.opensearch.indexmanagement.IndexManagementPlugin.Companion.INDEX_STAT
import org.opensearch.indexmanagement.IndexManagementPlugin.Companion.ISM_BASE_URI
import org.opensearch.indexmanagement.IndexManagementPlugin.Companion.POLICY_BASE_URI
import org.opensearch.indexmanagement.IndexManagementRestTestCase
import org.opensearch.indexmanagement.indexstatemanagement.model.ChangePolicy
import org.opensearch.indexmanagement.indexstatemanagement.model.ISMTemplate
import org.opensearch.indexmanagement.indexstatemanagement.model.ManagedIndexConfig
import org.opensearch.indexmanagement.indexstatemanagement.model.Policy
import org.opensearch.indexmanagement.indexstatemanagement.model.Policy.Companion.POLICY_TYPE
import org.opensearch.indexmanagement.indexstatemanagement.model.StateFilter
import org.opensearch.indexmanagement.indexstatemanagement.resthandler.RestExplainAction
import org.opensearch.indexmanagement.indexstatemanagement.settings.ManagedIndexSettings
import org.opensearch.indexmanagement.indexstatemanagement.util.FAILED_INDICES
Expand Down Expand Up @@ -71,6 +66,12 @@ import org.opensearch.jobscheduler.spi.schedule.IntervalSchedule
import org.opensearch.rest.RestRequest
import org.opensearch.search.SearchModule
import org.opensearch.core.rest.RestStatus
import org.opensearch.indexmanagement.indexstatemanagement.model.ChangePolicy
import org.opensearch.indexmanagement.indexstatemanagement.model.ExplainFilter
import org.opensearch.indexmanagement.indexstatemanagement.model.ISMTemplate
import org.opensearch.indexmanagement.indexstatemanagement.model.ManagedIndexConfig
import org.opensearch.indexmanagement.indexstatemanagement.model.Policy
import org.opensearch.indexmanagement.indexstatemanagement.model.StateFilter
import org.opensearch.indexmanagement.rollup.randomTermQuery
import org.opensearch.test.OpenSearchTestCase
import java.io.IOException
Expand Down Expand Up @@ -473,6 +474,8 @@ abstract class IndexStateManagementRestTestCase : IndexManagementRestTestCase()

protected fun ManagedIndexConfig.toHttpEntity(): HttpEntity = StringEntity(toJsonString(), APPLICATION_JSON)

protected fun ExplainFilter.toHttpEntity(): HttpEntity = StringEntity(toJsonString(), ContentType.APPLICATION_JSON)

protected fun ChangePolicy.toHttpEntity(): HttpEntity {
var string = "{\"${ChangePolicy.POLICY_ID_FIELD}\":\"$policyID\","
if (state != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.opensearch.indexmanagement.indexstatemanagement.action.SnapshotAction
import org.opensearch.indexmanagement.indexstatemanagement.model.ChangePolicy
import org.opensearch.indexmanagement.indexstatemanagement.model.Conditions
import org.opensearch.indexmanagement.indexstatemanagement.model.ErrorNotification
import org.opensearch.indexmanagement.indexstatemanagement.model.ExplainFilter
import org.opensearch.indexmanagement.indexstatemanagement.model.ISMTemplate
import org.opensearch.indexmanagement.indexstatemanagement.model.ManagedIndexConfig
import org.opensearch.indexmanagement.indexstatemanagement.model.Policy
Expand Down Expand Up @@ -305,6 +306,15 @@ fun randomByteSizeValue(): ByteSizeValue =
* End - Conditions helper functions
*/

fun randomExplainFilter(
policyID: String? = if (OpenSearchRestTestCase.randomBoolean()) OpenSearchRestTestCase.randomAlphaOfLength(10) else null,
state: String? = if (OpenSearchRestTestCase.randomBoolean()) OpenSearchRestTestCase.randomAlphaOfLength(10) else null,
actionType: String? = if (OpenSearchRestTestCase.randomBoolean()) OpenSearchRestTestCase.randomAlphaOfLength(10) else null,
failed: Boolean? = if (OpenSearchRestTestCase.randomBoolean()) OpenSearchRestTestCase.randomBoolean() else null
): ExplainFilter {
return ExplainFilter(policyID, state, actionType, failed)
}

fun randomChangePolicy(
policyID: String = OpenSearchRestTestCase.randomAlphaOfLength(10),
state: String? = if (OpenSearchRestTestCase.randomBoolean()) OpenSearchRestTestCase.randomAlphaOfLength(10) else null,
Expand Down Expand Up @@ -470,6 +480,11 @@ fun AllocationAction.toJsonString(): String {
return this.toXContent(builder, ToXContent.EMPTY_PARAMS).string()
}

fun ExplainFilter.toJsonString(): String {
val builder = XContentFactory.jsonBuilder()
return this.toXContent(builder, ToXContent.EMPTY_PARAMS).string()
}

fun ChangePolicy.toJsonString(): String {
val builder = XContentFactory.jsonBuilder()
return this.toXContent(builder, ToXContent.EMPTY_PARAMS).string()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.indexmanagement.indexstatemanagement.model

import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ActionMetaData
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.ManagedIndexMetaData
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.PolicyRetryInfoMetaData
import org.opensearch.indexmanagement.spi.indexstatemanagement.model.StateMetaData
import org.opensearch.test.OpenSearchTestCase

class ExplainFilterTests : OpenSearchTestCase() {
fun `test byMetadata`() {
val stateMetaData = StateMetaData("state", 100)
val actionMetaData = ActionMetaData("action", null, 0, false, 0, null, null)
val policyRetryInfoMetaData = PolicyRetryInfoMetaData(false, 1)

val managedIndexMetaDataAllNull = ManagedIndexMetaData("test", "indexUuid", "policy_id", null, null, null, null, null, null, null, null, null, null, null)
val managedIndexMetaDataNonNull = ManagedIndexMetaData("test", "indexUuid", "policy_id", null, null, null, null, null, null, stateMetaData, actionMetaData, null, policyRetryInfoMetaData, null)
val managedIndexMetaDataSomeNull = ManagedIndexMetaData("test", "indexUuid", "policy_id", null, null, null, null, null, null, null, actionMetaData, null, null, null)

val explainFilter = ExplainFilter(state = "state", actionType = "action", failed = false)

var res = explainFilter.byMetaData(managedIndexMetaDataAllNull)
assertFalse(res)

res = explainFilter.byMetaData(managedIndexMetaDataNonNull)
assertTrue(res)

res = explainFilter.byMetaData(managedIndexMetaDataSomeNull)
assertFalse(res)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.opensearch.indexmanagement.indexstatemanagement.randomChannel
import org.opensearch.indexmanagement.indexstatemanagement.randomCloseActionConfig
import org.opensearch.indexmanagement.indexstatemanagement.randomDeleteActionConfig
import org.opensearch.indexmanagement.indexstatemanagement.randomDestination
import org.opensearch.indexmanagement.indexstatemanagement.randomExplainFilter
import org.opensearch.indexmanagement.indexstatemanagement.randomForceMergeActionConfig
import org.opensearch.indexmanagement.indexstatemanagement.randomIndexPriorityActionConfig
import org.opensearch.indexmanagement.indexstatemanagement.randomManagedIndexConfig
Expand Down Expand Up @@ -258,6 +259,14 @@ class XContentTests : OpenSearchTestCase() {
assertEquals("Round tripping ManagedIndexMetaData doesn't work", metadata, parsedMetaData)
}

fun `test explain filter parsing`() {
val explainFilter = randomExplainFilter()

val explainFilterString = explainFilter.toJsonString()
val parsedExplainFilter = ExplainFilter.parse(parser(explainFilterString))
assertEquals("Round tripping ExplainFilter doesn't work", explainFilter, parsedExplainFilter)
}

fun `test change policy parsing`() {
val changePolicy = randomChangePolicy()

Expand Down
Loading

0 comments on commit e47aff8

Please sign in to comment.