From 12b90342aa17264e202278109f185060fd6c6481 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 13 Jun 2018 14:24:50 -0400 Subject: [PATCH 1/3] QA: Create xpack yaml feature --- qa/mixed-cluster/build.gradle | 1 - .../test/cat.templates/10_basic.yml | 2 +- .../test/rest/ESRestTestCase.java | 32 +++++++++++++++++-- .../test/rest/yaml/Features.java | 24 +++++++++++--- .../integration/MlRestTestStateCleaner.java | 10 +++--- .../rollup/RollupRestTestStateCleaner.java | 8 ++--- .../xpack/test/rest/XPackRestIT.java | 4 +-- .../build.gradle | 1 - .../ml/integration/DatafeedJobsRestIT.java | 2 +- .../xpack/ml/integration/MlJobIT.java | 2 +- 10 files changed, 62 insertions(+), 24 deletions(-) diff --git a/qa/mixed-cluster/build.gradle b/qa/mixed-cluster/build.gradle index da99bbb4c8036..ef1dc2f12e3de 100644 --- a/qa/mixed-cluster/build.gradle +++ b/qa/mixed-cluster/build.gradle @@ -59,7 +59,6 @@ for (Version version : bwcVersions.wireCompatible) { systemProperty 'tests.path.repo', new File(buildDir, "cluster/shared/repo") if ('zip'.equals(extension.distribution)) { systemProperty 'tests.rest.blacklist', [ - 'cat.templates/10_basic/No templates', 'cat.templates/10_basic/Sort templates', 'cat.templates/10_basic/Multiple template', ].join(',') diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.templates/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.templates/10_basic.yml index 78b7a4277570a..5fc00d2aa15a6 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.templates/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.templates/10_basic.yml @@ -15,7 +15,7 @@ --- "No templates": - skip: - features: default_shards + features: default_shards, no_xpack - do: cat.templates: {} diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java index df92b101bf1fd..f7c4d3d4673c0 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java @@ -32,6 +32,7 @@ import org.apache.http.ssl.SSLContexts; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksAction; +import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.RestClient; @@ -41,6 +42,8 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.common.xcontent.DeprecationHandler; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; @@ -91,13 +94,38 @@ public abstract class ESRestTestCase extends ESTestCase { /** * Convert the entity from a {@link Response} into a map of maps. */ - public Map entityAsMap(Response response) throws IOException { + public static Map entityAsMap(Response response) throws IOException { XContentType xContentType = XContentType.fromMediaTypeOrFormat(response.getEntity().getContentType().getValue()); - try (XContentParser parser = createParser(xContentType.xContent(), response.getEntity().getContent())) { + // EMPTY and THROW are fine here because `.map` doesn't use named x content or deprecation + try (XContentParser parser = xContentType.xContent().createParser( + NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + response.getEntity().getContent())) { return parser.map(); } } + /** + * Does the cluster being tested have xpack installed? + */ + public static boolean hasXPack() throws IOException { + RestClient client = adminClient(); + if (client == null) { + throw new IllegalStateException("must be called inside of a rest test case test"); + } + Map response = entityAsMap(client.performRequest(new Request("GET", "_nodes/plugins"))); + Map nodes = (Map) response.get("nodes"); + for (Map.Entry node : nodes.entrySet()) { + Map nodeInfo = (Map) node.getValue(); + for (Object module: (List) nodeInfo.get("modules")) { + Map moduleInfo = (Map) module; + if (moduleInfo.get("name").toString().startsWith("x-pack-")) { + return true; + } + } + } + return false; + } + private static List clusterHosts; /** * A client for the running Elasticsearch cluster diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/Features.java b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/Features.java index 757fc2218d51c..14662f1f833b7 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/Features.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/Features.java @@ -22,8 +22,12 @@ import java.util.Arrays; import java.util.List; +import org.elasticsearch.test.rest.ESRestTestCase; + import static java.util.Collections.unmodifiableList; +import java.io.IOException; + /** * Allows to register additional features supported by the tests runner. * This way any runner can add extra features and use proper skip sections to avoid @@ -53,11 +57,23 @@ private Features() { * Tells whether all the features provided as argument are supported */ public static boolean areAllSupported(List features) { - for (String feature : features) { - if (!SUPPORTED.contains(feature)) { - return false; + try { + for (String feature : features) { + if (feature.equals("xpack")) { + if (false == ESRestTestCase.hasXPack()) { + return false; + } + } else if (feature.equals("no_xpack")) { + if (ESRestTestCase.hasXPack()) { + return false; + } + } else if (false == SUPPORTED.contains(feature)) { + return false; + } } + return true; + } catch (IOException e) { + throw new RuntimeException("error checking if xpack is available", e); } - return true; } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java index 23f357eb1885e..4be0cefe525e6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java @@ -20,18 +20,16 @@ public class MlRestTestStateCleaner { private final Logger logger; private final RestClient adminClient; - private final ESRestTestCase testCase; - public MlRestTestStateCleaner(Logger logger, RestClient adminClient, ESRestTestCase testCase) { + public MlRestTestStateCleaner(Logger logger, RestClient adminClient) { this.logger = logger; this.adminClient = adminClient; - this.testCase = testCase; } public void clearMlMetadata() throws IOException { deleteAllDatafeeds(); deleteAllJobs(); - // indices will be deleted by the ESIntegTestCase class + // indices will be deleted by the ESRestTestCase class } @SuppressWarnings("unchecked") @@ -41,7 +39,7 @@ private void deleteAllDatafeeds() throws IOException { final Response datafeedsResponse = adminClient.performRequest(datafeedsRequest); @SuppressWarnings("unchecked") final List> datafeeds = - (List>) XContentMapValues.extractValue("datafeeds", testCase.entityAsMap(datafeedsResponse)); + (List>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse)); if (datafeeds == null) { return; } @@ -83,7 +81,7 @@ private void deleteAllJobs() throws IOException { final Response response = adminClient.performRequest(jobsRequest); @SuppressWarnings("unchecked") final List> jobConfigs = - (List>) XContentMapValues.extractValue("jobs", testCase.entityAsMap(response)); + (List>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response)); if (jobConfigs == null) { return; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupRestTestStateCleaner.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupRestTestStateCleaner.java index a9a8223863d72..9938f3a41962b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupRestTestStateCleaner.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupRestTestStateCleaner.java @@ -29,18 +29,16 @@ public class RollupRestTestStateCleaner { private final Logger logger; private final RestClient adminClient; - private final ESRestTestCase testCase; - public RollupRestTestStateCleaner(Logger logger, RestClient adminClient, ESRestTestCase testCase) { + public RollupRestTestStateCleaner(Logger logger, RestClient adminClient) { this.logger = logger; this.adminClient = adminClient; - this.testCase = testCase; } public void clearRollupMetadata() throws Exception { deleteAllJobs(); waitForPendingTasks(); - // indices will be deleted by the ESIntegTestCase class + // indices will be deleted by the ESRestTestCase class } private void waitForPendingTasks() throws Exception { @@ -75,7 +73,7 @@ private void waitForPendingTasks() throws Exception { @SuppressWarnings("unchecked") private void deleteAllJobs() throws Exception { Response response = adminClient.performRequest("GET", "/_xpack/rollup/job/_all"); - Map jobs = testCase.entityAsMap(response); + Map jobs = ESRestTestCase.entityAsMap(response); @SuppressWarnings("unchecked") List> jobConfigs = (List>) XContentMapValues.extractValue("jobs", jobs); diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java index 99a6e29e334f6..412c75f0e639c 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java @@ -252,7 +252,7 @@ public void cleanup() throws Exception { */ private void clearMlState() throws Exception { if (isMachineLearningTest()) { - new MlRestTestStateCleaner(logger, adminClient(), this).clearMlMetadata(); + new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata(); } } @@ -263,7 +263,7 @@ private void clearMlState() throws Exception { */ private void clearRollupState() throws Exception { if (isRollupTest()) { - new RollupRestTestStateCleaner(logger, adminClient(), this).clearRollupMetadata(); + new RollupRestTestStateCleaner(logger, adminClient()).clearRollupMetadata(); } } diff --git a/x-pack/qa/core-rest-tests-with-security/build.gradle b/x-pack/qa/core-rest-tests-with-security/build.gradle index 1ef0a3a98ec6e..b001e43a8909c 100644 --- a/x-pack/qa/core-rest-tests-with-security/build.gradle +++ b/x-pack/qa/core-rest-tests-with-security/build.gradle @@ -15,7 +15,6 @@ integTestRunner { ['cat.aliases/10_basic/Empty cluster', 'index/10_with_id/Index with ID', 'indices.get_alias/10_basic/Get alias against closed indices', - 'cat.templates/10_basic/No templates', 'cat.templates/10_basic/Sort templates', 'cat.templates/10_basic/Multiple template', ].join(',') diff --git a/x-pack/qa/ml-native-tests/src/test/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java b/x-pack/qa/ml-native-tests/src/test/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java index 6731e27aaac19..54d8090a7a421 100644 --- a/x-pack/qa/ml-native-tests/src/test/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java +++ b/x-pack/qa/ml-native-tests/src/test/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java @@ -802,7 +802,7 @@ public static void openJob(RestClient client, String jobId) throws IOException { @After public void clearMlState() throws Exception { - new MlRestTestStateCleaner(logger, adminClient(), this).clearMlMetadata(); + new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata(); XPackRestTestHelper.waitForPendingTasks(adminClient()); } diff --git a/x-pack/qa/ml-native-tests/src/test/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java b/x-pack/qa/ml-native-tests/src/test/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java index 114fbdd4e5dd3..6713e66692ded 100644 --- a/x-pack/qa/ml-native-tests/src/test/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java +++ b/x-pack/qa/ml-native-tests/src/test/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java @@ -676,7 +676,7 @@ private static String responseEntityToString(Response response) throws IOExcepti @After public void clearMlState() throws Exception { - new MlRestTestStateCleaner(logger, adminClient(), this).clearMlMetadata(); + new MlRestTestStateCleaner(logger, adminClient()).clearMlMetadata(); XPackRestTestHelper.waitForPendingTasks(adminClient()); } } From 30b3e7e203c4a5370bd576369f60a634d2f37304 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 13 Jun 2018 16:21:47 -0400 Subject: [PATCH 2/3] WIP --- qa/mixed-cluster/build.gradle | 6 ------ .../resources/rest-api-spec/test/cat.templates/10_basic.yml | 4 ++-- x-pack/qa/core-rest-tests-with-security/build.gradle | 2 -- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/qa/mixed-cluster/build.gradle b/qa/mixed-cluster/build.gradle index ef1dc2f12e3de..ac57d51def7c6 100644 --- a/qa/mixed-cluster/build.gradle +++ b/qa/mixed-cluster/build.gradle @@ -57,12 +57,6 @@ for (Version version : bwcVersions.wireCompatible) { tasks.getByName("${baseName}#mixedClusterTestRunner").configure { /* To support taking index snapshots, we have to set path.repo setting */ systemProperty 'tests.path.repo', new File(buildDir, "cluster/shared/repo") - if ('zip'.equals(extension.distribution)) { - systemProperty 'tests.rest.blacklist', [ - 'cat.templates/10_basic/Sort templates', - 'cat.templates/10_basic/Multiple template', - ].join(',') - } } } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.templates/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.templates/10_basic.yml index 5fc00d2aa15a6..fe0d7ee30730f 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.templates/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.templates/10_basic.yml @@ -177,7 +177,7 @@ --- "Sort templates": - skip: - features: default_shards + features: default_shards, no_xpack - do: indices.put_template: name: test @@ -227,7 +227,7 @@ --- "Multiple template": - skip: - features: default_shards + features: default_shards, no_xpack - do: indices.put_template: name: test_1 diff --git a/x-pack/qa/core-rest-tests-with-security/build.gradle b/x-pack/qa/core-rest-tests-with-security/build.gradle index b001e43a8909c..375675be70427 100644 --- a/x-pack/qa/core-rest-tests-with-security/build.gradle +++ b/x-pack/qa/core-rest-tests-with-security/build.gradle @@ -15,8 +15,6 @@ integTestRunner { ['cat.aliases/10_basic/Empty cluster', 'index/10_with_id/Index with ID', 'indices.get_alias/10_basic/Get alias against closed indices', - 'cat.templates/10_basic/Sort templates', - 'cat.templates/10_basic/Multiple template', ].join(',') systemProperty 'tests.rest.cluster.username', System.getProperty('tests.rest.cluster.username', 'test_user') From 3a7c53255e2a2e3eefb0109b1a7ab52e4260e563 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Mon, 18 Jun 2018 13:42:08 -0400 Subject: [PATCH 3/3] Fix import order --- .../main/java/org/elasticsearch/test/rest/yaml/Features.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/Features.java b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/Features.java index 14662f1f833b7..cfce0653d31c2 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/Features.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/Features.java @@ -19,6 +19,7 @@ package org.elasticsearch.test.rest.yaml; +import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -26,8 +27,6 @@ import static java.util.Collections.unmodifiableList; -import java.io.IOException; - /** * Allows to register additional features supported by the tests runner. * This way any runner can add extra features and use proper skip sections to avoid