@@ -7,6 +7,7 @@ package org.opensearch.alerting
7
7
8
8
import org.junit.Assert
9
9
import org.opensearch.action.admin.cluster.state.ClusterStateRequest
10
+ import org.opensearch.action.admin.indices.alias.Alias
10
11
import org.opensearch.action.admin.indices.close.CloseIndexRequest
11
12
import org.opensearch.action.admin.indices.create.CreateIndexRequest
12
13
import org.opensearch.action.admin.indices.delete.DeleteIndexRequest
@@ -23,7 +24,6 @@ import org.opensearch.alerting.action.SearchMonitorAction
23
24
import org.opensearch.alerting.action.SearchMonitorRequest
24
25
import org.opensearch.alerting.alerts.AlertIndices
25
26
import org.opensearch.alerting.core.ScheduledJobIndices
26
- import org.opensearch.alerting.model.DocumentLevelTriggerRunResult
27
27
import org.opensearch.alerting.transport.AlertingSingleNodeTestCase
28
28
import org.opensearch.alerting.util.DocLevelMonitorQueries
29
29
import org.opensearch.alerting.util.DocLevelMonitorQueries.Companion.INDEX_PATTERN_SUFFIX
@@ -755,12 +755,43 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
755
755
assertEquals(" Didn't match all 4 queries" , 1 , findings[0 ].docLevelQueries.size)
756
756
}
757
757
758
- fun `test execute monitor without create when no monitors exists ` () {
759
- val docQuery = DocLevelQuery (query = " test_field :\" us-west-2\" " , name = " 3" )
758
+ fun `test cleanup monitor on partial create monitor failure ` () {
759
+ val docQuery = DocLevelQuery (query = " dnbkjndsfkjbnds :\" us-west-2\" " , name = " 3" )
760
760
val docLevelInput = DocLevelMonitorInput (" description" , listOf (index), listOf (docQuery))
761
761
val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN )
762
762
val customQueryIndex = " custom_alerts_index"
763
- val analyzer = " whitespace"
763
+ val analyzer = " dfbdfbafd"
764
+ val testDoc = """ {
765
+ "rule": {"title": "some_title"},
766
+ "message": "msg 1 2 3 4"
767
+ }"""
768
+ indexDoc(index, " 2" , testDoc)
769
+ client().admin().indices()
770
+ .create(
771
+ CreateIndexRequest (customQueryIndex + " -000001" ).alias(Alias (customQueryIndex))
772
+ .mapping(
773
+ """
774
+ {
775
+ "_meta": {
776
+ "schema_version": 1
777
+ },
778
+ "properties": {
779
+ "query": {
780
+ "type": "percolator_ext"
781
+ },
782
+ "monitor_id": {
783
+ "type": "text"
784
+ },
785
+ "index": {
786
+ "type": "text"
787
+ }
788
+ }
789
+ }
790
+ """ .trimIndent()
791
+ )
792
+ )
793
+
794
+ client().admin().indices().close(CloseIndexRequest (customQueryIndex))
764
795
var monitor = randomDocumentLevelMonitor(
765
796
inputs = listOf (docLevelInput),
766
797
triggers = listOf (trigger),
@@ -769,31 +800,12 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
769
800
queryIndexMappingsByType = mapOf (Pair (" text" , mapOf (Pair (" analyzer" , analyzer)))),
770
801
)
771
802
)
772
- var executeMonitorResponse = executeMonitor(monitor, null )
773
- val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
774
- val testDoc = """ {
775
- "message" : "This is an error from IAD region",
776
- "test_strict_date_time" : "$testTime ",
777
- "test_field" : "us-west-2"
778
- }"""
779
-
780
- assertIndexNotExists(SCHEDULED_JOBS_INDEX )
781
-
782
- val createMonitorResponse = createMonitor(monitor)
783
-
784
- assertIndexExists(SCHEDULED_JOBS_INDEX )
785
-
786
- indexDoc(index, " 1" , testDoc)
787
-
788
- executeMonitorResponse = executeMonitor(monitor, createMonitorResponse?.id, dryRun = false )
789
-
790
- Assert .assertEquals(executeMonitorResponse!! .monitorRunResult.monitorName, monitor.name)
791
- Assert .assertEquals(executeMonitorResponse.monitorRunResult.triggerResults.size, 1 )
792
- Assert .assertEquals(
793
- (executeMonitorResponse.monitorRunResult.triggerResults.iterator().next().value as DocumentLevelTriggerRunResult )
794
- .triggeredDocs.size,
795
- 1
796
- )
803
+ try {
804
+ createMonitor(monitor)
805
+ fail(" monitor creation should fail due to incorrect analyzer name in test setup" )
806
+ } catch (e: Exception ) {
807
+ Assert .assertEquals(client().search(SearchRequest (SCHEDULED_JOBS_INDEX )).get().hits.hits.size, 0 )
808
+ }
797
809
}
798
810
799
811
fun `test execute monitor with custom query index and custom field mappings` () {
0 commit comments