Skip to content

Commit c42eade

Browse files
committed
Backporting "Sense for VirtualBox and $HOME when deciding to turn on vagrant testing. (#24636)"
1 parent 46667e5 commit c42eade

File tree

5 files changed

+145
-75
lines changed

5 files changed

+145
-75
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package org.elasticsearch.gradle.vagrant
2+
3+
import org.gradle.api.GradleException
4+
import org.gradle.api.InvalidUserDataException
5+
import org.gradle.api.Plugin
6+
import org.gradle.api.Project
7+
import org.gradle.process.ExecResult
8+
import org.gradle.process.internal.ExecException
9+
10+
/**
11+
* Global configuration for if Vagrant tasks are supported in this
12+
* build environment.
13+
*/
14+
class VagrantSupportPlugin implements Plugin<Project> {
15+
16+
@Override
17+
void apply(Project project) {
18+
if (project.rootProject.ext.has('vagrantEnvChecksDone') == false) {
19+
Map vagrantInstallation = getVagrantInstallation(project)
20+
Map virtualBoxInstallation = getVirtualBoxInstallation(project)
21+
22+
project.rootProject.ext.vagrantInstallation = vagrantInstallation
23+
project.rootProject.ext.virtualBoxInstallation = virtualBoxInstallation
24+
project.rootProject.ext.vagrantSupported = vagrantInstallation.supported && virtualBoxInstallation.supported
25+
project.rootProject.ext.vagrantEnvChecksDone = true
26+
27+
// Finding that HOME needs to be set when performing vagrant updates
28+
String homeLocation = System.getenv("HOME")
29+
if (project.rootProject.ext.vagrantSupported && homeLocation == null) {
30+
throw new GradleException("Could not locate \$HOME environment variable. Vagrant is enabled " +
31+
"and requires \$HOME to be set to function properly.")
32+
}
33+
}
34+
35+
addVerifyInstallationTasks(project)
36+
}
37+
38+
private Map getVagrantInstallation(Project project) {
39+
try {
40+
ByteArrayOutputStream pipe = new ByteArrayOutputStream()
41+
ExecResult runResult = project.exec {
42+
commandLine 'vagrant', '--version'
43+
standardOutput pipe
44+
ignoreExitValue true
45+
}
46+
String version = pipe.toString().trim()
47+
if (runResult.exitValue == 0) {
48+
if (version ==~ /Vagrant 1\.(8\.[6-9]|9\.[0-9])+/ || version ==~ /Vagrant 2\.[0-9]+\.[0-9]+/) {
49+
return [ 'supported' : true ]
50+
} else {
51+
return [ 'supported' : false,
52+
'info' : "Illegal version of vagrant [${version}]. Need [Vagrant 1.8.6+]" ]
53+
}
54+
} else {
55+
return [ 'supported' : false,
56+
'info' : "Could not read installed vagrant version:\n" + version ]
57+
}
58+
} catch (ExecException e) {
59+
// Exec still throws this if it cannot find the command, regardless if ignoreExitValue is set.
60+
// Swallow error. Vagrant isn't installed. Don't halt the build here.
61+
return [ 'supported' : false, 'info' : "Could not find vagrant: " + e.message ]
62+
}
63+
}
64+
65+
private Map getVirtualBoxInstallation(Project project) {
66+
try {
67+
ByteArrayOutputStream pipe = new ByteArrayOutputStream()
68+
ExecResult runResult = project.exec {
69+
commandLine 'vboxmanage', '--version'
70+
standardOutput = pipe
71+
ignoreExitValue true
72+
}
73+
String version = pipe.toString().trim()
74+
if (runResult.exitValue == 0) {
75+
try {
76+
String[] versions = version.split('\\.')
77+
int major = Integer.parseInt(versions[0])
78+
int minor = Integer.parseInt(versions[1])
79+
if ((major < 5) || (major == 5 && minor < 1)) {
80+
return [ 'supported' : false,
81+
'info' : "Illegal version of virtualbox [${version}]. Need [5.1+]" ]
82+
} else {
83+
return [ 'supported' : true ]
84+
}
85+
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
86+
return [ 'supported' : false,
87+
'info' : "Unable to parse version of virtualbox [${version}]. Required [5.1+]" ]
88+
}
89+
} else {
90+
return [ 'supported': false, 'info': "Could not read installed virtualbox version:\n" + version ]
91+
}
92+
} catch (ExecException e) {
93+
// Exec still throws this if it cannot find the command, regardless if ignoreExitValue is set.
94+
// Swallow error. VirtualBox isn't installed. Don't halt the build here.
95+
return [ 'supported' : false, 'info' : "Could not find virtualbox: " + e.message ]
96+
}
97+
}
98+
99+
private void addVerifyInstallationTasks(Project project) {
100+
createCheckVagrantVersionTask(project)
101+
createCheckVirtualBoxVersionTask(project)
102+
}
103+
104+
private void createCheckVagrantVersionTask(Project project) {
105+
project.tasks.create('vagrantCheckVersion') {
106+
description 'Check the Vagrant version'
107+
group 'Verification'
108+
doLast {
109+
if (project.rootProject.vagrantInstallation.supported == false) {
110+
throw new InvalidUserDataException(project.rootProject.vagrantInstallation.info)
111+
}
112+
}
113+
}
114+
}
115+
116+
private void createCheckVirtualBoxVersionTask(Project project) {
117+
project.tasks.create('virtualboxCheckVersion') {
118+
description 'Check the Virtualbox version'
119+
group 'Verification'
120+
doLast {
121+
if (project.rootProject.virtualBoxInstallation.supported == false) {
122+
throw new InvalidUserDataException(project.rootProject.virtualBoxInstallation.info)
123+
}
124+
}
125+
}
126+
}
127+
}

buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -228,43 +228,6 @@ class VagrantTestPlugin implements Plugin<Project> {
228228
vagrantSetUpTask.dependsOn copyBatsTests, copyBatsUtils, copyBatsArchives, createVersionFile, createUpgradeFromFile
229229
}
230230

231-
private static void createCheckVagrantVersionTask(Project project) {
232-
project.tasks.create('vagrantCheckVersion', Exec) {
233-
description 'Check the Vagrant version'
234-
group 'Verification'
235-
commandLine 'vagrant', '--version'
236-
standardOutput = new ByteArrayOutputStream()
237-
doLast {
238-
String version = standardOutput.toString().trim()
239-
if ((version ==~ /Vagrant 1\.(8\.[6-9]|9\.[0-9])+/) == false && (version ==~ /Vagrant 2\.[0-9]+\.[0-9]+/) == false) {
240-
throw new InvalidUserDataException("Illegal version of vagrant [${version}]. Need [Vagrant 1.8.6+]")
241-
}
242-
}
243-
}
244-
}
245-
246-
private static void createCheckVirtualBoxVersionTask(Project project) {
247-
project.tasks.create('virtualboxCheckVersion', Exec) {
248-
description 'Check the Virtualbox version'
249-
group 'Verification'
250-
commandLine 'vboxmanage', '--version'
251-
standardOutput = new ByteArrayOutputStream()
252-
doLast {
253-
String version = standardOutput.toString().trim()
254-
try {
255-
String[] versions = version.split('\\.')
256-
int major = Integer.parseInt(versions[0])
257-
int minor = Integer.parseInt(versions[1])
258-
if ((major < 5) || (major == 5 && minor < 1)) {
259-
throw new InvalidUserDataException("Illegal version of virtualbox [${version}]. Need [5.1+]")
260-
}
261-
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
262-
throw new InvalidUserDataException("Unable to parse version of virtualbox [${version}]. Required [5.1+]", e)
263-
}
264-
}
265-
}
266-
}
267-
268231
private static void createPackagingTestTask(Project project) {
269232
project.tasks.create('packagingTest') {
270233
group 'Verification'
@@ -292,8 +255,6 @@ class VagrantTestPlugin implements Plugin<Project> {
292255
createCleanTask(project)
293256
createStopTask(project)
294257
createSmokeTestTask(project)
295-
createCheckVagrantVersionTask(project)
296-
createCheckVirtualBoxVersionTask(project)
297258
createPrepareVagrantTestEnvTask(project)
298259
createPackagingTestTask(project)
299260
createPlatformTestTask(project)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=org.elasticsearch.gradle.vagrant.VagrantSupportPlugin

plugins/repository-hdfs/build.gradle

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ esplugin {
2929
classname 'org.elasticsearch.repositories.hdfs.HdfsPlugin'
3030
}
3131

32+
apply plugin: 'elasticsearch.vagrantsupport'
33+
3234
versions << [
3335
'hadoop2': '2.8.1'
3436
]
@@ -85,6 +87,7 @@ task krb5kdcUpdate(type: org.elasticsearch.gradle.vagrant.VagrantCommandTask) {
8587
subcommand 'update'
8688
boxName box
8789
environmentVars vagrantEnvVars
90+
dependsOn "vagrantCheckVersion", "virtualboxCheckVersion"
8891
}
8992

9093
task krb5kdcFixture(type: org.elasticsearch.gradle.test.VagrantFixture) {
@@ -135,7 +138,7 @@ task secureHdfsFixture(type: org.elasticsearch.gradle.test.AntFixture) {
135138
args miniHDFSArgs.toArray()
136139
}
137140

138-
boolean fixtureSupported = false;
141+
boolean fixtureSupported = false
139142
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
140143
// hdfs fixture will not start without hadoop native libraries on windows
141144
String nativePath = System.getenv("HADOOP_HOME")
@@ -163,48 +166,25 @@ if (fixtureSupported) {
163166
integTestRunner.systemProperty 'tests.rest.suite', 'hdfs_repository/10_basic'
164167
}
165168

166-
boolean secureFixtureSupported = false;
169+
boolean secureFixtureSupported = false
167170
if (fixtureSupported) {
168-
// Only do secure fixture support if the regular fixture is supported,
169-
// and if vagrant is installed. The ignoreExitValue on exec only matters
170-
// in cases where the command can be found and successfully started. In
171-
// situations where the vagrant command isn't able to be started at all
172-
// (it's not installed) then Gradle still throws ExecException.
173-
ByteArrayOutputStream pipe = new ByteArrayOutputStream()
174-
try {
175-
ExecResult runResult = exec {
176-
commandLine 'vagrant', '--version'
177-
standardOutput pipe
178-
ignoreExitValue true
179-
}
180-
String output = pipe.toString().trim()
181-
if (runResult.exitValue == 0) {
182-
secureFixtureSupported = (output ==~ /Vagrant 1\.(8\.[6-9]|9\.[0-9])+/)
183-
} else {
184-
logger.warn("Could not read installed vagrant version:\n" + output)
185-
}
186-
} catch (org.gradle.process.internal.ExecException e) {
187-
logger.warn("Could not find vagrant: " + e.message)
188-
// Swallow error. Vagrant isn't installed. Leave secure fixture support off.
189-
}
171+
secureFixtureSupported = project.rootProject.vagrantSupported
190172
}
191173

192174
// Create a Integration Test suite just for security based tests
193-
if (secureFixtureSupported && false) { // This fails due to a vagrant configuration issue - remove the false check to re-enable
194-
// This must execute before the afterEvaluate block from integTestSecure
195-
project.afterEvaluate {
196-
Path elasticsearchKT = project(':test:fixtures:krb5kdc-fixture').buildDir.toPath().resolve("keytabs").resolve("elasticsearch.keytab").toAbsolutePath()
197-
Path krb5conf = project(':test:fixtures:krb5kdc-fixture').buildDir.toPath().resolve("conf").resolve("krb5.conf").toAbsolutePath()
175+
// This must execute before the afterEvaluate block from integTestSecure
176+
project.afterEvaluate {
177+
Path elasticsearchKT = project(':test:fixtures:krb5kdc-fixture').buildDir.toPath().resolve("keytabs").resolve("elasticsearch.keytab").toAbsolutePath()
178+
Path krb5conf = project(':test:fixtures:krb5kdc-fixture').buildDir.toPath().resolve("conf").resolve("krb5.conf").toAbsolutePath()
198179

199180
project.integTestSecureCluster.dependsOn(project.bundlePlugin)
200181
project.integTestSecure.clusterConfig.plugin(project.path)
201182
project.integTestSecure.clusterConfig.extraConfigFile("repository-hdfs/krb5.keytab", "${elasticsearchKT}")
202-
203183
final String baseJvmArgs = "-Xms" + System.getProperty('tests.heap.size', '512m') +
204184
" " + "-Xmx" + System.getProperty('tests.heap.size', '512m') +
205185
" " + "-Djava.security.krb5.conf=${krb5conf}" +
206186
" " + System.getProperty('tests.jvm.argline', '')
207-
final String jvmArgs
187+
final String jvmArgs
208188
if (project.rootProject.ext.javaVersion == JavaVersion.VERSION_1_9) {
209189
jvmArgs = baseJvmArgs + " " + '--add-opens=java.security.jgss/sun.security.krb5=ALL-UNNAMED'
210190
} else {
@@ -214,18 +194,18 @@ if (secureFixtureSupported && false) { // This fails due to a vagrant configurat
214194
project.integTestSecure.clusterConfig.jvmArgs = jvmArgs
215195
}
216196

217-
RestIntegTestTask integTestSecure = project.tasks.create('integTestSecure', RestIntegTestTask.class) {
218-
description = "Runs rest tests against an elasticsearch cluster with HDFS secured by MIT Kerberos."
219-
}
197+
RestIntegTestTask integTestSecure = project.tasks.create('integTestSecure', RestIntegTestTask.class) {
198+
description = "Runs rest tests against an elasticsearch cluster with HDFS secured by MIT Kerberos."
199+
}
220200

221-
integTestSecure.mustRunAfter(project.integTest)
201+
if (secureFixtureSupported) {
222202
project.check.dependsOn(integTestSecure)
223203

224204
// Fixture dependencies
225205
integTestSecureCluster.dependsOn secureHdfsFixture, krb5kdcFixture
226206
integTestSecureRunner.systemProperty 'tests.rest.suite', 'secure_hdfs_repository'
227207
} else {
228-
logger.warn("secured hdfsFixture is unsupported, please install Vagrant 1.8.6+ to enable")
208+
integTestSecure.enabled = false
229209
}
230210

231211
thirdPartyAudit.excludes = [

qa/vagrant/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* under the License.
1818
*/
1919

20+
apply plugin: 'elasticsearch.vagrantsupport'
2021
apply plugin: 'elasticsearch.vagrant'
2122

2223
List<String> plugins = []

0 commit comments

Comments
 (0)