@@ -13,10 +13,12 @@ import org.gradle.api.tasks.Delete
13
13
import org.gradle.api.tasks.Exec
14
14
import org.gradle.api.tasks.TaskState
15
15
16
+ import static java.util.Collections.unmodifiableList
17
+
16
18
class VagrantTestPlugin implements Plugin<Project > {
17
19
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( [
20
22
' centos-6' ,
21
23
' centos-7' ,
22
24
' debian-8' ,
@@ -29,49 +31,53 @@ class VagrantTestPlugin implements Plugin<Project> {
29
31
' sles-12' ,
30
32
' ubuntu-1404' ,
31
33
' ubuntu-1604'
32
- ]
34
+ ])
33
35
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
+ ])
36
41
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 )
39
44
40
45
/* * Boxes used when sampling the tests **/
41
- static List<String > SAMPLE = [
46
+ static final List<String > SAMPLE = unmodifiableList( [
42
47
' 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 = []
45
53
46
54
/* * 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 = [:]
48
56
49
57
/* * 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( [
51
59
' archives:tar' ,
52
60
' archives:oss-tar' ,
53
61
' packages:rpm' ,
54
62
' packages:oss-rpm' ,
55
63
' packages:deb' ,
56
64
' packages:oss-deb'
57
- ]
65
+ ])
58
66
59
67
/* * 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' ])
61
69
62
70
private static final PACKAGING_CONFIGURATION = ' packaging'
63
71
private static final PACKAGING_TEST_CONFIGURATION = ' packagingTest'
64
72
private static final BATS = ' bats'
65
73
private static final String BATS_TEST_COMMAND = " cd \$ PACKAGING_ARCHIVES && sudo bats --tap \$ BATS_TESTS/*.$BATS "
66
74
private static final String PLATFORM_TEST_COMMAND = " rm -rf ~/elasticsearch && rsync -r /elasticsearch/ ~/elasticsearch && cd ~/elasticsearch && ./gradlew test integTest"
67
75
68
- static {
69
- collectAvailableBoxes()
70
- }
71
-
72
76
@Override
73
77
void apply (Project project ) {
74
78
79
+ collectAvailableBoxes(project)
80
+
75
81
// Creates the Vagrant extension for the project
76
82
project. extensions. create(' esvagrant' , VagrantPropertiesExtension , listSelectedBoxes(project))
77
83
@@ -86,12 +92,17 @@ class VagrantTestPlugin implements Plugin<Project> {
86
92
createVagrantTasks(project)
87
93
88
94
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 ' )
90
96
}
91
97
92
98
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} " )
95
106
}
96
107
}
97
108
@@ -102,26 +113,28 @@ class VagrantTestPlugin implements Plugin<Project> {
102
113
/**
103
114
* Enumerate all the boxes that we know about and could possibly choose to test
104
115
*/
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' )
107
122
if (windows_2012r2_box != null && windows_2012r2_box. isEmpty() == false ) {
108
- WINDOWS_BOXES . add(' windows-2012r2' )
123
+ AVAILABLE_BOXES . add(' windows-2012r2' )
109
124
VAGRANT_BOX_ENV_VARS [' VAGRANT_WINDOWS_2012R2_BOX' ] = windows_2012r2_box
110
125
}
111
126
112
- String windows_2016_box = System . getenv( ' VAGRANT_WINDOWS_2016_BOX ' )
127
+ String windows_2016_box = project . getProperties() . get( ' vagrant.windows-2016.id ' )
113
128
if (windows_2016_box != null && windows_2016_box. isEmpty() == false ) {
114
- WINDOWS_BOXES . add(' windows-2016' )
129
+ AVAILABLE_BOXES . add(' windows-2016' )
115
130
VAGRANT_BOX_ENV_VARS [' VAGRANT_WINDOWS_2016_BOX' ] = windows_2016_box
116
131
}
117
-
118
- BOXES = LINUX_BOXES + WINDOWS_BOXES
119
132
}
120
133
121
134
/**
122
135
* Enumerate all the boxes that we have chosen to test
123
136
*/
124
- private List<String > listSelectedBoxes (Project project ) {
137
+ private static List<String > listSelectedBoxes (Project project ) {
125
138
String vagrantBoxes = project. getProperties(). get(' vagrant.boxes' , ' sample' )
126
139
switch (vagrantBoxes) {
127
140
case ' sample' :
@@ -131,7 +144,9 @@ class VagrantTestPlugin implements Plugin<Project> {
131
144
case ' windows-all' :
132
145
return WINDOWS_BOXES
133
146
case ' all' :
134
- return BOXES
147
+ return ALL_BOXES
148
+ case ' ' :
149
+ return []
135
150
default :
136
151
return vagrantBoxes. split(' ,' )
137
152
}
@@ -314,36 +329,57 @@ class VagrantTestPlugin implements Plugin<Project> {
314
329
private static void createPackagingTestTask (Project project ) {
315
330
project. tasks. create(' packagingTest' ) {
316
331
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."
322
333
dependsOn ' vagrantCheckVersion'
323
334
}
324
335
}
325
336
326
337
private static void createPlatformTestTask (Project project ) {
327
338
project. tasks. create(' platformTest' ) {
328
339
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
+ }
333
368
dependsOn ' vagrantCheckVersion'
334
369
}
335
370
}
336
371
337
- private static void createVagrantTasks (Project project ) {
372
+ private void createVagrantTasks (Project project ) {
338
373
createCleanTask(project)
339
374
createStopTask(project)
340
375
createSmokeTestTask(project)
341
376
createPrepareVagrantTestEnvTask(project)
342
377
createPackagingTestTask(project)
343
378
createPlatformTestTask(project)
379
+ createBoxListTasks(project)
344
380
}
345
381
346
- private static void createVagrantBoxesTasks (Project project ) {
382
+ private void createVagrantBoxesTasks (Project project ) {
347
383
assert project. extensions. esvagrant. boxes != null
348
384
349
385
assert project. tasks. stop != null
@@ -375,10 +411,11 @@ class VagrantTestPlugin implements Plugin<Project> {
375
411
' VAGRANT_CWD' : " ${ project.rootDir.absolutePath} " ,
376
412
' VAGRANT_VAGRANTFILE' : ' Vagrantfile' ,
377
413
' VAGRANT_PROJECT_DIR' : " ${ project.projectDir.absolutePath} "
378
- ] + VAGRANT_BOX_ENV_VARS
414
+ ]
415
+ vagrantEnvVars. putAll(VAGRANT_BOX_ENV_VARS )
379
416
380
417
// Each box gets it own set of tasks
381
- for (String box : BOXES ) {
418
+ for (String box : AVAILABLE_BOXES ) {
382
419
String boxTask = box. capitalize(). replace(' -' , ' ' )
383
420
384
421
// 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> {
445
482
finalizedBy halt
446
483
}
447
484
vagrantSmokeTest. dependsOn(smoke)
448
- if (box in LINUX_BOXES ) {
485
+ if (LINUX_BOXES . contains(box) ) {
449
486
smoke. commandLine = [' vagrant' , ' ssh' , box, ' --command' ,
450
487
" set -o pipefail && echo 'Hello from ${ project.path} ' | sed -ue 's/^/ ${ box} : /'" ]
451
488
} else {
452
489
smoke. commandLine = [' vagrant' , ' winrm' , box, ' --command' ,
453
490
" Write-Host ' ${ box} : Hello from ${ project.path} '" ]
454
491
}
455
492
456
- if (box in LINUX_BOXES ) {
493
+ if (LINUX_BOXES . contains(box) ) {
457
494
Task batsPackagingTest = project. tasks. create(" vagrant${ boxTask} #batsPackagingTest" , BatsOverVagrantTask ) {
458
495
remoteCommand BATS_TEST_COMMAND
459
496
boxName box
@@ -486,7 +523,7 @@ class VagrantTestPlugin implements Plugin<Project> {
486
523
project. extensions. esvagrant. testClass != null
487
524
}
488
525
489
- if (box in LINUX_BOXES ) {
526
+ if (LINUX_BOXES . contains(box) ) {
490
527
javaPackagingTest. command = ' ssh'
491
528
javaPackagingTest. args = [' --command' , ' bash "$PACKAGING_TESTS/run-tests.sh"' ]
492
529
} else {
@@ -509,7 +546,7 @@ class VagrantTestPlugin implements Plugin<Project> {
509
546
* This test is unmaintained and was created to run on Linux. We won't allow it to run on Windows
510
547
* until it's been brought back into maintenance
511
548
*/
512
- if (box in LINUX_BOXES ) {
549
+ if (LINUX_BOXES . contains(box) ) {
513
550
Task platform = project. tasks. create(" vagrant${ boxTask} #platformTest" , VagrantCommandTask ) {
514
551
command ' ssh'
515
552
boxName box
0 commit comments