@@ -13,10 +13,12 @@ import org.gradle.api.tasks.Delete
1313import org.gradle.api.tasks.Exec
1414import org.gradle.api.tasks.TaskState
1515
16+ import static java.util.Collections.unmodifiableList
17+
1618class VagrantTestPlugin implements Plugin<Project > {
1719
18- /* * All available boxes **/
19- static List<String > LINUX_BOXES = [
20+ /* * All Linux boxes that we test. These are all always supplied **/
21+ static final List<String > LINUX_BOXES = unmodifiableList( [
2022 ' centos-6' ,
2123 ' centos-7' ,
2224 ' debian-8' ,
@@ -29,49 +31,53 @@ class VagrantTestPlugin implements Plugin<Project> {
2931 ' sles-12' ,
3032 ' ubuntu-1404' ,
3133 ' ubuntu-1604'
32- ]
34+ ])
3335
34- /* * Windows boxes that are available - map of box names to image IDs **/
35- static List<String > WINDOWS_BOXES = []
36+ /* * All Windows boxes that we test, which may or may not be supplied **/
37+ static final List<String > WINDOWS_BOXES = unmodifiableList([
38+ ' windows-2012r2' ,
39+ ' windows-2016'
40+ ])
3641
37- /* * All available boxes **/
38- static List<String > BOXES
42+ /* * All boxes that we test, some of which may not be supplied **/
43+ static final List<String > ALL_BOXES = unmodifiableList( LINUX_BOXES + WINDOWS_BOXES )
3944
4045 /* * Boxes used when sampling the tests **/
41- static List<String > SAMPLE = [
46+ static final List<String > SAMPLE = unmodifiableList( [
4247 ' centos-7' ,
43- ' ubuntu-1404' ,
44- ]
48+ ' ubuntu-1404'
49+ ])
50+
51+ /* * Boxes that have been supplied and are available for testing **/
52+ List<String > AVAILABLE_BOXES = []
4553
4654 /* * extra env vars to pass to vagrant for box configuration **/
47- static Map<String , String > VAGRANT_BOX_ENV_VARS = [:]
55+ Map<String , String > VAGRANT_BOX_ENV_VARS = [:]
4856
4957 /* * All distributions to bring into test VM, whether or not they are used **/
50- static List<String > DISTRIBUTIONS = [
58+ static final List<String > DISTRIBUTIONS = unmodifiableList( [
5159 ' archives:tar' ,
5260 ' archives:oss-tar' ,
5361 ' packages:rpm' ,
5462 ' packages:oss-rpm' ,
5563 ' packages:deb' ,
5664 ' packages:oss-deb'
57- ]
65+ ])
5866
5967 /* * Packages onboarded for upgrade tests **/
60- static List<String > UPGRADE_FROM_ARCHIVES = [' rpm' , ' deb' ]
68+ static final List<String > UPGRADE_FROM_ARCHIVES = unmodifiableList( [' rpm' , ' deb' ])
6169
6270 private static final PACKAGING_CONFIGURATION = ' packaging'
6371 private static final PACKAGING_TEST_CONFIGURATION = ' packagingTest'
6472 private static final BATS = ' bats'
6573 private static final String BATS_TEST_COMMAND = " cd \$ PACKAGING_ARCHIVES && sudo bats --tap \$ BATS_TESTS/*.$BATS "
6674 private static final String PLATFORM_TEST_COMMAND = " rm -rf ~/elasticsearch && rsync -r /elasticsearch/ ~/elasticsearch && cd ~/elasticsearch && ./gradlew test integTest"
6775
68- static {
69- collectAvailableBoxes()
70- }
71-
7276 @Override
7377 void apply (Project project ) {
7478
79+ collectAvailableBoxes(project)
80+
7581 // Creates the Vagrant extension for the project
7682 project. extensions. create(' esvagrant' , VagrantPropertiesExtension , listSelectedBoxes(project))
7783
@@ -86,12 +92,17 @@ class VagrantTestPlugin implements Plugin<Project> {
8692 createVagrantTasks(project)
8793
8894 if (project. extensions. esvagrant. boxes == null || project. extensions. esvagrant. boxes. size() == 0 ) {
89- throw new InvalidUserDataException (' Vagrant boxes cannot be null or empty for esvagrant ' )
95+ throw new InvalidUserDataException (' Must specify at least one vagrant box ' )
9096 }
9197
9298 for (String box : project. extensions. esvagrant. boxes) {
93- if (BOXES . contains(box) == false ) {
94- throw new InvalidUserDataException (" Vagrant box [${ box} ] not found, available virtual machines are ${ BOXES} " )
99+ if (ALL_BOXES . contains(box) == false ) {
100+ throw new InvalidUserDataException (" Vagrant box [${ box} ] is unknown to this plugin. Valid boxes are ${ ALL_BOXES} " )
101+ }
102+
103+ if (AVAILABLE_BOXES . contains(box) == false ) {
104+ throw new InvalidUserDataException (" Vagrant box [${ box} ] is not available because an image is not supplied for it. " +
105+ " Available boxes with supplied images are ${ AVAILABLE_BOXES} " )
95106 }
96107 }
97108
@@ -102,26 +113,28 @@ class VagrantTestPlugin implements Plugin<Project> {
102113 /**
103114 * Enumerate all the boxes that we know about and could possibly choose to test
104115 */
105- private static void collectAvailableBoxes () {
106- String windows_2012r2_box = System . getenv(' VAGRANT_WINDOWS_2012R2_BOX' )
116+ private void collectAvailableBoxes (Project project ) {
117+ // these images are hardcoded in the Vagrantfile and are always available
118+ AVAILABLE_BOXES . addAll(LINUX_BOXES )
119+
120+ // these images need to be provided at runtime
121+ String windows_2012r2_box = project. getProperties(). get(' vagrant.windows-2012r2.id' )
107122 if (windows_2012r2_box != null && windows_2012r2_box. isEmpty() == false ) {
108- WINDOWS_BOXES . add(' windows-2012r2' )
123+ AVAILABLE_BOXES . add(' windows-2012r2' )
109124 VAGRANT_BOX_ENV_VARS [' VAGRANT_WINDOWS_2012R2_BOX' ] = windows_2012r2_box
110125 }
111126
112- String windows_2016_box = System . getenv( ' VAGRANT_WINDOWS_2016_BOX ' )
127+ String windows_2016_box = project . getProperties() . get( ' vagrant.windows-2016.id ' )
113128 if (windows_2016_box != null && windows_2016_box. isEmpty() == false ) {
114- WINDOWS_BOXES . add(' windows-2016' )
129+ AVAILABLE_BOXES . add(' windows-2016' )
115130 VAGRANT_BOX_ENV_VARS [' VAGRANT_WINDOWS_2016_BOX' ] = windows_2016_box
116131 }
117-
118- BOXES = LINUX_BOXES + WINDOWS_BOXES
119132 }
120133
121134 /**
122135 * Enumerate all the boxes that we have chosen to test
123136 */
124- private List<String > listSelectedBoxes (Project project ) {
137+ private static List<String > listSelectedBoxes (Project project ) {
125138 String vagrantBoxes = project. getProperties(). get(' vagrant.boxes' , ' sample' )
126139 switch (vagrantBoxes) {
127140 case ' sample' :
@@ -131,7 +144,9 @@ class VagrantTestPlugin implements Plugin<Project> {
131144 case ' windows-all' :
132145 return WINDOWS_BOXES
133146 case ' all' :
134- return BOXES
147+ return ALL_BOXES
148+ case ' ' :
149+ return []
135150 default :
136151 return vagrantBoxes. split(' ,' )
137152 }
@@ -314,36 +329,57 @@ class VagrantTestPlugin implements Plugin<Project> {
314329 private static void createPackagingTestTask (Project project ) {
315330 project. tasks. create(' packagingTest' ) {
316331 group ' Verification'
317- description " Tests yum/apt packages using vagrant and bats.\n " +
318- " Specify the vagrant boxes to test using the gradle property 'vagrant.boxes'.\n " +
319- " 'sample' can be used to test a single yum and apt box. 'all' can be used to\n " +
320- " test all available boxes. The available boxes are: \n " +
321- " ${ BOXES} "
332+ description " Tests distribution installation on different platforms using vagrant. See TESTING.asciidoc for details."
322333 dependsOn ' vagrantCheckVersion'
323334 }
324335 }
325336
326337 private static void createPlatformTestTask (Project project ) {
327338 project. tasks. create(' platformTest' ) {
328339 group ' Verification'
329- description " Test unit and integ tests on different platforms using vagrant.\n " +
330- " Specify the vagrant boxes to test using the gradle property 'vagrant.boxes'.\n " +
331- " 'all' can be used to test all available boxes. The available boxes are: \n " +
332- " ${ BOXES} "
340+ description " Test unit and integ tests on different platforms using vagrant. See TESTING.asciidoc for details. This test " +
341+ " is unmaintained."
342+ dependsOn ' vagrantCheckVersion'
343+ }
344+ }
345+
346+ private void createBoxListTasks (Project project ) {
347+ project. tasks. create(' listAllBoxes' ) {
348+ group ' Verification'
349+ description ' List all vagrant boxes which can be tested by this plugin'
350+ doLast {
351+ println (" All vagrant boxes supported by ${ project.path} " )
352+ for (String box : ALL_BOXES ) {
353+ println (box)
354+ }
355+ }
356+ dependsOn ' vagrantCheckVersion'
357+ }
358+
359+ project. tasks. create(' listAvailableBoxes' ) {
360+ group ' Verification'
361+ description ' List all vagrant boxes which are available for testing'
362+ doLast {
363+ println (" All vagrant boxes available to ${ project.path} " )
364+ for (String box : AVAILABLE_BOXES ) {
365+ println (box)
366+ }
367+ }
333368 dependsOn ' vagrantCheckVersion'
334369 }
335370 }
336371
337- private static void createVagrantTasks (Project project ) {
372+ private void createVagrantTasks (Project project ) {
338373 createCleanTask(project)
339374 createStopTask(project)
340375 createSmokeTestTask(project)
341376 createPrepareVagrantTestEnvTask(project)
342377 createPackagingTestTask(project)
343378 createPlatformTestTask(project)
379+ createBoxListTasks(project)
344380 }
345381
346- private static void createVagrantBoxesTasks (Project project ) {
382+ private void createVagrantBoxesTasks (Project project ) {
347383 assert project. extensions. esvagrant. boxes != null
348384
349385 assert project. tasks. stop != null
@@ -375,10 +411,11 @@ class VagrantTestPlugin implements Plugin<Project> {
375411 ' VAGRANT_CWD' : " ${ project.rootDir.absolutePath} " ,
376412 ' VAGRANT_VAGRANTFILE' : ' Vagrantfile' ,
377413 ' VAGRANT_PROJECT_DIR' : " ${ project.projectDir.absolutePath} "
378- ] + VAGRANT_BOX_ENV_VARS
414+ ]
415+ vagrantEnvVars. putAll(VAGRANT_BOX_ENV_VARS )
379416
380417 // Each box gets it own set of tasks
381- for (String box : BOXES ) {
418+ for (String box : AVAILABLE_BOXES ) {
382419 String boxTask = box. capitalize(). replace(' -' , ' ' )
383420
384421 // always add a halt task for all boxes, so clean makes sure they are all shutdown
@@ -445,15 +482,15 @@ class VagrantTestPlugin implements Plugin<Project> {
445482 finalizedBy halt
446483 }
447484 vagrantSmokeTest. dependsOn(smoke)
448- if (box in LINUX_BOXES ) {
485+ if (LINUX_BOXES . contains(box) ) {
449486 smoke. commandLine = [' vagrant' , ' ssh' , box, ' --command' ,
450487 " set -o pipefail && echo 'Hello from ${ project.path} ' | sed -ue 's/^/ ${ box} : /'" ]
451488 } else {
452489 smoke. commandLine = [' vagrant' , ' winrm' , box, ' --command' ,
453490 " Write-Host ' ${ box} : Hello from ${ project.path} '" ]
454491 }
455492
456- if (box in LINUX_BOXES ) {
493+ if (LINUX_BOXES . contains(box) ) {
457494 Task batsPackagingTest = project. tasks. create(" vagrant${ boxTask} #batsPackagingTest" , BatsOverVagrantTask ) {
458495 remoteCommand BATS_TEST_COMMAND
459496 boxName box
@@ -486,7 +523,7 @@ class VagrantTestPlugin implements Plugin<Project> {
486523 project. extensions. esvagrant. testClass != null
487524 }
488525
489- if (box in LINUX_BOXES ) {
526+ if (LINUX_BOXES . contains(box) ) {
490527 javaPackagingTest. command = ' ssh'
491528 javaPackagingTest. args = [' --command' , ' bash "$PACKAGING_TESTS/run-tests.sh"' ]
492529 } else {
@@ -509,7 +546,7 @@ class VagrantTestPlugin implements Plugin<Project> {
509546 * This test is unmaintained and was created to run on Linux. We won't allow it to run on Windows
510547 * until it's been brought back into maintenance
511548 */
512- if (box in LINUX_BOXES ) {
549+ if (LINUX_BOXES . contains(box) ) {
513550 Task platform = project. tasks. create(" vagrant${ boxTask} #platformTest" , VagrantCommandTask ) {
514551 command ' ssh'
515552 boxName box
0 commit comments