Skip to content

Commit

Permalink
clean up bwc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelandis committed Nov 4, 2024
1 parent 892ac08 commit b4b85c8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package org.elasticsearch.xpack.remotecluster;

import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
Expand All @@ -16,6 +17,8 @@
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchResponseUtils;
import org.elasticsearch.test.rest.ObjectPath;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.junit.Before;

import java.io.IOException;
Expand All @@ -26,6 +29,7 @@
import java.util.stream.Collectors;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
Expand All @@ -50,6 +54,7 @@ public void setUp() throws Exception {
ensureRemoteFulfillingClusterIsConnected(useProxyMode);
super.setUp();
}

public void testBwcCCSViaRCS1orRCS2() throws Exception {

// Fulfilling cluster
Expand Down Expand Up @@ -167,38 +172,35 @@ public void testBwcCCSViaRCS1orRCS2() throws Exception {

// Check that we can search the fulfilling cluster from the querying cluster
final boolean alsoSearchLocally = randomBoolean();
final String remoteClusterName = randomFrom("my_remote_cluster", "*", "my_remote_*");
final String remoteIndexName = randomFrom("remote_index1", "*");
final var searchRequest = new Request(
"GET",
String.format(
Locale.ROOT,
"/%s%s:%s/_search?ccs_minimize_roundtrips=%s",
alsoSearchLocally ? "local_index," : "",
randomFrom("my_remote_cluster", "*", "my_remote_*"),
randomFrom("remote_index1", "*"),
remoteClusterName,
remoteIndexName,
randomBoolean()
)
);
final String sendRequestWith = randomFrom("user", "apikey");
final Response response = sendRequestWith.equals("user")
? performRequestWithRemoteAccessUser(searchRequest)
: performRequestWithApiKey(searchRequest, apiKeyEncoded);
String esqlCommand = String.format(Locale.ROOT, "FROM %s,%s:%s | LIMIT 10", "local_index", remoteClusterName, remoteIndexName);
// send request with user
Response response = performRequestWithRemoteAccessUser(searchRequest);
assertOK(response);
final SearchResponse searchResponse;
try (var parser = responseAsParser(response)) {
searchResponse = SearchResponseUtils.parseSearchResponse(parser);
assertSearchResponse(SearchResponseUtils.parseSearchResponse(parser), alsoSearchLocally);
}
try {
final List<String> actualIndices = Arrays.stream(searchResponse.getHits().getHits())
.map(SearchHit::getIndex)
.collect(Collectors.toList());
if (alsoSearchLocally) {
assertThat(actualIndices, containsInAnyOrder("remote_index1", "local_index"));
} else {
assertThat(actualIndices, containsInAnyOrder("remote_index1"));
}
} finally {
searchResponse.decRef();
assertEsqlResponse(performRequestWithRemoteAccessUser(esqlRequest(esqlCommand)));

// send request with apikey
response = performRequestWithApiKey(searchRequest, apiKeyEncoded);
assertOK(response);
try (var parser = responseAsParser(response)) {
assertSearchResponse(SearchResponseUtils.parseSearchResponse(parser), alsoSearchLocally);
}
assertEsqlResponse(performRequestWithApiKey(esqlRequest(esqlCommand), apiKeyEncoded));
}
}

Expand Down Expand Up @@ -250,4 +252,37 @@ private void setupQueryClusterRCS1(boolean useProxyMode) throws IOException {
updateClusterSettings(builder.build());
}

private Request esqlRequest(String command) throws IOException {
XContentBuilder body = JsonXContent.contentBuilder();
body.startObject();
body.field("query", command);
body.field("include_ccs_metadata", true);
body.endObject();
Request request = new Request("POST", "_query");
request.setJsonEntity(org.elasticsearch.common.Strings.toString(body));
return request;
}

private void assertSearchResponse(SearchResponse searchResponse, boolean alsoSearchLocally) {
try {
final List<String> actualIndices = Arrays.stream(searchResponse.getHits().getHits())
.map(SearchHit::getIndex)
.collect(Collectors.toList());
if (alsoSearchLocally) {
assertThat(actualIndices, containsInAnyOrder("remote_index1", "local_index"));
} else {
assertThat(actualIndices, containsInAnyOrder("remote_index1"));
}
} finally {
searchResponse.decRef();
}
}

private void assertEsqlResponse(Response response) throws IOException {
assertOK(response);
String responseAsString = EntityUtils.toString(response.getEntity());
assertThat(responseAsString, containsString("\"my_remote_cluster\":{\"status\":\"successful\""));
assertThat(responseAsString, containsString("local_bar"));
assertThat(responseAsString, containsString("bar"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public class RemoteClusterSecurityBWCToRCS1ClusterRestIT extends AbstractRemoteC

queryCluster = ElasticsearchCluster.local()
.version(Version.CURRENT)
.distribution(DistributionType.INTEG_TEST)
.distribution(DistributionType.DEFAULT)
.setting("xpack.ml.enabled", "false")
.name("query-cluster")
.apply(commonClusterConfig)
.setting("xpack.security.remote_cluster_client.ssl.enabled", "true")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class RemoteClusterSecurityBWCToRCS2ClusterRestIT extends AbstractRemoteC

queryCluster = ElasticsearchCluster.local()
.name("query-cluster")
.distribution(DistributionType.DEFAULT)
.setting("xpack.ml.enabled", "false")
.apply(commonClusterConfig)
.setting("xpack.security.remote_cluster_client.ssl.enabled", "true")
.setting("xpack.security.remote_cluster_client.ssl.certificate_authorities", "remote-cluster-ca.crt")
Expand Down

0 comments on commit b4b85c8

Please sign in to comment.