|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.alerting |
| 7 | + |
| 8 | +import org.junit.Assert |
| 9 | +import org.opensearch.action.admin.indices.create.CreateIndexRequest |
| 10 | +import org.opensearch.alerting.action.GetAlertsAction |
| 11 | +import org.opensearch.alerting.action.GetAlertsRequest |
| 12 | +import org.opensearch.alerting.core.ScheduledJobIndices |
| 13 | +import org.opensearch.alerting.core.model.DocLevelMonitorInput |
| 14 | +import org.opensearch.alerting.core.model.DocLevelQuery |
| 15 | +import org.opensearch.alerting.core.model.ScheduledJob.Companion.SCHEDULED_JOBS_INDEX |
| 16 | +import org.opensearch.alerting.model.Table |
| 17 | +import org.opensearch.alerting.transport.AlertingSingleNodeTestCase |
| 18 | +import org.opensearch.common.settings.Settings |
| 19 | +import java.time.ZonedDateTime |
| 20 | +import java.time.format.DateTimeFormatter |
| 21 | +import java.time.temporal.ChronoUnit.MILLIS |
| 22 | + |
| 23 | +/** |
| 24 | + * For 2.3 this was backported and some of the tests are not consistent |
| 25 | + * due to missing features launched in newer versions |
| 26 | + */ |
| 27 | +class MonitorDataSourcesIT : AlertingSingleNodeTestCase() { |
| 28 | + |
| 29 | + fun `test execute monitor with dryrun`() { |
| 30 | + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") |
| 31 | + val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery)) |
| 32 | + val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN) |
| 33 | + var monitor = randomDocumentLevelMonitor( |
| 34 | + inputs = listOf(docLevelInput), |
| 35 | + triggers = listOf(trigger), |
| 36 | + ) |
| 37 | + val monitorResponse = createMonitor(monitor) |
| 38 | + val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS)) |
| 39 | + val testDoc = """{ |
| 40 | + "message" : "This is an error from IAD region", |
| 41 | + "test_strict_date_time" : "$testTime", |
| 42 | + "test_field" : "us-west-2" |
| 43 | + }""" |
| 44 | + assertFalse(monitorResponse?.id.isNullOrEmpty()) |
| 45 | + monitor = monitorResponse!!.monitor |
| 46 | + indexDoc(index, "1", testDoc) |
| 47 | + val id = monitorResponse.id |
| 48 | + val executeMonitorResponse = executeMonitor(monitor, id, true) |
| 49 | + Assert.assertEquals(executeMonitorResponse!!.monitorRunResult.monitorName, monitor.name) |
| 50 | + Assert.assertEquals(executeMonitorResponse.monitorRunResult.triggerResults.size, 1) |
| 51 | + searchAlerts(id) |
| 52 | + val table = Table("asc", "id", null, 1, 0, "") |
| 53 | + var getAlertsResponse = client() |
| 54 | + .execute(GetAlertsAction.INSTANCE, GetAlertsRequest(table, "ALL", "ALL", null)) |
| 55 | + .get() |
| 56 | + Assert.assertTrue(getAlertsResponse != null) |
| 57 | + Assert.assertTrue(getAlertsResponse.alerts.size == 0) |
| 58 | + getAlertsResponse = client() |
| 59 | + .execute(GetAlertsAction.INSTANCE, GetAlertsRequest(table, "ALL", "ALL", id)) |
| 60 | + .get() |
| 61 | + Assert.assertTrue(getAlertsResponse != null) |
| 62 | + Assert.assertTrue(getAlertsResponse.alerts.size == 0) |
| 63 | + } |
| 64 | + |
| 65 | + fun `test execute pre-existing monitor and update`() { |
| 66 | + val request = CreateIndexRequest(SCHEDULED_JOBS_INDEX).mapping(ScheduledJobIndices.scheduledJobMappings()) |
| 67 | + .settings(Settings.builder().put("index.hidden", true).build()) |
| 68 | + client().admin().indices().create(request) |
| 69 | + val monitorStringWithoutName = """ |
| 70 | + { |
| 71 | + "monitor": { |
| 72 | + "type": "monitor", |
| 73 | + "schema_version": 0, |
| 74 | + "name": "UayEuXpZtb", |
| 75 | + "monitor_type": "doc_level_monitor", |
| 76 | + "user": { |
| 77 | + "name": "", |
| 78 | + "backend_roles": [], |
| 79 | + "roles": [], |
| 80 | + "custom_attribute_names": [], |
| 81 | + "user_requested_tenant": null |
| 82 | + }, |
| 83 | + "enabled": true, |
| 84 | + "enabled_time": 1662753436791, |
| 85 | + "schedule": { |
| 86 | + "period": { |
| 87 | + "interval": 5, |
| 88 | + "unit": "MINUTES" |
| 89 | + } |
| 90 | + }, |
| 91 | + "inputs": [{ |
| 92 | + "doc_level_input": { |
| 93 | + "description": "description", |
| 94 | + "indices": [ |
| 95 | + "$index" |
| 96 | + ], |
| 97 | + "queries": [{ |
| 98 | + "id": "63efdcce-b5a1-49f4-a25f-6b5f9496a755", |
| 99 | + "name": "3", |
| 100 | + "query": "test_field:\"us-west-2\"", |
| 101 | + "tags": [] |
| 102 | + }] |
| 103 | + } |
| 104 | + }], |
| 105 | + "triggers": [{ |
| 106 | + "document_level_trigger": { |
| 107 | + "id": "OGnTI4MBv6qt0ATc9Phk", |
| 108 | + "name": "mrbHRMevYI", |
| 109 | + "severity": "1", |
| 110 | + "condition": { |
| 111 | + "script": { |
| 112 | + "source": "return true", |
| 113 | + "lang": "painless" |
| 114 | + } |
| 115 | + }, |
| 116 | + "actions": [] |
| 117 | + } |
| 118 | + }], |
| 119 | + "last_update_time": 1662753436791 |
| 120 | + } |
| 121 | + } |
| 122 | + """.trimIndent() |
| 123 | + val monitorId = "abc" |
| 124 | + indexDoc(SCHEDULED_JOBS_INDEX, monitorId, monitorStringWithoutName) |
| 125 | + val getMonitorResponse = getMonitorResponse(monitorId) |
| 126 | + Assert.assertNotNull(getMonitorResponse) |
| 127 | + Assert.assertNotNull(getMonitorResponse.monitor) |
| 128 | + val monitor = getMonitorResponse.monitor |
| 129 | + |
| 130 | + val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS)) |
| 131 | + val testDoc = """{ |
| 132 | + "message" : "This is an error from IAD region", |
| 133 | + "test_strict_date_time" : "$testTime", |
| 134 | + "test_field" : "us-west-2" |
| 135 | + }""" |
| 136 | + indexDoc(index, "1", testDoc) |
| 137 | + var executeMonitorResponse = executeMonitor(monitor!!, monitorId, false) |
| 138 | + Assert.assertNotNull(executeMonitorResponse) |
| 139 | + if (executeMonitorResponse != null) { |
| 140 | + Assert.assertNotNull(executeMonitorResponse.monitorRunResult.monitorName) |
| 141 | + } |
| 142 | + val alerts = searchAlerts(monitorId) |
| 143 | + assertEquals(alerts.size, 1) |
| 144 | + |
| 145 | + val updateMonitorResponse = updateMonitor( |
| 146 | + monitor.copy(id = monitorId), |
| 147 | + monitorId |
| 148 | + ) |
| 149 | + Assert.assertNotNull(updateMonitorResponse) |
| 150 | + indexDoc(index, "2", testDoc) |
| 151 | + executeMonitorResponse = executeMonitor(updateMonitorResponse!!.monitor, monitorId, false) |
| 152 | + val table = Table("asc", "id", null, 1, 0, "") |
| 153 | + var getAlertsResponse = client() |
| 154 | + .execute(GetAlertsAction.INSTANCE, GetAlertsRequest(table, "ALL", "ALL", null)) |
| 155 | + .get() |
| 156 | + Assert.assertTrue(getAlertsResponse != null) |
| 157 | + Assert.assertTrue(getAlertsResponse.alerts.size == 1) |
| 158 | + getAlertsResponse = client() |
| 159 | + .execute(GetAlertsAction.INSTANCE, GetAlertsRequest(table, "ALL", "ALL", monitorId)) |
| 160 | + .get() |
| 161 | + Assert.assertTrue(getAlertsResponse != null) |
| 162 | + Assert.assertTrue(getAlertsResponse.alerts.size == 1) |
| 163 | + } |
| 164 | + |
| 165 | + fun `test get alerts by list of monitors containing both existent and non-existent ids`() { |
| 166 | + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") |
| 167 | + val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery)) |
| 168 | + val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN) |
| 169 | + var monitor = randomDocumentLevelMonitor( |
| 170 | + inputs = listOf(docLevelInput), |
| 171 | + triggers = listOf(trigger), |
| 172 | + ) |
| 173 | + val monitorResponse = createMonitor(monitor) |
| 174 | + val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS)) |
| 175 | + val testDoc = """{ |
| 176 | + "message" : "This is an error from IAD region", |
| 177 | + "test_strict_date_time" : "$testTime", |
| 178 | + "test_field" : "us-west-2" |
| 179 | + }""" |
| 180 | + |
| 181 | + monitor = monitorResponse!!.monitor |
| 182 | + |
| 183 | + val id = monitorResponse.id |
| 184 | + |
| 185 | + var monitor1 = randomDocumentLevelMonitor( |
| 186 | + inputs = listOf(docLevelInput), |
| 187 | + triggers = listOf(trigger), |
| 188 | + ) |
| 189 | + val monitorResponse1 = createMonitor(monitor1) |
| 190 | + monitor1 = monitorResponse1!!.monitor |
| 191 | + val id1 = monitorResponse1.id |
| 192 | + indexDoc(index, "1", testDoc) |
| 193 | + executeMonitor(monitor1, id1, false) |
| 194 | + executeMonitor(monitor, id, false) |
| 195 | + val alerts = searchAlerts(id) |
| 196 | + assertEquals("Alert saved for test monitor", 1, alerts.size) |
| 197 | + val alerts1 = searchAlerts(id) |
| 198 | + assertEquals("Alert saved for test monitor", 1, alerts1.size) |
| 199 | + val table = Table("asc", "id", null, 1000, 0, "") |
| 200 | + var getAlertsResponse = client() |
| 201 | + .execute( |
| 202 | + GetAlertsAction.INSTANCE, |
| 203 | + GetAlertsRequest(table, "ALL", "ALL", null) |
| 204 | + ) |
| 205 | + .get() |
| 206 | + |
| 207 | + Assert.assertTrue(getAlertsResponse != null) |
| 208 | + Assert.assertTrue(getAlertsResponse.alerts.size == 2) |
| 209 | + |
| 210 | + var alertsResponseForRequestWithoutCustomIndex = client() |
| 211 | + .execute( |
| 212 | + GetAlertsAction.INSTANCE, |
| 213 | + GetAlertsRequest(table, "ALL", "ALL", null) |
| 214 | + ) |
| 215 | + .get() |
| 216 | + Assert.assertTrue(alertsResponseForRequestWithoutCustomIndex != null) |
| 217 | + Assert.assertTrue(alertsResponseForRequestWithoutCustomIndex.alerts.size == 2) |
| 218 | + var getAlertsByAlertIds = client() |
| 219 | + .execute( |
| 220 | + GetAlertsAction.INSTANCE, |
| 221 | + GetAlertsRequest(table, "ALL", "ALL", null) |
| 222 | + ) |
| 223 | + .get() |
| 224 | + Assert.assertTrue(getAlertsByAlertIds != null) |
| 225 | + Assert.assertTrue(getAlertsByAlertIds.alerts.size == 2) |
| 226 | + |
| 227 | + var getAlertsByWrongAlertIds = client() |
| 228 | + .execute( |
| 229 | + GetAlertsAction.INSTANCE, |
| 230 | + GetAlertsRequest(table, "ALL", "ALL", null) |
| 231 | + ) |
| 232 | + .get() |
| 233 | + |
| 234 | + Assert.assertTrue(getAlertsByWrongAlertIds != null) |
| 235 | + } |
| 236 | +} |
0 commit comments