From d2fbb0dd4bb9e36dcddda98f80a50b2b21b5a9b8 Mon Sep 17 00:00:00 2001 From: David Turner Date: Wed, 11 Dec 2024 12:54:31 +0000 Subject: [PATCH] Migrate `discovery-ec2` YAML tests to Java REST tests (#118427) (#118448) Really just to create a starting point for a more comprehensive Java REST test suite, the test itself is not very interesting. --- plugins/discovery-ec2/build.gradle | 8 +--- .../ec2/DiscoveryEc2PluginLoadedIT.java | 48 +++++++++++++++++++ .../ec2/CloudAwsClientYamlTestSuiteIT.java | 28 ----------- .../test/discovery_ec2/10_basic.yml | 16 ------- .../elasticsearch/test/rest/ObjectPath.java | 19 ++++++-- 5 files changed, 65 insertions(+), 54 deletions(-) create mode 100644 plugins/discovery-ec2/src/javaRestTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2PluginLoadedIT.java delete mode 100644 plugins/discovery-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/CloudAwsClientYamlTestSuiteIT.java delete mode 100644 plugins/discovery-ec2/src/yamlRestTest/resources/rest-api-spec/test/discovery_ec2/10_basic.yml diff --git a/plugins/discovery-ec2/build.gradle b/plugins/discovery-ec2/build.gradle index 756487a147abc..629683f1dd909 100644 --- a/plugins/discovery-ec2/build.gradle +++ b/plugins/discovery-ec2/build.gradle @@ -8,8 +8,8 @@ import org.elasticsearch.gradle.internal.info.BuildParams * your election, the "Elastic License 2.0", the "GNU Affero General Public * License v3.0 only", or the "Server Side Public License, v 1". */ -apply plugin: 'elasticsearch.legacy-yaml-rest-test' apply plugin: 'elasticsearch.internal-cluster-test' +apply plugin: 'elasticsearch.internal-java-rest-test' esplugin { description 'The EC2 discovery plugin allows to use AWS API for the unicast discovery mechanism.' @@ -31,12 +31,6 @@ dependencies { api "joda-time:joda-time:2.10.10" } -restResources { - restApi { - include '_common', 'cluster', 'nodes' - } -} - tasks.named("dependencyLicenses").configure { mapping from: /aws-java-sdk-.*/, to: 'aws-java-sdk' mapping from: /jackson-.*/, to: 'jackson' diff --git a/plugins/discovery-ec2/src/javaRestTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2PluginLoadedIT.java b/plugins/discovery-ec2/src/javaRestTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2PluginLoadedIT.java new file mode 100644 index 0000000000000..f0f22b2136fc7 --- /dev/null +++ b/plugins/discovery-ec2/src/javaRestTest/java/org/elasticsearch/discovery/ec2/DiscoveryEc2PluginLoadedIT.java @@ -0,0 +1,48 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +package org.elasticsearch.discovery.ec2; + +import org.elasticsearch.client.Request; +import org.elasticsearch.test.cluster.ElasticsearchCluster; +import org.elasticsearch.test.rest.ESRestTestCase; +import org.junit.ClassRule; + +import java.io.IOException; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; + +import static org.hamcrest.Matchers.hasItem; + +public class DiscoveryEc2PluginLoadedIT extends ESRestTestCase { + + @ClassRule + public static ElasticsearchCluster cluster = ElasticsearchCluster.local().plugin("discovery-ec2").build(); + + @Override + protected String getTestRestCluster() { + return cluster.getHttpAddresses(); + } + + public void testPluginLoaded() throws IOException { + final var nodesInfoResponse = assertOKAndCreateObjectPath(client().performRequest(new Request("GET", "/_nodes/plugins"))); + for (final var nodeId : nodesInfoResponse.evaluateMapKeys("nodes")) { + final var pluginCount = asInstanceOf(List.class, nodesInfoResponse.evaluateExact("nodes", nodeId, "plugins")).size(); + final var pluginNames = new HashSet(); + for (int i = 0; i < pluginCount; i++) { + pluginNames.add( + Objects.requireNonNull(nodesInfoResponse.evaluateExact("nodes", nodeId, "plugins", Integer.toString(i), "name")) + ); + } + assertThat(pluginNames, hasItem("discovery-ec2")); + } + } + +} diff --git a/plugins/discovery-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/CloudAwsClientYamlTestSuiteIT.java b/plugins/discovery-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/CloudAwsClientYamlTestSuiteIT.java deleted file mode 100644 index 7a8e4eed9f928..0000000000000 --- a/plugins/discovery-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/CloudAwsClientYamlTestSuiteIT.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -package org.elasticsearch.discovery.ec2; - -import com.carrotsearch.randomizedtesting.annotations.Name; -import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; - -import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; -import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; - -public class CloudAwsClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase { - - public CloudAwsClientYamlTestSuiteIT(@Name("yaml") ClientYamlTestCandidate testCandidate) { - super(testCandidate); - } - - @ParametersFactory - public static Iterable parameters() throws Exception { - return ESClientYamlSuiteTestCase.createParameters(); - } -} diff --git a/plugins/discovery-ec2/src/yamlRestTest/resources/rest-api-spec/test/discovery_ec2/10_basic.yml b/plugins/discovery-ec2/src/yamlRestTest/resources/rest-api-spec/test/discovery_ec2/10_basic.yml deleted file mode 100644 index ba51c623fe888..0000000000000 --- a/plugins/discovery-ec2/src/yamlRestTest/resources/rest-api-spec/test/discovery_ec2/10_basic.yml +++ /dev/null @@ -1,16 +0,0 @@ -# Integration tests for Discovery EC2 component -# -"Discovery EC2 loaded": - - skip: - reason: "contains is a newly added assertion" - features: contains - - do: - cluster.state: {} - - # Get master node id - - set: { master_node: master } - - - do: - nodes.info: {} - - - contains: { nodes.$master.plugins: { name: discovery-ec2 } } diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/ObjectPath.java b/test/framework/src/main/java/org/elasticsearch/test/rest/ObjectPath.java index fd1b3a57313c3..9e15f482411a7 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/ObjectPath.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/ObjectPath.java @@ -70,11 +70,24 @@ public T evaluate(String path) throws IOException { /** * Returns the object corresponding to the provided path if present, null otherwise */ - @SuppressWarnings("unchecked") public T evaluate(String path, Stash stash) throws IOException { - String[] parts = parsePath(path); + return evaluateExact(stash, parsePath(path)); + } + + /** + * Returns the object corresponding to the provided path if present, null otherwise + */ + public T evaluateExact(String... path) throws IOException { + return evaluateExact(Stash.EMPTY, path); + } + + /** + * Returns the object corresponding to the provided path if present, null otherwise + */ + @SuppressWarnings("unchecked") + public T evaluateExact(Stash stash, String... path) throws IOException { Object result = this.object; - for (String part : parts) { + for (String part : path) { result = evaluate(part, result, stash); if (result == null) { return null;