-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce simple public yaml-rest-test plugin (7.x backport) (#77054)
* Introduce simple public yaml-rest-test plugin (#76554) This introduces a basic public yaml rest test plugin that is supposed to be used by external elasticsearch plugin authors. This is driven by #76215 - Rename yaml-rest-test to intern-yaml-rest-test - Use public yaml plugin in example plugins Co-authored-by: Mark Vieira <[email protected]> * Fix test assertion after output normalization Co-authored-by: Mark Vieira <[email protected]>
- Loading branch information
1 parent
a98df4e
commit 4039c35
Showing
66 changed files
with
290 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...ools/src/integTest/groovy/org/elasticsearch/gradle/test/YamlRestTestPluginFuncTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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 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 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.gradle.test | ||
|
||
import org.elasticsearch.gradle.VersionProperties | ||
import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest | ||
import org.gradle.testkit.runner.TaskOutcome | ||
|
||
class YamlRestTestPluginFuncTest extends AbstractGradleFuncTest { | ||
|
||
def "declares default dependencies"() { | ||
given: | ||
buildFile << """ | ||
plugins { | ||
id 'elasticsearch.yaml-rest-test' | ||
} | ||
""" | ||
|
||
when: | ||
def output = gradleRunner("dependencies").build().output | ||
then: | ||
output.contains(""" | ||
restTestSpecs | ||
/--- org.elasticsearch:rest-api-spec:${VersionProperties.elasticsearch} FAILED""") | ||
output.contains(normalized(""" | ||
yamlRestTestImplementation - Implementation only dependencies for source set 'yaml rest test'. (n) | ||
/--- org.elasticsearch.test:framework:${VersionProperties.elasticsearch} (n)""")) | ||
} | ||
|
||
def "yamlRestTest does nothing when there are no tests"() { | ||
given: | ||
buildFile << """ | ||
plugins { | ||
id 'elasticsearch.yaml-rest-test' | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
dependencies { | ||
yamlRestTestImplementation "org.elasticsearch.test:framework:7.14.0" | ||
restTestSpecs "org.elasticsearch:rest-api-spec:7.14.0" | ||
} | ||
""" | ||
|
||
when: | ||
def result = gradleRunner("yamlRestTest").build() | ||
then: | ||
result.task(':compileYamlRestTestJava').outcome == TaskOutcome.NO_SOURCE | ||
result.task(':processYamlRestTestResources').outcome == TaskOutcome.NO_SOURCE | ||
result.task(':yamlRestTest').outcome == TaskOutcome.NO_SOURCE | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
build-tools/src/main/java/org/elasticsearch/gradle/test/YamlRestTestPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* 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 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 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.gradle.test; | ||
|
||
import org.elasticsearch.gradle.VersionProperties; | ||
import org.elasticsearch.gradle.plugin.PluginBuildPlugin; | ||
import org.elasticsearch.gradle.testclusters.ElasticsearchCluster; | ||
import org.elasticsearch.gradle.testclusters.StandaloneRestIntegTestTask; | ||
import org.elasticsearch.gradle.testclusters.TestClustersPlugin; | ||
import org.elasticsearch.gradle.transform.UnzipTransform; | ||
import org.gradle.api.Action; | ||
import org.gradle.api.NamedDomainObjectContainer; | ||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.Task; | ||
import org.gradle.api.artifacts.Configuration; | ||
import org.gradle.api.artifacts.ConfigurationContainer; | ||
import org.gradle.api.artifacts.dsl.DependencyHandler; | ||
import org.gradle.api.artifacts.type.ArtifactTypeDefinition; | ||
import org.gradle.api.attributes.Attribute; | ||
import org.gradle.api.internal.artifacts.ArtifactAttributes; | ||
import org.gradle.api.plugins.JavaBasePlugin; | ||
import org.gradle.api.tasks.Copy; | ||
import org.gradle.api.tasks.SourceSet; | ||
import org.gradle.api.tasks.SourceSetContainer; | ||
import org.gradle.api.tasks.TaskProvider; | ||
import org.gradle.api.tasks.bundling.Zip; | ||
|
||
import java.io.File; | ||
|
||
import static org.elasticsearch.gradle.plugin.PluginBuildPlugin.BUNDLE_PLUGIN_TASK_NAME; | ||
|
||
public class YamlRestTestPlugin implements Plugin<Project> { | ||
|
||
public static final String REST_TEST_SPECS_CONFIGURATION_NAME = "restTestSpecs"; | ||
public static final String YAML_REST_TEST = "yamlRestTest"; | ||
|
||
@Override | ||
public void apply(Project project) { | ||
project.getPluginManager().apply(GradleTestPolicySetupPlugin.class); | ||
project.getPluginManager().apply(TestClustersPlugin.class); | ||
project.getPluginManager().apply(JavaBasePlugin.class); | ||
|
||
Attribute<Boolean> restAttribute = Attribute.of("restSpecs", Boolean.class); | ||
project.getDependencies().getAttributesSchema().attribute(restAttribute); | ||
project.getDependencies().getArtifactTypes().maybeCreate(ArtifactTypeDefinition.JAR_TYPE); | ||
project.getDependencies().registerTransform(UnzipTransform.class, transformSpec -> { | ||
transformSpec.getFrom() | ||
.attribute(ArtifactAttributes.ARTIFACT_FORMAT, ArtifactTypeDefinition.JAR_TYPE) | ||
.attribute(restAttribute, true); | ||
transformSpec.getTo() | ||
.attribute(ArtifactAttributes.ARTIFACT_FORMAT, ArtifactTypeDefinition.DIRECTORY_TYPE) | ||
.attribute(restAttribute, true); | ||
}); | ||
|
||
ConfigurationContainer configurations = project.getConfigurations(); | ||
Configuration restTestSpecs = configurations.create(REST_TEST_SPECS_CONFIGURATION_NAME); | ||
restTestSpecs.getAttributes().attribute(ArtifactAttributes.ARTIFACT_FORMAT, ArtifactTypeDefinition.DIRECTORY_TYPE); | ||
restTestSpecs.getAttributes().attribute(restAttribute, true); | ||
|
||
TaskProvider<Copy> copyRestTestSpecs = project.getTasks().register("copyRestTestSpecs", Copy.class, t -> { | ||
t.from(restTestSpecs); | ||
t.into(new File(project.getBuildDir(), "restResources/restspec")); | ||
}); | ||
|
||
var sourceSets = project.getExtensions().getByType(SourceSetContainer.class); | ||
var testSourceSet = sourceSets.maybeCreate(YAML_REST_TEST); | ||
NamedDomainObjectContainer<ElasticsearchCluster> testClusters = (NamedDomainObjectContainer<ElasticsearchCluster>) project | ||
.getExtensions() | ||
.getByName(TestClustersPlugin.EXTENSION_NAME); | ||
|
||
testSourceSet.getOutput().dir(copyRestTestSpecs.map(Task::getOutputs)); | ||
Configuration yamlRestTestImplementation = configurations.getByName(testSourceSet.getImplementationConfigurationName()); | ||
setupDefaultDependencies(project.getDependencies(), restTestSpecs, yamlRestTestImplementation); | ||
var cluster = testClusters.maybeCreate(YAML_REST_TEST); | ||
TaskProvider<StandaloneRestIntegTestTask> yamlRestTestTask = setupTestTask(project, testSourceSet, cluster); | ||
project.getPlugins().withType(PluginBuildPlugin.class, p -> { | ||
TaskProvider<Zip> bundle = project.getTasks().withType(Zip.class).named(BUNDLE_PLUGIN_TASK_NAME); | ||
cluster.plugin(bundle.flatMap(Zip::getArchiveFile)); | ||
yamlRestTestTask.configure(t -> t.dependsOn(bundle)); | ||
}); | ||
} | ||
|
||
private static void setupDefaultDependencies( | ||
DependencyHandler dependencyHandler, | ||
Configuration restTestSpecs, | ||
Configuration yamlRestTestImplementation | ||
) { | ||
String elasticsearchVersion = VersionProperties.getElasticsearch(); | ||
yamlRestTestImplementation.defaultDependencies( | ||
deps -> deps.add(dependencyHandler.create("org.elasticsearch.test:framework:" + elasticsearchVersion)) | ||
); | ||
|
||
restTestSpecs.defaultDependencies( | ||
deps -> deps.add(dependencyHandler.create("org.elasticsearch:rest-api-spec:" + elasticsearchVersion)) | ||
); | ||
} | ||
|
||
private TaskProvider<StandaloneRestIntegTestTask> setupTestTask( | ||
Project project, | ||
SourceSet testSourceSet, | ||
ElasticsearchCluster cluster | ||
) { | ||
return project.getTasks().register("yamlRestTest", StandaloneRestIntegTestTask.class, task -> { | ||
task.useCluster(cluster); | ||
task.setTestClassesDirs(testSourceSet.getOutput().getClassesDirs()); | ||
task.setClasspath(testSourceSet.getRuntimeClasspath()); | ||
|
||
var nonInputProperties = new SystemPropertyCommandLineArgumentProvider(); | ||
nonInputProperties.systemProperty("tests.rest.cluster", () -> String.join(",", cluster.getAllHttpSocketURI())); | ||
nonInputProperties.systemProperty("tests.cluster", () -> String.join(",", cluster.getAllTransportPortURI())); | ||
nonInputProperties.systemProperty("tests.clustername", () -> cluster.getName()); | ||
task.getJvmArgumentProviders().add(nonInputProperties); | ||
task.systemProperty("tests.rest.load_packaged", Boolean.FALSE.toString()); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.