Converting randomized testing to create a separate unitTest task instead of replacing the builtin test task (#36311)

- Create a separate unitTest task instead of Gradle's built in 
- convert all configuration to use the new task 
- the  built in task is now disabled
This commit is contained in:
Alpar Torok 2018-12-19 08:25:20 +02:00 committed by GitHub
parent 8015560bd4
commit e9ef5bdce8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
67 changed files with 87 additions and 122 deletions

View File

@ -24,7 +24,7 @@ mainClassName = 'org.openjdk.jmh.Main'
assemble.enabled = false
archivesBaseName = 'elasticsearch-benchmarks'
test.enabled = false
unitTest.enabled = false
dependencies {
compile("org.elasticsearch:elasticsearch:${version}") {

View File

@ -179,9 +179,7 @@ if (project != rootProject) {
jarHell.enabled = false
thirdPartyAudit.enabled = false
test {
include "**/*Tests.class"
exclude "**/*IT.class"
unitTest {
// The test task is configured to runtimeJava version, but build-tools doesn't support all of them, so test
// with compiler instead on the ones that are too old.
if (project.runtimeJavaVersion <= JavaVersion.VERSION_1_10) {

View File

@ -4,30 +4,14 @@ import com.carrotsearch.ant.tasks.junit4.JUnit4
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.UnknownTaskException
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.tasks.TaskContainer
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.testing.Test
class RandomizedTestingPlugin implements Plugin<Project> {
void apply(Project project) {
setupSeed(project)
replaceTestTask(project.tasks)
createUnitTestTask(project.tasks)
configureAnt(project.ant)
configureSanityCheck(project)
}
private static void configureSanityCheck(Project project) {
// Check the task graph to confirm tasks were indeed replaced
// https://github.com/elastic/elasticsearch/issues/31324
project.rootProject.getGradle().getTaskGraph().whenReady {
Task test = project.getTasks().findByName("test")
if (test != null && (test instanceof RandomizedTestingTask) == false) {
throw new IllegalStateException("Test task was not replaced in project ${project.path}. Found ${test.getClass()}")
}
}
}
/**
@ -57,35 +41,15 @@ class RandomizedTestingPlugin implements Plugin<Project> {
}
}
static void replaceTestTask(TaskContainer tasks) {
// Gradle 4.8 introduced lazy tasks, thus we deal both with the `test` task as well as it's provider
// https://github.com/gradle/gradle/issues/5730#issuecomment-398822153
// since we can't be sure if the task was ever realized, we remove both the provider and the task
TaskProvider<Test> oldTestProvider
try {
oldTestProvider = tasks.named('test')
} catch (UnknownTaskException unused) {
// no test task, ok, user will use testing task on their own
return
static void createUnitTestTask(TaskContainer tasks) {
// only create a unitTest task if the `test` task exists as some project don't make use of it.
tasks.matching { it.name == "test" }.all {
// We don't want to run any tests with the Gradle test runner since we add our own randomized runner
it.enabled = false
RandomizedTestingTask unitTest = tasks.create('unitTest', RandomizedTestingTask)
unitTest.description = 'Runs unit tests with the randomized testing framework'
it.dependsOn unitTest
}
Test oldTestTask = oldTestProvider.get()
// we still have to use replace here despite the remove above because the task container knows about the provider
// by the same name
RandomizedTestingTask newTestTask = tasks.replace('test', RandomizedTestingTask)
newTestTask.configure{
group = JavaBasePlugin.VERIFICATION_GROUP
description = 'Runs unit tests with the randomized testing framework'
dependsOn oldTestTask.dependsOn, 'testClasses'
classpath = oldTestTask.classpath
testClassesDirs = oldTestTask.project.sourceSets.test.output.classesDirs
}
// hack so check task depends on custom test
Task checkTask = tasks.getByName('check')
checkTask.dependsOn.remove(oldTestProvider)
checkTask.dependsOn.remove(oldTestTask)
checkTask.dependsOn.add(newTestTask)
}
static void configureAnt(AntBuilder ant) {

View File

@ -40,6 +40,7 @@ import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.execution.TaskExecutionGraph
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
@ -888,15 +889,22 @@ class BuildPlugin implements Plugin<Project> {
parallelism System.getProperty('tests.jvms', project.rootProject.ext.defaultParallel)
onNonEmptyWorkDirectory 'wipe'
leaveTemporary true
project.sourceSets.matching { it.name == "test" }.all { test ->
task.testClassesDirs = test.output.classesDirs
task.classpath = test.runtimeClasspath
}
group = JavaBasePlugin.VERIFICATION_GROUP
dependsOn 'testClasses'
// Make sure all test tasks are configured properly
if (name != "test") {
project.tasks.matching { it.name == "test"}.all { testTask ->
task.testClassesDirs = testTask.testClassesDirs
task.classpath = testTask.classpath
task.shouldRunAfter testTask
}
}
if (name == "unitTest") {
include("**/*Tests.class")
}
// TODO: why are we not passing maxmemory to junit4?
jvmArg '-Xmx' + System.getProperty('tests.heap.size', '512m')
@ -986,8 +994,6 @@ class BuildPlugin implements Plugin<Project> {
exclude '**/*$*.class'
dependsOn(project.tasks.testClasses)
project.plugins.withType(ShadowPlugin).whenPluginAdded {
// Test against a shadow jar if we made one
classpath -= project.tasks.compileJava.outputs.files

View File

@ -55,8 +55,6 @@ public class RestIntegTestTask extends DefaultTask {
super.dependsOn(runner)
clusterInit = project.tasks.create(name: "${name}Cluster#init", dependsOn: project.testClasses)
runner.dependsOn(clusterInit)
runner.classpath = project.sourceSets.test.runtimeClasspath
runner.testClassesDirs = project.sourceSets.test.output.classesDirs
clusterConfig = project.extensions.create("${name}Cluster", ClusterConfiguration.class, project)
// override/add more for rest tests

View File

@ -27,7 +27,7 @@ forbiddenApisTest.enabled = false
// requires dependency on testing fw
jarHell.enabled = false
// we don't have tests for now
test.enabled = false
unitTest.enabled = false
task hello {
doFirst {

View File

@ -29,7 +29,7 @@ archivesBaseName = 'client-benchmarks'
mainClassName = 'org.elasticsearch.client.benchmark.BenchmarkMain'
// never try to invoke tests on the benchmark project - there aren't any
test.enabled = false
unitTest.enabled = false
dependencies {
compile 'org.apache.commons:commons-math3:3.2'

View File

@ -36,5 +36,5 @@ dependenciesInfo.enabled = false
compileJava.options.compilerArgs << "-Xlint:-cast,-deprecation,-rawtypes,-try,-unchecked"
// no unit tests
test.enabled = false
unitTest.enabled = false
integTest.enabled = false

View File

@ -55,4 +55,4 @@ namingConventions.enabled = false
//we aren't releasing this jar
thirdPartyAudit.enabled = false
test.enabled = false
unitTest.enabled = false

View File

@ -39,7 +39,6 @@ bwcVersions.forPreviousUnreleased { VersionCollection.UnreleasedVersionInfo unre
apply plugin: 'distribution'
// Not published so no need to assemble
assemble.enabled = false
assemble.dependsOn.remove('buildBwcVersion')
File checkoutDir = file("${buildDir}/bwc/checkout-${bwcBranch}")

View File

@ -7,7 +7,7 @@ forbiddenApisMain {
replaceSignatureFiles 'jdk-signatures'
}
test.enabled = false
unitTest.enabled = false
namingConventions.enabled = false
javadoc.enabled = false
loggerUsageCheck.enabled = false

View File

@ -35,7 +35,7 @@ dependencyLicenses {
mapping from: /bc.*/, to: 'bouncycastle'
}
test {
unitTest {
// TODO: find a way to add permissions for the tests in this module
systemProperty 'tests.security.manager', 'false'
}

View File

@ -26,7 +26,7 @@ dependencies {
compile "org.elasticsearch:elasticsearch-core:${version}"
}
test.enabled = false
unitTest.enabled = false
// Since CLI does not depend on :server, it cannot run the jarHell task
jarHell.enabled = false

View File

@ -17,7 +17,7 @@
* under the License.
*/
test.enabled = false
unitTest.enabled = false
// test depend on ES core...
forbiddenApisMain.enabled = false

View File

@ -37,7 +37,7 @@ dependencyLicenses {
mapping from: /asm-.*/, to: 'asm'
}
test {
unitTest {
jvmArg '-XX:-OmitStackTraceInFastThrow'
}

View File

@ -37,4 +37,4 @@ dependencies {
}
// no tests...yet?
test.enabled = false
unitTest.enabled = false

View File

@ -45,7 +45,7 @@ run {
setting 'reindex.remote.whitelist', '127.0.0.1:*'
}
test {
unitTest {
/*
* We have to disable setting the number of available processors as tests in the
* same JVM randomize processors and will step on each other if we allow them to

View File

@ -47,7 +47,7 @@ dependencyLicenses {
mapping from: /netty-.*/, to: 'netty'
}
test {
unitTest {
/*
* We have to disable setting the number of available processors as tests in the same JVM randomize processors and will step on each
* other if we allow them to set the number of available processors as it's set-once in Netty.

View File

@ -64,7 +64,7 @@ task writeTestJavaPolicy {
}
}
test {
unitTest {
dependsOn writeTestJavaPolicy
// this is needed for insecure plugins, remove if possible!
systemProperty 'tests.artifact', project.name

View File

@ -31,7 +31,7 @@ check {
dependsOn 'qa:gce:check'
}
test {
unitTest {
// this is needed for insecure plugins, remove if possible!
systemProperty 'tests.artifact', project.name
}

View File

@ -32,4 +32,4 @@ integTestCluster {
}
// this plugin has no unit tests, only rest tests
tasks.test.enabled = false
tasks.unitTest.enabled = false

View File

@ -35,4 +35,4 @@ if (System.getProperty('tests.distribution') == null) {
integTestCluster.distribution = 'oss-zip'
}
test.enabled = false
unitTest.enabled = false

View File

@ -27,7 +27,7 @@ esplugin {
}
// No unit tests in this example
test.enabled = false
unitTest.enabled = false
task exampleFixture(type: org.elasticsearch.gradle.test.AntFixture) {
dependsOn testClasses

View File

@ -26,5 +26,5 @@ esplugin {
noticeFile rootProject.file('NOTICE.txt')
}
test.enabled = false
unitTest.enabled = false

View File

@ -76,7 +76,7 @@ task testRepositoryCreds(type: RandomizedTestingTask) {
}
project.check.dependsOn(testRepositoryCreds)
test {
unitTest {
// these are tested explicitly in separate test tasks
exclude '**/*CredentialsTests.class'
exclude '**/S3BlobStoreRepositoryTests.class'

View File

@ -32,6 +32,6 @@ integTestRunner {
systemProperty 'runtime.java.home', "${project.runtimeJavaHome}"
}
test.enabled = false
unitTest.enabled = false
check.dependsOn integTest

View File

@ -31,7 +31,7 @@ dependencies {
// TODO: give each evil test its own fresh JVM for more isolation.
test {
unitTest {
systemProperty 'tests.security.manager', 'false'
}

View File

@ -93,7 +93,7 @@ for (Version version : bwcVersions.indexCompatible) {
}
}
test.enabled = false // no unit tests for rolling upgrades, only the rest integration test
unitTest.enabled = false // no unit tests for rolling upgrades, only the rest integration test
// basic integ tests includes testing bwc against the most recent version
task integTest {

View File

@ -60,7 +60,7 @@ for (Version version : bwcVersions.wireCompatible) {
}
}
test.enabled = false // no unit tests for rolling upgrades, only the rest integration test
unitTest.enabled = false // no unit tests for rolling upgrades, only the rest integration test
// basic integ tests includes testing bwc against the most recent version
task integTest {

View File

@ -53,6 +53,6 @@ task integTest {
dependsOn = [mixedClusterTest]
}
test.enabled = false // no unit tests for multi-cluster-search, only the rest integration test
unitTest.enabled = false // no unit tests for multi-cluster-search, only the rest integration test
check.dependsOn(integTest)

View File

@ -140,7 +140,7 @@ for (Version version : bwcVersions.wireCompatible) {
}
}
test.enabled = false // no unit tests for rolling upgrades, only the rest integration test
unitTest.enabled = false // no unit tests for rolling upgrades, only the rest integration test
// basic integ tests includes testing bwc against the most recent version
task integTest {

View File

@ -71,7 +71,7 @@ forbiddenApisMain {
}
// we don't have additional tests for the tests themselves
tasks.test.enabled = false
tasks.unitTest.enabled = false
// this project doesn't get published
tasks.dependencyLicenses.enabled = false

View File

@ -53,7 +53,7 @@ for (Version version : bwcVersions.indexCompatible) {
bwcTest.dependsOn(versionBwcTest)
}
test.enabled = false
unitTest.enabled = false
task integTest {
if (project.bwc_tests_enabled) {

View File

@ -211,7 +211,7 @@ if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
check.dependsOn(integTest)
test.enabled = false
unitTest.enabled = false
dependencyLicenses.enabled = false
dependenciesInfo.enabled = false

View File

@ -2,5 +2,5 @@ apply plugin: 'elasticsearch.build'
apply plugin: 'nebula.maven-base-publish'
apply plugin: 'nebula.maven-scm'
test.enabled = false
unitTest.enabled = false
jarHell.enabled = false

View File

@ -322,7 +322,7 @@ if (isEclipse == false || project.path == ":server-tests") {
task integTest(type: RandomizedTestingTask,
group: JavaBasePlugin.VERIFICATION_GROUP,
description: 'Multi-node tests',
dependsOn: test.dependsOn) {
dependsOn: unitTest.dependsOn) {
include '**/*IT.class'
}
check.dependsOn integTest

View File

@ -39,4 +39,4 @@ task hdfs(type: JavaExec) {
// just a test fixture: we aren't using jars in releases
thirdPartyAudit.enabled = false
// TODO: add a simple HDFS client test for this fixture
test.enabled = false
unitTest.enabled = false

View File

@ -74,7 +74,7 @@ task destroy(type: org.elasticsearch.gradle.vagrant.VagrantCommandTask) {
}
thirdPartyAudit.enabled = false
test.enabled = false
unitTest.enabled = false
// installKDC uses tabs in it for the Kerberos ACL file.
// Ignore it for pattern checking.

View File

@ -24,7 +24,7 @@ a "ports" file with the port on which Elasticsearch is running.
"""
apply plugin: 'elasticsearch.build'
test.enabled = false
unitTest.enabled = false
dependencies {
// Just for the constants....

View File

@ -70,7 +70,7 @@ task namingConventionsMain(type: org.elasticsearch.gradle.precommit.NamingConven
}
precommit.dependsOn namingConventionsMain
test.configure {
unitTest {
systemProperty 'tests.gradle_index_compat_versions', bwcVersions.indexCompatible.join(',')
systemProperty 'tests.gradle_wire_compat_versions', bwcVersions.wireCompatible.join(',')
systemProperty 'tests.gradle_unreleased_versions', bwcVersions.unreleased.join(',')

View File

@ -23,7 +23,7 @@ compileTestJava.options.compilerArgs << "-Xlint:-try"
task internalClusterTest(type: RandomizedTestingTask,
group: JavaBasePlugin.VERIFICATION_GROUP,
description: 'Java fantasy integration tests',
dependsOn: test.dependsOn) {
dependsOn: unitTest.dependsOn) {
include '**/*IT.class'
systemProperty 'es.set.netty.runtime.available.processors', 'false'
}

View File

@ -1,7 +1,7 @@
import org.elasticsearch.gradle.test.RestIntegTestTask
apply plugin: 'elasticsearch.build'
test.enabled = false
unitTest.enabled = false
dependencies {
compile project(':test:framework')

View File

@ -59,4 +59,4 @@ followClusterTestRunner {
}
check.dependsOn followClusterTest
test.enabled = false // no unit tests for multi-cluster-search, only the rest integration test
unitTest.enabled = false // no unit tests for multi-cluster-search, only the rest integration test

View File

@ -61,4 +61,4 @@ followClusterTestRunner {
}
check.dependsOn followClusterTest
test.enabled = false // no unit tests for multi-cluster-search, only the rest integration test
unitTest.enabled = false // no unit tests for multi-cluster-search, only the rest integration test

View File

@ -38,4 +38,4 @@ followClusterTestRunner {
}
check.dependsOn followClusterTest
test.enabled = false
unitTest.enabled = false

View File

@ -33,4 +33,4 @@ restTestCluster {
}
check.dependsOn restTest
test.enabled = false
unitTest.enabled = false

View File

@ -73,4 +73,4 @@ followClusterTestRunner {
}
check.dependsOn followClusterTest
test.enabled = false // no unit tests for multi-cluster-search, only the rest integration test
unitTest.enabled = false // no unit tests for multi-cluster-search, only the rest integration test

View File

@ -101,7 +101,7 @@ sourceSets.test.java {
srcDir '../../license-tools/src/main/java'
}
test {
unitTest {
/*
* We have to disable setting the number of available processors as tests in the same JVM randomize processors and will step on each
* other if we allow them to set the number of available processors as it's set-once in Netty.

View File

@ -1,7 +1,7 @@
import org.elasticsearch.gradle.test.RestIntegTestTask
apply plugin: 'elasticsearch.build'
test.enabled = false
unitTest.enabled = false
dependencies {
compile project(':test:framework')

View File

@ -42,4 +42,4 @@ restTestCluster {
}
check.dependsOn restTest
test.enabled = false
unitTest.enabled = false

View File

@ -97,7 +97,7 @@ integTest.enabled = false
task internalClusterTest(type: RandomizedTestingTask,
group: JavaBasePlugin.VERIFICATION_GROUP,
description: 'Multi-node tests',
dependsOn: test.dependsOn) {
dependsOn: unitTest.dependsOn) {
include '**/*IT.class'
systemProperty 'es.set.netty.runtime.available.processors', 'false'
}

View File

@ -59,7 +59,7 @@ integTest.enabled = false
task internalClusterTest(type: RandomizedTestingTask,
group: JavaBasePlugin.VERIFICATION_GROUP,
description: 'Multi-node tests',
dependsOn: test.dependsOn) {
dependsOn: unitTest.dependsOn) {
include '**/*IT.class'
systemProperty 'es.set.netty.runtime.available.processors', 'false'
}

View File

@ -281,7 +281,7 @@ run {
plugin xpackModule('core')
}
test {
unitTest {
/*
* We have to disable setting the number of available processors as tests in the same JVM randomize processors and will step on each
* other if we allow them to set the number of available processors as it's set-once in Netty.

View File

@ -23,7 +23,7 @@ dependencyLicenses {
}
if (project.inFipsJvm) {
test.enabled = false
tesunitTestt.enabled = false
// Forbiden APIs non-portable checks fail because bouncy castle classes being used from the FIPS JDK since those are
// not part of the Java specification - all of this is as designed, so we have to relax this check for FIPS.
tasks.withType(CheckForbiddenApis) {

View File

@ -36,7 +36,7 @@ dependencyLicenses {
ignoreSha 'elasticsearch'
}
test {
unitTest {
// don't use the shaded jar for tests
classpath += project.tasks.compileJava.outputs.files
classpath -= project.tasks.shadowJar.outputs.files

View File

@ -21,7 +21,7 @@ dependencies {
/* disable unit tests because these are all integration tests used
* other qa projects. */
test.enabled = false
unitTest.enabled = false
dependencyLicenses.enabled = false
dependenciesInfo.enabled = false

View File

@ -132,7 +132,7 @@ task runcli {
}
// Use the jar for testing so we can get the proper version information
test {
unitTest {
classpath -= compileJava.outputs.files
classpath -= configurations.compile
classpath -= configurations.runtime

View File

@ -32,7 +32,7 @@ integTest.enabled = false
task internalClusterTest(type: RandomizedTestingTask,
group: JavaBasePlugin.VERIFICATION_GROUP,
description: 'Multi-node tests',
dependsOn: test.dependsOn) {
dependsOn: unitTest.dependsOn) {
include '**/*IT.class'
systemProperty 'es.set.netty.runtime.available.processors', 'false'
}

View File

@ -103,7 +103,7 @@ run {
plugin xpackModule('core')
}
test {
unitTest {
/*
* We have to disable setting the number of available processors as tests in the same JVM randomize processors and will step on each
* other if we allow them to set the number of available processors as it's set-once in Netty.

View File

@ -5,7 +5,7 @@ dependencies {
testCompile project(path: xpackModule('security'), configuration: 'testArtifacts')
}
test {
unitTest {
systemProperty 'tests.security.manager', 'false'
include '**/*Tests.class'
}

View File

@ -8,7 +8,7 @@ import java.util.regex.Matcher
// Apply the java plugin to this project so the sources can be edited in an IDE
apply plugin: 'elasticsearch.build'
test.enabled = false
unitTest.enabled = false
dependencies {
// "org.elasticsearch.plugin:x-pack-core:${version}" doesn't work with idea because the testArtifacts are also here
@ -243,8 +243,8 @@ subprojects {
bwcTest.dependsOn(versionBwcTest)
}
}
test.enabled = false // no unit tests for full cluster restarts, only the rest integration test
unitTest.enabled = false // no unit tests for full cluster restarts, only the rest integration test
// basic integ tests includes testing bwc against the most recent version
task integTest {

View File

@ -75,5 +75,5 @@ task integTest {
dependsOn = [mixedClusterTest]
}
test.enabled = false // no unit tests for multi-cluster-search, only the rest integration test
unitTest.enabled = false // no unit tests for multi-cluster-search, only the rest integration test
check.dependsOn(integTest)

View File

@ -23,10 +23,10 @@ task copyIdpTrust(type: Copy) {
}
if (project.rootProject.vagrantSupported) {
project.sourceSets.test.output.dir(outputDir, builtBy: copyIdpTrust)
test.dependsOn openLdapFixture
test.finalizedBy idpFixtureProject.halt
unitTest.dependsOn openLdapFixture
unitTest.finalizedBy idpFixtureProject.halt
} else {
test.enabled = false
unitTest.enabled = false
testingConventions.enabled = false
}

View File

@ -122,7 +122,7 @@ for (Version version : bwcVersions.wireCompatible) {
}
}
test.enabled = false // no unit tests for rolling upgrades, only the rest integration test
unitTest.enabled = false // no unit tests for rolling upgrades, only the rest integration test
// basic integ tests includes testing bwc against the most recent version
task integTest {

View File

@ -7,7 +7,7 @@ import java.util.regex.Matcher
// Apply the java plugin to this project so the sources can be edited in an IDE
apply plugin: 'elasticsearch.build'
test.enabled = false
unitTest.enabled = false
dependencies {
// "org.elasticsearch.plugin:x-pack-core:${version}" doesn't work with idea because the testArtifacts are also here
@ -327,7 +327,7 @@ subprojects {
}
}
test.enabled = false // no unit tests for rolling upgrades, only the rest integration test
unitTest.enabled = false // no unit tests for rolling upgrades, only the rest integration test
// basic integ tests includes testing bwc against the most recent version
task integTest {

View File

@ -19,7 +19,7 @@ forbiddenPatterns {
exclude '**/*.der'
}
test {
unitTest {
/*
* We have to disable setting the number of available processors as tests in the same JVM randomize processors and will step on each
* other if we allow them to set the number of available processors as it's set-once in Netty.

View File

@ -38,5 +38,5 @@ task destroy(type: org.elasticsearch.gradle.vagrant.VagrantCommandTask) {
}
thirdPartyAudit.enabled = false
test.enabled = false
unitTest.enabled = false
jarHell.enabled = false