From 392f99d69538f884b33ce1c06b1a0ef40de2303e Mon Sep 17 00:00:00 2001 From: Jake Landis Date: Fri, 18 Sep 2020 12:49:37 -0500 Subject: [PATCH] [7.x] Make yamlRestTest and javaRestTest tasks lazy (#62515) (#62632) This commit changes the yamlRestTest and javaRestTest tasks to be lazily created. This change requires pro-actively creating the testClusters container so that the configuration can be applied without any changes to the build.gradle files. related: #60261 related: #47804 --- .../gradle/test/RestTestBasePlugin.java | 2 +- .../gradle/test/rest/JavaRestTestPlugin.java | 13 +++-- .../gradle/test/rest/RestTestUtil.java | 58 ++++++++++--------- .../gradle/test/rest/YamlRestTestPlugin.java | 13 +++-- 4 files changed, 47 insertions(+), 39 deletions(-) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/test/RestTestBasePlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/test/RestTestBasePlugin.java index 04ffa4ed64d..439c3539776 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/test/RestTestBasePlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/test/RestTestBasePlugin.java @@ -41,7 +41,7 @@ public class RestTestBasePlugin implements Plugin { NamedDomainObjectContainer testClusters = (NamedDomainObjectContainer) project .getExtensions() .getByName(TestClustersPlugin.EXTENSION_NAME); - ElasticsearchCluster cluster = testClusters.create(restIntegTestTask.getName()); + ElasticsearchCluster cluster = testClusters.maybeCreate(restIntegTestTask.getName()); restIntegTestTask.useCluster(cluster); restIntegTestTask.include("**/*IT.class"); restIntegTestTask.systemProperty("tests.rest.load_packaged", Boolean.FALSE.toString()); diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/JavaRestTestPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/JavaRestTestPlugin.java index 911c186eb7d..b0a9a8d68f1 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/JavaRestTestPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/JavaRestTestPlugin.java @@ -27,12 +27,13 @@ import org.elasticsearch.gradle.util.GradleUtils; import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.plugins.JavaBasePlugin; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; +import static org.elasticsearch.gradle.test.rest.RestTestUtil.createTestCluster; +import static org.elasticsearch.gradle.test.rest.RestTestUtil.registerTask; import static org.elasticsearch.gradle.test.rest.RestTestUtil.setupDependencies; -import static org.elasticsearch.gradle.test.rest.RestTestUtil.setupRunnerTask; -import static org.elasticsearch.gradle.test.rest.RestTestUtil.setupTask; /** * Apply this plugin to run the Java based REST tests. @@ -52,11 +53,11 @@ public class JavaRestTestPlugin implements Plugin { SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); SourceSet javaTestSourceSet = sourceSets.create(SOURCE_SET_NAME); - // setup the javaRestTest task - RestIntegTestTask javaRestTestTask = setupTask(project, SOURCE_SET_NAME); + // create the test cluster container + createTestCluster(project, javaTestSourceSet); - // setup the runner task - setupRunnerTask(project, javaRestTestTask, javaTestSourceSet); + // setup the javaRestTest task + Provider javaRestTestTask = registerTask(project, javaTestSourceSet); // setup dependencies setupDependencies(project, javaTestSourceSet); diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/RestTestUtil.java b/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/RestTestUtil.java index 5afb66c0343..1667d674870 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/RestTestUtil.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/RestTestUtil.java @@ -22,8 +22,12 @@ package org.elasticsearch.gradle.test.rest; import org.elasticsearch.gradle.VersionProperties; import org.elasticsearch.gradle.info.BuildParams; import org.elasticsearch.gradle.test.RestIntegTestTask; +import org.elasticsearch.gradle.testclusters.ElasticsearchCluster; +import org.elasticsearch.gradle.testclusters.TestClustersPlugin; +import org.gradle.api.NamedDomainObjectContainer; import org.gradle.api.Project; import org.gradle.api.plugins.JavaBasePlugin; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.bundling.Zip; @@ -34,37 +38,39 @@ public class RestTestUtil { private RestTestUtil() {} - /** - * Creates a task with the source set name of type {@link RestIntegTestTask} - */ - static RestIntegTestTask setupTask(Project project, String sourceSetName) { - // create task - note can not use .register due to the work in RestIntegTestTask's constructor :( - // see: https://github.com/elastic/elasticsearch/issues/47804 - RestIntegTestTask testTask = project.getTasks().create(sourceSetName, RestIntegTestTask.class); - testTask.setGroup(JavaBasePlugin.VERIFICATION_GROUP); - testTask.setDescription("Runs the REST tests against an external cluster"); - // make the new test run after unit tests - testTask.mustRunAfter(project.getTasks().named("test")); - return testTask; + static ElasticsearchCluster createTestCluster(Project project, SourceSet sourceSet) { + // eagerly create the testCluster container so it is easily available for configuration + @SuppressWarnings("unchecked") + NamedDomainObjectContainer testClusters = (NamedDomainObjectContainer) project + .getExtensions() + .getByName(TestClustersPlugin.EXTENSION_NAME); + return testClusters.create(sourceSet.getName()); } /** - * Creates the runner task and configures the test clusters + * Creates a task with the source set name of type {@link RestIntegTestTask} */ - static void setupRunnerTask(Project project, RestIntegTestTask testTask, SourceSet sourceSet) { - testTask.setTestClassesDirs(sourceSet.getOutput().getClassesDirs()); - testTask.setClasspath(sourceSet.getRuntimeClasspath()); - - // if this a module or plugin, it may have an associated zip file with it's contents, add that to the test cluster - project.getPluginManager().withPlugin("elasticsearch.esplugin", plugin -> { - Zip bundle = (Zip) project.getTasks().getByName("bundlePlugin"); - testTask.dependsOn(bundle); - if (project.getPath().contains("modules:")) { - testTask.getClusters().forEach(c -> c.module(bundle.getArchiveFile())); - } else { - testTask.getClusters().forEach(c -> c.plugin(project.getObjects().fileProperty().value(bundle.getArchiveFile()))); - } + static Provider registerTask(Project project, SourceSet sourceSet) { + // lazily create the test task + Provider testProvider = project.getTasks().register(sourceSet.getName(), RestIntegTestTask.class, testTask -> { + testTask.setGroup(JavaBasePlugin.VERIFICATION_GROUP); + testTask.setDescription("Runs the REST tests against an external cluster"); + testTask.mustRunAfter(project.getTasks().named("test")); + testTask.setTestClassesDirs(sourceSet.getOutput().getClassesDirs()); + testTask.setClasspath(sourceSet.getRuntimeClasspath()); + // if this a module or plugin, it may have an associated zip file with it's contents, add that to the test cluster + project.getPluginManager().withPlugin("elasticsearch.esplugin", plugin -> { + Zip bundle = (Zip) project.getTasks().getByName("bundlePlugin"); + testTask.dependsOn(bundle); + if (project.getPath().contains("modules:")) { + testTask.getClusters().forEach(c -> c.module(bundle.getArchiveFile())); + } else { + testTask.getClusters().forEach(c -> c.plugin(project.getObjects().fileProperty().value(bundle.getArchiveFile()))); + } + }); }); + + return testProvider; } /** diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/YamlRestTestPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/YamlRestTestPlugin.java index 588d65d1fba..7f4f974c6c1 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/YamlRestTestPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/YamlRestTestPlugin.java @@ -27,12 +27,13 @@ import org.elasticsearch.gradle.util.GradleUtils; import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.plugins.JavaBasePlugin; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; +import static org.elasticsearch.gradle.test.rest.RestTestUtil.createTestCluster; +import static org.elasticsearch.gradle.test.rest.RestTestUtil.registerTask; import static org.elasticsearch.gradle.test.rest.RestTestUtil.setupDependencies; -import static org.elasticsearch.gradle.test.rest.RestTestUtil.setupRunnerTask; -import static org.elasticsearch.gradle.test.rest.RestTestUtil.setupTask; /** * Apply this plugin to run the YAML based REST tests. @@ -53,11 +54,11 @@ public class YamlRestTestPlugin implements Plugin { SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); SourceSet yamlTestSourceSet = sourceSets.create(SOURCE_SET_NAME); - // setup the yamlRestTest task - RestIntegTestTask yamlRestTestTask = setupTask(project, SOURCE_SET_NAME); + // create the test cluster container + createTestCluster(project, yamlTestSourceSet); - // setup the runner task - setupRunnerTask(project, yamlRestTestTask, yamlTestSourceSet); + // setup the yamlRestTest task + Provider yamlRestTestTask = registerTask(project, yamlTestSourceSet); // setup the dependencies setupDependencies(project, yamlTestSourceSet);