From 9c4f7e2124035ef4a382ba7d00ccc9e7e36f56d0 Mon Sep 17 00:00:00 2001 From: naveenpajjuri Date: Mon, 9 May 2022 12:50:08 +0530 Subject: [PATCH] added basic sanity tests for single cluster scenario. (#393) Signed-off-by: Naveen Pajjuri Co-authored-by: Naveen Pajjuri --- build.gradle | 27 +++++- .../singleCluster/SingleClusterSanityIT.kt | 89 +++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/org/opensearch/replication/singleCluster/SingleClusterSanityIT.kt diff --git a/build.gradle b/build.gradle index 41595157..0c30f286 100644 --- a/build.gradle +++ b/build.gradle @@ -401,6 +401,7 @@ integTest { // We skip BWC test here as those get run as part of separate target `bwcTestSuite`. filter { excludeTestsMatching "org.opensearch.replication.bwc.*IT" + excludeTestsMatching "org.opensearch.replication.singleCluster.SingleClusterSanityIT" } /* @@ -612,7 +613,7 @@ def securityPluginOld = new Callable() { } // We maintain 2 set of clusters here. One for full cluster restart and one for rolling restart + mixed cluster. -List clusters = ["bwcLeader0", "bwcFollower0", "bwcLeader1", "bwcFollower1"] +List clusters = ["bwcLeader0", "bwcFollower0", "bwcLeader1", "bwcFollower1","singleCluster"] // TODO: Make BWC test work with security plugin clusters.each { name -> testClusters { @@ -858,3 +859,27 @@ task "bwcTestSuite"(type: RestIntegTestTask) { dependsOn tasks.named("rollingUpgradeClusterTask") dependsOn tasks.named("fullRestartClusterTask") } + +task integTestSingleCluster(type: RestIntegTestTask) { + useCluster testClusters.singleCluster + doFirst { + getClusters().forEach { cluster -> + String alltransportSocketURI = cluster.nodes.stream().flatMap { node -> + node.getAllTransportPortURI().stream() + }.collect(Collectors.joining(",")) + String allHttpSocketURI = cluster.nodes.stream().flatMap { node -> + node.getAllHttpSocketURI().stream() + }.collect(Collectors.joining(",")) + + systemProperty "tests.cluster.${cluster.name}.http_hosts", "${-> allHttpSocketURI}" + systemProperty "tests.cluster.${cluster.name}.transport_hosts", "${-> alltransportSocketURI}" + systemProperty "tests.cluster.${cluster.name}.security_enabled", "${-> securityEnabled.toString()}" + configureCluster(cluster, securityEnabled) + } + } + + filter { + setIncludePatterns("org.opensearch.replication.singleCluster.SingleClusterSanityIT") + } + nonInputProperties.systemProperty('tests.sanitySingleCluster', "integTestSingleCluster") +} \ No newline at end of file diff --git a/src/test/kotlin/org/opensearch/replication/singleCluster/SingleClusterSanityIT.kt b/src/test/kotlin/org/opensearch/replication/singleCluster/SingleClusterSanityIT.kt new file mode 100644 index 00000000..87f021fa --- /dev/null +++ b/src/test/kotlin/org/opensearch/replication/singleCluster/SingleClusterSanityIT.kt @@ -0,0 +1,89 @@ +package org.opensearch.replication.singleCluster + + +import org.apache.logging.log4j.LogManager +import org.junit.BeforeClass +import org.opensearch.client.ResponseException +import org.opensearch.replication.MultiClusterRestTestCase +import org.opensearch.replication.StartReplicationRequest +import org.opensearch.replication.startReplication +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.Assert +import org.opensearch.replication.stopReplication +import java.util.stream.Collectors + + + + +class SingleClusterSanityIT : MultiClusterRestTestCase() { + + companion object { + private val log = LogManager.getLogger(SingleClusterSanityIT::class.java) + private const val standaloneClusterName = "singleCluster" + private const val REPLICATION_PLUGIN_NAME = "opensearch-cross-cluster-replication" + private const val NUM_NODES = 3 + private const val SAMPLE_INDEX = "sample_test_index" + + @BeforeClass + @JvmStatic + fun setupTestClusters() { + val clusters = HashMap() + clusters.put(standaloneClusterName, createTestCluster(standaloneClusterName, true, true, true, false)) + testClusters = clusters + } + + enum class ClusterState(val value: String) { + SINGLE_CLUSTER_SANITY_SUITE("integTestSingleCluster"); + + companion object { + fun from(s: String): ClusterState? = values().find { it.value == s } + } + } + } + + @Throws(Exception::class) + fun testReplicationPluginWithSingleCluster() { + when(ClusterState.from(System.getProperty("tests.sanitySingleCluster"))) { + ClusterState.SINGLE_CLUSTER_SANITY_SUITE -> basicReplicationSanityWithSingleCluster() + } + } + + fun basicReplicationSanityWithSingleCluster() { + verifyReplicationPluginInstallationOnAllNodes(standaloneClusterName) + VerifyReplicationApis(standaloneClusterName) + } + + @Throws(java.lang.Exception::class) + private fun verifyReplicationPluginInstallationOnAllNodes(clusterName: String) { + val restClient = getClientForCluster(clusterName) + for (i in 0 until NUM_NODES) { + val responseMap = getAsMap(restClient.lowLevelClient, "_nodes/$clusterName-$i/plugins")["nodes"] + as Map>? + Assert.assertTrue(responseMap!!.values.isNotEmpty()) + for (response in responseMap!!.values) { + val plugins = response["plugins"] as List>? + val pluginNames: Set = plugins!!.stream().map { map: Map -> + map["name"] + }.collect(Collectors.toSet()).orEmpty() + Assert.assertTrue(pluginNames.contains(REPLICATION_PLUGIN_NAME)) + } + } + } + + @Throws(java.lang.Exception::class) + private fun VerifyReplicationApis(clusterName: String) { + val follower = getClientForCluster(standaloneClusterName) + assertThatThrownBy { + follower.startReplication( + StartReplicationRequest("sample_connection", SAMPLE_INDEX, SAMPLE_INDEX), + waitForRestore = true + ) + }.isInstanceOf(ResponseException::class.java).hasMessageContaining("no such remote cluster") + assertThatThrownBy { + follower.stopReplication(standaloneClusterName) + }.isInstanceOf(ResponseException::class.java) + .hasMessageContaining("No replication in progress for index:"+standaloneClusterName) + } + +} +