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.
This commit is contained in:
Nik Everett 2018-07-11 10:04:17 -04:00 committed by GitHub
parent eda6d182b5
commit 38e09a1508
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 50 deletions

View File

@ -21,12 +21,6 @@ package org.elasticsearch.test.rest;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy; import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
import org.apache.http.ssl.SSLContexts; import org.apache.http.ssl.SSLContexts;
@ -68,16 +62,12 @@ import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static java.util.Collections.sort; import static java.util.Collections.sort;
import static java.util.Collections.unmodifiableList; import static java.util.Collections.unmodifiableList;
import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.anyOf;
@ -307,25 +297,25 @@ public abstract class ESRestTestCase extends ESTestCase {
* the snapshots intact in the repository. * the snapshots intact in the repository.
*/ */
private void wipeSnapshots() throws IOException { private void wipeSnapshots() throws IOException {
for (Map.Entry<String, ?> repo : entityAsMap(adminClient.performRequest("GET", "_snapshot/_all")).entrySet()) { for (Map.Entry<String, ?> repo : entityAsMap(adminClient.performRequest(new Request("GET", "/_snapshot/_all"))).entrySet()) {
String repoName = repo.getKey(); String repoName = repo.getKey();
Map<?, ?> repoSpec = (Map<?, ?>) repo.getValue(); Map<?, ?> repoSpec = (Map<?, ?>) repo.getValue();
String repoType = (String) repoSpec.get("type"); String repoType = (String) repoSpec.get("type");
if (false == preserveSnapshotsUponCompletion() && repoType.equals("fs")) { if (false == preserveSnapshotsUponCompletion() && repoType.equals("fs")) {
// All other repo types we really don't have a chance of being able to iterate properly, sadly. // All other repo types we really don't have a chance of being able to iterate properly, sadly.
String url = "_snapshot/" + repoName + "/_all"; Request listRequest = new Request("GET", "/_snapshot/" + repoName + "/_all");
Map<String, String> params = singletonMap("ignore_unavailable", "true"); listRequest.addParameter("ignore_unavailable", "true");
List<?> snapshots = (List<?>) entityAsMap(adminClient.performRequest("GET", url, params)).get("snapshots"); List<?> snapshots = (List<?>) entityAsMap(adminClient.performRequest(listRequest)).get("snapshots");
for (Object snapshot : snapshots) { for (Object snapshot : snapshots) {
Map<?, ?> snapshotInfo = (Map<?, ?>) snapshot; Map<?, ?> snapshotInfo = (Map<?, ?>) snapshot;
String name = (String) snapshotInfo.get("snapshot"); String name = (String) snapshotInfo.get("snapshot");
logger.debug("wiping snapshot [{}/{}]", repoName, name); logger.debug("wiping snapshot [{}/{}]", repoName, name);
adminClient().performRequest("DELETE", "_snapshot/" + repoName + "/" + name); adminClient().performRequest(new Request("DELETE", "/_snapshot/" + repoName + "/" + name));
} }
} }
if (preserveReposUponCompletion() == false) { if (preserveReposUponCompletion() == false) {
logger.debug("wiping snapshot repository [{}]", repoName); logger.debug("wiping snapshot repository [{}]", repoName);
adminClient().performRequest("DELETE", "_snapshot/" + repoName); adminClient().performRequest(new Request("DELETE", "_snapshot/" + repoName));
} }
} }
} }
@ -334,7 +324,7 @@ public abstract class ESRestTestCase extends ESTestCase {
* Remove any cluster settings. * Remove any cluster settings.
*/ */
private void wipeClusterSettings() throws IOException { private void wipeClusterSettings() throws IOException {
Map<?, ?> getResponse = entityAsMap(adminClient().performRequest("GET", "/_cluster/settings")); Map<?, ?> getResponse = entityAsMap(adminClient().performRequest(new Request("GET", "/_cluster/settings")));
boolean mustClear = false; boolean mustClear = false;
XContentBuilder clearCommand = JsonXContent.contentBuilder(); XContentBuilder clearCommand = JsonXContent.contentBuilder();
@ -355,8 +345,9 @@ public abstract class ESRestTestCase extends ESTestCase {
clearCommand.endObject(); clearCommand.endObject();
if (mustClear) { if (mustClear) {
adminClient().performRequest("PUT", "/_cluster/settings", emptyMap(), new StringEntity( Request request = new Request("PUT", "/_cluster/settings");
Strings.toString(clearCommand), ContentType.APPLICATION_JSON)); request.setJsonEntity(Strings.toString(clearCommand));
adminClient().performRequest(request);
} }
} }
@ -365,7 +356,7 @@ public abstract class ESRestTestCase extends ESTestCase {
* other tests. * other tests.
*/ */
private void logIfThereAreRunningTasks() throws InterruptedException, IOException { private void logIfThereAreRunningTasks() throws InterruptedException, IOException {
Set<String> runningTasks = runningTasks(adminClient().performRequest("GET", "_tasks")); Set<String> runningTasks = runningTasks(adminClient().performRequest(new Request("GET", "/_tasks")));
// Ignore the task list API - it doesn't count against us // Ignore the task list API - it doesn't count against us
runningTasks.remove(ListTasksAction.NAME); runningTasks.remove(ListTasksAction.NAME);
runningTasks.remove(ListTasksAction.NAME + "[n]"); runningTasks.remove(ListTasksAction.NAME + "[n]");
@ -389,7 +380,7 @@ public abstract class ESRestTestCase extends ESTestCase {
private void waitForClusterStateUpdatesToFinish() throws Exception { private void waitForClusterStateUpdatesToFinish() throws Exception {
assertBusy(() -> { assertBusy(() -> {
try { try {
Response response = adminClient().performRequest("GET", "_cluster/pending_tasks"); Response response = adminClient().performRequest(new Request("GET", "/_cluster/pending_tasks"));
List<?> tasks = (List<?>) entityAsMap(response).get("tasks"); List<?> tasks = (List<?>) entityAsMap(response).get("tasks");
if (false == tasks.isEmpty()) { if (false == tasks.isEmpty()) {
StringBuilder message = new StringBuilder("there are still running tasks:"); StringBuilder message = new StringBuilder("there are still running tasks:");
@ -514,12 +505,12 @@ public abstract class ESRestTestCase extends ESTestCase {
* @param index index to test for * @param index index to test for
**/ **/
protected static void ensureGreen(String index) throws IOException { protected static void ensureGreen(String index) throws IOException {
Map<String, String> params = new HashMap<>(); Request request = new Request("GET", "/_cluster/health/" + index);
params.put("wait_for_status", "green"); request.addParameter("wait_for_status", "green");
params.put("wait_for_no_relocating_shards", "true"); request.addParameter("wait_for_no_relocating_shards", "true");
params.put("timeout", "70s"); request.addParameter("timeout", "70s");
params.put("level", "shards"); request.addParameter("level", "shards");
assertOK(client().performRequest("GET", "_cluster/health/" + index, params)); client().performRequest(request);
} }
/** /**
@ -527,11 +518,11 @@ public abstract class ESRestTestCase extends ESTestCase {
* in the cluster and doesn't require to know how many nodes/replica there are. * in the cluster and doesn't require to know how many nodes/replica there are.
*/ */
protected static void ensureNoInitializingShards() throws IOException { protected static void ensureNoInitializingShards() throws IOException {
Map<String, String> params = new HashMap<>(); Request request = new Request("GET", "/_cluster/health");
params.put("wait_for_no_initializing_shards", "true"); request.addParameter("wait_for_no_initializing_shards", "true");
params.put("timeout", "70s"); request.addParameter("timeout", "70s");
params.put("level", "shards"); request.addParameter("level", "shards");
assertOK(client().performRequest("GET", "_cluster/health/", params)); client().performRequest(request);
} }
protected static void createIndex(String name, Settings settings) throws IOException { protected static void createIndex(String name, Settings settings) throws IOException {
@ -539,9 +530,10 @@ public abstract class ESRestTestCase extends ESTestCase {
} }
protected static void createIndex(String name, Settings settings, String mapping) throws IOException { protected static void createIndex(String name, Settings settings, String mapping) throws IOException {
assertOK(client().performRequest(HttpPut.METHOD_NAME, name, Collections.emptyMap(), Request request = new Request("PUT", "/" + name);
new StringEntity("{ \"settings\": " + Strings.toString(settings) request.setJsonEntity("{\n \"settings\": " + Strings.toString(settings)
+ ", \"mappings\" : {" + mapping + "} }", ContentType.APPLICATION_JSON))); + ", \"mappings\" : {" + mapping + "} }");
client().performRequest(request);
} }
protected static void updateIndexSettings(String index, Settings.Builder settings) throws IOException { protected static void updateIndexSettings(String index, Settings.Builder settings) throws IOException {
@ -549,42 +541,42 @@ public abstract class ESRestTestCase extends ESTestCase {
} }
private static void updateIndexSettings(String index, Settings settings) throws IOException { private static void updateIndexSettings(String index, Settings settings) throws IOException {
assertOK(client().performRequest("PUT", index + "/_settings", Collections.emptyMap(), Request request = new Request("PUT", "/" + index + "/_settings");
new StringEntity(Strings.toString(settings), ContentType.APPLICATION_JSON))); request.setJsonEntity(Strings.toString(settings));
client().performRequest(request);
} }
protected static Map<String, Object> getIndexSettings(String index) throws IOException { protected static Map<String, Object> getIndexSettings(String index) throws IOException {
Map<String, String> params = new HashMap<>(); Request request = new Request("GET", "/" + index + "/_settings");
params.put("flat_settings", "true"); request.addParameter("flat_settings", "true");
Response response = client().performRequest(HttpGet.METHOD_NAME, index + "/_settings", params); Response response = client().performRequest(request);
assertOK(response);
try (InputStream is = response.getEntity().getContent()) { try (InputStream is = response.getEntity().getContent()) {
return XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true); return XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true);
} }
} }
protected static boolean indexExists(String index) throws IOException { protected static boolean indexExists(String index) throws IOException {
Response response = client().performRequest(HttpHead.METHOD_NAME, index); Response response = client().performRequest(new Request("HEAD", "/" + index));
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode(); return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
} }
protected static void closeIndex(String index) throws IOException { protected static void closeIndex(String index) throws IOException {
Response response = client().performRequest(HttpPost.METHOD_NAME, index + "/_close"); Response response = client().performRequest(new Request("POST", "/" + index + "/_close"));
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
} }
protected static void openIndex(String index) throws IOException { protected static void openIndex(String index) throws IOException {
Response response = client().performRequest(HttpPost.METHOD_NAME, index + "/_open"); Response response = client().performRequest(new Request("POST", "/" + index + "/_open"));
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
} }
protected static boolean aliasExists(String alias) throws IOException { protected static boolean aliasExists(String alias) throws IOException {
Response response = client().performRequest(HttpHead.METHOD_NAME, "/_alias/" + alias); Response response = client().performRequest(new Request("HEAD", "/_alias/" + alias));
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode(); return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
} }
protected static boolean aliasExists(String index, String alias) throws IOException { protected static boolean aliasExists(String index, String alias) throws IOException {
Response response = client().performRequest(HttpHead.METHOD_NAME, "/" + index + "/_alias/" + alias); Response response = client().performRequest(new Request("HEAD", "/" + index + "/_alias/" + alias));
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode(); return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
} }
@ -602,7 +594,7 @@ public abstract class ESRestTestCase extends ESTestCase {
} }
protected static Map<String, Object> getAsMap(final String endpoint) throws IOException { protected static Map<String, Object> getAsMap(final String endpoint) throws IOException {
Response response = client().performRequest(HttpGet.METHOD_NAME, endpoint); Response response = client().performRequest(new Request("GET", endpoint));
XContentType entityContentType = XContentType.fromMediaTypeOrFormat(response.getEntity().getContentType().getValue()); XContentType entityContentType = XContentType.fromMediaTypeOrFormat(response.getEntity().getContentType().getValue());
Map<String, Object> responseEntity = XContentHelper.convertToMap(entityContentType.xContent(), Map<String, Object> responseEntity = XContentHelper.convertToMap(entityContentType.xContent(),
response.getEntity().getContent(), false); response.getEntity().getContent(), false);

View File

@ -47,7 +47,6 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -282,7 +281,9 @@ public abstract class ESClientYamlSuiteTestCase extends ESRestTestCase {
private static Tuple<Version, Version> readVersionsFromCatNodes(RestClient restClient) throws IOException { private static Tuple<Version, Version> readVersionsFromCatNodes(RestClient restClient) throws IOException {
// we simply go to the _cat/nodes API and parse all versions in the cluster // we simply go to the _cat/nodes API and parse all versions in the cluster
Response response = restClient.performRequest("GET", "/_cat/nodes", Collections.singletonMap("h", "version,master")); Request request = new Request("GET", "/_cat/nodes");
request.addParameter("h", "version,master");
Response response = restClient.performRequest(request);
ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse(response); ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse(response);
String nodesCatResponse = restTestResponse.getBodyAsString(); String nodesCatResponse = restTestResponse.getBodyAsString();
String[] split = nodesCatResponse.split("\n"); String[] split = nodesCatResponse.split("\n");
@ -310,7 +311,7 @@ public abstract class ESClientYamlSuiteTestCase extends ESRestTestCase {
Version version = null; Version version = null;
for (int i = 0; i < numHosts; i++) { for (int i = 0; i < numHosts; i++) {
//we don't really use the urls here, we rely on the client doing round-robin to touch all the nodes in the cluster //we don't really use the urls here, we rely on the client doing round-robin to touch all the nodes in the cluster
Response response = restClient.performRequest("GET", "/"); Response response = restClient.performRequest(new Request("GET", "/"));
ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse(response); ClientYamlTestResponse restTestResponse = new ClientYamlTestResponse(response);
Object latestVersion = restTestResponse.evaluate("version.number"); Object latestVersion = restTestResponse.evaluate("version.number");
if (latestVersion == null) { if (latestVersion == null) {