Skip to content

Commit 907a0fe

Browse files
committed
Switch test framework to new style requests (#31939)
In #29623 we added `Request` object flavored requests to the low level REST client and in #30315 we deprecated the old `performRequest`s. This changes all calls in the `test/framework` project to use the new versions.
1 parent e0c3b45 commit 907a0fe

File tree

2 files changed

+44
-50
lines changed

2 files changed

+44
-50
lines changed

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

+39-47
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121

2222
import org.apache.http.Header;
2323
import org.apache.http.HttpHost;
24-
import org.apache.http.client.methods.HttpGet;
25-
import org.apache.http.client.methods.HttpHead;
26-
import org.apache.http.client.methods.HttpPost;
27-
import org.apache.http.client.methods.HttpPut;
28-
import org.apache.http.entity.ContentType;
29-
import org.apache.http.entity.StringEntity;
3024
import org.apache.http.message.BasicHeader;
3125
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
3226
import org.apache.http.ssl.SSLContexts;
@@ -68,16 +62,12 @@
6862
import java.security.NoSuchAlgorithmException;
6963
import java.security.cert.CertificateException;
7064
import java.util.ArrayList;
71-
import java.util.Collections;
72-
import java.util.HashMap;
7365
import java.util.HashSet;
7466
import java.util.List;
7567
import java.util.Map;
7668
import java.util.Set;
7769
import java.util.concurrent.TimeUnit;
7870

79-
import static java.util.Collections.emptyMap;
80-
import static java.util.Collections.singletonMap;
8171
import static java.util.Collections.sort;
8272
import static java.util.Collections.unmodifiableList;
8373
import static org.hamcrest.Matchers.anyOf;
@@ -307,25 +297,25 @@ private void wipeCluster() throws IOException {
307297
* the snapshots intact in the repository.
308298
*/
309299
private void wipeSnapshots() throws IOException {
310-
for (Map.Entry<String, ?> repo : entityAsMap(adminClient.performRequest("GET", "_snapshot/_all")).entrySet()) {
300+
for (Map.Entry<String, ?> repo : entityAsMap(adminClient.performRequest(new Request("GET", "/_snapshot/_all"))).entrySet()) {
311301
String repoName = repo.getKey();
312302
Map<?, ?> repoSpec = (Map<?, ?>) repo.getValue();
313303
String repoType = (String) repoSpec.get("type");
314304
if (false == preserveSnapshotsUponCompletion() && repoType.equals("fs")) {
315305
// All other repo types we really don't have a chance of being able to iterate properly, sadly.
316-
String url = "_snapshot/" + repoName + "/_all";
317-
Map<String, String> params = singletonMap("ignore_unavailable", "true");
318-
List<?> snapshots = (List<?>) entityAsMap(adminClient.performRequest("GET", url, params)).get("snapshots");
306+
Request listRequest = new Request("GET", "/_snapshot/" + repoName + "/_all");
307+
listRequest.addParameter("ignore_unavailable", "true");
308+
List<?> snapshots = (List<?>) entityAsMap(adminClient.performRequest(listRequest)).get("snapshots");
319309
for (Object snapshot : snapshots) {
320310
Map<?, ?> snapshotInfo = (Map<?, ?>) snapshot;
321311
String name = (String) snapshotInfo.get("snapshot");
322312
logger.debug("wiping snapshot [{}/{}]", repoName, name);
323-
adminClient().performRequest("DELETE", "_snapshot/" + repoName + "/" + name);
313+
adminClient().performRequest(new Request("DELETE", "/_snapshot/" + repoName + "/" + name));
324314
}
325315
}
326316
if (preserveReposUponCompletion() == false) {
327317
logger.debug("wiping snapshot repository [{}]", repoName);
328-
adminClient().performRequest("DELETE", "_snapshot/" + repoName);
318+
adminClient().performRequest(new Request("DELETE", "_snapshot/" + repoName));
329319
}
330320
}
331321
}
@@ -334,7 +324,7 @@ private void wipeSnapshots() throws IOException {
334324
* Remove any cluster settings.
335325
*/
336326
private void wipeClusterSettings() throws IOException {
337-
Map<?, ?> getResponse = entityAsMap(adminClient().performRequest("GET", "/_cluster/settings"));
327+
Map<?, ?> getResponse = entityAsMap(adminClient().performRequest(new Request("GET", "/_cluster/settings")));
338328

339329
boolean mustClear = false;
340330
XContentBuilder clearCommand = JsonXContent.contentBuilder();
@@ -355,8 +345,9 @@ private void wipeClusterSettings() throws IOException {
355345
clearCommand.endObject();
356346

357347
if (mustClear) {
358-
adminClient().performRequest("PUT", "/_cluster/settings", emptyMap(), new StringEntity(
359-
Strings.toString(clearCommand), ContentType.APPLICATION_JSON));
348+
Request request = new Request("PUT", "/_cluster/settings");
349+
request.setJsonEntity(Strings.toString(clearCommand));
350+
adminClient().performRequest(request);
360351
}
361352
}
362353

@@ -365,7 +356,7 @@ private void wipeClusterSettings() throws IOException {
365356
* other tests.
366357
*/
367358
private void logIfThereAreRunningTasks() throws InterruptedException, IOException {
368-
Set<String> runningTasks = runningTasks(adminClient().performRequest("GET", "_tasks"));
359+
Set<String> runningTasks = runningTasks(adminClient().performRequest(new Request("GET", "/_tasks")));
369360
// Ignore the task list API - it doesn't count against us
370361
runningTasks.remove(ListTasksAction.NAME);
371362
runningTasks.remove(ListTasksAction.NAME + "[n]");
@@ -389,7 +380,7 @@ private void logIfThereAreRunningTasks() throws InterruptedException, IOExceptio
389380
private void waitForClusterStateUpdatesToFinish() throws Exception {
390381
assertBusy(() -> {
391382
try {
392-
Response response = adminClient().performRequest("GET", "_cluster/pending_tasks");
383+
Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
393384
List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
394385
if (false == tasks.isEmpty()) {
395386
StringBuilder message = new StringBuilder("there are still running tasks:");
@@ -514,77 +505,78 @@ protected static void assertOK(Response response) {
514505
* @param index index to test for
515506
**/
516507
protected static void ensureGreen(String index) throws IOException {
517-
Map<String, String> params = new HashMap<>();
518-
params.put("wait_for_status", "green");
519-
params.put("wait_for_no_relocating_shards", "true");
520-
params.put("timeout", "70s");
521-
params.put("level", "shards");
522-
assertOK(client().performRequest("GET", "_cluster/health/" + index, params));
508+
Request request = new Request("GET", "/_cluster/health/" + index);
509+
request.addParameter("wait_for_status", "green");
510+
request.addParameter("wait_for_no_relocating_shards", "true");
511+
request.addParameter("timeout", "70s");
512+
request.addParameter("level", "shards");
513+
client().performRequest(request);
523514
}
524515

525516
/**
526517
* waits until all shard initialization is completed. This is a handy alternative to ensureGreen as it relates to all shards
527518
* in the cluster and doesn't require to know how many nodes/replica there are.
528519
*/
529520
protected static void ensureNoInitializingShards() throws IOException {
530-
Map<String, String> params = new HashMap<>();
531-
params.put("wait_for_no_initializing_shards", "true");
532-
params.put("timeout", "70s");
533-
params.put("level", "shards");
534-
assertOK(client().performRequest("GET", "_cluster/health/", params));
521+
Request request = new Request("GET", "/_cluster/health");
522+
request.addParameter("wait_for_no_initializing_shards", "true");
523+
request.addParameter("timeout", "70s");
524+
request.addParameter("level", "shards");
525+
client().performRequest(request);
535526
}
536527

537528
protected static void createIndex(String name, Settings settings) throws IOException {
538529
createIndex(name, settings, "");
539530
}
540531

541532
protected static void createIndex(String name, Settings settings, String mapping) throws IOException {
542-
assertOK(client().performRequest(HttpPut.METHOD_NAME, name, Collections.emptyMap(),
543-
new StringEntity("{ \"settings\": " + Strings.toString(settings)
544-
+ ", \"mappings\" : {" + mapping + "} }", ContentType.APPLICATION_JSON)));
533+
Request request = new Request("PUT", "/" + name);
534+
request.setJsonEntity("{\n \"settings\": " + Strings.toString(settings)
535+
+ ", \"mappings\" : {" + mapping + "} }");
536+
client().performRequest(request);
545537
}
546538

547539
protected static void updateIndexSettings(String index, Settings.Builder settings) throws IOException {
548540
updateIndexSettings(index, settings.build());
549541
}
550542

551543
private static void updateIndexSettings(String index, Settings settings) throws IOException {
552-
assertOK(client().performRequest("PUT", index + "/_settings", Collections.emptyMap(),
553-
new StringEntity(Strings.toString(settings), ContentType.APPLICATION_JSON)));
544+
Request request = new Request("PUT", "/" + index + "/_settings");
545+
request.setJsonEntity(Strings.toString(settings));
546+
client().performRequest(request);
554547
}
555548

556549
protected static Map<String, Object> getIndexSettings(String index) throws IOException {
557-
Map<String, String> params = new HashMap<>();
558-
params.put("flat_settings", "true");
559-
Response response = client().performRequest(HttpGet.METHOD_NAME, index + "/_settings", params);
560-
assertOK(response);
550+
Request request = new Request("GET", "/" + index + "/_settings");
551+
request.addParameter("flat_settings", "true");
552+
Response response = client().performRequest(request);
561553
try (InputStream is = response.getEntity().getContent()) {
562554
return XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true);
563555
}
564556
}
565557

566558
protected static boolean indexExists(String index) throws IOException {
567-
Response response = client().performRequest(HttpHead.METHOD_NAME, index);
559+
Response response = client().performRequest(new Request("HEAD", "/" + index));
568560
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
569561
}
570562

571563
protected static void closeIndex(String index) throws IOException {
572-
Response response = client().performRequest(HttpPost.METHOD_NAME, index + "/_close");
564+
Response response = client().performRequest(new Request("POST", "/" + index + "/_close"));
573565
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
574566
}
575567

576568
protected static void openIndex(String index) throws IOException {
577-
Response response = client().performRequest(HttpPost.METHOD_NAME, index + "/_open");
569+
Response response = client().performRequest(new Request("POST", "/" + index + "/_open"));
578570
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
579571
}
580572

581573
protected static boolean aliasExists(String alias) throws IOException {
582-
Response response = client().performRequest(HttpHead.METHOD_NAME, "/_alias/" + alias);
574+
Response response = client().performRequest(new Request("HEAD", "/_alias/" + alias));
583575
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
584576
}
585577

586578
protected static boolean aliasExists(String index, String alias) throws IOException {
587-
Response response = client().performRequest(HttpHead.METHOD_NAME, "/" + index + "/_alias/" + alias);
579+
Response response = client().performRequest(new Request("HEAD", "/" + index + "/_alias/" + alias));
588580
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
589581
}
590582

@@ -602,7 +594,7 @@ protected static Map<String, Object> getAlias(final String index, final String a
602594
}
603595

604596
protected static Map<String, Object> getAsMap(final String endpoint) throws IOException {
605-
Response response = client().performRequest(HttpGet.METHOD_NAME, endpoint);
597+
Response response = client().performRequest(new Request("GET", endpoint));
606598
XContentType entityContentType = XContentType.fromMediaTypeOrFormat(response.getEntity().getContentType().getValue());
607599
Map<String, Object> responseEntity = XContentHelper.convertToMap(entityContentType.xContent(),
608600
response.getEntity().getContent(), false);

test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.http.HttpHost;
2424
import org.elasticsearch.Version;
2525
import org.elasticsearch.client.Node;
26+
import org.elasticsearch.client.Request;
2627
import org.elasticsearch.client.Response;
2728
import org.elasticsearch.client.ResponseException;
2829
import org.elasticsearch.client.RestClient;
@@ -46,7 +47,6 @@
4647
import java.nio.file.Files;
4748
import java.nio.file.Path;
4849
import java.util.ArrayList;
49-
import java.util.Collections;
5050
import java.util.Comparator;
5151
import java.util.HashMap;
5252
import java.util.HashSet;
@@ -287,7 +287,9 @@ private static void validateSpec(ClientYamlSuiteRestSpec restSpec) {
287287

288288
private static Tuple<Version, Version> readVersionsFromCatNodes(RestClient restClient) throws IOException {
289289
// we simply go to the _cat/nodes API and parse all versions in the cluster
290-
Response response = restClient.performRequest("GET", "/_cat/nodes", Collections.singletonMap("h", "version,master"));
290+
Request request = new Request("GET", "/_cat/nodes");
291+
request.addParameter("h", "version,master");
292+
Response response = restClient.performRequest(request);
291293
ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse(response);
292294
String nodesCatResponse = restTestResponse.getBodyAsString();
293295
String[] split = nodesCatResponse.split("\n");
@@ -315,7 +317,7 @@ private static Version readVersionsFromInfo(RestClient restClient, int numHosts)
315317
Version version = null;
316318
for (int i = 0; i < numHosts; i++) {
317319
//we don't really use the urls here, we rely on the client doing round-robin to touch all the nodes in the cluster
318-
Response response = restClient.performRequest("GET", "/");
320+
Response response = restClient.performRequest(new Request("GET", "/"));
319321
ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse(response);
320322
Object latestVersion = restTestResponse.evaluate("version.number");
321323
if (latestVersion == null) {

0 commit comments

Comments
 (0)