diff --git a/sample-remote-monitor-plugin/build.gradle b/sample-remote-monitor-plugin/build.gradle index 03493b915..7c1845b16 100644 --- a/sample-remote-monitor-plugin/build.gradle +++ b/sample-remote-monitor-plugin/build.gradle @@ -64,103 +64,3 @@ javaRestTest { testClusters.javaRestTest { testDistribution = 'INTEG_TEST' } - -task integTest(type: RestIntegTestTask) { - description = "Run tests against a cluster" - testClassesDirs = sourceSets.test.output.classesDirs - classpath = sourceSets.test.runtimeClasspath - - if (System.getProperty("https") == null || System.getProperty("https") == "false") { - filter { - includeTestsMatching "org.opensearch.alerting.SampleRemoteMonitorIT" - } - } -} -tasks.named("check").configure { dependsOn(integTest) } - -integTest { - if (project.hasProperty('excludeTests')) { - project.properties['excludeTests']?.replaceAll('\\s', '')?.split('[,;]')?.each { - exclude "${it}" - } - } - systemProperty 'tests.security.manager', 'false' - systemProperty 'java.io.tmpdir', es_tmp_dir.absolutePath - - systemProperty "https", System.getProperty("https") - systemProperty "user", System.getProperty("user") - systemProperty "password", System.getProperty("password") - // Tell the test JVM if the cluster JVM is running under a debugger so that tests can use longer timeouts for - // requests. The 'doFirst' delays reading the debug setting on the cluster till execution time. - doFirst { - // Tell the test JVM if the cluster JVM is running under a debugger so that tests can - // use longer timeouts for requests. - def isDebuggingCluster = getDebug() || System.getProperty("test.debug") != null - systemProperty 'cluster.debug', isDebuggingCluster - // Set number of nodes system property to be used in tests - systemProperty 'cluster.number_of_nodes', "${_numNodes}" - // There seems to be an issue when running multi node run or integ tasks with unicast_hosts - // not being written, the waitForAllConditions ensures it's written - getClusters().forEach { cluster -> - cluster.waitForAllConditions() - } - } - - // The -Dcluster.debug option makes the cluster debuggable; this makes the tests debuggable - if (System.getProperty("test.debug") != null) { - jvmArgs '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=8000' - } -} -project.getTasks().getByName('bundlePlugin').dependsOn(rootProject.project(":alerting").tasks.getByName('assemble')) -Zip bundle = (Zip) project.getTasks().getByName("bundlePlugin"); -Zip rootBundle = (Zip) rootProject.project(":alerting").getTasks().getByName("bundlePlugin"); -integTest.dependsOn(bundle) -integTest.getClusters().forEach{c -> { - c.plugin(rootProject.project(":alerting").getObjects().fileProperty().value(rootBundle.getArchiveFile())) - c.plugin(project.getObjects().fileProperty().value(bundle.getArchiveFile())) -}} - -testClusters.integTest { - testDistribution = 'ARCHIVE' - - // Cluster shrink exception thrown if we try to set numberOfNodes to 1, so only apply if > 1 - if (_numNodes > 1) numberOfNodes = _numNodes - // When running integration tests it doesn't forward the --debug-jvm to the cluster anymore - // i.e. we have to use a custom property to flag when we want to debug OpenSearch JVM - // since we also support multi node integration tests we increase debugPort per node - if (System.getProperty("cluster.debug") != null) { - def debugPort = 5005 - nodes.forEach { node -> - node.jvmArgs("-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=*:${debugPort}") - debugPort += 1 - } - } - setting 'path.repo', repo.absolutePath -} - -run { - doFirst { - // There seems to be an issue when running multi node run or integ tasks with unicast_hosts - // not being written, the waitForAllConditions ensures it's written - getClusters().forEach { cluster -> - cluster.waitForAllConditions() - } - } - useCluster testClusters.integTest -} - -// As of ES 7.7 the sample-extension-plugin is being added to the list of plugins for the testCluster during build before -// the job-scheduler plugin is causing build failures. -// The job-scheduler zip is added explicitly above but the sample-extension-plugin is added implicitly at some time during evaluation. -// Will need to do a deep dive to find out exactly what task adds the sample-extension-plugin and add job-scheduler there but a temporary hack is to -// reorder the plugins list after evaluation but prior to task execution when the plugins are installed. -afterEvaluate { - testClusters.javaRestTest.nodes.each { node -> - def nodePlugins = node.plugins - def firstPlugin = nodePlugins.get(0) - if (firstPlugin.provider == project.bundlePlugin.archiveFile) { - nodePlugins.remove(0) - nodePlugins.add(firstPlugin) - } - } -} \ No newline at end of file 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 e81d017cd..e69de29bb 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 @@ -1,342 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.alerting; - -import org.apache.http.Header; -import org.apache.http.HttpEntity; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.junit.AfterClass; -import org.junit.Assert; -import org.opensearch.alerting.monitor.runners.SampleRemoteDocLevelMonitorRunner; -import org.opensearch.alerting.monitor.runners.SampleRemoteMonitorRunner1; -import org.opensearch.alerting.monitor.runners.SampleRemoteMonitorRunner2; -import org.opensearch.client.Request; -import org.opensearch.client.RequestOptions; -import org.opensearch.client.Response; -import org.opensearch.client.RestClient; -import org.opensearch.client.WarningsHandler; -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.List; -import java.util.Locale; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - -public class SampleRemoteMonitorIT extends OpenSearchRestTestCase { - - @SuppressWarnings("unchecked") - public void testSingleSampleMonitor() throws IOException, InterruptedException { - Response response = makeRequest(client(), "POST", "_plugins/_sample_remote_monitor/monitor", Map.of("run_monitor", "single"), null); - Assert.assertEquals("Unable to create remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - Map responseJson = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - response.getEntity().getContent() - ).map(); - String monitorId = responseJson.get("_id").toString(); - - response = makeRequest(client(), "POST", "/_plugins/_alerting/monitors/" + monitorId + "/_execute", Map.of(), null); - Assert.assertEquals("Unable to execute remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - AtomicBoolean found = new AtomicBoolean(false); - OpenSearchRestTestCase.waitUntil( - () -> { - try { - Response searchResponse = makeRequest(client(), "POST", SampleRemoteMonitorRunner1.SAMPLE_MONITOR_RUNNER1_INDEX + "/_search", Map.of(), - new StringEntity("{\"query\":{\"match_all\":{}}}", ContentType.APPLICATION_JSON)); - Map searchResponseJson = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - searchResponse.getEntity().getContent() - ).map(); - found.set(Integer.parseInt((((Map) ((Map) searchResponseJson.get("hits")).get("total")).get("value")).toString()) == 1 && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).containsKey("hello") && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).get("hello").toString().equals("1")); - return found.get(); - } catch (IOException ex) { - return false; - } - }, 10, TimeUnit.SECONDS); - Assert.assertTrue(found.get()); - } - - @SuppressWarnings("unchecked") - public void testMultipleSampleMonitors() throws IOException, InterruptedException { - Response response = makeRequest(client(), "POST", "_plugins/_sample_remote_monitor/monitor", Map.of("run_monitor", "multiple"), null); - Assert.assertEquals("Unable to create remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - Map responseJson = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - response.getEntity().getContent() - ).map(); - String monitorIds = responseJson.get("_id").toString(); - String firstMonitorId = monitorIds.split(" ")[0]; - String secondMonitorId = monitorIds.split(" ")[1]; - - response = makeRequest(client(), "POST", "/_plugins/_alerting/monitors/" + firstMonitorId + "/_execute", Map.of(), null); - Assert.assertEquals("Unable to execute remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - response = makeRequest(client(), "POST", "/_plugins/_alerting/monitors/" + secondMonitorId + "/_execute", Map.of(), null); - Assert.assertEquals("Unable to execute remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - AtomicBoolean found = new AtomicBoolean(false); - OpenSearchRestTestCase.waitUntil( - () -> { - try { - Response searchResponse = makeRequest(client(), "POST", SampleRemoteMonitorRunner1.SAMPLE_MONITOR_RUNNER1_INDEX + "/_search", Map.of(), - new StringEntity("{\"query\":{\"match_all\":{}}}", ContentType.APPLICATION_JSON)); - Map searchResponseJson = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - searchResponse.getEntity().getContent() - ).map(); - found.set(Integer.parseInt((((Map) ((Map) searchResponseJson.get("hits")).get("total")).get("value")).toString()) == 1); - return found.get(); - } catch (IOException ex) { - return false; - } - }, 10, TimeUnit.SECONDS); - Assert.assertTrue(found.get()); - - found.set(false); - OpenSearchRestTestCase.waitUntil( - () -> { - try { - Response searchResponse = makeRequest(client(), "POST", SampleRemoteMonitorRunner2.SAMPLE_MONITOR_RUNNER2_INDEX + "/_search", Map.of(), - new StringEntity("{\"query\":{\"match_all\":{}}}", ContentType.APPLICATION_JSON)); - Map searchResponseJson = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - searchResponse.getEntity().getContent() - ).map(); - found.set(Integer.parseInt((((Map) ((Map) searchResponseJson.get("hits")).get("total")).get("value")).toString()) == 1 && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).containsKey("doc_level_input") && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).get("doc_level_input").toString().equals("test:1")); - return found.get(); - } catch (IOException ex) { - return false; - } - }, 10, TimeUnit.SECONDS); - Assert.assertTrue(found.get()); - } - - @SuppressWarnings("unchecked") - public void testSampleRemoteDocLevelMonitor() throws IOException, InterruptedException { - createIndex("index", Settings.builder().put("number_of_shards", "7").build()); - Response response = makeRequest(client(), "POST", "_plugins/_sample_remote_monitor/monitor", Map.of("run_monitor", "doc_level"), null); - Assert.assertEquals("Unable to create remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - Map responseJson = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - response.getEntity().getContent() - ).map(); - String monitorId = responseJson.get("_id").toString(); - - response = makeRequest(client(), "POST", "/_plugins/_alerting/monitors/" + monitorId + "/_execute", Map.of(), null); - Assert.assertEquals("Unable to execute remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - AtomicBoolean found = new AtomicBoolean(false); - OpenSearchRestTestCase.waitUntil( - () -> { - try { - Response searchResponse = makeRequest(client(), "POST", SampleRemoteDocLevelMonitorRunner.SAMPLE_REMOTE_DOC_LEVEL_MONITOR_RUNNER_INDEX + "/_search", Map.of(), - new StringEntity("{\"query\":{\"match_all\":{}}}", ContentType.APPLICATION_JSON)); - Map searchResponseJson = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - searchResponse.getEntity().getContent() - ).map(); - found.set(Integer.parseInt((((Map) ((Map) searchResponseJson.get("hits")).get("total")).get("value")).toString()) > 0 && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).containsKey("hello") && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).get("hello").toString().equals("hello")); - return found.get(); - } catch (IOException ex) { - return false; - } - }, 10, TimeUnit.SECONDS); - Assert.assertTrue(found.get()); - } - - @SuppressWarnings("unchecked") - public void testSampleRemoteDocLevelMonitorWithDynamicMetadataUpdate() throws IOException, InterruptedException { - createIndex("index1", Settings.builder().put("number_of_shards", "7").build()); - Response response = makeRequest(client(), "POST", "_plugins/_sample_remote_monitor/monitor", - Map.of("run_monitor", "doc_level", "index", "index1"), null); - Assert.assertEquals("Unable to create remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - Map responseJson = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - response.getEntity().getContent() - ).map(); - String monitorId = responseJson.get("_id").toString(); - - response = makeRequest(client(), "POST", "/_plugins/_alerting/monitors/" + monitorId + "/_execute", Map.of(), null); - Assert.assertEquals("Unable to execute remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - createIndex("index2", Settings.builder().put("number_of_shards", "7").build()); - String updatedMonitor = String.format(Locale.ROOT, "{\"type\":\"monitor\",\"name\":\"remote_doc_level_monitor\",\"monitor_type\":\"remote_doc_level_monitor\"," + - "\"user\":{\"name\":\"\",\"backend_roles\":[],\"roles\":[],\"custom_attribute_names\":[],\"user_requested_tenant\":null},\"enabled\":true,\"schedule\":{\"period\":{\"interval\":5,\"unit\":\"MINUTES\"}}," + - "\"inputs\":[{\"remote_doc_level_monitor_input\":{\"size\":24,\"input\":\"BWhlbGxvCgEFd29ybGQBAAAAAQAAAAIA\",\"doc_level_input\":" + - "{\"doc_level_input\":{\"description\":\"description\",\"indices\":[%s],\"queries\":[]}}}}],\"" + - "triggers\":[{\"remote_monitor_trigger\":{\"id\":\"id\",\"name\":\"name\",\"severity\":\"1\"," + - "\"actions\":[{\"id\":\"id\",\"name\":\"name\",\"destination_id\":\"destinationId\",\"message_template\":{\"source\":\"Hello World\"," + - "\"lang\":\"mustache\"},\"throttle_enabled\":false,\"subject_template\":{\"source\":\"Hello World\",\"lang\":\"mustache\"}," + - "\"throttle\":{\"value\":60,\"unit\":\"MINUTES\"}}],\"size\":24,\"trigger\":\"BWhlbGxvCgEEdGVzdAM/gAAAAAAAAQAA\"}}]" + - "}", "\"index1\", \"index2\""); - makeRequest(client(), "PUT", "/_plugins/_alerting/monitors/" + monitorId, Map.of(), new StringEntity(updatedMonitor, ContentType.APPLICATION_JSON)); - - response = makeRequest(client(), "POST", "/_plugins/_alerting/monitors/" + monitorId + "/_execute", Map.of(), null); - Assert.assertEquals("Unable to execute remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - makeRequest(client(), "DELETE", "/index1", Map.of(), null); - - response = makeRequest(client(), "POST", "/_plugins/_alerting/monitors/" + monitorId + "/_execute", Map.of(), null); - Assert.assertEquals("Unable to execute remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - AtomicBoolean found = new AtomicBoolean(false); - OpenSearchRestTestCase.waitUntil( - () -> { - try { - Response searchResponse = makeRequest(client(), "POST", SampleRemoteDocLevelMonitorRunner.SAMPLE_REMOTE_DOC_LEVEL_MONITOR_RUNNER_INDEX + "/_search", Map.of(), - new StringEntity("{\"query\":{\"match_all\":{}}}", ContentType.APPLICATION_JSON)); - Map searchResponseJson = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - searchResponse.getEntity().getContent() - ).map(); - found.set(Integer.parseInt((((Map) ((Map) searchResponseJson.get("hits")).get("total")).get("value")).toString()) > 0 && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).containsKey("hello") && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).get("hello").toString().equals("hello")); - return found.get(); - } catch (IOException ex) { - return false; - } - }, 10, TimeUnit.SECONDS); - Assert.assertTrue(found.get()); - } - - @SuppressWarnings("unchecked") - public void testSampleRemoteDocLevelMonitorWithAlias() throws IOException, InterruptedException { - String indexAlias = "test_alias"; - createIndex("index-000001", Settings.EMPTY); - makeRequest(client(), "POST", "_aliases", Map.of(), - new StringEntity(String.format(Locale.ROOT, "{\"actions\":[{\"add\":{\"index\":\"index-000001\",\"alias\":\"%s\",\"is_write_index\":true}}]}", indexAlias), ContentType.APPLICATION_JSON)); - Response response = makeRequest(client(), "POST", "_plugins/_sample_remote_monitor/monitor", Map.of("run_monitor", "doc_level", "index", indexAlias), null); - Assert.assertEquals("Unable to create remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - Map responseJson = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - response.getEntity().getContent() - ).map(); - String monitorId = responseJson.get("_id").toString(); - - response = makeRequest(client(), "POST", "/_plugins/_alerting/monitors/" + monitorId + "/_execute", Map.of(), null); - Assert.assertEquals("Unable to execute remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - makeRequest(client(), "POST", String.format(Locale.ROOT, "%s/_rollover", indexAlias), Map.of(), - new StringEntity("", ContentType.APPLICATION_JSON)); - - response = makeRequest(client(), "POST", "/_plugins/_alerting/monitors/" + monitorId + "/_execute", Map.of(), null); - Assert.assertEquals("Unable to execute remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - - response = makeRequest(client(), "POST", "/_plugins/_alerting/monitors/" + monitorId + "/_execute", Map.of(), null); - Assert.assertEquals("Unable to execute remote monitor", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); - AtomicBoolean found = new AtomicBoolean(false); - OpenSearchRestTestCase.waitUntil( - () -> { - try { - Response searchResponse = makeRequest(client(), "POST", SampleRemoteDocLevelMonitorRunner.SAMPLE_REMOTE_DOC_LEVEL_MONITOR_RUNNER_INDEX + "/_search", Map.of(), - new StringEntity("{\"query\":{\"match_all\":{}}}", ContentType.APPLICATION_JSON)); - Map searchResponseJson = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - searchResponse.getEntity().getContent() - ).map(); - found.set(Integer.parseInt((((Map) ((Map) searchResponseJson.get("hits")).get("total")).get("value")).toString()) > 0 && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).containsKey("hello") && - ((Map) ((List>) ((Map) searchResponseJson.get("hits")).get("hits")).get(0).get("_source")).get("hello").toString().equals("hello")); - return found.get(); - } catch (IOException ex) { - return false; - } - }, 10, TimeUnit.SECONDS); - Assert.assertTrue(found.get()); - } - - protected Response makeRequest( - RestClient client, - String method, - String endpoint, - Map params, - HttpEntity entity, - Header... headers - ) throws IOException { - Request request = new Request(method, endpoint); - RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder(); - options.setWarningsHandler(WarningsHandler.PERMISSIVE); - - for (Header header : headers) { - options.addHeader(header.getName(), header.getValue()); - } - request.setOptions(options.build()); - request.addParameters(params); - if (entity != null) { - request.setEntity(entity); - } - 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