Skip to content

Commit

Permalink
Switch some watcher tests to new style Requests (#33044)
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/smoke-test-monitoring-with-watcher`,
`x-pack/qa/smoke-test-watcher`, and
`x-pack/qa/smoke-test-watcher-with-security` projects to use the new
versions.
  • Loading branch information
nik9000 committed Aug 22, 2018
1 parent 14be9d7 commit 4977baf
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 93 deletions.
Expand Up @@ -5,12 +5,10 @@
*/
package org.elasticsearch.smoketest;

import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.test.rest.ESRestTestCase;
Expand All @@ -23,7 +21,6 @@
import org.junit.After;

import java.io.IOException;
import java.util.Collections;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput;
Expand All @@ -36,25 +33,25 @@ public class MonitoringWithWatcherRestIT extends ESRestTestCase {

@After
public void cleanExporters() throws Exception {
String body = Strings.toString(jsonBuilder().startObject().startObject("transient")
.nullField("xpack.monitoring.exporters.*")
.endObject().endObject());
assertOK(adminClient().performRequest("PUT", "_cluster/settings", Collections.emptyMap(),
new StringEntity(body, ContentType.APPLICATION_JSON)));

assertOK(adminClient().performRequest("DELETE", ".watch*", Collections.emptyMap()));
Request request = new Request("PUT", "/_cluster/settings");
request.setJsonEntity(Strings.toString(jsonBuilder().startObject()
.startObject("transient")
.nullField("xpack.monitoring.exporters.*")
.endObject().endObject()));
adminClient().performRequest(request);
adminClient().performRequest(new Request("DELETE", "/.watch*"));
}

public void testThatLocalExporterAddsWatches() throws Exception {
String watchId = createMonitoringWatch();

String body = BytesReference.bytes(jsonBuilder().startObject().startObject("transient")
.field("xpack.monitoring.exporters.my_local_exporter.type", "local")
.field("xpack.monitoring.exporters.my_local_exporter.cluster_alerts.management.enabled", true)
.endObject().endObject()).utf8ToString();

adminClient().performRequest("PUT", "_cluster/settings", Collections.emptyMap(),
new StringEntity(body, ContentType.APPLICATION_JSON));
Request request = new Request("PUT", "/_cluster/settings");
request.setJsonEntity(Strings.toString(jsonBuilder().startObject()
.startObject("transient")
.field("xpack.monitoring.exporters.my_local_exporter.type", "local")
.field("xpack.monitoring.exporters.my_local_exporter.cluster_alerts.management.enabled", true)
.endObject().endObject()));
adminClient().performRequest(request);

assertTotalWatchCount(ClusterAlertsUtil.WATCH_IDS.length);

Expand All @@ -65,30 +62,30 @@ public void testThatHttpExporterAddsWatches() throws Exception {
String watchId = createMonitoringWatch();
String httpHost = getHttpHost();

String body = BytesReference.bytes(jsonBuilder().startObject().startObject("transient")
.field("xpack.monitoring.exporters.my_http_exporter.type", "http")
.field("xpack.monitoring.exporters.my_http_exporter.host", httpHost)
.field("xpack.monitoring.exporters.my_http_exporter.cluster_alerts.management.enabled", true)
.endObject().endObject()).utf8ToString();

adminClient().performRequest("PUT", "_cluster/settings", Collections.emptyMap(),
new StringEntity(body, ContentType.APPLICATION_JSON));
Request request = new Request("PUT", "/_cluster/settings");
request.setJsonEntity(Strings.toString(jsonBuilder().startObject()
.startObject("transient")
.field("xpack.monitoring.exporters.my_http_exporter.type", "http")
.field("xpack.monitoring.exporters.my_http_exporter.host", httpHost)
.field("xpack.monitoring.exporters.my_http_exporter.cluster_alerts.management.enabled", true)
.endObject().endObject()));
adminClient().performRequest(request);

assertTotalWatchCount(ClusterAlertsUtil.WATCH_IDS.length);

assertMonitoringWatchHasBeenOverWritten(watchId);
}

private void assertMonitoringWatchHasBeenOverWritten(String watchId) throws Exception {
ObjectPath path = ObjectPath.createFromResponse(client().performRequest("GET", "_xpack/watcher/watch/" + watchId));
ObjectPath path = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/_xpack/watcher/watch/" + watchId)));
String interval = path.evaluate("watch.trigger.schedule.interval");
assertThat(interval, is("1m"));
}

private void assertTotalWatchCount(int expectedWatches) throws Exception {
assertBusy(() -> {
assertOK(client().performRequest("POST", ".watches/_refresh"));
ObjectPath path = ObjectPath.createFromResponse(client().performRequest("POST", ".watches/_count"));
assertOK(client().performRequest(new Request("POST", "/.watches/_refresh")));
ObjectPath path = ObjectPath.createFromResponse(client().performRequest(new Request("POST", "/.watches/_count")));
int count = path.evaluate("count");
assertThat(count, is(expectedWatches));
});
Expand All @@ -97,28 +94,28 @@ private void assertTotalWatchCount(int expectedWatches) throws Exception {
private String createMonitoringWatch() throws Exception {
String clusterUUID = getClusterUUID();
String watchId = clusterUUID + "_kibana_version_mismatch";
String sampleWatch = WatchSourceBuilders.watchBuilder()
Request request = new Request("PUT", "/_xpack/watcher/watch/" + watchId);
request.setJsonEntity(WatchSourceBuilders.watchBuilder()
.trigger(TriggerBuilders.schedule(new IntervalSchedule(new IntervalSchedule.Interval(1000, MINUTES))))
.input(simpleInput())
.addAction("logme", ActionBuilders.loggingAction("foo"))
.buildAsBytes(XContentType.JSON).utf8ToString();
client().performRequest("PUT", "_xpack/watcher/watch/" + watchId, Collections.emptyMap(),
new StringEntity(sampleWatch, ContentType.APPLICATION_JSON));
.buildAsBytes(XContentType.JSON).utf8ToString());
client().performRequest(request);
return watchId;
}

private String getClusterUUID() throws Exception {
Response response = client().performRequest("GET", "_cluster/state/metadata", Collections.emptyMap());
Response response = client().performRequest(new Request("GET", "/_cluster/state/metadata"));
ObjectPath objectPath = ObjectPath.createFromResponse(response);
String clusterUUID = objectPath.evaluate("metadata.cluster_uuid");
return clusterUUID;
}

public String getHttpHost() throws IOException {
ObjectPath path = ObjectPath.createFromResponse(client().performRequest("GET", "_cluster/state", Collections.emptyMap()));
ObjectPath path = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/_cluster/state")));
String masterNodeId = path.evaluate("master_node");

ObjectPath nodesPath = ObjectPath.createFromResponse(client().performRequest("GET", "_nodes", Collections.emptyMap()));
ObjectPath nodesPath = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/_nodes")));
String httpHost = nodesPath.evaluate("nodes." + masterNodeId + ".http.publish_address");
return httpHost;
}
Expand Down
Expand Up @@ -7,9 +7,7 @@

import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.Request;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
Expand Down Expand Up @@ -49,9 +47,9 @@ public void startWatcher() throws Exception {
emptyList(), emptyMap());

// create one document in this index, so we can test in the YAML tests, that the index cannot be accessed
Response resp = adminClient().performRequest("PUT", "/index_not_allowed_to_read/doc/1", Collections.emptyMap(),
new StringEntity("{\"foo\":\"bar\"}", ContentType.APPLICATION_JSON));
assertThat(resp.getStatusLine().getStatusCode(), is(201));
Request request = new Request("PUT", "/index_not_allowed_to_read/doc/1");
request.setJsonEntity("{\"foo\":\"bar\"}");
adminClient().performRequest(request);

assertBusy(() -> {
ClientYamlTestResponse response =
Expand Down Expand Up @@ -129,4 +127,3 @@ protected Settings restAdminSettings() {
.build();
}
}

Expand Up @@ -5,9 +5,8 @@
*/
package org.elasticsearch.smoketest;

import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.SecureString;
Expand All @@ -21,7 +20,6 @@
import org.junit.Before;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -41,27 +39,28 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {

@Before
public void startWatcher() throws Exception {
StringEntity entity = new StringEntity("{ \"value\" : \"15\" }", ContentType.APPLICATION_JSON);
assertOK(adminClient().performRequest("PUT", "my_test_index/doc/1", Collections.singletonMap("refresh", "true"), entity));
Request createAllowedDoc = new Request("PUT", "/my_test_index/doc/1");
createAllowedDoc.setJsonEntity("{ \"value\" : \"15\" }");
createAllowedDoc.addParameter("refresh", "true");
adminClient().performRequest(createAllowedDoc);

// delete the watcher history to not clutter with entries from other test
adminClient().performRequest("DELETE", ".watcher-history-*", Collections.emptyMap());
adminClient().performRequest(new Request("DELETE", ".watcher-history-*"));

// create one document in this index, so we can test in the YAML tests, that the index cannot be accessed
Response resp = adminClient().performRequest("PUT", "/index_not_allowed_to_read/doc/1", Collections.emptyMap(),
new StringEntity("{\"foo\":\"bar\"}", ContentType.APPLICATION_JSON));
assertThat(resp.getStatusLine().getStatusCode(), is(201));
Request createNotAllowedDoc = new Request("PUT", "/index_not_allowed_to_read/doc/1");
createNotAllowedDoc.setJsonEntity("{\"foo\":\"bar\"}");
adminClient().performRequest(createNotAllowedDoc);

assertBusy(() -> {
try {
Response statsResponse = adminClient().performRequest("GET", "_xpack/watcher/stats");
Response statsResponse = adminClient().performRequest(new Request("GET", "/_xpack/watcher/stats"));
ObjectPath objectPath = ObjectPath.createFromResponse(statsResponse);
String state = objectPath.evaluate("stats.0.watcher_state");

switch (state) {
case "stopped":
Response startResponse = adminClient().performRequest("POST", "_xpack/watcher/_start");
assertOK(startResponse);
Response startResponse = adminClient().performRequest(new Request("POST", "/_xpack/watcher/_start"));
String body = EntityUtils.toString(startResponse.getEntity());
assertThat(body, containsString("\"acknowledged\":true"));
break;
Expand All @@ -82,18 +81,18 @@ public void startWatcher() throws Exception {

assertBusy(() -> {
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) {
assertOK(adminClient().performRequest("HEAD", "_template/" + template));
assertOK(adminClient().performRequest(new Request("HEAD", "_template/" + template)));
}
});
}

@After
public void stopWatcher() throws Exception {
assertOK(adminClient().performRequest("DELETE", "my_test_index"));
adminClient().performRequest(new Request("DELETE", "/my_test_index"));

assertBusy(() -> {
try {
Response statsResponse = adminClient().performRequest("GET", "_xpack/watcher/stats");
Response statsResponse = adminClient().performRequest(new Request("GET", "/_xpack/watcher/stats"));
ObjectPath objectPath = ObjectPath.createFromResponse(statsResponse);
String state = objectPath.evaluate("stats.0.watcher_state");

Expand All @@ -106,8 +105,7 @@ public void stopWatcher() throws Exception {
case "starting":
throw new AssertionError("waiting until starting state reached started state to stop");
case "started":
Response stopResponse = adminClient().performRequest("POST", "_xpack/watcher/_stop", Collections.emptyMap());
assertOK(stopResponse);
Response stopResponse = adminClient().performRequest(new Request("POST", "/_xpack/watcher/_stop"));
String body = EntityUtils.toString(stopResponse.getEntity());
assertThat(body, containsString("\"acknowledged\":true"));
break;
Expand Down Expand Up @@ -210,7 +208,7 @@ public void testSearchTransformHasPermissions() throws Exception {
boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met");
assertThat(conditionMet, is(true));

ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest("GET", "my_test_index/doc/my-id"));
ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/my_test_index/doc/my-id")));
String value = getObjectPath.evaluate("_source.hits.hits.0._source.value");
assertThat(value, is("15"));
}
Expand Down Expand Up @@ -238,8 +236,7 @@ public void testSearchTransformInsufficientPermissions() throws Exception {

getWatchHistoryEntry(watchId);

Response response = adminClient().performRequest("GET", "my_test_index/doc/some-id",
Collections.singletonMap("ignore", "404"));
Response response = adminClient().performRequest(new Request("HEAD", "/my_test_index/doc/some-id"));
assertThat(response.getStatusLine().getStatusCode(), is(404));
}

Expand All @@ -262,7 +259,7 @@ public void testIndexActionHasPermissions() throws Exception {
boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met");
assertThat(conditionMet, is(true));

ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest("GET", "my_test_index/doc/my-id"));
ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/my_test_index/doc/my-id")));
String spam = getObjectPath.evaluate("_source.spam");
assertThat(spam, is("eggs"));
}
Expand All @@ -286,16 +283,14 @@ public void testIndexActionInsufficientPrivileges() throws Exception {
boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met");
assertThat(conditionMet, is(true));

Response response = adminClient().performRequest("GET", "index_not_allowed_to_read/doc/my-id",
Collections.singletonMap("ignore", "404"));
Response response = adminClient().performRequest(new Request("HEAD", "/index_not_allowed_to_read/doc/my-id"));
assertThat(response.getStatusLine().getStatusCode(), is(404));
}

private void indexWatch(String watchId, XContentBuilder builder) throws Exception {
StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);

Response response = client().performRequest("PUT", "_xpack/watcher/watch/" + watchId, Collections.emptyMap(), entity);
assertOK(response);
Request request = new Request("PUT", "/_xpack/watcher/watch/" + watchId);
request.setJsonEntity(Strings.toString(builder));
Response response = client().performRequest(request);
Map<String, Object> responseMap = entityAsMap(response);
assertThat(responseMap, hasEntry("_id", watchId));
}
Expand All @@ -307,7 +302,7 @@ private ObjectPath getWatchHistoryEntry(String watchId) throws Exception {
private ObjectPath getWatchHistoryEntry(String watchId, String state) throws Exception {
final AtomicReference<ObjectPath> objectPathReference = new AtomicReference<>();
assertBusy(() -> {
client().performRequest("POST", ".watcher-history-*/_refresh");
client().performRequest(new Request("POST", "/.watcher-history-*/_refresh"));

try (XContentBuilder builder = jsonBuilder()) {
builder.startObject();
Expand All @@ -323,8 +318,9 @@ private ObjectPath getWatchHistoryEntry(String watchId, String state) throws Exc
.endObject().endArray();
builder.endObject();

StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
Response response = client().performRequest("POST", ".watcher-history-*/_search", Collections.emptyMap(), entity);
Request searchRequest = new Request("POST", "/.watcher-history-*/_search");
searchRequest.setJsonEntity(Strings.toString(builder));
Response response = client().performRequest(searchRequest);
ObjectPath objectPath = ObjectPath.createFromResponse(response);
int totalHits = objectPath.evaluate("hits.total");
assertThat(totalHits, is(greaterThanOrEqualTo(1)));
Expand Down

0 comments on commit 4977baf

Please sign in to comment.