update XPackRestTestCase to use low level RestClient

Original commit: elastic/x-pack-elasticsearch@8c16c9b06e
This commit is contained in:
javanna 2016-05-20 11:17:24 +02:00 committed by Luca Cavanna
parent e882fb3a18
commit 57f8063c3a
1 changed files with 40 additions and 62 deletions

View File

@ -5,15 +5,12 @@
*/ */
package org.elasticsearch.xpack.test.rest; package org.elasticsearch.xpack.test.rest;
import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.util.EntityUtils;
import org.apache.http.client.methods.HttpDelete; import org.elasticsearch.client.ElasticsearchResponse;
import org.apache.http.client.methods.HttpGet; import org.elasticsearch.client.ElasticsearchResponseException;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -24,7 +21,9 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.license.plugin.TestUtils; import org.elasticsearch.license.plugin.TestUtils;
import org.elasticsearch.shield.authc.esnative.ReservedRealm;
import org.elasticsearch.shield.authc.support.SecuredString; import org.elasticsearch.shield.authc.support.SecuredString;
import org.elasticsearch.shield.authz.store.ReservedRolesStore;
import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.RestTestCandidate; import org.elasticsearch.test.rest.RestTestCandidate;
import org.elasticsearch.test.rest.parser.RestTestParseException; import org.elasticsearch.test.rest.parser.RestTestParseException;
@ -33,8 +32,6 @@ import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -59,31 +56,17 @@ public abstract class XPackRestTestCase extends ESRestTestCase {
@Before @Before
public void startWatcher() throws Exception { public void startWatcher() throws Exception {
try (CloseableHttpClient client = HttpClients.createMinimal(new BasicHttpClientConnectionManager())) { try (ElasticsearchResponse response = getRestClient()
URL url = getClusterUrls()[0]; .performRequest("PUT", "/_xpack/watcher/_start", Collections.emptyMap(), null)) {
HttpPut request = new HttpPut(new URI("http", assertThat(response.getStatusLine().getStatusCode(), is(200));
null,
url.getHost(),
url.getPort(),
"/_xpack/watcher/_start", null, null));
request.addHeader("Authorization", BASIC_AUTH_VALUE);
try (CloseableHttpResponse response = client.execute(request)) {
}
} }
} }
@After @After
public void stopWatcher() throws Exception { public void stopWatcher() throws Exception {
try(CloseableHttpClient client = HttpClients.createMinimal(new BasicHttpClientConnectionManager())) { try (ElasticsearchResponse response = getRestClient()
URL url = getClusterUrls()[0]; .performRequest("PUT", "/_xpack/watcher/_stop", Collections.emptyMap(), null)) {
HttpPut request = new HttpPut(new URI("http", assertThat(response.getStatusLine().getStatusCode(), is(200));
null,
url.getHost(),
url.getPort(),
"/_xpack/watcher/_stop", null, null));
request.addHeader("Authorization", BASIC_AUTH_VALUE);
try (CloseableHttpResponse response = client.execute(request)) {
}
} }
} }
@ -102,45 +85,40 @@ public abstract class XPackRestTestCase extends ESRestTestCase {
@After @After
public void clearShieldUsersAndRoles() throws Exception { public void clearShieldUsersAndRoles() throws Exception {
//TODO change this to use RestClient, also look for usages of HttpClient directly
// we cannot delete the .security index from a rest test since we aren't the internal user, lets wipe the data // we cannot delete the .security index from a rest test since we aren't the internal user, lets wipe the data
// TODO remove this once the built-in SUPERUSER role is added that can delete the index and we use the built in admin user here // TODO remove this once the built-in SUPERUSER role is added that can delete the index and we use the built in admin user here
try (CloseableHttpClient client = HttpClients.createMinimal(new BasicHttpClientConnectionManager())) { String response;
final URL url = getClusterUrls()[0]; try (ElasticsearchResponse elasticsearchResponse = getRestClient()
HttpGet getUsersRequest = new HttpGet(new URI("http", null, url.getHost(), url.getPort(), "/_xpack/security/user", null, null)); .performRequest("GET", "/_xpack/security/user", Collections.emptyMap(), null)) {
getUsersRequest.addHeader("Authorization", BASIC_AUTH_VALUE); assertThat(elasticsearchResponse.getStatusLine().getStatusCode(), is(200));
try (CloseableHttpResponse closeableHttpResponse = client.execute(getUsersRequest)) { response = Streams.copyToString(new InputStreamReader(elasticsearchResponse.getEntity().getContent(), StandardCharsets.UTF_8));
assertThat(closeableHttpResponse.getStatusLine().getStatusCode(), is(200)); }
String response = Streams.copyToString(
new InputStreamReader(closeableHttpResponse.getEntity().getContent(), StandardCharsets.UTF_8));
Map<String, Object> responseMap = XContentFactory.xContent(response).createParser(response).map();
Map<String, Object> responseMap = XContentFactory.xContent(response).createParser(response).map();
// in the structure of this API, the users are the keyset // in the structure of this API, the users are the keyset
for (String user : responseMap.keySet()) { for (String user : responseMap.keySet()) {
HttpDelete delete = new HttpDelete(new URI("http", null, url.getHost(), url.getPort(), if (ReservedRealm.isReserved(user) == false) {
"/_xpack/security/user/" + user, null, null)); try (ElasticsearchResponse elasticsearchResponse = getRestClient()
delete.addHeader("Authorization", BASIC_AUTH_VALUE); .performRequest("DELETE", "/_xpack/security/user/" + user, Collections.emptyMap(), null)) {
try (CloseableHttpResponse deleteResponse = client.execute(delete)) { assertThat(EntityUtils.toString(elasticsearchResponse.getEntity()), elasticsearchResponse.getStatusLine().getStatusCode(), is(200));
} catch(ElasticsearchResponseException e) {
logger.error(e.getResponseBody());
} }
} }
} }
HttpGet getRolesRequest = new HttpGet(new URI("http", null, url.getHost(), url.getPort(), "/_xpack/security/role", try (ElasticsearchResponse elasticsearchResponse = getRestClient()
null, null)); .performRequest("GET", "/_xpack/security/role", Collections.emptyMap(), null)) {
getRolesRequest.addHeader("Authorization", BASIC_AUTH_VALUE); assertThat(elasticsearchResponse.getStatusLine().getStatusCode(), is(200));
try (CloseableHttpResponse closeableHttpResponse = client.execute(getRolesRequest)) { response = Streams.copyToString(new InputStreamReader(elasticsearchResponse.getEntity().getContent(), StandardCharsets.UTF_8));
assertThat(closeableHttpResponse.getStatusLine().getStatusCode(), is(200)); }
String response = Streams.copyToString( responseMap = XContentFactory.xContent(response).createParser(response).map();
new InputStreamReader(closeableHttpResponse.getEntity().getContent(), StandardCharsets.UTF_8)); // in the structure of this API, the roles are the keyset
Map<String, Object> responseMap = XContentFactory.xContent(response).createParser(response).map();
// in the structure of this API, the users are the keyset
for (String role : responseMap.keySet()) { for (String role : responseMap.keySet()) {
HttpDelete delete = new HttpDelete(new URI("http", null, url.getHost(), url.getPort(), if (ReservedRolesStore.isReserved(role) == false) {
"/_xpack/security/role/" + role, null, null)); try (ElasticsearchResponse elasticsearchResponse = getRestClient()
delete.addHeader("Authorization", BASIC_AUTH_VALUE); .performRequest("DELETE", "/_xpack/security/role/" + role, Collections.emptyMap(), null)) {
try (CloseableHttpResponse deleteResponse = client.execute(delete)) { assertThat(elasticsearchResponse.getStatusLine().getStatusCode(), is(200));
}
} }
} }
} }