Switch some x-pack tests to new style Requests (#32500)
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.
This commit is contained in:
parent
28a0df2c7f
commit
fcf8cadd9a
|
@ -5,16 +5,13 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.ml.integration;
|
package org.elasticsearch.xpack.ml.integration;
|
||||||
|
|
||||||
import org.apache.http.entity.ContentType;
|
import org.elasticsearch.client.Request;
|
||||||
import org.apache.http.entity.StringEntity;
|
|
||||||
import org.elasticsearch.client.ResponseException;
|
import org.elasticsearch.client.ResponseException;
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.test.rest.ESRestTestCase;
|
import org.elasticsearch.test.rest.ESRestTestCase;
|
||||||
import org.elasticsearch.xpack.ml.MachineLearning;
|
import org.elasticsearch.xpack.ml.MachineLearning;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
|
||||||
|
@ -27,30 +24,40 @@ public class MlPluginDisabledIT extends ESRestTestCase {
|
||||||
public void testActionsFail() throws Exception {
|
public void testActionsFail() throws Exception {
|
||||||
XContentBuilder xContentBuilder = jsonBuilder();
|
XContentBuilder xContentBuilder = jsonBuilder();
|
||||||
xContentBuilder.startObject();
|
xContentBuilder.startObject();
|
||||||
|
{
|
||||||
xContentBuilder.field("actions-fail-job", "foo");
|
xContentBuilder.field("actions-fail-job", "foo");
|
||||||
xContentBuilder.field("description", "Analysis of response time by airline");
|
xContentBuilder.field("description", "Analysis of response time by airline");
|
||||||
|
|
||||||
xContentBuilder.startObject("analysis_config");
|
xContentBuilder.startObject("analysis_config");
|
||||||
|
{
|
||||||
xContentBuilder.field("bucket_span", "3600s");
|
xContentBuilder.field("bucket_span", "3600s");
|
||||||
xContentBuilder.startArray("detectors");
|
xContentBuilder.startArray("detectors");
|
||||||
|
{
|
||||||
xContentBuilder.startObject();
|
xContentBuilder.startObject();
|
||||||
|
{
|
||||||
xContentBuilder.field("function", "metric");
|
xContentBuilder.field("function", "metric");
|
||||||
xContentBuilder.field("field_name", "responsetime");
|
xContentBuilder.field("field_name", "responsetime");
|
||||||
xContentBuilder.field("by_field_name", "airline");
|
xContentBuilder.field("by_field_name", "airline");
|
||||||
|
}
|
||||||
xContentBuilder.endObject();
|
xContentBuilder.endObject();
|
||||||
|
}
|
||||||
xContentBuilder.endArray();
|
xContentBuilder.endArray();
|
||||||
|
}
|
||||||
xContentBuilder.endObject();
|
xContentBuilder.endObject();
|
||||||
|
|
||||||
xContentBuilder.startObject("data_description");
|
xContentBuilder.startObject("data_description");
|
||||||
|
{
|
||||||
xContentBuilder.field("format", "xcontent");
|
xContentBuilder.field("format", "xcontent");
|
||||||
xContentBuilder.field("time_field", "time");
|
xContentBuilder.field("time_field", "time");
|
||||||
xContentBuilder.field("time_format", "epoch");
|
xContentBuilder.field("time_format", "epoch");
|
||||||
|
}
|
||||||
xContentBuilder.endObject();
|
xContentBuilder.endObject();
|
||||||
|
}
|
||||||
xContentBuilder.endObject();
|
xContentBuilder.endObject();
|
||||||
|
|
||||||
ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest("put",
|
Request request = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/foo");
|
||||||
MachineLearning.BASE_PATH + "anomaly_detectors/foo", Collections.emptyMap(),
|
request.setJsonEntity(Strings.toString(xContentBuilder));
|
||||||
new StringEntity(Strings.toString(xContentBuilder), ContentType.APPLICATION_JSON)));
|
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]"));
|
assertThat(exception.getMessage(), containsString("no handler found for uri [/_xpack/ml/anomaly_detectors/foo] and method [PUT]"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
package org.elasticsearch.xpack.security.audit;
|
package org.elasticsearch.xpack.security.audit;
|
||||||
|
|
||||||
import com.carrotsearch.hppc.cursors.ObjectCursor;
|
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.admin.indices.template.get.GetIndexTemplatesResponse;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
||||||
|
@ -111,10 +110,12 @@ public class IndexAuditIT extends ESIntegTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testIndexAuditTrailWorking() throws Exception {
|
public void testIndexAuditTrailWorking() throws Exception {
|
||||||
Response response = getRestClient().performRequest("GET", "/",
|
Request request = new Request("GET", "/");
|
||||||
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
|
RequestOptions.Builder options = request.getOptions().toBuilder();
|
||||||
UsernamePasswordToken.basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray()))));
|
options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
|
||||||
assertThat(response.getStatusLine().getStatusCode(), is(200));
|
UsernamePasswordToken.basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())));
|
||||||
|
request.setOptions(options);
|
||||||
|
Response response = getRestClient().performRequest(request);
|
||||||
final AtomicReference<ClusterState> lastClusterState = new AtomicReference<>();
|
final AtomicReference<ClusterState> lastClusterState = new AtomicReference<>();
|
||||||
final boolean found = awaitSecurityAuditIndex(lastClusterState, QueryBuilders.matchQuery("principal", USER));
|
final boolean found = awaitSecurityAuditIndex(lastClusterState, QueryBuilders.matchQuery("principal", USER));
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,7 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.multi_node;
|
package org.elasticsearch.multi_node;
|
||||||
|
|
||||||
import org.apache.http.entity.ContentType;
|
import org.elasticsearch.client.Request;
|
||||||
import org.apache.http.entity.StringEntity;
|
|
||||||
import org.elasticsearch.client.Response;
|
import org.elasticsearch.client.Response;
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
import org.elasticsearch.common.settings.SecureString;
|
import org.elasticsearch.common.settings.SecureString;
|
||||||
|
@ -16,10 +15,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.test.rest.ESRestTestCase;
|
import org.elasticsearch.test.rest.ESRestTestCase;
|
||||||
import org.elasticsearch.test.rest.yaml.ObjectPath;
|
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.common.xcontent.XContentFactory.jsonBuilder;
|
||||||
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
|
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
@ -59,12 +54,15 @@ public class GlobalCheckpointSyncActionIT extends ESRestTestCase {
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
}
|
}
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
final StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
|
Request createIndexRequest = new Request("PUT", "/test-index");
|
||||||
client().performRequest("PUT", "test-index", Collections.emptyMap(), entity);
|
createIndexRequest.setJsonEntity(Strings.toString(builder));
|
||||||
|
client().performRequest(createIndexRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for the replica to recover
|
// 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
|
// index some documents
|
||||||
final int numberOfDocuments = randomIntBetween(0, 128);
|
final int numberOfDocuments = randomIntBetween(0, 128);
|
||||||
|
@ -75,17 +73,18 @@ public class GlobalCheckpointSyncActionIT extends ESRestTestCase {
|
||||||
builder.field("foo", i);
|
builder.field("foo", i);
|
||||||
}
|
}
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
final StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
|
Request indexRequest = new Request("PUT", "/test-index/test-type/" + i);
|
||||||
client().performRequest("PUT", "/test-index/test-type/" + i, Collections.emptyMap(), entity);
|
indexRequest.setJsonEntity(Strings.toString(builder));
|
||||||
|
client().performRequest(indexRequest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// we have to wait for the post-operation global checkpoint sync to propagate to the replica
|
// we have to wait for the post-operation global checkpoint sync to propagate to the replica
|
||||||
assertBusy(() -> {
|
assertBusy(() -> {
|
||||||
final Map<String, String> params = new HashMap<>(2);
|
final Request request = new Request("GET", "/test-index/_stats");
|
||||||
params.put("level", "shards");
|
request.addParameter("level", "shards");
|
||||||
params.put("filter_path", "**.seq_no");
|
request.addParameter("filter_path", "**.seq_no");
|
||||||
final Response response = client().performRequest("GET", "/test-index/_stats", params);
|
final Response response = client().performRequest(request);
|
||||||
final ObjectPath path = ObjectPath.createFromResponse(response);
|
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
|
// 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");
|
final int shard0GlobalCheckpoint = path.evaluate("indices.test-index.shards.0.0.seq_no.global_checkpoint");
|
||||||
|
|
Loading…
Reference in New Issue