Merge pull request #15210 from rjernst/buildsrc_test_cleanup

Cleanup integ test classes in buildSrc to be less groovyish
This commit is contained in:
Ryan Ernst 2015-12-04 11:24:21 -08:00
commit a6752c4da9
8 changed files with 102 additions and 115 deletions

View File

@ -23,18 +23,19 @@ import org.elasticsearch.gradle.test.RestIntegTestTask
import org.elasticsearch.gradle.test.RunTask import org.elasticsearch.gradle.test.RunTask
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.Task import org.gradle.api.Task
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.bundling.Zip import org.gradle.api.tasks.bundling.Zip
/** /**
* Encapsulates build configuration for an Elasticsearch plugin. * Encapsulates build configuration for an Elasticsearch plugin.
*/ */
class PluginBuildPlugin extends BuildPlugin { public class PluginBuildPlugin extends BuildPlugin {
@Override @Override
void apply(Project project) { public void apply(Project project) {
super.apply(project) super.apply(project)
configureDependencies(project) configureDependencies(project)
// this afterEvaluate must happen before the afterEvaluate added by integTest configure, // this afterEvaluate must happen before the afterEvaluate added by integTest creation,
// so that the file name resolution for installing the plugin will be setup // so that the file name resolution for installing the plugin will be setup
project.afterEvaluate { project.afterEvaluate {
String name = project.pluginProperties.extension.name String name = project.pluginProperties.extension.name
@ -45,18 +46,12 @@ class PluginBuildPlugin extends BuildPlugin {
project.tasks.run.dependsOn(project.bundlePlugin) project.tasks.run.dependsOn(project.bundlePlugin)
project.tasks.run.clusterConfig.plugin(name, project.bundlePlugin.outputs.files) project.tasks.run.clusterConfig.plugin(name, project.bundlePlugin.outputs.files)
} }
RestIntegTestTask.configure(project) createIntegTestTask(project)
RunTask.configure(project) createBundleTask(project)
Task bundle = configureBundleTask(project) project.tasks.create('run', RunTask) // allow running ES with this plugin in the foreground of a build
project.configurations.archives.artifacts.removeAll { it.archiveTask.is project.jar }
project.configurations.getByName('default').extendsFrom = []
project.artifacts {
archives bundle
'default' bundle
}
} }
static void configureDependencies(Project project) { private static void configureDependencies(Project project) {
project.dependencies { project.dependencies {
provided "org.elasticsearch:elasticsearch:${project.versions.elasticsearch}" provided "org.elasticsearch:elasticsearch:${project.versions.elasticsearch}"
testCompile "org.elasticsearch:test-framework:${project.versions.elasticsearch}" testCompile "org.elasticsearch:test-framework:${project.versions.elasticsearch}"
@ -72,21 +67,36 @@ class PluginBuildPlugin extends BuildPlugin {
} }
} }
static Task configureBundleTask(Project project) { /** Adds an integTest task which runs rest tests */
PluginPropertiesTask buildProperties = project.tasks.create(name: 'pluginProperties', type: PluginPropertiesTask) private static void createIntegTestTask(Project project) {
File pluginMetadata = project.file("src/main/plugin-metadata") RestIntegTestTask integTest = project.tasks.create('integTest', RestIntegTestTask.class)
project.sourceSets.test { integTest.mustRunAfter(project.precommit, project.test)
output.dir(buildProperties.generatedResourcesDir, builtBy: 'pluginProperties') project.check.dependsOn(integTest)
resources { }
srcDir pluginMetadata
} /**
} * Adds a bundlePlugin task which builds the zip containing the plugin jars,
Task bundle = project.tasks.create(name: 'bundlePlugin', type: Zip, dependsOn: [project.jar, buildProperties]) * metadata, properties, and packaging files
bundle.configure { */
from buildProperties private static void createBundleTask(Project project) {
from pluginMetadata File pluginMetadata = project.file('src/main/plugin-metadata')
from project.jar
from bundle.project.configurations.runtime - bundle.project.configurations.provided // create a task to build the properties file for this plugin
PluginPropertiesTask buildProperties = project.tasks.create('pluginProperties', PluginPropertiesTask.class)
// add the plugin properties and metadata to test resources, so unit tests can
// know about the plugin (used by test security code to statically initialize the plugin in unit tests)
SourceSet testSourceSet = project.sourceSets.test
testSourceSet.output.dir(buildProperties.generatedResourcesDir, builtBy: 'pluginProperties')
testSourceSet.resources.srcDir(pluginMetadata)
// create the actual bundle task, which zips up all the files for the plugin
Zip bundle = project.tasks.create(name: 'bundlePlugin', type: Zip, dependsOn: [project.jar, buildProperties]) {
from buildProperties // plugin properties file
from pluginMetadata // metadata (eg custom security policy)
from project.jar // this plugin's jar
from project.configurations.runtime - project.configurations.provided // the dep jars
// extra files for the plugin to go into the zip
from('src/main/packaging') // TODO: move all config/bin/_size/etc into packaging from('src/main/packaging') // TODO: move all config/bin/_size/etc into packaging
from('src/main') { from('src/main') {
include 'config/**' include 'config/**'
@ -97,6 +107,13 @@ class PluginBuildPlugin extends BuildPlugin {
} }
} }
project.assemble.dependsOn(bundle) project.assemble.dependsOn(bundle)
return bundle
// remove jar from the archives (things that will be published), and set it to the zip
project.configurations.archives.artifacts.removeAll { it.archiveTask.is project.jar }
project.artifacts.add('archives', bundle)
// also make the zip the default artifact (used when depending on this project)
project.configurations.getByName('default').extendsFrom = []
project.artifacts.add('default', bundle)
} }
} }

View File

@ -31,55 +31,38 @@ import org.gradle.util.ConfigureUtil
* Runs integration tests, but first starts an ES cluster, * Runs integration tests, but first starts an ES cluster,
* and passes the ES cluster info as parameters to the tests. * and passes the ES cluster info as parameters to the tests.
*/ */
class RestIntegTestTask extends RandomizedTestingTask { public class RestIntegTestTask extends RandomizedTestingTask {
ClusterConfiguration clusterConfig = new ClusterConfiguration() ClusterConfiguration clusterConfig = new ClusterConfiguration()
/** Flag indicating whether the rest tests in the rest spec should be run. */
@Input @Input
boolean includePackaged = false boolean includePackaged = false
static RestIntegTestTask configure(Project project) { public RestIntegTestTask() {
Map integTestOptions = [ description = 'Runs rest tests against an elasticsearch cluster.'
name: 'integTest', group = JavaBasePlugin.VERIFICATION_GROUP
type: RestIntegTestTask, dependsOn(project.testClasses)
dependsOn: 'testClasses', classpath = project.sourceSets.test.runtimeClasspath
group: JavaBasePlugin.VERIFICATION_GROUP, testClassesDir = project.sourceSets.test.output.classesDir
description: 'Runs rest tests against an elasticsearch cluster.'
] // start with the common test configuration
RestIntegTestTask integTest = project.tasks.create(integTestOptions) configure(BuildPlugin.commonTestConfig(project))
integTest.configure(BuildPlugin.commonTestConfig(project)) // override/add more for rest tests
integTest.configure { parallelism = '1'
include '**/*IT.class' include('**/*IT.class')
systemProperty 'tests.rest.load_packaged', 'false' systemProperty('tests.rest.load_packaged', 'false')
}
RandomizedTestingTask test = project.tasks.findByName('test') // copy the rest spec/tests into the test resources
if (test != null) {
integTest.classpath = test.classpath
integTest.testClassesDir = test.testClassesDir
integTest.mustRunAfter(test)
}
integTest.mustRunAfter(project.precommit)
project.check.dependsOn(integTest)
RestSpecHack.configureDependencies(project) RestSpecHack.configureDependencies(project)
project.afterEvaluate { project.afterEvaluate {
integTest.dependsOn(RestSpecHack.configureTask(project, integTest.includePackaged)) dependsOn(RestSpecHack.configureTask(project, includePackaged))
systemProperty('tests.cluster', "localhost:${clusterConfig.baseTransportPort}")
} }
return integTest
}
RestIntegTestTask() {
// this must run after all projects have been configured, so we know any project // this must run after all projects have been configured, so we know any project
// references can be accessed as a fully configured // references can be accessed as a fully configured
project.gradle.projectsEvaluated { project.gradle.projectsEvaluated {
Task test = project.tasks.findByName('test')
if (test != null) {
mustRunAfter(test)
}
ClusterFormationTasks.setup(project, this, clusterConfig) ClusterFormationTasks.setup(project, this, clusterConfig)
configure {
parallelism '1'
systemProperty 'tests.cluster', "localhost:${clusterConfig.baseTransportPort}"
}
} }
} }
@ -92,11 +75,11 @@ class RestIntegTestTask extends RandomizedTestingTask {
} }
@Input @Input
void cluster(Closure closure) { public void cluster(Closure closure) {
ConfigureUtil.configure(closure, clusterConfig) ConfigureUtil.configure(closure, clusterConfig)
} }
ClusterConfiguration getCluster() { public ClusterConfiguration getCluster() {
return clusterConfig return clusterConfig
} }
} }

View File

@ -28,12 +28,12 @@ import org.gradle.api.tasks.Copy
* currently must be available on the local filesystem. This class encapsulates * currently must be available on the local filesystem. This class encapsulates
* setting up tasks to copy the rest spec api to test resources. * setting up tasks to copy the rest spec api to test resources.
*/ */
class RestSpecHack { public class RestSpecHack {
/** /**
* Sets dependencies needed to copy the rest spec. * Sets dependencies needed to copy the rest spec.
* @param project The project to add rest spec dependency to * @param project The project to add rest spec dependency to
*/ */
static void configureDependencies(Project project) { public static void configureDependencies(Project project) {
project.configurations { project.configurations {
restSpec restSpec
} }
@ -48,7 +48,7 @@ class RestSpecHack {
* @param project The project to add the copy task to * @param project The project to add the copy task to
* @param includePackagedTests true if the packaged tests should be copied, false otherwise * @param includePackagedTests true if the packaged tests should be copied, false otherwise
*/ */
static Task configureTask(Project project, boolean includePackagedTests) { public static Task configureTask(Project project, boolean includePackagedTests) {
Map copyRestSpecProps = [ Map copyRestSpecProps = [
name : 'copyRestSpec', name : 'copyRestSpec',
type : Copy, type : Copy,
@ -65,7 +65,6 @@ class RestSpecHack {
project.idea { project.idea {
module { module {
if (scopes.TEST != null) { if (scopes.TEST != null) {
// TODO: need to add the TEST scope somehow for rest test plugin...
scopes.TEST.plus.add(project.configurations.restSpec) scopes.TEST.plus.add(project.configurations.restSpec)
} }
} }

View File

@ -18,22 +18,18 @@
*/ */
package org.elasticsearch.gradle.test package org.elasticsearch.gradle.test
import com.carrotsearch.gradle.junit4.RandomizedTestingTask
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
/** Configures the build to have a rest integration test. */ /** A plugin to add rest integration tests. Used for qa projects. */
class RestTestPlugin implements Plugin<Project> { public class RestTestPlugin implements Plugin<Project> {
@Override @Override
void apply(Project project) { public void apply(Project project) {
project.pluginManager.apply(StandaloneTestBasePlugin) project.pluginManager.apply(StandaloneTestBasePlugin)
RandomizedTestingTask integTest = RestIntegTestTask.configure(project) RestIntegTestTask integTest = project.tasks.create('integTest', RestIntegTestTask.class)
RestSpecHack.configureDependencies(project) integTest.mustRunAfter(project.precommit)
integTest.configure { project.check.dependsOn(integTest)
classpath = project.sourceSets.test.runtimeClasspath
testClassesDir project.sourceSets.test.output.classesDir
}
} }
} }

View File

@ -2,13 +2,17 @@ package org.elasticsearch.gradle.test
import org.gradle.api.DefaultTask import org.gradle.api.DefaultTask
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.internal.tasks.options.Option import org.gradle.api.internal.tasks.options.Option
import org.gradle.util.ConfigureUtil
class RunTask extends DefaultTask { public class RunTask extends DefaultTask {
ClusterConfiguration clusterConfig = new ClusterConfiguration(baseHttpPort: 9200, baseTransportPort: 9300, daemonize: false) ClusterConfiguration clusterConfig = new ClusterConfiguration(baseHttpPort: 9200, baseTransportPort: 9300, daemonize: false)
RunTask() { public RunTask() {
description = "Runs elasticsearch with '${project.path}'"
group = 'Verification'
project.afterEvaluate { project.afterEvaluate {
ClusterFormationTasks.setup(project, this, clusterConfig) ClusterFormationTasks.setup(project, this, clusterConfig)
} }
@ -22,11 +26,10 @@ class RunTask extends DefaultTask {
clusterConfig.debug = enabled; clusterConfig.debug = enabled;
} }
static void configure(Project project) { /** Configure the cluster that will be run. */
RunTask task = project.tasks.create( @Override
name: 'run', public Task configure(Closure closure) {
type: RunTask, ConfigureUtil.configure(closure, clusterConfig)
description: "Runs elasticsearch with '${project.path}'", return this
group: 'Verification')
} }
} }

View File

@ -27,35 +27,26 @@ import org.elasticsearch.gradle.precommit.PrecommitTasks
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.plugins.JavaBasePlugin import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.plugins.ide.eclipse.model.EclipseClasspath
/** Configures the build to have a rest integration test. */ /** Configures the build to have a rest integration test. */
class StandaloneTestBasePlugin implements Plugin<Project> { public class StandaloneTestBasePlugin implements Plugin<Project> {
@Override @Override
void apply(Project project) { public void apply(Project project) {
project.pluginManager.apply(JavaBasePlugin) project.pluginManager.apply(JavaBasePlugin)
project.pluginManager.apply(RandomizedTestingPlugin) project.pluginManager.apply(RandomizedTestingPlugin)
BuildPlugin.globalBuildInfo(project) BuildPlugin.globalBuildInfo(project)
BuildPlugin.configureRepositories(project) BuildPlugin.configureRepositories(project)
// remove some unnecessary tasks for a qa test
project.tasks.removeAll { it.name in ['assemble', 'buildDependents'] }
// only setup tests to build // only setup tests to build
project.sourceSets { project.sourceSets.create('test')
test project.dependencies.add('testCompile', "org.elasticsearch:test-framework:${VersionProperties.elasticsearch}")
}
project.dependencies { project.eclipse.classpath.sourceSets = [project.sourceSets.test]
testCompile "org.elasticsearch:test-framework:${VersionProperties.elasticsearch}" project.eclipse.classpath.plusConfigurations = [project.configurations.testRuntime]
}
project.eclipse {
classpath {
sourceSets = [project.sourceSets.test]
plusConfigurations = [project.configurations.testRuntime]
}
}
PrecommitTasks.create(project, false) PrecommitTasks.create(project, false)
project.check.dependsOn(project.precommit) project.check.dependsOn(project.precommit)
} }

View File

@ -25,11 +25,11 @@ import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.plugins.JavaBasePlugin import org.gradle.api.plugins.JavaBasePlugin
/** Configures the build to have only unit tests. */ /** A plugin to add tests only. Used for QA tests that run arbitrary unit tests. */
class StandaloneTestPlugin implements Plugin<Project> { public class StandaloneTestPlugin implements Plugin<Project> {
@Override @Override
void apply(Project project) { public void apply(Project project) {
project.pluginManager.apply(StandaloneTestBasePlugin) project.pluginManager.apply(StandaloneTestBasePlugin)
Map testOptions = [ Map testOptions = [
@ -41,10 +41,8 @@ class StandaloneTestPlugin implements Plugin<Project> {
] ]
RandomizedTestingTask test = project.tasks.create(testOptions) RandomizedTestingTask test = project.tasks.create(testOptions)
test.configure(BuildPlugin.commonTestConfig(project)) test.configure(BuildPlugin.commonTestConfig(project))
test.configure { test.classpath = project.sourceSets.test.runtimeClasspath
classpath = project.sourceSets.test.runtimeClasspath test.testClassesDir project.sourceSets.test.output.classesDir
testClassesDir project.sourceSets.test.output.classesDir
}
test.mustRunAfter(project.precommit) test.mustRunAfter(project.precommit)
project.check.dependsOn(test) project.check.dependsOn(test)
} }

View File

@ -51,7 +51,7 @@ subprojects {
* Rest test config * * Rest test config *
*****************************************************************************/ *****************************************************************************/
apply plugin: 'elasticsearch.rest-test' apply plugin: 'elasticsearch.rest-test'
integTest { project.integTest {
includePackaged true includePackaged true
} }
@ -305,7 +305,7 @@ task updateShas(type: UpdateShasTask) {
parentTask = dependencyLicenses parentTask = dependencyLicenses
} }
RunTask.configure(project) task run(type: RunTask) {}
/** /**
* Build some variables that are replaced in the packages. This includes both * Build some variables that are replaced in the packages. This includes both