diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/action/DocLevelMonitorFanOutRequestTests.kt b/alerting/src/test/kotlin/org/opensearch/alerting/action/DocLevelMonitorFanOutRequestTests.kt deleted file mode 100644 index 684b9a630..000000000 --- a/alerting/src/test/kotlin/org/opensearch/alerting/action/DocLevelMonitorFanOutRequestTests.kt +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting.action - -import org.opensearch.alerting.ALWAYS_RUN -import org.opensearch.alerting.randomDocumentLevelMonitor -import org.opensearch.alerting.randomDocumentLevelTrigger -import org.opensearch.common.io.stream.BytesStreamOutput -import org.opensearch.commons.alerting.action.DocLevelMonitorFanOutRequest -import org.opensearch.commons.alerting.model.ActionExecutionTime -import org.opensearch.commons.alerting.model.DocLevelMonitorInput -import org.opensearch.commons.alerting.model.DocLevelQuery -import org.opensearch.commons.alerting.model.IndexExecutionContext -import org.opensearch.commons.alerting.model.IntervalSchedule -import org.opensearch.commons.alerting.model.Monitor -import org.opensearch.commons.alerting.model.MonitorMetadata -import org.opensearch.commons.alerting.model.Workflow -import org.opensearch.commons.alerting.model.WorkflowRunContext -import org.opensearch.core.common.io.stream.StreamInput -import org.opensearch.core.index.shard.ShardId -import org.opensearch.index.seqno.SequenceNumbers -import org.opensearch.test.OpenSearchTestCase -import java.time.Instant -import java.time.temporal.ChronoUnit -import java.util.UUID - -class DocLevelMonitorFanOutRequestTests : OpenSearchTestCase() { - - fun `test doc level monitor fan out request as stream`() { - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", fields = listOf(), name = "3") - val docLevelInput = DocLevelMonitorInput("description", listOf("test-index"), listOf(docQuery)) - - val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN) - val monitor = randomDocumentLevelMonitor( - inputs = listOf(docLevelInput), - triggers = listOf(trigger), - enabled = true, - schedule = IntervalSchedule(1, ChronoUnit.MINUTES) - ) - val monitorMetadata = MonitorMetadata( - "test", - SequenceNumbers.UNASSIGNED_SEQ_NO, - SequenceNumbers.UNASSIGNED_PRIMARY_TERM, - Monitor.NO_ID, - listOf(ActionExecutionTime("", Instant.now())), - mutableMapOf("index" to mutableMapOf("1" to "1")), - mutableMapOf("test-index" to ".opensearch-sap-test_windows-queries-000001") - ) - val indexExecutionContext = IndexExecutionContext( - listOf(docQuery), - mutableMapOf("index" to mutableMapOf("1" to "1")), - mutableMapOf("index" to mutableMapOf("1" to "1")), - "test-index", - "test-index", - listOf("test-index"), - listOf("test-index"), - listOf("test-field"), - listOf("1", "2") - ) - val workflowRunContext = WorkflowRunContext( - Workflow.NO_ID, - Workflow.NO_ID, - Monitor.NO_ID, - mutableMapOf("index" to listOf("1")), - true - ) - val docLevelMonitorFanOutRequest = DocLevelMonitorFanOutRequest( - monitor, - false, - monitorMetadata, - UUID.randomUUID().toString(), - indexExecutionContext, - listOf(ShardId("test-index", UUID.randomUUID().toString(), 0)), - listOf("test-index"), - workflowRunContext - ) - val out = BytesStreamOutput() - docLevelMonitorFanOutRequest.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newDocLevelMonitorFanOutRequest = DocLevelMonitorFanOutRequest(sin) - assertEquals(docLevelMonitorFanOutRequest.monitor, newDocLevelMonitorFanOutRequest.monitor) - assertEquals(docLevelMonitorFanOutRequest.executionId, newDocLevelMonitorFanOutRequest.executionId) - assertEquals(docLevelMonitorFanOutRequest.monitorMetadata, newDocLevelMonitorFanOutRequest.monitorMetadata) - assertEquals(docLevelMonitorFanOutRequest.indexExecutionContext, newDocLevelMonitorFanOutRequest.indexExecutionContext) - assertEquals(docLevelMonitorFanOutRequest.shardIds, newDocLevelMonitorFanOutRequest.shardIds) - assertEquals(docLevelMonitorFanOutRequest.workflowRunContext, newDocLevelMonitorFanOutRequest.workflowRunContext) - } -} diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/action/DocLevelMonitorFanOutResponseTests.kt b/alerting/src/test/kotlin/org/opensearch/alerting/action/DocLevelMonitorFanOutResponseTests.kt deleted file mode 100644 index bb9c18705..000000000 --- a/alerting/src/test/kotlin/org/opensearch/alerting/action/DocLevelMonitorFanOutResponseTests.kt +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting.action - -import org.opensearch.alerting.randomDocumentLevelTriggerRunResult -import org.opensearch.common.io.stream.BytesStreamOutput -import org.opensearch.commons.alerting.action.DocLevelMonitorFanOutResponse -import org.opensearch.commons.alerting.model.InputRunResults -import org.opensearch.core.common.io.stream.StreamInput -import org.opensearch.test.OpenSearchTestCase - -class DocLevelMonitorFanOutResponseTests : OpenSearchTestCase() { - fun `test doc level monitor fan out response with errors as stream`() { - val docLevelMonitorFanOutResponse = DocLevelMonitorFanOutResponse( - "nodeid", - "eid", - "monitorId", - mutableMapOf("index" to mutableMapOf("1" to "1")), - InputRunResults(error = null), - mapOf("1" to randomDocumentLevelTriggerRunResult(), "2" to randomDocumentLevelTriggerRunResult()) - ) - val out = BytesStreamOutput() - docLevelMonitorFanOutResponse.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newDocLevelMonitorFanOutResponse = DocLevelMonitorFanOutResponse(sin) - assertEquals(docLevelMonitorFanOutResponse.nodeId, newDocLevelMonitorFanOutResponse.nodeId) - assertEquals(docLevelMonitorFanOutResponse.executionId, newDocLevelMonitorFanOutResponse.executionId) - assertEquals(docLevelMonitorFanOutResponse.monitorId, newDocLevelMonitorFanOutResponse.monitorId) - assertEquals(docLevelMonitorFanOutResponse.lastRunContexts, newDocLevelMonitorFanOutResponse.lastRunContexts) - assertEquals(docLevelMonitorFanOutResponse.inputResults, newDocLevelMonitorFanOutResponse.inputResults) - assertEquals(docLevelMonitorFanOutResponse.triggerResults, newDocLevelMonitorFanOutResponse.triggerResults) - } - - fun `test doc level monitor fan out response as stream`() { - val workflow = DocLevelMonitorFanOutResponse( - "nodeid", - "eid", - "monitorId", - mapOf("index" to mapOf("1" to "1")) as MutableMap, - InputRunResults(), - mapOf("1" to randomDocumentLevelTriggerRunResult(), "2" to randomDocumentLevelTriggerRunResult()) - ) - val out = BytesStreamOutput() - workflow.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newWorkflow = DocLevelMonitorFanOutResponse(sin) - assertEquals(workflow.nodeId, newWorkflow.nodeId) - assertEquals(workflow.executionId, newWorkflow.executionId) - assertEquals(workflow.monitorId, newWorkflow.monitorId) - assertEquals(workflow.lastRunContexts, newWorkflow.lastRunContexts) - assertEquals(workflow.inputResults, newWorkflow.inputResults) - assertEquals(workflow.triggerResults, newWorkflow.triggerResults) - } -} diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/model/WriteableTests.kt b/alerting/src/test/kotlin/org/opensearch/alerting/model/WriteableTests.kt index a840902b7..d30864ec0 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/model/WriteableTests.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/model/WriteableTests.kt @@ -5,108 +5,18 @@ package org.opensearch.alerting.model -import org.junit.Assert import org.opensearch.alerting.model.destination.email.EmailAccount import org.opensearch.alerting.model.destination.email.EmailGroup -import org.opensearch.alerting.randomBucketLevelMonitorRunResult -import org.opensearch.alerting.randomBucketLevelTriggerRunResult -import org.opensearch.alerting.randomDocumentLevelMonitorRunResult import org.opensearch.alerting.randomEmailAccount import org.opensearch.alerting.randomEmailGroup -import org.opensearch.alerting.randomInputRunResults -import org.opensearch.alerting.randomQueryLevelMonitorRunResult -import org.opensearch.alerting.randomQueryLevelTriggerRunResult -import org.opensearch.common.UUIDs import org.opensearch.common.io.stream.BytesStreamOutput -import org.opensearch.commons.alerting.model.ActionRunResult -import org.opensearch.commons.alerting.model.BucketLevelTriggerRunResult -import org.opensearch.commons.alerting.model.DocumentLevelTriggerRunResult -import org.opensearch.commons.alerting.model.InputRunResults -import org.opensearch.commons.alerting.model.MonitorRunResult -import org.opensearch.commons.alerting.model.QueryLevelTriggerRunResult import org.opensearch.commons.alerting.model.SearchInput import org.opensearch.core.common.io.stream.StreamInput import org.opensearch.search.builder.SearchSourceBuilder import org.opensearch.test.OpenSearchTestCase -import java.time.Instant class WriteableTests : OpenSearchTestCase() { - fun `test actionrunresult as stream`() { - val actionRunResult = randomActionRunResult() - val out = BytesStreamOutput() - actionRunResult.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newActionRunResult = ActionRunResult(sin) - assertEquals("Round tripping ActionRunResult doesn't work", actionRunResult, newActionRunResult) - } - - fun `test query-level triggerrunresult as stream`() { - val runResult = randomQueryLevelTriggerRunResult() - val out = BytesStreamOutput() - runResult.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newRunResult = QueryLevelTriggerRunResult(sin) - assertEquals(runResult.triggerName, newRunResult.triggerName) - assertEquals(runResult.triggered, newRunResult.triggered) - assertEquals(runResult.error, newRunResult.error) - assertEquals(runResult.actionResults, newRunResult.actionResults) - } - - fun `test bucket-level triggerrunresult as stream`() { - val runResult = randomBucketLevelTriggerRunResult() - val out = BytesStreamOutput() - runResult.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newRunResult = BucketLevelTriggerRunResult(sin) - assertEquals("Round tripping ActionRunResult doesn't work", runResult, newRunResult) - } - - fun `test doc-level triggerrunresult as stream`() { - val runResult = randomDocumentLevelTriggerRunResult() - val out = BytesStreamOutput() - runResult.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newRunResult = DocumentLevelTriggerRunResult(sin) - assertEquals("Round tripping ActionRunResult doesn't work", runResult, newRunResult) - } - - fun `test inputrunresult as stream`() { - val runResult = randomInputRunResults() - val out = BytesStreamOutput() - runResult.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newRunResult = InputRunResults.readFrom(sin) - assertEquals("Round tripping InputRunResults doesn't work", runResult, newRunResult) - } - - fun `test query-level monitorrunresult as stream`() { - val runResult = randomQueryLevelMonitorRunResult() - val out = BytesStreamOutput() - runResult.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newRunResult = MonitorRunResult(sin) - assertEquals("Round tripping MonitorRunResult doesn't work", runResult, newRunResult) - } - - fun `test bucket-level monitorrunresult as stream`() { - val runResult = randomBucketLevelMonitorRunResult() - val out = BytesStreamOutput() - runResult.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newRunResult = MonitorRunResult(sin) - assertEquals("Round tripping MonitorRunResult doesn't work", runResult, newRunResult) - } - - fun `test doc-level monitorrunresult as stream`() { - val runResult = randomDocumentLevelMonitorRunResult() - val out = BytesStreamOutput() - runResult.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newRunResult = MonitorRunResult(sin) - assertEquals("Round tripping MonitorRunResult doesn't work", runResult, newRunResult) - } - fun `test searchinput as stream`() { val input = SearchInput(emptyList(), SearchSourceBuilder()) val out = BytesStreamOutput() @@ -133,35 +43,4 @@ class WriteableTests : OpenSearchTestCase() { val newEmailGroup = EmailGroup.readFrom(sin) assertEquals("Round tripping EmailGroup doesn't work", emailGroup, newEmailGroup) } - - fun `test DocumentLevelTriggerRunResult as stream`() { - val workflow = randomDocumentLevelTriggerRunResult() - val out = BytesStreamOutput() - workflow.writeTo(out) - val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes) - val newWorkflow = DocumentLevelTriggerRunResult(sin) - Assert.assertEquals("Round tripping dltrr failed", newWorkflow, workflow) - } - - fun randomDocumentLevelTriggerRunResult(): DocumentLevelTriggerRunResult { - val map = mutableMapOf() - map.plus(Pair("key1", randomActionRunResult())) - map.plus(Pair("key2", randomActionRunResult())) - return DocumentLevelTriggerRunResult( - "trigger-name", - mutableListOf(UUIDs.randomBase64UUID().toString()), - null, - mutableMapOf(Pair("alertId", map)) - ) - } - - fun randomActionRunResult(): ActionRunResult { - val map = mutableMapOf() - map.plus(Pair("key1", "val1")) - map.plus(Pair("key2", "val2")) - return ActionRunResult( - "1234", "test-action", map, - false, Instant.now(), null - ) - } } diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/model/XContentTests.kt b/alerting/src/test/kotlin/org/opensearch/alerting/model/XContentTests.kt index 2e1096a44..56fbd9866 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/model/XContentTests.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/model/XContentTests.kt @@ -14,10 +14,8 @@ import org.opensearch.alerting.randomAlert import org.opensearch.alerting.randomEmailAccount import org.opensearch.alerting.randomEmailGroup import org.opensearch.alerting.toJsonString -import org.opensearch.common.xcontent.XContentFactory import org.opensearch.commons.alerting.model.ActionExecutionResult import org.opensearch.commons.alerting.model.Alert -import org.opensearch.commons.alerting.model.MonitorMetadata import org.opensearch.commons.alerting.util.string import org.opensearch.core.xcontent.ToXContent import org.opensearch.test.OpenSearchTestCase @@ -79,17 +77,4 @@ class XContentTests : OpenSearchTestCase() { val parsedEmailGroup = EmailGroup.parse(parser(emailGroupString)) assertEquals("Round tripping EmailGroup doesn't work", emailGroup, parsedEmailGroup) } - - fun `test MonitorMetadata`() { - val monitorMetadata = MonitorMetadata( - id = "monitorId-metadata", - monitorId = "monitorId", - lastActionExecutionTimes = emptyList(), - lastRunContext = emptyMap(), - sourceToQueryIndexMapping = mutableMapOf() - ) - val monitorMetadataString = monitorMetadata.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS).string() - val parsedMonitorMetadata = MonitorMetadata.parse(parser(monitorMetadataString)) - assertEquals("Round tripping MonitorMetadata doesn't work", monitorMetadata, parsedMonitorMetadata) - } } diff --git a/sample-remote-monitor-plugin/src/test/java/org/opensearch/alerting/SampleRemoteMonitorIT.java b/sample-remote-monitor-plugin/src/test/java/org/opensearch/alerting/SampleRemoteMonitorIT.java index bc07da3ec..0dfa49582 100644 --- a/sample-remote-monitor-plugin/src/test/java/org/opensearch/alerting/SampleRemoteMonitorIT.java +++ b/sample-remote-monitor-plugin/src/test/java/org/opensearch/alerting/SampleRemoteMonitorIT.java @@ -9,6 +9,7 @@ import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.io.entity.StringEntity; +import org.junit.AfterClass; import org.junit.Assert; import org.opensearch.alerting.monitor.runners.SampleRemoteDocLevelMonitorRunner; import org.opensearch.alerting.monitor.runners.SampleRemoteMonitorRunner1; @@ -21,11 +22,20 @@ import org.opensearch.common.settings.Settings; import org.opensearch.common.xcontent.LoggingDeprecationHandler; import org.opensearch.common.xcontent.json.JsonXContent; +import org.opensearch.core.common.Strings; import org.opensearch.core.rest.RestStatus; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.test.rest.OpenSearchRestTestCase; +import javax.management.MBeanServerInvocationHandler; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -183,4 +193,34 @@ protected Response makeRequest( } return client.performRequest(request); } + + @AfterClass + public static void dumpCoverage() throws IOException, MalformedObjectNameException { + // jacoco.dir is set in esplugin-coverage.gradle, if it doesn't exist we don't + // want to collect coverage so we can return early + String jacocoBuildPath = System.getProperty("jacoco.dir"); + if (Strings.isNullOrEmpty(jacocoBuildPath)) { + return; + } + + String serverUrl = "service:jmx:rmi:///jndi/rmi://127.0.0.1:7777/jmxrmi"; + try (JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(serverUrl))) { + IProxy proxy = MBeanServerInvocationHandler.newProxyInstance( + connector.getMBeanServerConnection(), new ObjectName("org.jacoco:type=Runtime"), IProxy.class, + false); + + Path path = org.opensearch.common.io.PathUtils.get(jacocoBuildPath + "/integTestRunner.exec"); + Files.write(path, proxy.getExecutionData(false)); + } catch (Exception ex) { + throw new RuntimeException("Failed to dump coverage: " + ex); + } + } + + public interface IProxy { + byte[] getExecutionData(boolean reset); + + void dump(boolean reset); + + void reset(); + } } \ No newline at end of file