Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
Signed-off-by: monusingh-1 <[email protected]>
  • Loading branch information
monusingh-1 committed May 3, 2024
1 parent da8746f commit 770fe16
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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.replication.util.ValidationUtil.validatePattern
import java.util.Collections
import java.util.function.BiConsumer
import java.util.function.BiFunction
Expand Down Expand Up @@ -110,11 +111,14 @@ class UpdateAutoFollowPatternRequest: AcknowledgedRequest<UpdateAutoFollowPatter
}

if(action == Action.REMOVE) {
if(pattern != null) {
if (pattern != null) {
validationException.addValidationError("Unexpected pattern")
}
} else if(pattern == null) {
validationException.addValidationError("Missing pattern")
} else {
if(pattern == null)
validationException.addValidationError("Missing pattern")
else
validatePattern(pattern, validationException)
}

return if(validationException.validationErrors().isEmpty()) return null else validationException
Expand Down
23 changes: 23 additions & 0 deletions src/main/kotlin/org/opensearch/replication/util/ValidationUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ object ValidationUtil {
validationException.addValidationError("Value $name must not start with '.'")
}


/**
* Validate the pattern against the rules that we have for indexPattern name.
*/

fun validatePattern(pattern: String?, validationException: ValidationException) {

if (!Strings.validFileNameExcludingAstrix(pattern))
validationException.addValidationError("Autofollow pattern: $pattern must not contain the following characters ${Strings.INVALID_FILENAME_CHARS}")

if (pattern.isNullOrEmpty() == true)
validationException.addValidationError("Autofollow pattern: $pattern must not be empty")

if ((pattern?.contains("#") ?: false)|| (pattern?.contains(":") ?: false))
validationException.addValidationError("Autofollow pattern: $pattern must not contain '#' or ':'")

if ((pattern?.startsWith('_') ?: false) || (pattern?.startsWith('-') ?: false))
validationException.addValidationError("Autofollow pattern: $pattern must not start with '_' or '-'")
}




/**
* validate leader index version for compatibility
* If on higher version - Replication will not be allowed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,52 @@ class UpdateAutoFollowPatternIT: MultiClusterRestTestCase() {
}
}

fun `test auto follow should fail on indexPattern validation failure`() {
val followerClient = getClientForCluster(FOLLOWER)
createConnectionBetweenClusters(FOLLOWER, LEADER, connectionAlias)
assertPatternValidation(followerClient, "testPattern,",
"Autofollow pattern: testPattern, must not contain the following characters")
assertPatternValidation(followerClient, "testPat?",
"Autofollow pattern: testPat? must not contain the following characters")
assertPatternValidation(followerClient, "test#",
"Autofollow pattern: test# must not contain '#' or ':'")
assertPatternValidation(followerClient, "test:",
"Autofollow pattern: test: must not contain '#' or ':'")
assertPatternValidation(followerClient, "_test",
"Autofollow pattern: _test must not start with '_' or '-'")
assertPatternValidation(followerClient, "-leader",
"Autofollow pattern: -leader must not start with '_' or '-'")
assertPatternValidation(followerClient, "",
"Autofollow pattern: must not be empty")

}
private fun assertPatternValidation(followerClient: RestHighLevelClient, pattern: String,
errorMsg: String) {
Assertions.assertThatThrownBy {
followerClient.updateAutoFollowPattern(connectionAlias, indexPatternName, pattern)
}.isInstanceOf(ResponseException::class.java)
.hasMessageContaining(errorMsg)
}

fun `test auto follow should succeed on valid indexPatterns`() {
val followerClient = getClientForCluster(FOLLOWER)
createConnectionBetweenClusters(FOLLOWER, LEADER, connectionAlias)
assertValidPatternValidation(followerClient, "test-leader")
assertValidPatternValidation(followerClient, "test*")
assertValidPatternValidation(followerClient, "leader-*")
assertValidPatternValidation(followerClient, "leader_test")
assertValidPatternValidation(followerClient, "Leader_Test-*")
assertValidPatternValidation(followerClient, "Leader_*")

}

private fun assertValidPatternValidation(followerClient: RestHighLevelClient, pattern: String) {
Assertions.assertThatCode {
followerClient.updateAutoFollowPattern(connectionAlias, indexPatternName, pattern)
}.doesNotThrowAnyException()
}


fun `test autofollow task with concurrent job setting set to run single job`() {
val followerClient = getClientForCluster(FOLLOWER)
val leaderClient = getClientForCluster(LEADER)
Expand Down

0 comments on commit 770fe16

Please sign in to comment.