Skip to content

Commit e9ef5bd

Browse files
authored
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
1 parent 8015560 commit e9ef5bd

File tree

67 files changed

+87
-122
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+87
-122
lines changed

benchmarks/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mainClassName = 'org.openjdk.jmh.Main'
2424
assemble.enabled = false
2525
archivesBaseName = 'elasticsearch-benchmarks'
2626

27-
test.enabled = false
27+
unitTest.enabled = false
2828

2929
dependencies {
3030
compile("org.elasticsearch:elasticsearch:${version}") {

buildSrc/build.gradle

+1-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ if (project != rootProject) {
179179
jarHell.enabled = false
180180
thirdPartyAudit.enabled = false
181181

182-
test {
183-
include "**/*Tests.class"
184-
exclude "**/*IT.class"
182+
unitTest {
185183
// The test task is configured to runtimeJava version, but build-tools doesn't support all of them, so test
186184
// with compiler instead on the ones that are too old.
187185
if (project.runtimeJavaVersion <= JavaVersion.VERSION_1_10) {

buildSrc/src/main/groovy/com/carrotsearch/gradle/junit4/RandomizedTestingPlugin.groovy

+9-45
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,14 @@ import com.carrotsearch.ant.tasks.junit4.JUnit4
44
import org.gradle.api.Plugin
55
import org.gradle.api.Project
66
import org.gradle.api.Task
7-
import org.gradle.api.UnknownTaskException
8-
import org.gradle.api.plugins.JavaBasePlugin
97
import org.gradle.api.tasks.TaskContainer
10-
import org.gradle.api.tasks.TaskProvider
11-
import org.gradle.api.tasks.testing.Test
128

139
class RandomizedTestingPlugin implements Plugin<Project> {
1410

1511
void apply(Project project) {
1612
setupSeed(project)
17-
replaceTestTask(project.tasks)
13+
createUnitTestTask(project.tasks)
1814
configureAnt(project.ant)
19-
configureSanityCheck(project)
20-
}
21-
22-
private static void configureSanityCheck(Project project) {
23-
// Check the task graph to confirm tasks were indeed replaced
24-
// https://github.com/elastic/elasticsearch/issues/31324
25-
project.rootProject.getGradle().getTaskGraph().whenReady {
26-
Task test = project.getTasks().findByName("test")
27-
if (test != null && (test instanceof RandomizedTestingTask) == false) {
28-
throw new IllegalStateException("Test task was not replaced in project ${project.path}. Found ${test.getClass()}")
29-
}
30-
}
3115
}
3216

3317
/**
@@ -57,35 +41,15 @@ class RandomizedTestingPlugin implements Plugin<Project> {
5741
}
5842
}
5943

60-
static void replaceTestTask(TaskContainer tasks) {
61-
// Gradle 4.8 introduced lazy tasks, thus we deal both with the `test` task as well as it's provider
62-
// https://github.com/gradle/gradle/issues/5730#issuecomment-398822153
63-
// since we can't be sure if the task was ever realized, we remove both the provider and the task
64-
TaskProvider<Test> oldTestProvider
65-
try {
66-
oldTestProvider = tasks.named('test')
67-
} catch (UnknownTaskException unused) {
68-
// no test task, ok, user will use testing task on their own
69-
return
44+
static void createUnitTestTask(TaskContainer tasks) {
45+
// only create a unitTest task if the `test` task exists as some project don't make use of it.
46+
tasks.matching { it.name == "test" }.all {
47+
// We don't want to run any tests with the Gradle test runner since we add our own randomized runner
48+
it.enabled = false
49+
RandomizedTestingTask unitTest = tasks.create('unitTest', RandomizedTestingTask)
50+
unitTest.description = 'Runs unit tests with the randomized testing framework'
51+
it.dependsOn unitTest
7052
}
71-
Test oldTestTask = oldTestProvider.get()
72-
73-
// we still have to use replace here despite the remove above because the task container knows about the provider
74-
// by the same name
75-
RandomizedTestingTask newTestTask = tasks.replace('test', RandomizedTestingTask)
76-
newTestTask.configure{
77-
group = JavaBasePlugin.VERIFICATION_GROUP
78-
description = 'Runs unit tests with the randomized testing framework'
79-
dependsOn oldTestTask.dependsOn, 'testClasses'
80-
classpath = oldTestTask.classpath
81-
testClassesDirs = oldTestTask.project.sourceSets.test.output.classesDirs
82-
}
83-
84-
// hack so check task depends on custom test
85-
Task checkTask = tasks.getByName('check')
86-
checkTask.dependsOn.remove(oldTestProvider)
87-
checkTask.dependsOn.remove(oldTestTask)
88-
checkTask.dependsOn.add(newTestTask)
8953
}
9054

9155
static void configureAnt(AntBuilder ant) {

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

+10-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.gradle.api.artifacts.ProjectDependency
4040
import org.gradle.api.artifacts.ResolvedArtifact
4141
import org.gradle.api.artifacts.dsl.RepositoryHandler
4242
import org.gradle.api.execution.TaskExecutionGraph
43+
import org.gradle.api.plugins.JavaBasePlugin
4344
import org.gradle.api.plugins.JavaPlugin
4445
import org.gradle.api.publish.maven.MavenPublication
4546
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
@@ -888,15 +889,22 @@ class BuildPlugin implements Plugin<Project> {
888889
parallelism System.getProperty('tests.jvms', project.rootProject.ext.defaultParallel)
889890
onNonEmptyWorkDirectory 'wipe'
890891
leaveTemporary true
892+
project.sourceSets.matching { it.name == "test" }.all { test ->
893+
task.testClassesDirs = test.output.classesDirs
894+
task.classpath = test.runtimeClasspath
895+
}
896+
group = JavaBasePlugin.VERIFICATION_GROUP
897+
dependsOn 'testClasses'
891898

892899
// Make sure all test tasks are configured properly
893900
if (name != "test") {
894901
project.tasks.matching { it.name == "test"}.all { testTask ->
895-
task.testClassesDirs = testTask.testClassesDirs
896-
task.classpath = testTask.classpath
897902
task.shouldRunAfter testTask
898903
}
899904
}
905+
if (name == "unitTest") {
906+
include("**/*Tests.class")
907+
}
900908

901909
// TODO: why are we not passing maxmemory to junit4?
902910
jvmArg '-Xmx' + System.getProperty('tests.heap.size', '512m')
@@ -986,8 +994,6 @@ class BuildPlugin implements Plugin<Project> {
986994

987995
exclude '**/*$*.class'
988996

989-
dependsOn(project.tasks.testClasses)
990-
991997
project.plugins.withType(ShadowPlugin).whenPluginAdded {
992998
// Test against a shadow jar if we made one
993999
classpath -= project.tasks.compileJava.outputs.files

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy

-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public class RestIntegTestTask extends DefaultTask {
5555
super.dependsOn(runner)
5656
clusterInit = project.tasks.create(name: "${name}Cluster#init", dependsOn: project.testClasses)
5757
runner.dependsOn(clusterInit)
58-
runner.classpath = project.sourceSets.test.runtimeClasspath
59-
runner.testClassesDirs = project.sourceSets.test.output.classesDirs
6058
clusterConfig = project.extensions.create("${name}Cluster", ClusterConfiguration.class, project)
6159

6260
// override/add more for rest tests

buildSrc/src/testKit/elasticsearch.build/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ forbiddenApisTest.enabled = false
2727
// requires dependency on testing fw
2828
jarHell.enabled = false
2929
// we don't have tests for now
30-
test.enabled = false
30+
unitTest.enabled = false
3131

3232
task hello {
3333
doFirst {

client/benchmark/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ archivesBaseName = 'client-benchmarks'
2929
mainClassName = 'org.elasticsearch.client.benchmark.BenchmarkMain'
3030

3131
// never try to invoke tests on the benchmark project - there aren't any
32-
test.enabled = false
32+
unitTest.enabled = false
3333

3434
dependencies {
3535
compile 'org.apache.commons:commons-math3:3.2'

client/client-benchmark-noop-api-plugin/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ dependenciesInfo.enabled = false
3636
compileJava.options.compilerArgs << "-Xlint:-cast,-deprecation,-rawtypes,-try,-unchecked"
3737

3838
// no unit tests
39-
test.enabled = false
39+
unitTest.enabled = false
4040
integTest.enabled = false

client/test/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ namingConventions.enabled = false
5555

5656
//we aren't releasing this jar
5757
thirdPartyAudit.enabled = false
58-
test.enabled = false
58+
unitTest.enabled = false

distribution/bwc/build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ bwcVersions.forPreviousUnreleased { VersionCollection.UnreleasedVersionInfo unre
3939
apply plugin: 'distribution'
4040
// Not published so no need to assemble
4141
assemble.enabled = false
42-
assemble.dependsOn.remove('buildBwcVersion')
4342

4443
File checkoutDir = file("${buildDir}/bwc/checkout-${bwcBranch}")
4544

distribution/tools/java-version-checker/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ forbiddenApisMain {
77
replaceSignatureFiles 'jdk-signatures'
88
}
99

10-
test.enabled = false
10+
unitTest.enabled = false
1111
namingConventions.enabled = false
1212
javadoc.enabled = false
1313
loggerUsageCheck.enabled = false

distribution/tools/plugin-cli/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ dependencyLicenses {
3535
mapping from: /bc.*/, to: 'bouncycastle'
3636
}
3737

38-
test {
38+
unitTest {
3939
// TODO: find a way to add permissions for the tests in this module
4040
systemProperty 'tests.security.manager', 'false'
4141
}

libs/cli/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dependencies {
2626
compile "org.elasticsearch:elasticsearch-core:${version}"
2727
}
2828

29-
test.enabled = false
29+
unitTest.enabled = false
3030
// Since CLI does not depend on :server, it cannot run the jarHell task
3131
jarHell.enabled = false
3232

libs/plugin-classloader/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
test.enabled = false
20+
unitTest.enabled = false
2121

2222
// test depend on ES core...
2323
forbiddenApisMain.enabled = false

modules/lang-painless/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dependencyLicenses {
3737
mapping from: /asm-.*/, to: 'asm'
3838
}
3939

40-
test {
40+
unitTest {
4141
jvmArg '-XX:-OmitStackTraceInFastThrow'
4242
}
4343

modules/lang-painless/spi/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ dependencies {
3737
}
3838

3939
// no tests...yet?
40-
test.enabled = false
40+
unitTest.enabled = false

modules/reindex/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ run {
4545
setting 'reindex.remote.whitelist', '127.0.0.1:*'
4646
}
4747

48-
test {
48+
unitTest {
4949
/*
5050
* We have to disable setting the number of available processors as tests in the
5151
* same JVM randomize processors and will step on each other if we allow them to

modules/transport-netty4/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dependencyLicenses {
4747
mapping from: /netty-.*/, to: 'netty'
4848
}
4949

50-
test {
50+
unitTest {
5151
/*
5252
* We have to disable setting the number of available processors as tests in the same JVM randomize processors and will step on each
5353
* other if we allow them to set the number of available processors as it's set-once in Netty.

plugins/discovery-ec2/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ task writeTestJavaPolicy {
6464
}
6565
}
6666

67-
test {
67+
unitTest {
6868
dependsOn writeTestJavaPolicy
6969
// this is needed for insecure plugins, remove if possible!
7070
systemProperty 'tests.artifact', project.name

plugins/discovery-gce/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ check {
3131
dependsOn 'qa:gce:check'
3232
}
3333

34-
test {
34+
unitTest {
3535
// this is needed for insecure plugins, remove if possible!
3636
systemProperty 'tests.artifact', project.name
3737
}

plugins/examples/custom-suggester/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ integTestCluster {
3232
}
3333

3434
// this plugin has no unit tests, only rest tests
35-
tasks.test.enabled = false
35+
tasks.unitTest.enabled = false

plugins/examples/painless-whitelist/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ if (System.getProperty('tests.distribution') == null) {
3535
integTestCluster.distribution = 'oss-zip'
3636
}
3737

38-
test.enabled = false
38+
unitTest.enabled = false

plugins/examples/rest-handler/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ esplugin {
2727
}
2828

2929
// No unit tests in this example
30-
test.enabled = false
30+
unitTest.enabled = false
3131

3232
task exampleFixture(type: org.elasticsearch.gradle.test.AntFixture) {
3333
dependsOn testClasses

plugins/examples/script-expert-scoring/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ esplugin {
2626
noticeFile rootProject.file('NOTICE.txt')
2727
}
2828

29-
test.enabled = false
29+
unitTest.enabled = false
3030

plugins/repository-s3/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ task testRepositoryCreds(type: RandomizedTestingTask) {
7676
}
7777
project.check.dependsOn(testRepositoryCreds)
7878

79-
test {
79+
unitTest {
8080
// these are tested explicitly in separate test tasks
8181
exclude '**/*CredentialsTests.class'
8282
exclude '**/S3BlobStoreRepositoryTests.class'

qa/die-with-dignity/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ integTestRunner {
3232
systemProperty 'runtime.java.home', "${project.runtimeJavaHome}"
3333
}
3434

35-
test.enabled = false
35+
unitTest.enabled = false
3636

3737
check.dependsOn integTest

qa/evil-tests/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies {
3131

3232
// TODO: give each evil test its own fresh JVM for more isolation.
3333

34-
test {
34+
unitTest {
3535
systemProperty 'tests.security.manager', 'false'
3636
}
3737

qa/full-cluster-restart/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ for (Version version : bwcVersions.indexCompatible) {
9393
}
9494
}
9595

96-
test.enabled = false // no unit tests for rolling upgrades, only the rest integration test
96+
unitTest.enabled = false // no unit tests for rolling upgrades, only the rest integration test
9797

9898
// basic integ tests includes testing bwc against the most recent version
9999
task integTest {

qa/mixed-cluster/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ for (Version version : bwcVersions.wireCompatible) {
6060
}
6161
}
6262

63-
test.enabled = false // no unit tests for rolling upgrades, only the rest integration test
63+
unitTest.enabled = false // no unit tests for rolling upgrades, only the rest integration test
6464

6565
// basic integ tests includes testing bwc against the most recent version
6666
task integTest {

qa/multi-cluster-search/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ task integTest {
5353
dependsOn = [mixedClusterTest]
5454
}
5555

56-
test.enabled = false // no unit tests for multi-cluster-search, only the rest integration test
56+
unitTest.enabled = false // no unit tests for multi-cluster-search, only the rest integration test
5757

5858
check.dependsOn(integTest)

qa/rolling-upgrade/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ for (Version version : bwcVersions.wireCompatible) {
140140
}
141141
}
142142

143-
test.enabled = false // no unit tests for rolling upgrades, only the rest integration test
143+
unitTest.enabled = false // no unit tests for rolling upgrades, only the rest integration test
144144

145145
// basic integ tests includes testing bwc against the most recent version
146146
task integTest {

qa/vagrant/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ forbiddenApisMain {
7171
}
7272

7373
// we don't have additional tests for the tests themselves
74-
tasks.test.enabled = false
74+
tasks.unitTest.enabled = false
7575

7676
// this project doesn't get published
7777
tasks.dependencyLicenses.enabled = false

qa/verify-version-constants/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ for (Version version : bwcVersions.indexCompatible) {
5353
bwcTest.dependsOn(versionBwcTest)
5454
}
5555

56-
test.enabled = false
56+
unitTest.enabled = false
5757

5858
task integTest {
5959
if (project.bwc_tests_enabled) {

qa/wildfly/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
211211

212212
check.dependsOn(integTest)
213213

214-
test.enabled = false
214+
unitTest.enabled = false
215215

216216
dependencyLicenses.enabled = false
217217
dependenciesInfo.enabled = false

rest-api-spec/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ apply plugin: 'elasticsearch.build'
22
apply plugin: 'nebula.maven-base-publish'
33
apply plugin: 'nebula.maven-scm'
44

5-
test.enabled = false
5+
unitTest.enabled = false
66
jarHell.enabled = false

0 commit comments

Comments
 (0)