Skip to content

Commit

Permalink
Switch some x-pack tests to new style Requests (#32500)
Browse files Browse the repository at this point in the history
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 `x-pack/qa/audit-tests`,
`x-pack/qa/ml-disabled`, and `x-pack/qa/multi-node` projects to use the
new versions.
  • Loading branch information
nik9000 authored Aug 21, 2018
1 parent 28a0df2 commit fcf8cad
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
*/
package org.elasticsearch.xpack.ml.integration;

import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.xpack.ml.MachineLearning;

import java.util.Collections;

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

Expand All @@ -27,30 +24,40 @@ public class MlPluginDisabledIT extends ESRestTestCase {
public void testActionsFail() throws Exception {
XContentBuilder xContentBuilder = jsonBuilder();
xContentBuilder.startObject();
xContentBuilder.field("actions-fail-job", "foo");
xContentBuilder.field("description", "Analysis of response time by airline");

xContentBuilder.startObject("analysis_config");
xContentBuilder.field("bucket_span", "3600s");
xContentBuilder.startArray("detectors");
xContentBuilder.startObject();
xContentBuilder.field("function", "metric");
xContentBuilder.field("field_name", "responsetime");
xContentBuilder.field("by_field_name", "airline");
xContentBuilder.endObject();
xContentBuilder.endArray();
xContentBuilder.endObject();

xContentBuilder.startObject("data_description");
xContentBuilder.field("format", "xcontent");
xContentBuilder.field("time_field", "time");
xContentBuilder.field("time_format", "epoch");
xContentBuilder.endObject();
{
xContentBuilder.field("actions-fail-job", "foo");
xContentBuilder.field("description", "Analysis of response time by airline");

xContentBuilder.startObject("analysis_config");
{
xContentBuilder.field("bucket_span", "3600s");
xContentBuilder.startArray("detectors");
{
xContentBuilder.startObject();
{
xContentBuilder.field("function", "metric");
xContentBuilder.field("field_name", "responsetime");
xContentBuilder.field("by_field_name", "airline");
}
xContentBuilder.endObject();
}
xContentBuilder.endArray();
}
xContentBuilder.endObject();

xContentBuilder.startObject("data_description");
{
xContentBuilder.field("format", "xcontent");
xContentBuilder.field("time_field", "time");
xContentBuilder.field("time_format", "epoch");
}
xContentBuilder.endObject();
}
xContentBuilder.endObject();

ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest("put",
MachineLearning.BASE_PATH + "anomaly_detectors/foo", Collections.emptyMap(),
new StringEntity(Strings.toString(xContentBuilder), ContentType.APPLICATION_JSON)));
Request request = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/foo");
request.setJsonEntity(Strings.toString(xContentBuilder));
ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest(request));
assertThat(exception.getMessage(), containsString("no handler found for uri [/_xpack/ml/anomaly_detectors/foo] and method [PUT]"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.elasticsearch.xpack.security.audit;

import com.carrotsearch.hppc.cursors.ObjectCursor;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
Expand Down Expand Up @@ -111,10 +110,12 @@ public NamedWriteableRegistry getNamedWriteableRegistry() {
}

public void testIndexAuditTrailWorking() throws Exception {
Response response = getRestClient().performRequest("GET", "/",
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
UsernamePasswordToken.basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray()))));
assertThat(response.getStatusLine().getStatusCode(), is(200));
Request request = new Request("GET", "/");
RequestOptions.Builder options = request.getOptions().toBuilder();
options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
UsernamePasswordToken.basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())));
request.setOptions(options);
Response response = getRestClient().performRequest(request);
final AtomicReference<ClusterState> lastClusterState = new AtomicReference<>();
final boolean found = awaitSecurityAuditIndex(lastClusterState, QueryBuilders.matchQuery("principal", USER));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/
package org.elasticsearch.multi_node;

import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.SecureString;
Expand All @@ -16,10 +15,6 @@
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.yaml.ObjectPath;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -59,12 +54,15 @@ public void testGlobalCheckpointSyncActionRunsAsPrivilegedUser() throws Exceptio
builder.endObject();
}
builder.endObject();
final StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
client().performRequest("PUT", "test-index", Collections.emptyMap(), entity);
Request createIndexRequest = new Request("PUT", "/test-index");
createIndexRequest.setJsonEntity(Strings.toString(builder));
client().performRequest(createIndexRequest);
}

// wait for the replica to recover
client().performRequest("GET", "/_cluster/health", Collections.singletonMap("wait_for_status", "green"));
Request healthRequest = new Request("GET", "/_cluster/health");
healthRequest.addParameter("wait_for_status", "green");
client().performRequest(healthRequest);

// index some documents
final int numberOfDocuments = randomIntBetween(0, 128);
Expand All @@ -75,17 +73,18 @@ public void testGlobalCheckpointSyncActionRunsAsPrivilegedUser() throws Exceptio
builder.field("foo", i);
}
builder.endObject();
final StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
client().performRequest("PUT", "/test-index/test-type/" + i, Collections.emptyMap(), entity);
Request indexRequest = new Request("PUT", "/test-index/test-type/" + i);
indexRequest.setJsonEntity(Strings.toString(builder));
client().performRequest(indexRequest);
}
}

// we have to wait for the post-operation global checkpoint sync to propagate to the replica
assertBusy(() -> {
final Map<String, String> params = new HashMap<>(2);
params.put("level", "shards");
params.put("filter_path", "**.seq_no");
final Response response = client().performRequest("GET", "/test-index/_stats", params);
final Request request = new Request("GET", "/test-index/_stats");
request.addParameter("level", "shards");
request.addParameter("filter_path", "**.seq_no");
final Response response = client().performRequest(request);
final ObjectPath path = ObjectPath.createFromResponse(response);
// int looks funny here since global checkpoints are longs but the response parser does not know enough to treat them as long
final int shard0GlobalCheckpoint = path.evaluate("indices.test-index.shards.0.0.seq_no.global_checkpoint");
Expand Down

0 comments on commit fcf8cad

Please sign in to comment.