|
| 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.gradle.api.NamedDomainObjectContainer; |
| 17 | +import org.gradle.api.Plugin; |
| 18 | +import org.gradle.api.Project; |
| 19 | +import org.gradle.api.plugins.JavaBasePlugin; |
| 20 | +import org.gradle.api.tasks.SourceSetContainer; |
| 21 | +import org.gradle.api.tasks.TaskProvider; |
| 22 | +import org.gradle.api.tasks.bundling.Zip; |
| 23 | +import org.gradle.language.base.plugins.LifecycleBasePlugin; |
| 24 | + |
| 25 | +import static org.elasticsearch.gradle.plugin.PluginBuildPlugin.BUNDLE_PLUGIN_TASK_NAME; |
| 26 | + |
| 27 | +public class JavaRestTestPlugin implements Plugin<Project> { |
| 28 | + |
| 29 | + public static final String JAVA_REST_TEST = "javaRestTest"; |
| 30 | + |
| 31 | + @Override |
| 32 | + public void apply(Project project) { |
| 33 | + project.getPluginManager().apply(GradleTestPolicySetupPlugin.class); |
| 34 | + project.getPluginManager().apply(TestClustersPlugin.class); |
| 35 | + project.getPluginManager().apply(JavaBasePlugin.class); |
| 36 | + |
| 37 | + // Setup source set and dependencies |
| 38 | + var sourceSets = project.getExtensions().getByType(SourceSetContainer.class); |
| 39 | + var testSourceSet = sourceSets.maybeCreate(JAVA_REST_TEST); |
| 40 | + var javaRestTestImplementation = project.getConfigurations().getByName(testSourceSet.getImplementationConfigurationName()); |
| 41 | + |
| 42 | + String elasticsearchVersion = VersionProperties.getElasticsearch(); |
| 43 | + javaRestTestImplementation.defaultDependencies( |
| 44 | + deps -> deps.add(project.getDependencies().create("org.elasticsearch.test:framework:" + elasticsearchVersion)) |
| 45 | + ); |
| 46 | + |
| 47 | + // Register test cluster |
| 48 | + NamedDomainObjectContainer<ElasticsearchCluster> testClusters = (NamedDomainObjectContainer<ElasticsearchCluster>) project |
| 49 | + .getExtensions() |
| 50 | + .getByName(TestClustersPlugin.EXTENSION_NAME); |
| 51 | + var cluster = testClusters.maybeCreate(JAVA_REST_TEST); |
| 52 | + |
| 53 | + // Register test task |
| 54 | + TaskProvider<StandaloneRestIntegTestTask> javaRestTestTask = project.getTasks() |
| 55 | + .register(JAVA_REST_TEST, StandaloneRestIntegTestTask.class, task -> { |
| 56 | + task.useCluster(cluster); |
| 57 | + task.setTestClassesDirs(testSourceSet.getOutput().getClassesDirs()); |
| 58 | + task.setClasspath(testSourceSet.getRuntimeClasspath()); |
| 59 | + |
| 60 | + var nonInputProperties = new SystemPropertyCommandLineArgumentProvider(); |
| 61 | + nonInputProperties.systemProperty("tests.rest.cluster", () -> String.join(",", cluster.getAllHttpSocketURI())); |
| 62 | + nonInputProperties.systemProperty("tests.cluster", () -> String.join(",", cluster.getAllTransportPortURI())); |
| 63 | + nonInputProperties.systemProperty("tests.clustername", () -> cluster.getName()); |
| 64 | + task.getJvmArgumentProviders().add(nonInputProperties); |
| 65 | + }); |
| 66 | + |
| 67 | + // Register plugin bundle with test cluster |
| 68 | + project.getPlugins().withType(PluginBuildPlugin.class, p -> { |
| 69 | + TaskProvider<Zip> bundle = project.getTasks().withType(Zip.class).named(BUNDLE_PLUGIN_TASK_NAME); |
| 70 | + cluster.plugin(bundle.flatMap(Zip::getArchiveFile)); |
| 71 | + javaRestTestTask.configure(t -> t.dependsOn(bundle)); |
| 72 | + }); |
| 73 | + |
| 74 | + // Wire up to check task |
| 75 | + project.getTasks().named(LifecycleBasePlugin.CHECK_TASK_NAME).configure(check -> check.dependsOn(javaRestTestTask)); |
| 76 | + } |
| 77 | +} |
0 commit comments