|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.gradle.test; |
| 10 | + |
| 11 | +import org.elasticsearch.gradle.VersionProperties; |
| 12 | +import org.elasticsearch.gradle.plugin.PluginBuildPlugin; |
| 13 | +import org.elasticsearch.gradle.testclusters.ElasticsearchCluster; |
| 14 | +import org.elasticsearch.gradle.testclusters.StandaloneRestIntegTestTask; |
| 15 | +import org.elasticsearch.gradle.testclusters.TestClustersPlugin; |
| 16 | +import org.elasticsearch.gradle.transform.UnzipTransform; |
| 17 | +import org.gradle.api.Action; |
| 18 | +import org.gradle.api.NamedDomainObjectContainer; |
| 19 | +import org.gradle.api.Plugin; |
| 20 | +import org.gradle.api.Project; |
| 21 | +import org.gradle.api.Task; |
| 22 | +import org.gradle.api.artifacts.Configuration; |
| 23 | +import org.gradle.api.artifacts.ConfigurationContainer; |
| 24 | +import org.gradle.api.artifacts.dsl.DependencyHandler; |
| 25 | +import org.gradle.api.artifacts.type.ArtifactTypeDefinition; |
| 26 | +import org.gradle.api.attributes.Attribute; |
| 27 | +import org.gradle.api.internal.artifacts.ArtifactAttributes; |
| 28 | +import org.gradle.api.plugins.JavaBasePlugin; |
| 29 | +import org.gradle.api.tasks.Copy; |
| 30 | +import org.gradle.api.tasks.SourceSet; |
| 31 | +import org.gradle.api.tasks.SourceSetContainer; |
| 32 | +import org.gradle.api.tasks.TaskProvider; |
| 33 | +import org.gradle.api.tasks.bundling.Zip; |
| 34 | + |
| 35 | +import java.io.File; |
| 36 | + |
| 37 | +import static org.elasticsearch.gradle.plugin.PluginBuildPlugin.BUNDLE_PLUGIN_TASK_NAME; |
| 38 | + |
| 39 | +public class YamlRestTestPlugin implements Plugin<Project> { |
| 40 | + |
| 41 | + public static final String REST_TEST_SPECS_CONFIGURATION_NAME = "restTestSpecs"; |
| 42 | + public static final String YAML_REST_TEST = "yamlRestTest"; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void apply(Project project) { |
| 46 | + project.getPluginManager().apply(GradleTestPolicySetupPlugin.class); |
| 47 | + project.getPluginManager().apply(TestClustersPlugin.class); |
| 48 | + project.getPluginManager().apply(JavaBasePlugin.class); |
| 49 | + |
| 50 | + Attribute<Boolean> restAttribute = Attribute.of("restSpecs", Boolean.class); |
| 51 | + project.getDependencies().getAttributesSchema().attribute(restAttribute); |
| 52 | + project.getDependencies().getArtifactTypes().maybeCreate(ArtifactTypeDefinition.JAR_TYPE); |
| 53 | + project.getDependencies().registerTransform(UnzipTransform.class, transformSpec -> { |
| 54 | + transformSpec.getFrom() |
| 55 | + .attribute(ArtifactAttributes.ARTIFACT_FORMAT, ArtifactTypeDefinition.JAR_TYPE) |
| 56 | + .attribute(restAttribute, true); |
| 57 | + transformSpec.getTo() |
| 58 | + .attribute(ArtifactAttributes.ARTIFACT_FORMAT, ArtifactTypeDefinition.DIRECTORY_TYPE) |
| 59 | + .attribute(restAttribute, true); |
| 60 | + }); |
| 61 | + |
| 62 | + ConfigurationContainer configurations = project.getConfigurations(); |
| 63 | + Configuration restTestSpecs = configurations.create(REST_TEST_SPECS_CONFIGURATION_NAME); |
| 64 | + restTestSpecs.getAttributes().attribute(ArtifactAttributes.ARTIFACT_FORMAT, ArtifactTypeDefinition.DIRECTORY_TYPE); |
| 65 | + restTestSpecs.getAttributes().attribute(restAttribute, true); |
| 66 | + |
| 67 | + TaskProvider<Copy> copyRestTestSpecs = project.getTasks().register("copyRestTestSpecs", Copy.class, t -> { |
| 68 | + t.from(restTestSpecs); |
| 69 | + t.into(new File(project.getBuildDir(), "restResources/restspec")); |
| 70 | + }); |
| 71 | + |
| 72 | + var sourceSets = project.getExtensions().getByType(SourceSetContainer.class); |
| 73 | + var testSourceSet = sourceSets.maybeCreate(YAML_REST_TEST); |
| 74 | + NamedDomainObjectContainer<ElasticsearchCluster> testClusters = (NamedDomainObjectContainer<ElasticsearchCluster>) project |
| 75 | + .getExtensions() |
| 76 | + .getByName(TestClustersPlugin.EXTENSION_NAME); |
| 77 | + |
| 78 | + testSourceSet.getOutput().dir(copyRestTestSpecs.map(Task::getOutputs)); |
| 79 | + Configuration yamlRestTestImplementation = configurations.getByName(testSourceSet.getImplementationConfigurationName()); |
| 80 | + setupDefaultDependencies(project.getDependencies(), restTestSpecs, yamlRestTestImplementation); |
| 81 | + var cluster = testClusters.maybeCreate(YAML_REST_TEST); |
| 82 | + TaskProvider<StandaloneRestIntegTestTask> yamlRestTestTask = setupTestTask(project, testSourceSet, cluster); |
| 83 | + project.getPlugins().withType(PluginBuildPlugin.class, p -> { |
| 84 | + TaskProvider<Zip> bundle = project.getTasks().withType(Zip.class).named(BUNDLE_PLUGIN_TASK_NAME); |
| 85 | + cluster.plugin(bundle.flatMap(Zip::getArchiveFile)); |
| 86 | + yamlRestTestTask.configure(t -> t.dependsOn(bundle)); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + private static void setupDefaultDependencies( |
| 91 | + DependencyHandler dependencyHandler, |
| 92 | + Configuration restTestSpecs, |
| 93 | + Configuration yamlRestTestImplementation |
| 94 | + ) { |
| 95 | + String elasticsearchVersion = VersionProperties.getElasticsearch(); |
| 96 | + yamlRestTestImplementation.defaultDependencies( |
| 97 | + deps -> deps.add(dependencyHandler.create("org.elasticsearch.test:framework:" + elasticsearchVersion)) |
| 98 | + ); |
| 99 | + |
| 100 | + restTestSpecs.defaultDependencies( |
| 101 | + deps -> deps.add(dependencyHandler.create("org.elasticsearch:rest-api-spec:" + elasticsearchVersion)) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + private TaskProvider<StandaloneRestIntegTestTask> setupTestTask( |
| 106 | + Project project, |
| 107 | + SourceSet testSourceSet, |
| 108 | + ElasticsearchCluster cluster |
| 109 | + ) { |
| 110 | + return project.getTasks().register("yamlRestTest", StandaloneRestIntegTestTask.class, task -> { |
| 111 | + task.useCluster(cluster); |
| 112 | + task.setTestClassesDirs(testSourceSet.getOutput().getClassesDirs()); |
| 113 | + task.setClasspath(testSourceSet.getRuntimeClasspath()); |
| 114 | + |
| 115 | + var nonInputProperties = new SystemPropertyCommandLineArgumentProvider(); |
| 116 | + nonInputProperties.systemProperty("tests.rest.cluster", () -> String.join(",", cluster.getAllHttpSocketURI())); |
| 117 | + nonInputProperties.systemProperty("tests.cluster", () -> String.join(",", cluster.getAllTransportPortURI())); |
| 118 | + nonInputProperties.systemProperty("tests.clustername", () -> cluster.getName()); |
| 119 | + task.getJvmArgumentProviders().add(nonInputProperties); |
| 120 | + task.systemProperty("tests.rest.load_packaged", Boolean.FALSE.toString()); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments