From 7bf909194222f851a5e10ba8012e85624c2c124f Mon Sep 17 00:00:00 2001 From: Andy Bristol Date: Mon, 26 Mar 2018 13:43:09 -0700 Subject: [PATCH] [test] packaging: gradle tasks for groovy tests (#29046) The vagrant test plugin adds tasks for the groovy packaging tests, which run after the bats packaging test tasks.Rename the 'bats' configuration to 'packaging' and remove the option to inherit archives from this configuration. --- TESTING.asciidoc | 6 +- Vagrantfile | 8 +- .../vagrant/VagrantPropertiesExtension.groovy | 7 - .../gradle/vagrant/VagrantTestPlugin.groovy | 166 +++++++++--------- qa/vagrant/build.gradle | 4 +- 5 files changed, 95 insertions(+), 96 deletions(-) diff --git a/TESTING.asciidoc b/TESTING.asciidoc index f7725bd17d7..4a432d48181 100644 --- a/TESTING.asciidoc +++ b/TESTING.asciidoc @@ -414,16 +414,16 @@ and in another window: ---------------------------------------------------- vagrant up centos-7 --provider virtualbox && vagrant ssh centos-7 -cd $BATS_ARCHIVES +cd $PACKAGING_ARCHIVES sudo -E bats $BATS_TESTS/*rpm*.bats ---------------------------------------------------- If you wanted to retest all the release artifacts on a single VM you could: ------------------------------------------------- -./gradlew setupBats +./gradlew setupPackagingTest cd qa/vagrant; vagrant up ubuntu-1404 --provider virtualbox && vagrant ssh ubuntu-1404 -cd $BATS_ARCHIVES +cd $PACKAGING_ARCHIVES sudo -E bats $BATS_TESTS/*.bats ------------------------------------------------- diff --git a/Vagrantfile b/Vagrantfile index 683a5d83732..6f81ba0273c 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -334,9 +334,9 @@ export TAR=/elasticsearch/distribution/tar/build/distributions export RPM=/elasticsearch/distribution/rpm/build/distributions export DEB=/elasticsearch/distribution/deb/build/distributions export BATS=/project/build/bats -export BATS_UTILS=/project/build/bats/utils -export BATS_TESTS=/project/build/bats/tests -export BATS_ARCHIVES=/project/build/bats/archives +export BATS_UTILS=/project/build/packaging/bats/utils +export BATS_TESTS=/project/build/packaging/bats/tests +export PACKAGING_ARCHIVES=/project/build/packaging/archives VARS cat \<\ /etc/sudoers.d/elasticsearch_vars Defaults env_keep += "ZIP" @@ -346,7 +346,7 @@ Defaults env_keep += "DEB" Defaults env_keep += "BATS" Defaults env_keep += "BATS_UTILS" Defaults env_keep += "BATS_TESTS" -Defaults env_keep += "BATS_ARCHIVES" +Defaults env_keep += "PACKAGING_ARCHIVES" SUDOERS_VARS chmod 0440 /etc/sudoers.d/elasticsearch_vars SHELL diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantPropertiesExtension.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantPropertiesExtension.groovy index e6e7fca62f9..c6d0f1d0425 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantPropertiesExtension.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantPropertiesExtension.groovy @@ -37,9 +37,6 @@ class VagrantPropertiesExtension { @Input Boolean inheritTests - @Input - Boolean inheritTestArchives - @Input Boolean inheritTestUtils @@ -60,10 +57,6 @@ class VagrantPropertiesExtension { this.inheritTests = inheritTests } - void setInheritTestArchives(Boolean inheritTestArchives) { - this.inheritTestArchives = inheritTestArchives - } - void setInheritTestUtils(Boolean inheritTestUtils) { this.inheritTestUtils = inheritTestUtils } diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy index fc15258215e..d7d1c01e7dd 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy @@ -1,6 +1,5 @@ package org.elasticsearch.gradle.vagrant -import com.carrotsearch.gradle.junit4.RandomizedTestingPlugin import org.apache.tools.ant.taskdefs.condition.Os import org.elasticsearch.gradle.FileContentsTask import org.elasticsearch.gradle.LoggedExec @@ -43,8 +42,9 @@ class VagrantTestPlugin implements Plugin { /** Packages onboarded for upgrade tests **/ static List UPGRADE_FROM_ARCHIVES = ['rpm', 'deb'] + private static final PACKAGING_CONFIGURATION = 'packaging' private static final BATS = 'bats' - private static final String BATS_TEST_COMMAND ="cd \$BATS_ARCHIVES && sudo bats --tap \$BATS_TESTS/*.$BATS" + private static final String BATS_TEST_COMMAND ="cd \$PACKAGING_ARCHIVES && sudo bats --tap \$BATS_TESTS/*.$BATS" private static final String PLATFORM_TEST_COMMAND ="rm -rf ~/elasticsearch && rsync -r /elasticsearch/ ~/elasticsearch && cd ~/elasticsearch && ./gradlew test integTest" @Override @@ -53,11 +53,11 @@ class VagrantTestPlugin implements Plugin { // Creates the Vagrant extension for the project project.extensions.create('esvagrant', VagrantPropertiesExtension, listVagrantBoxes(project)) - // Add required repositories for Bats tests - configureBatsRepositories(project) + // Add required repositories for packaging tests + configurePackagingArchiveRepositories(project) // Creates custom configurations for Bats testing files (and associated scripts and archives) - createBatsConfiguration(project) + createPackagingConfiguration(project) // Creates all the main Vagrant tasks createVagrantTasks(project) @@ -87,7 +87,7 @@ class VagrantTestPlugin implements Plugin { } } - private static void configureBatsRepositories(Project project) { + private static void configurePackagingArchiveRepositories(Project project) { RepositoryHandler repos = project.repositories // Try maven central first, it'll have releases before 5.0.0 @@ -102,10 +102,10 @@ class VagrantTestPlugin implements Plugin { } } - private static void createBatsConfiguration(Project project) { - project.configurations.create(BATS) + private static void createPackagingConfiguration(Project project) { + project.configurations.create(PACKAGING_CONFIGURATION) - String upgradeFromVersion = System.getProperty("tests.packaging.upgradeVersion"); + String upgradeFromVersion = System.getProperty("tests.packaging.upgradeVersion") if (upgradeFromVersion == null) { String firstPartOfSeed = project.rootProject.testSeed.tokenize(':').get(0) final long seed = Long.parseUnsignedLong(firstPartOfSeed, 16) @@ -120,12 +120,14 @@ class VagrantTestPlugin implements Plugin { } else { it = "packages:${it}" } - project.dependencies.add(BATS, project.dependencies.project(path: ":distribution:${it}", configuration: 'default')) + project.dependencies.add(PACKAGING_CONFIGURATION, + project.dependencies.project(path: ":distribution:${it}", configuration: 'default')) } UPGRADE_FROM_ARCHIVES.each { // The version of elasticsearch that we upgrade *from* - project.dependencies.add(BATS, "org.elasticsearch.distribution.${it}:elasticsearch:${upgradeFromVersion}@${it}") + project.dependencies.add(PACKAGING_CONFIGURATION, + "org.elasticsearch.distribution.${it}:elasticsearch:${upgradeFromVersion}@${it}") } project.extensions.esvagrant.upgradeFromVersion = upgradeFromVersion @@ -154,22 +156,28 @@ class VagrantTestPlugin implements Plugin { } private static void createPrepareVagrantTestEnvTask(Project project) { - File batsDir = new File("${project.buildDir}/${BATS}") + File packagingDir = new File(project.buildDir, PACKAGING_CONFIGURATION) - Task createBatsDirsTask = project.tasks.create('createBatsDirs') - createBatsDirsTask.outputs.dir batsDir - createBatsDirsTask.doLast { - batsDir.mkdirs() + File archivesDir = new File(packagingDir, 'archives') + Copy copyPackagingArchives = project.tasks.create('copyPackagingArchives', Copy) { + into archivesDir + from project.configurations[PACKAGING_CONFIGURATION] } - Copy copyBatsArchives = project.tasks.create('copyBatsArchives', Copy) { - dependsOn createBatsDirsTask - into "${batsDir}/archives" - from project.configurations[BATS] + Task createVersionFile = project.tasks.create('createVersionFile', FileContentsTask) { + dependsOn copyPackagingArchives + file "${archivesDir}/version" + contents project.version } + Task createUpgradeFromFile = project.tasks.create('createUpgradeFromFile', FileContentsTask) { + dependsOn copyPackagingArchives + file "${archivesDir}/upgrade_from_version" + contents project.extensions.esvagrant.upgradeFromVersion + } + + File batsDir = new File(packagingDir, BATS) Copy copyBatsTests = project.tasks.create('copyBatsTests', Copy) { - dependsOn createBatsDirsTask into "${batsDir}/tests" from { "${project.extensions.esvagrant.batsDir}/tests" @@ -177,7 +185,6 @@ class VagrantTestPlugin implements Plugin { } Copy copyBatsUtils = project.tasks.create('copyBatsUtils', Copy) { - dependsOn createBatsDirsTask into "${batsDir}/utils" from { "${project.extensions.esvagrant.batsDir}/utils" @@ -185,42 +192,30 @@ class VagrantTestPlugin implements Plugin { } // Now we iterate over dependencies of the bats configuration. When a project dependency is found, - // we bring back its own archives, test files or test utils. + // we bring back its test files or test utils. project.afterEvaluate { - project.configurations.bats.dependencies.findAll {it.targetConfiguration == BATS }.each { d -> - if (d instanceof DefaultProjectDependency) { - DefaultProjectDependency externalBatsDependency = (DefaultProjectDependency) d - Project externalBatsProject = externalBatsDependency.dependencyProject - String externalBatsDir = externalBatsProject.extensions.esvagrant.batsDir + project.configurations[PACKAGING_CONFIGURATION].dependencies + .findAll {it.targetConfiguration == PACKAGING_CONFIGURATION } + .each { d -> + if (d instanceof DefaultProjectDependency) { + DefaultProjectDependency externalBatsDependency = (DefaultProjectDependency) d + Project externalBatsProject = externalBatsDependency.dependencyProject + String externalBatsDir = externalBatsProject.extensions.esvagrant.batsDir - if (project.extensions.esvagrant.inheritTests) { - copyBatsTests.from(externalBatsProject.files("${externalBatsDir}/tests")) + if (project.extensions.esvagrant.inheritTests) { + copyBatsTests.from(externalBatsProject.files("${externalBatsDir}/tests")) + } + if (project.extensions.esvagrant.inheritTestUtils) { + copyBatsUtils.from(externalBatsProject.files("${externalBatsDir}/utils")) + } } - if (project.extensions.esvagrant.inheritTestArchives) { - copyBatsArchives.from(externalBatsDependency.projectConfiguration.files) - } - if (project.extensions.esvagrant.inheritTestUtils) { - copyBatsUtils.from(externalBatsProject.files("${externalBatsDir}/utils")) - } - } } } - Task createVersionFile = project.tasks.create('createVersionFile', FileContentsTask) { - dependsOn createBatsDirsTask - file "${batsDir}/archives/version" - contents project.version - } - - Task createUpgradeFromFile = project.tasks.create('createUpgradeFromFile', FileContentsTask) { - dependsOn createBatsDirsTask - file "${batsDir}/archives/upgrade_from_version" - contents project.extensions.esvagrant.upgradeFromVersion - } - - Task vagrantSetUpTask = project.tasks.create('setupBats') + Task vagrantSetUpTask = project.tasks.create('setupPackagingTest') vagrantSetUpTask.dependsOn 'vagrantCheckVersion' - vagrantSetUpTask.dependsOn copyBatsTests, copyBatsUtils, copyBatsArchives, createVersionFile, createUpgradeFromFile + vagrantSetUpTask.dependsOn copyPackagingArchives, createVersionFile, createUpgradeFromFile + vagrantSetUpTask.dependsOn copyBatsTests, copyBatsUtils } private static void createPackagingTestTask(Project project) { @@ -270,8 +265,8 @@ class VagrantTestPlugin implements Plugin { assert project.tasks.virtualboxCheckVersion != null Task virtualboxCheckVersion = project.tasks.virtualboxCheckVersion - assert project.tasks.setupBats != null - Task setupBats = project.tasks.setupBats + assert project.tasks.setupPackagingTest != null + Task setupPackagingTest = project.tasks.setupPackagingTest assert project.tasks.packagingTest != null Task packagingTest = project.tasks.packagingTest @@ -308,7 +303,7 @@ class VagrantTestPlugin implements Plugin { environmentVars vagrantEnvVars dependsOn vagrantCheckVersion, virtualboxCheckVersion } - update.mustRunAfter(setupBats) + update.mustRunAfter(setupPackagingTest) /* * Destroying before every execution can be annoying while iterating on tests locally. Therefore, we provide a flag @@ -359,32 +354,39 @@ class VagrantTestPlugin implements Plugin { } vagrantSmokeTest.dependsOn(smoke) - Task packaging = project.tasks.create("vagrant${boxTask}#packagingTest", BatsOverVagrantTask) { + Task batsPackagingTest = project.tasks.create("vagrant${boxTask}#batsPackagingTest", BatsOverVagrantTask) { remoteCommand BATS_TEST_COMMAND boxName box environmentVars vagrantEnvVars - dependsOn up, setupBats + dependsOn up, setupPackagingTest finalizedBy halt } - TaskExecutionAdapter packagingReproListener = new TaskExecutionAdapter() { - @Override - void afterExecute(Task task, TaskState state) { - final String gradlew = Os.isFamily(Os.FAMILY_WINDOWS) ? "gradlew" : "./gradlew" - if (state.failure != null) { - println "REPRODUCE WITH: ${gradlew} ${packaging.path} " + - "-Dtests.seed=${project.testSeed} " - } - } + TaskExecutionAdapter batsPackagingReproListener = createReproListener(project, batsPackagingTest.path) + batsPackagingTest.doFirst { + project.gradle.addListener(batsPackagingReproListener) } - packaging.doFirst { - project.gradle.addListener(packagingReproListener) - } - packaging.doLast { - project.gradle.removeListener(packagingReproListener) + batsPackagingTest.doLast { + project.gradle.removeListener(batsPackagingReproListener) } if (project.extensions.esvagrant.boxes.contains(box)) { - packagingTest.dependsOn(packaging) + packagingTest.dependsOn(batsPackagingTest) + } + + // This task doesn't do anything yet. In the future it will execute a jar containing tests on the vm + Task groovyPackagingTest = project.tasks.create("vagrant${boxTask}#groovyPackagingTest") + groovyPackagingTest.dependsOn(up) + groovyPackagingTest.finalizedBy(halt) + + TaskExecutionAdapter groovyPackagingReproListener = createReproListener(project, groovyPackagingTest.path) + groovyPackagingTest.doFirst { + project.gradle.addListener(groovyPackagingReproListener) + } + groovyPackagingTest.doLast { + project.gradle.removeListener(groovyPackagingReproListener) + } + if (project.extensions.esvagrant.boxes.contains(box)) { + packagingTest.dependsOn(groovyPackagingTest) } Task platform = project.tasks.create("vagrant${boxTask}#platformTest", VagrantCommandTask) { @@ -395,15 +397,7 @@ class VagrantTestPlugin implements Plugin { finalizedBy halt args '--command', PLATFORM_TEST_COMMAND + " -Dtests.seed=${-> project.testSeed}" } - TaskExecutionAdapter platformReproListener = new TaskExecutionAdapter() { - @Override - void afterExecute(Task task, TaskState state) { - if (state.failure != null) { - println "REPRODUCE WITH: gradle ${platform.path} " + - "-Dtests.seed=${project.testSeed} " - } - } - } + TaskExecutionAdapter platformReproListener = createReproListener(project, platform.path) platform.doFirst { project.gradle.addListener(platformReproListener) } @@ -415,4 +409,16 @@ class VagrantTestPlugin implements Plugin { } } } + + private static TaskExecutionAdapter createReproListener(Project project, String reproTaskPath) { + return new TaskExecutionAdapter() { + @Override + void afterExecute(Task task, TaskState state) { + final String gradlew = Os.isFamily(Os.FAMILY_WINDOWS) ? "gradlew" : "./gradlew" + if (state.failure != null) { + println "REPRODUCE WITH: ${gradlew} ${reproTaskPath} -Dtests.seed=${project.testSeed} " + } + } + } + } } diff --git a/qa/vagrant/build.gradle b/qa/vagrant/build.gradle index f28f6afd2fc..4086cf22057 100644 --- a/qa/vagrant/build.gradle +++ b/qa/vagrant/build.gradle @@ -25,14 +25,14 @@ for (Project subproj : project.rootProject.subprojects) { if (subproj.path.startsWith(':plugins:') || subproj.path.equals(':example-plugins:custom-settings')) { // add plugin as a dep dependencies { - bats project(path: "${subproj.path}", configuration: 'zip') + packaging project(path: "${subproj.path}", configuration: 'zip') } plugins.add(subproj.name) } } plugins = plugins.toSorted() -setupBats { +setupPackagingTest { doFirst { File expectedPlugins = file('build/plugins/expected') expectedPlugins.parentFile.mkdirs()