diff --git a/build.gradle b/build.gradle index 0e34228..bff9ba7 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,5 @@ +import org.opensearch.gradle.test.RestIntegTestTask + apply plugin: 'java' apply plugin: 'idea' apply plugin: 'opensearch.opensearchplugin' @@ -12,12 +14,21 @@ opensearchplugin { } -// disabling some unnecessary validations for this plugin +// Allow test cases to be named Tests without having to be inherited from LuceneTestCase. +// see https://github.com/elastic/elasticsearch/blob/323f312bbc829a63056a79ebe45adced5099f6e6/buildSrc/src/main/java/org/elasticsearch/gradle/precommit/TestingConventionsTasks.java testingConventions.enabled = false + +licenseHeaders.enabled = true +dependencyLicenses.enabled = false +thirdPartyAudit.enabled = false loggerUsageCheck.enabled = false -validateNebulaPom.enabled = false buildscript { + ext { + opensearch_group = "org.opensearch" + opensearch_version = System.getProperty("opensearch.version", "1.0.0-beta1") + } + repositories { mavenCentral() jcenter() @@ -25,7 +36,7 @@ buildscript { } dependencies { - classpath "org.opensearch.gradle:build-tools:1.0.0-beta1" + classpath "${opensearch_group}.gradle:build-tools:${opensearch_version}" } } @@ -37,3 +48,67 @@ dependencies { // required for the yaml test to run yamlRestTestImplementation 'org.apache.logging.log4j:log4j-core:2.11.1' } + +test { + include '**/*Tests.class' + systemProperty 'tests.security.manager', 'false' +} + +// See package README.md for details on using these tasks. +def _numNodes = findProperty('numNodes') as Integer ?: 1 + +task integTest(type: RestIntegTestTask) { + description = "Run tests against a cluster" + testClassesDirs = sourceSets.test.output.classesDirs + classpath = sourceSets.test.runtimeClasspath +} +tasks.named("check").configure { dependsOn(integTest) } + +integTest { + // The 'doFirst' delays 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 --debug-jvm command-line option makes the cluster debuggable; this makes the tests debuggable + if (System.getProperty("test.debug") != null) { + jvmArgs '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005' + } +} + +testClusters.integTest { + testDistribution = "INTEG_TEST" + // 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 elasticsearch JVM + // since we also support multi node integration tests we increase debugPort per node + if (System.getProperty("opensearch.debug") != null) { + def debugPort = 5005 + nodes.forEach { node -> + node.jvmArgs("-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=*:${debugPort}") + debugPort += 1 + } + } + plugin(project.tasks.bundlePlugin.archiveFile) +} + +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 +} \ No newline at end of file diff --git a/src/test/java/org/opensearch/rest/action/HelloWorldPluginIT.java b/src/test/java/org/opensearch/rest/action/HelloWorldPluginIT.java new file mode 100644 index 0000000..59a8127 --- /dev/null +++ b/src/test/java/org/opensearch/rest/action/HelloWorldPluginIT.java @@ -0,0 +1,67 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + + +package org.opensearch.rest.action; + +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; +import org.apache.http.entity.ContentType; +import org.apache.http.nio.entity.NStringEntity; +import org.apache.http.util.EntityUtils; +import org.opensearch.client.Request; +import org.opensearch.client.Response; +import org.opensearch.plugins.Plugin; +import org.opensearch.test.OpenSearchIntegTestCase; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; + +@ThreadLeakScope(ThreadLeakScope.Scope.NONE) +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.SUITE) +public class HelloWorldPluginIT extends OpenSearchIntegTestCase { + + @Override + protected Collection> nodePlugins() { + return Collections.singletonList(HelloWorldPlugin.class); + } + + public void testPluginInstalled() throws IOException { + Response response = createRestClient().performRequest(new Request("GET", "/_cat/plugins")); + String body = EntityUtils.toString(response.getEntity()); + + logger.info("response body: {}", body); + assertThat(body, containsString("my-rest-plugin")); + } + + public void testPluginIsWorkingNoValue() throws IOException { + Response response = createRestClient().performRequest(new Request("GET", "/hello-world")); + String body = EntityUtils.toString(response.getEntity()); + + logger.info("response body: {}", body); + assertThat(body, equalTo("Hi! Your plugin is installed and working:)")); + } + + public void testPluginIsWorkingWithValue() throws IOException { + Request request = new Request("POST", "/hello-world"); + request.setEntity( + new NStringEntity( + "{\"name\":\"Amitai\"}", + ContentType.APPLICATION_JSON + ) + ); + Response response = createRestClient().performRequest(request); + String body = EntityUtils.toString(response.getEntity()); + + logger.info("response body: {}", body); + assertThat(body, equalTo("Hi Amitai! Your plugin is installed and working:)")); + } +} \ No newline at end of file