Skip to content

Commit ecf8657

Browse files
martijnvgkcm
authored andcommitted
[CCR] Add qa test library (#34611)
* Introduced test qa lib that all CCR qa modules depend on to avoid test code duplication.
1 parent 4a0b61c commit ecf8657

File tree

10 files changed

+294
-496
lines changed

10 files changed

+294
-496
lines changed

x-pack/plugin/ccr/qa/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import org.elasticsearch.gradle.test.RestIntegTestTask
22

3+
apply plugin: 'elasticsearch.build'
4+
test.enabled = false
5+
6+
dependencies {
7+
compile project(':test:framework')
8+
}
9+
310
subprojects {
411
project.tasks.withType(RestIntegTestTask) {
512
final File xPackResources = new File(xpackProject('plugin').projectDir, 'src/test/resources')

x-pack/plugin/ccr/qa/chain/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ apply plugin: 'elasticsearch.standalone-test'
55
dependencies {
66
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts')
77
testCompile project(path: xpackModule('ccr'), configuration: 'runtime')
8+
testCompile project(':x-pack:plugin:ccr:qa')
89
}
910

1011
task leaderClusterTest(type: RestIntegTestTask) {

x-pack/plugin/ccr/qa/chain/src/test/java/org/elasticsearch/xpack/ccr/ChainIT.java

Lines changed: 7 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,10 @@
66

77
package org.elasticsearch.xpack.ccr;
88

9-
import org.apache.http.HttpHost;
10-
import org.apache.http.util.EntityUtils;
11-
import org.elasticsearch.client.Request;
12-
import org.elasticsearch.client.Response;
139
import org.elasticsearch.client.RestClient;
14-
import org.elasticsearch.common.Strings;
1510
import org.elasticsearch.common.settings.Settings;
16-
import org.elasticsearch.common.xcontent.XContentBuilder;
17-
import org.elasticsearch.common.xcontent.XContentHelper;
18-
import org.elasticsearch.common.xcontent.json.JsonXContent;
19-
import org.elasticsearch.common.xcontent.support.XContentMapValues;
20-
import org.elasticsearch.test.rest.ESRestTestCase;
2111

22-
import java.io.IOException;
23-
import java.util.List;
24-
import java.util.Map;
25-
26-
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
27-
import static org.hamcrest.Matchers.equalTo;
28-
29-
public class ChainIT extends ESRestTestCase {
30-
31-
private final String targetCluster = System.getProperty("tests.target_cluster");
32-
33-
@Override
34-
protected boolean preserveClusterUponCompletion() {
35-
return true;
36-
}
12+
public class ChainIT extends ESCCRRestTestCase {
3713

3814
public void testFollowIndex() throws Exception {
3915
final int numDocs = 128;
@@ -60,23 +36,23 @@ public void testFollowIndex() throws Exception {
6036
index(client(), leaderIndexName, Integer.toString(i), "field", i, "filtered_field", "true");
6137
}
6238
refresh(leaderIndexName);
63-
verifyDocuments(leaderIndexName, numDocs);
39+
verifyDocuments(leaderIndexName, numDocs, "filtered_field:true");
6440
} else if ("middle".equals(targetCluster)) {
6541
logger.info("Running against middle cluster");
6642
followIndex("leader_cluster", leaderIndexName, middleIndexName);
67-
assertBusy(() -> verifyDocuments(middleIndexName, numDocs));
43+
assertBusy(() -> verifyDocuments(middleIndexName, numDocs, "filtered_field:true"));
6844
try (RestClient leaderClient = buildLeaderClient()) {
6945
int id = numDocs;
7046
index(leaderClient, leaderIndexName, Integer.toString(id), "field", id, "filtered_field", "true");
7147
index(leaderClient, leaderIndexName, Integer.toString(id + 1), "field", id + 1, "filtered_field", "true");
7248
index(leaderClient, leaderIndexName, Integer.toString(id + 2), "field", id + 2, "filtered_field", "true");
7349
}
74-
assertBusy(() -> verifyDocuments(middleIndexName, numDocs + 3));
50+
assertBusy(() -> verifyDocuments(middleIndexName, numDocs + 3, "filtered_field:true"));
7551
} else if ("follow".equals(targetCluster)) {
7652
logger.info("Running against follow cluster");
7753
final String followIndexName = "follow";
7854
followIndex("middle_cluster", middleIndexName, followIndexName);
79-
assertBusy(() -> verifyDocuments(followIndexName, numDocs + 3));
55+
assertBusy(() -> verifyDocuments(followIndexName, numDocs + 3, "filtered_field:true"));
8056

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

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

92-
assertBusy(() -> verifyDocuments(followIndexName, numDocs + 6));
68+
assertBusy(() -> verifyDocuments(followIndexName, numDocs + 6, "filtered_field:true"));
9369
} else {
9470
fail("unexpected target cluster [" + targetCluster + "]");
9571
}
9672
}
9773

98-
private static void index(RestClient client, String index, String id, Object... fields) throws IOException {
99-
XContentBuilder document = jsonBuilder().startObject();
100-
for (int i = 0; i < fields.length; i += 2) {
101-
document.field((String) fields[i], fields[i + 1]);
102-
}
103-
document.endObject();
104-
final Request request = new Request("POST", "/" + index + "/_doc/" + id);
105-
request.setJsonEntity(Strings.toString(document));
106-
assertOK(client.performRequest(request));
107-
}
108-
109-
private static void refresh(String index) throws IOException {
110-
assertOK(client().performRequest(new Request("POST", "/" + index + "/_refresh")));
111-
}
112-
113-
private static void followIndex(String leaderCluster, String leaderIndex, String followIndex) throws IOException {
114-
final Request request = new Request("PUT", "/" + followIndex + "/_ccr/follow");
115-
request.setJsonEntity(
116-
"{\"leader_cluster\": \"" + leaderCluster + "\", \"leader_index\": \"" + leaderIndex + "\", \"poll_timeout\": \"10ms\"}");
117-
assertOK(client().performRequest(request));
118-
}
119-
120-
private static void verifyDocuments(String index, int expectedNumDocs) throws IOException {
121-
verifyDocuments(index, expectedNumDocs, client());
122-
}
123-
124-
private static void verifyDocuments(final String index, final int expectedNumDocs, final RestClient client) throws IOException {
125-
final Request request = new Request("GET", "/" + index + "/_search");
126-
request.addParameter("size", Integer.toString(expectedNumDocs));
127-
request.addParameter("sort", "field:asc");
128-
request.addParameter("q", "filtered_field:true");
129-
Map<String, ?> response = toMap(client.performRequest(request));
130-
131-
int numDocs = (int) XContentMapValues.extractValue("hits.total", response);
132-
assertThat(numDocs, equalTo(expectedNumDocs));
133-
134-
List<?> hits = (List<?>) XContentMapValues.extractValue("hits.hits", response);
135-
assertThat(hits.size(), equalTo(expectedNumDocs));
136-
for (int i = 0; i < expectedNumDocs; i++) {
137-
int value = (int) XContentMapValues.extractValue("_source.field", (Map<?, ?>) hits.get(i));
138-
assertThat(i, equalTo(value));
139-
}
140-
}
141-
142-
private static Map<String, Object> toMap(Response response) throws IOException {
143-
return toMap(EntityUtils.toString(response.getEntity()));
144-
}
145-
146-
private static Map<String, Object> toMap(String response) {
147-
return XContentHelper.convertToMap(JsonXContent.jsonXContent, response, false);
148-
}
149-
150-
private RestClient buildLeaderClient() throws IOException {
151-
assert "leader".equals(targetCluster) == false;
152-
return buildClient(System.getProperty("tests.leader_host"));
153-
}
154-
155-
private RestClient buildMiddleClient() throws IOException {
156-
assert "middle".equals(targetCluster) == false;
157-
return buildClient(System.getProperty("tests.middle_host"));
158-
}
159-
160-
private RestClient buildClient(final String url) throws IOException {
161-
int portSeparator = url.lastIndexOf(':');
162-
HttpHost httpHost = new HttpHost(url.substring(0, portSeparator),
163-
Integer.parseInt(url.substring(portSeparator + 1)), getProtocol());
164-
return buildClient(Settings.EMPTY, new HttpHost[]{httpHost});
165-
}
166-
16774
}

x-pack/plugin/ccr/qa/multi-cluster-with-non-compliant-license/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ apply plugin: 'elasticsearch.standalone-test'
55
dependencies {
66
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts')
77
testCompile project(path: xpackModule('ccr'), configuration: 'runtime')
8+
testCompile project(':x-pack:plugin:ccr:qa:')
89
}
910

1011
task leaderClusterTest(type: RestIntegTestTask) {
@@ -17,7 +18,7 @@ leaderClusterTestCluster {
1718
}
1819

1920
leaderClusterTestRunner {
20-
systemProperty 'tests.is_leader_cluster', 'true'
21+
systemProperty 'tests.target_cluster', 'leader'
2122
}
2223

2324
task writeJavaPolicy {
@@ -49,7 +50,7 @@ followClusterTestCluster {
4950

5051
followClusterTestRunner {
5152
systemProperty 'java.security.policy', "file://${buildDir}/tmp/java.policy"
52-
systemProperty 'tests.is_leader_cluster', 'false'
53+
systemProperty 'tests.target_cluster', 'follow'
5354
systemProperty 'tests.leader_host', "${-> leaderClusterTest.nodes.get(0).httpUri()}"
5455
systemProperty 'log', "${-> followClusterTest.getNodes().get(0).homeDir}/logs/${-> followClusterTest.getNodes().get(0).clusterName}.log"
5556
finalizedBy 'leaderClusterTestCluster#stop'

x-pack/plugin/ccr/qa/multi-cluster-with-non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import org.apache.lucene.util.Constants;
1010
import org.elasticsearch.client.Request;
1111
import org.elasticsearch.client.ResponseException;
12-
import org.elasticsearch.common.Booleans;
1312
import org.elasticsearch.common.io.PathUtils;
14-
import org.elasticsearch.test.rest.ESRestTestCase;
1513

1614
import java.nio.file.Files;
1715
import java.util.Iterator;
@@ -22,17 +20,10 @@
2220
import static org.hamcrest.Matchers.equalTo;
2321
import static org.hamcrest.Matchers.hasToString;
2422

25-
public class CcrMultiClusterLicenseIT extends ESRestTestCase {
26-
27-
private final boolean runningAgainstLeaderCluster = Booleans.parseBoolean(System.getProperty("tests.is_leader_cluster"));
28-
29-
@Override
30-
protected boolean preserveClusterUponCompletion() {
31-
return true;
32-
}
23+
public class CcrMultiClusterLicenseIT extends ESCCRRestTestCase {
3324

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

4233
public void testAutoFollow() throws Exception {
4334
assumeFalse("windows is the worst", Constants.WINDOWS);
44-
if (runningAgainstLeaderCluster == false) {
35+
if ("follow".equals(targetCluster)) {
4536
final Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern");
4637
request.setJsonEntity("{\"leader_index_patterns\":[\"*\"], \"leader_cluster\": \"leader_cluster\"}");
4738
client().performRequest(request);

x-pack/plugin/ccr/qa/multi-cluster-with-security/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ apply plugin: 'elasticsearch.standalone-test'
55
dependencies {
66
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts')
77
testCompile project(path: xpackModule('ccr'), configuration: 'runtime')
8+
testCompile project(':x-pack:plugin:ccr:qa')
89
}
910

1011
task leaderClusterTest(type: RestIntegTestTask) {
@@ -35,7 +36,7 @@ leaderClusterTestCluster {
3536
}
3637

3738
leaderClusterTestRunner {
38-
systemProperty 'tests.is_leader_cluster', 'true'
39+
systemProperty 'tests.target_cluster', 'leader'
3940
}
4041

4142
task followClusterTest(type: RestIntegTestTask) {}
@@ -66,7 +67,7 @@ followClusterTestCluster {
6667
}
6768

6869
followClusterTestRunner {
69-
systemProperty 'tests.is_leader_cluster', 'false'
70+
systemProperty 'tests.target_cluster', 'follow'
7071
systemProperty 'tests.leader_host', "${-> leaderClusterTest.nodes.get(0).httpUri()}"
7172
finalizedBy 'leaderClusterTestCluster#stop'
7273
}

0 commit comments

Comments
 (0)