Skip to content

Commit

Permalink
[CCR] Add qa test library (elastic#34611)
Browse files Browse the repository at this point in the history
* Introduced test qa lib that all CCR qa modules depend on to avoid
test code duplication.
  • Loading branch information
martijnvg committed Oct 23, 2018
1 parent 9ab3bb7 commit 6393f27
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 496 deletions.
7 changes: 7 additions & 0 deletions x-pack/plugin/ccr/qa/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import org.elasticsearch.gradle.test.RestIntegTestTask

apply plugin: 'elasticsearch.build'
test.enabled = false

dependencies {
compile project(':test:framework')
}

subprojects {
project.tasks.withType(RestIntegTestTask) {
final File xPackResources = new File(xpackProject('plugin').projectDir, 'src/test/resources')
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugin/ccr/qa/chain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ apply plugin: 'elasticsearch.standalone-test'
dependencies {
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts')
testCompile project(path: xpackModule('ccr'), configuration: 'runtime')
testCompile project(':x-pack:plugin:ccr:qa')
}

task leaderClusterTest(type: RestIntegTestTask) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,10 @@

package org.elasticsearch.xpack.ccr;

import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.test.rest.ESRestTestCase;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.equalTo;

public class ChainIT extends ESRestTestCase {

private final String targetCluster = System.getProperty("tests.target_cluster");

@Override
protected boolean preserveClusterUponCompletion() {
return true;
}
public class ChainIT extends ESCCRRestTestCase {

public void testFollowIndex() throws Exception {
final int numDocs = 128;
Expand All @@ -60,23 +36,23 @@ public void testFollowIndex() throws Exception {
index(client(), leaderIndexName, Integer.toString(i), "field", i, "filtered_field", "true");
}
refresh(leaderIndexName);
verifyDocuments(leaderIndexName, numDocs);
verifyDocuments(leaderIndexName, numDocs, "filtered_field:true");
} else if ("middle".equals(targetCluster)) {
logger.info("Running against middle cluster");
followIndex("leader_cluster", leaderIndexName, middleIndexName);
assertBusy(() -> verifyDocuments(middleIndexName, numDocs));
assertBusy(() -> verifyDocuments(middleIndexName, numDocs, "filtered_field:true"));
try (RestClient leaderClient = buildLeaderClient()) {
int id = numDocs;
index(leaderClient, leaderIndexName, Integer.toString(id), "field", id, "filtered_field", "true");
index(leaderClient, leaderIndexName, Integer.toString(id + 1), "field", id + 1, "filtered_field", "true");
index(leaderClient, leaderIndexName, Integer.toString(id + 2), "field", id + 2, "filtered_field", "true");
}
assertBusy(() -> verifyDocuments(middleIndexName, numDocs + 3));
assertBusy(() -> verifyDocuments(middleIndexName, numDocs + 3, "filtered_field:true"));
} else if ("follow".equals(targetCluster)) {
logger.info("Running against follow cluster");
final String followIndexName = "follow";
followIndex("middle_cluster", middleIndexName, followIndexName);
assertBusy(() -> verifyDocuments(followIndexName, numDocs + 3));
assertBusy(() -> verifyDocuments(followIndexName, numDocs + 3, "filtered_field:true"));

try (RestClient leaderClient = buildLeaderClient()) {
int id = numDocs + 3;
Expand All @@ -86,82 +62,13 @@ public void testFollowIndex() throws Exception {
}

try (RestClient middleClient = buildMiddleClient()) {
assertBusy(() -> verifyDocuments(middleIndexName, numDocs + 6, middleClient));
assertBusy(() -> verifyDocuments(middleIndexName, numDocs + 6, "filtered_field:true", middleClient));
}

assertBusy(() -> verifyDocuments(followIndexName, numDocs + 6));
assertBusy(() -> verifyDocuments(followIndexName, numDocs + 6, "filtered_field:true"));
} else {
fail("unexpected target cluster [" + targetCluster + "]");
}
}

private static void index(RestClient client, String index, String id, Object... fields) throws IOException {
XContentBuilder document = jsonBuilder().startObject();
for (int i = 0; i < fields.length; i += 2) {
document.field((String) fields[i], fields[i + 1]);
}
document.endObject();
final Request request = new Request("POST", "/" + index + "/_doc/" + id);
request.setJsonEntity(Strings.toString(document));
assertOK(client.performRequest(request));
}

private static void refresh(String index) throws IOException {
assertOK(client().performRequest(new Request("POST", "/" + index + "/_refresh")));
}

private static void followIndex(String leaderCluster, String leaderIndex, String followIndex) throws IOException {
final Request request = new Request("PUT", "/" + followIndex + "/_ccr/follow");
request.setJsonEntity(
"{\"leader_cluster\": \"" + leaderCluster + "\", \"leader_index\": \"" + leaderIndex + "\", \"poll_timeout\": \"10ms\"}");
assertOK(client().performRequest(request));
}

private static void verifyDocuments(String index, int expectedNumDocs) throws IOException {
verifyDocuments(index, expectedNumDocs, client());
}

private static void verifyDocuments(final String index, final int expectedNumDocs, final RestClient client) throws IOException {
final Request request = new Request("GET", "/" + index + "/_search");
request.addParameter("size", Integer.toString(expectedNumDocs));
request.addParameter("sort", "field:asc");
request.addParameter("q", "filtered_field:true");
Map<String, ?> response = toMap(client.performRequest(request));

int numDocs = (int) XContentMapValues.extractValue("hits.total", response);
assertThat(numDocs, equalTo(expectedNumDocs));

List<?> hits = (List<?>) XContentMapValues.extractValue("hits.hits", response);
assertThat(hits.size(), equalTo(expectedNumDocs));
for (int i = 0; i < expectedNumDocs; i++) {
int value = (int) XContentMapValues.extractValue("_source.field", (Map<?, ?>) hits.get(i));
assertThat(i, equalTo(value));
}
}

private static Map<String, Object> toMap(Response response) throws IOException {
return toMap(EntityUtils.toString(response.getEntity()));
}

private static Map<String, Object> toMap(String response) {
return XContentHelper.convertToMap(JsonXContent.jsonXContent, response, false);
}

private RestClient buildLeaderClient() throws IOException {
assert "leader".equals(targetCluster) == false;
return buildClient(System.getProperty("tests.leader_host"));
}

private RestClient buildMiddleClient() throws IOException {
assert "middle".equals(targetCluster) == false;
return buildClient(System.getProperty("tests.middle_host"));
}

private RestClient buildClient(final String url) throws IOException {
int portSeparator = url.lastIndexOf(':');
HttpHost httpHost = new HttpHost(url.substring(0, portSeparator),
Integer.parseInt(url.substring(portSeparator + 1)), getProtocol());
return buildClient(Settings.EMPTY, new HttpHost[]{httpHost});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ apply plugin: 'elasticsearch.standalone-test'
dependencies {
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts')
testCompile project(path: xpackModule('ccr'), configuration: 'runtime')
testCompile project(':x-pack:plugin:ccr:qa:')
}

task leaderClusterTest(type: RestIntegTestTask) {
Expand All @@ -17,7 +18,7 @@ leaderClusterTestCluster {
}

leaderClusterTestRunner {
systemProperty 'tests.is_leader_cluster', 'true'
systemProperty 'tests.target_cluster', 'leader'
}

task writeJavaPolicy {
Expand Down Expand Up @@ -49,7 +50,7 @@ followClusterTestCluster {

followClusterTestRunner {
systemProperty 'java.security.policy', "file://${buildDir}/tmp/java.policy"
systemProperty 'tests.is_leader_cluster', 'false'
systemProperty 'tests.target_cluster', 'follow'
systemProperty 'tests.leader_host', "${-> leaderClusterTest.nodes.get(0).httpUri()}"
systemProperty 'log', "${-> followClusterTest.getNodes().get(0).homeDir}/logs/${-> followClusterTest.getNodes().get(0).clusterName}.log"
finalizedBy 'leaderClusterTestCluster#stop'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import org.apache.lucene.util.Constants;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.test.rest.ESRestTestCase;

import java.nio.file.Files;
import java.util.Iterator;
Expand All @@ -22,17 +20,10 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasToString;

public class CcrMultiClusterLicenseIT extends ESRestTestCase {

private final boolean runningAgainstLeaderCluster = Booleans.parseBoolean(System.getProperty("tests.is_leader_cluster"));

@Override
protected boolean preserveClusterUponCompletion() {
return true;
}
public class CcrMultiClusterLicenseIT extends ESCCRRestTestCase {

public void testFollow() {
if (runningAgainstLeaderCluster == false) {
if ("follow".equals(targetCluster)) {
final Request request = new Request("PUT", "/follower/_ccr/follow");
request.setJsonEntity("{\"leader_cluster\": \"leader_cluster\", \"leader_index\": \"leader\"}");
assertNonCompliantLicense(request);
Expand All @@ -41,7 +32,7 @@ public void testFollow() {

public void testAutoFollow() throws Exception {
assumeFalse("windows is the worst", Constants.WINDOWS);
if (runningAgainstLeaderCluster == false) {
if ("follow".equals(targetCluster)) {
final Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern");
request.setJsonEntity("{\"leader_index_patterns\":[\"*\"], \"leader_cluster\": \"leader_cluster\"}");
client().performRequest(request);
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugin/ccr/qa/multi-cluster-with-security/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ apply plugin: 'elasticsearch.standalone-test'
dependencies {
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts')
testCompile project(path: xpackModule('ccr'), configuration: 'runtime')
testCompile project(':x-pack:plugin:ccr:qa')
}

task leaderClusterTest(type: RestIntegTestTask) {
Expand Down Expand Up @@ -35,7 +36,7 @@ leaderClusterTestCluster {
}

leaderClusterTestRunner {
systemProperty 'tests.is_leader_cluster', 'true'
systemProperty 'tests.target_cluster', 'leader'
}

task followClusterTest(type: RestIntegTestTask) {}
Expand Down Expand Up @@ -66,7 +67,7 @@ followClusterTestCluster {
}

followClusterTestRunner {
systemProperty 'tests.is_leader_cluster', 'false'
systemProperty 'tests.target_cluster', 'follow'
systemProperty 'tests.leader_host', "${-> leaderClusterTest.nodes.get(0).httpUri()}"
finalizedBy 'leaderClusterTestCluster#stop'
}
Expand Down
Loading

0 comments on commit 6393f27

Please sign in to comment.