Skip to content

Commit 2737bee

Browse files
committed
Adding unit tests
Signed-off-by: Tarun-kishore <[email protected]>
1 parent 784023f commit 2737bee

File tree

3 files changed

+109
-5
lines changed

3 files changed

+109
-5
lines changed

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ISMTemplateService.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ import org.opensearch.indexmanagement.util.IndexManagementException
2222
@Suppress("ComplexMethod")
2323
fun validateFormat(indexPatterns: List<String>): OpenSearchException? {
2424
val indexPatternFormatErrors = mutableListOf<String>()
25+
var hasInclusionPattern = false
26+
2527
for (indexPattern in indexPatterns) {
2628
// Strip the exclusion prefix (-) if present for validation
2729
val isExclusionPattern = indexPattern.startsWith("-")
30+
if (!isExclusionPattern) {
31+
hasInclusionPattern = true
32+
}
2833
val patternToValidate = if (isExclusionPattern) {
2934
indexPattern.substring(1)
3035
} else {
@@ -52,6 +57,11 @@ fun validateFormat(indexPatterns: List<String>): OpenSearchException? {
5257
}
5358
}
5459

60+
// Check if there's at least one inclusion pattern
61+
if (!hasInclusionPattern) {
62+
indexPatternFormatErrors.add("index_patterns must contain at least one inclusion pattern (patterns cannot be all exclusions)")
63+
}
64+
5565
if (indexPatternFormatErrors.size > 0) {
5666
val validationException = ValidationException()
5767
validationException.addValidationErrors(indexPatternFormatErrors)

src/main/kotlin/org/opensearch/indexmanagement/indexstatemanagement/ManagedIndexCoordinator.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,8 @@ class ManagedIndexCoordinator(
419419
}
420420
}
421421

422-
// If there are no inclusion patterns, treat it as no match
423-
if (inclusionPatterns.isEmpty()) {
424-
return false
425-
}
426-
427422
// Check if index matches any inclusion pattern
423+
// Note: inclusionPatterns.isEmpty() is prevented by validation in ISMTemplateService
428424
val matchesInclusion = inclusionPatterns.any { pattern ->
429425
Regex.simpleMatch(pattern, indexName)
430426
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.indexmanagement.indexstatemanagement
7+
8+
import org.opensearch.indexmanagement.util.IndexManagementException
9+
import org.opensearch.test.OpenSearchTestCase
10+
11+
class ISMTemplateServiceTests : OpenSearchTestCase() {
12+
fun `test validateFormat with pattern containing hash`() {
13+
val patterns = listOf("log#*")
14+
val exception = validateFormat(patterns)
15+
assertNotNull("Expected validation error for pattern with #", exception)
16+
assertTrue(exception is IndexManagementException)
17+
assertTrue(exception!!.message!!.contains("must not contain a '#'"))
18+
}
19+
20+
fun `test validateFormat with exclusion pattern containing hash`() {
21+
val patterns = listOf("log-*", "-test#*")
22+
val exception = validateFormat(patterns)
23+
assertNotNull("Expected validation error for exclusion pattern with #", exception)
24+
assertTrue(exception is IndexManagementException)
25+
assertTrue(exception!!.message!!.contains("must not contain a '#'"))
26+
}
27+
28+
fun `test validateFormat with pattern containing colon`() {
29+
val patterns = listOf("log:*")
30+
val exception = validateFormat(patterns)
31+
assertNotNull("Expected validation error for pattern with :", exception)
32+
assertTrue(exception is IndexManagementException)
33+
assertTrue(exception!!.message!!.contains("must not contain a ':'"))
34+
}
35+
36+
fun `test validateFormat with exclusion pattern containing colon`() {
37+
val patterns = listOf("log-*", "-test:*")
38+
val exception = validateFormat(patterns)
39+
assertNotNull("Expected validation error for exclusion pattern with :", exception)
40+
assertTrue(exception is IndexManagementException)
41+
assertTrue(exception!!.message!!.contains("must not contain a ':'"))
42+
}
43+
44+
fun `test validateFormat with pattern starting with underscore`() {
45+
val patterns = listOf("_log*")
46+
val exception = validateFormat(patterns)
47+
assertNotNull("Expected validation error for pattern starting with _", exception)
48+
assertTrue(exception is IndexManagementException)
49+
assertTrue(exception!!.message!!.contains("must not start with '_'"))
50+
}
51+
52+
fun `test validateFormat with exclusion pattern starting with underscore`() {
53+
val patterns = listOf("log-*", "-_test*")
54+
val exception = validateFormat(patterns)
55+
assertNotNull("Expected validation error for exclusion pattern starting with _", exception)
56+
assertTrue(exception is IndexManagementException)
57+
assertTrue(exception!!.message!!.contains("must not start with '_'"))
58+
}
59+
60+
fun `test validateFormat with empty exclusion pattern`() {
61+
val patterns = listOf("log-*", "-")
62+
val exception = validateFormat(patterns)
63+
assertNotNull("Expected validation error for empty exclusion pattern", exception)
64+
assertTrue(exception is IndexManagementException)
65+
assertTrue(exception!!.message!!.contains("must have content after '-' exclusion prefix"))
66+
}
67+
68+
fun `test validateFormat with only exclusion patterns`() {
69+
val patterns = listOf("-log-test-*", "-log-debug-*")
70+
val exception = validateFormat(patterns)
71+
assertNotNull("Expected validation error for only exclusion patterns", exception)
72+
assertTrue(exception is IndexManagementException)
73+
assertTrue(exception!!.message!!.contains("must contain at least one inclusion pattern"))
74+
}
75+
76+
fun `test validateFormat with valid inclusion and exclusion patterns`() {
77+
val patterns = listOf("log-*", "-log-test-*", "-log-*-debug-*")
78+
val exception = validateFormat(patterns)
79+
assertNull("Expected no validation error for valid patterns", exception)
80+
}
81+
82+
fun `test validateFormat with valid inclusion patterns only`() {
83+
val patterns = listOf("log-*", "app-*")
84+
val exception = validateFormat(patterns)
85+
assertNull("Expected no validation error for valid inclusion patterns", exception)
86+
}
87+
88+
fun `test validateFormat with empty string pattern`() {
89+
val patterns = listOf("")
90+
val exception = validateFormat(patterns)
91+
// Empty string is treated as an inclusion pattern, so it should not fail the "only exclusions" check
92+
// It may fail other validations depending on Strings.validFileNameExcludingAstrix
93+
// For now, we're just testing that it doesn't fail the exclusion-only check
94+
if (exception != null) {
95+
assertFalse(exception.message!!.contains("must contain at least one inclusion pattern"))
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)