-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch some watcher tests to new style Requests #33044
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
||
|
@@ -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; | ||
|
@@ -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"); | ||
|
||
|
@@ -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; | ||
|
@@ -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")); | ||
} | ||
|
@@ -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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't the ignore param needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that comes for free with HEAD. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh right! |
||
Response response = adminClient().performRequest(new Request("HEAD", "/my_test_index/doc/some-id")); | ||
assertThat(response.getStatusLine().getStatusCode(), is(404)); | ||
} | ||
|
||
|
@@ -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")); | ||
} | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you remove this assertion intentionally? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. It just asserts that we return a 200 or 201. Since we through an exception on non-2xx replies anyway I figured it isn't useful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense |
||
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)); | ||
} | ||
|
@@ -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(); | ||
|
@@ -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))); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why remove this assertion?