Skip to content

Commit

Permalink
update alerting with latest common-utils commit
Browse files Browse the repository at this point in the history
Signed-off-by: Subhobrata Dey <[email protected]>
  • Loading branch information
sbcd90 committed May 29, 2024
1 parent 8bd00b4 commit a840958
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 284 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryLevelTriggerRunResult>(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<BucketLevelTriggerRunResult>(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<DocumentLevelTriggerRunResult>(sin)
assertEquals("Round tripping MonitorRunResult doesn't work", runResult, newRunResult)
}

fun `test searchinput as stream`() {
val input = SearchInput(emptyList(), SearchSourceBuilder())
val out = BytesStreamOutput()
Expand All @@ -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<String, ActionRunResult>()
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<String, String>()
map.plus(Pair("key1", "val1"))
map.plus(Pair("key2", "val2"))
return ActionRunResult(
"1234", "test-action", map,
false, Instant.now(), null
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}

0 comments on commit a840958

Please sign in to comment.