Skip to content

Commit f706ea1

Browse files
committed
[test] java tests for archive packaging
Ports the first couple tests for archive distributions from the old bats project to the new java project that includes windows platforms, consolidating them into one test method that tests that the distributions can be extracted and their contents verified. Includes the zip distributions which were not tested in the bats project. For elastic#26741
1 parent a8b4a98 commit f706ea1

19 files changed

+1300
-17
lines changed

TESTING.asciidoc

+24-4
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,9 @@ into it
472472
vagrant ssh debian-9
473473
--------------------------------------------
474474

475-
Now inside the VM, to run the https://github.com/sstephenson/bats[bats] packaging tests
475+
Now inside the VM, start the packaging tests from the terminal. There are two packaging
476+
test projects. The old ones are written with https://github.com/sstephenson/bats[bats]
477+
and only run on linux. To run them do
476478

477479
--------------------------------------------
478480
cd $PACKAGING_ARCHIVES
@@ -484,18 +486,36 @@ sudo bats $BATS_TESTS/*.bats
484486
sudo bats $BATS_TESTS/20_tar_package.bats $BATS_TESTS/25_tar_plugins.bats
485487
--------------------------------------------
486488

487-
To run the Java packaging tests, again inside the VM
489+
The new packaging tests are written in Java and run on both linux and windows. On
490+
linux (again, inside the VM)
488491

489492
--------------------------------------------
490-
bash $PACKAGING_TESTS/run-tests.sh
493+
# run the full suite
494+
sudo bash $PACKAGING_TESTS/run-tests.sh
495+
496+
# run specific test cases
497+
sudo bash $PACKAGING_TESTS/run-tests.sh \
498+
org.elasticsearch.packaging.test.DefaultZipTests \
499+
org.elasticsearch.packaging.test.OssZipTests
491500
--------------------------------------------
492501

493-
or on Windows
502+
or on Windows, from a terminal running as Administrator
494503

495504
--------------------------------------------
505+
# run the full suite
496506
powershell -File $Env:PACKAGING_TESTS/run-tests.ps1
507+
508+
# run specific test cases
509+
powershell -File $Env:PACKAGING_TESTS/run-tests.ps1 `
510+
org.elasticsearch.packaging.test.DefaultZipTests `
511+
org.elasticsearch.packaging.test.OssZipTests
497512
--------------------------------------------
498513

514+
Note that on Windows boxes when running from inside the GUI, you may have to log out and
515+
back in to the `vagrant` user (password `vagrant`) for the environment variables that
516+
locate the packaging tests and distributions to take effect, due to how vagrant provisions
517+
Windows machines.
518+
499519
When you've made changes you want to test, keep the VM up and reload the tests and
500520
distributions inside by running (on the host)
501521

Vagrantfile

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ def linux_common(config,
237237

238238
config.vm.provision 'markerfile', type: 'shell', inline: <<-SHELL
239239
touch /etc/is_vagrant_vm
240+
touch /is_vagrant_vm # for consistency between linux and windows
240241
SHELL
241242

242243
# This prevents leftovers from previous tests using the

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

+20-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class VagrantTestPlugin implements Plugin<Project> {
5252
static final List<String> DISTRIBUTIONS = unmodifiableList([
5353
'archives:tar',
5454
'archives:oss-tar',
55+
'archives:zip',
56+
'archives:oss-zip',
5557
'packages:rpm',
5658
'packages:oss-rpm',
5759
'packages:deb',
@@ -242,13 +244,27 @@ class VagrantTestPlugin implements Plugin<Project> {
242244
Task createLinuxRunnerScript = project.tasks.create('createLinuxRunnerScript', FileContentsTask) {
243245
dependsOn copyPackagingTests
244246
file "${testsDir}/run-tests.sh"
245-
contents "java -cp \"\$PACKAGING_TESTS/*\" org.junit.runner.JUnitCore ${-> project.extensions.esvagrant.testClass}"
247+
contents """\
248+
if [ "\$#" -eq 0 ]; then
249+
test_args=( "${-> project.extensions.esvagrant.testClass}" )
250+
else
251+
test_args=( "\$@" )
252+
fi
253+
java -cp "\$PACKAGING_TESTS/*" org.elasticsearch.packaging.VMTestRunner "\${test_args[@]}"
254+
"""
246255
}
247256
Task createWindowsRunnerScript = project.tasks.create('createWindowsRunnerScript', FileContentsTask) {
248257
dependsOn copyPackagingTests
249258
file "${testsDir}/run-tests.ps1"
259+
// the use of $args rather than param() here is deliberate because the syntax for array (multivalued) parameters is likely
260+
// a little trappy for those unfamiliar with powershell
250261
contents """\
251-
java -cp "\$Env:PACKAGING_TESTS/*" org.junit.runner.JUnitCore ${-> project.extensions.esvagrant.testClass}
262+
if (\$args.Count -eq 0) {
263+
\$testArgs = @("${-> project.extensions.esvagrant.testClass}")
264+
} else {
265+
\$testArgs = \$args
266+
}
267+
java -cp "\$Env:PACKAGING_TESTS/*" org.elasticsearch.packaging.VMTestRunner @testArgs
252268
exit \$LASTEXITCODE
253269
"""
254270
}
@@ -525,9 +541,10 @@ class VagrantTestPlugin implements Plugin<Project> {
525541

526542
if (LINUX_BOXES.contains(box)) {
527543
javaPackagingTest.command = 'ssh'
528-
javaPackagingTest.args = ['--command', 'bash "$PACKAGING_TESTS/run-tests.sh"']
544+
javaPackagingTest.args = ['--command', 'sudo bash "$PACKAGING_TESTS/run-tests.sh"']
529545
} else {
530546
javaPackagingTest.command = 'winrm'
547+
// winrm commands run as administrator
531548
javaPackagingTest.args = ['--command', 'powershell -File "$Env:PACKAGING_TESTS/run-tests.ps1"']
532549
}
533550

qa/vagrant/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ plugins {
2929
dependencies {
3030
compile "junit:junit:${versions.junit}"
3131
compile "org.hamcrest:hamcrest-core:${versions.hamcrest}"
32+
compile "org.hamcrest:hamcrest-library:${versions.hamcrest}"
3233

33-
// needs to be on the classpath for JarHell
34-
testRuntime project(':libs:elasticsearch-core')
34+
compile project(':libs:elasticsearch-core')
3535

3636
// pulls in the jar built by this project and its dependencies
3737
packagingTest project(path: project.path, configuration: 'runtime')

qa/vagrant/src/main/java/org/elasticsearch/packaging/PackagingTests.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@
1919

2020
package org.elasticsearch.packaging;
2121

22-
import org.junit.Test;
22+
import org.elasticsearch.packaging.test.OssTarTests;
23+
import org.elasticsearch.packaging.test.OssZipTests;
24+
import org.elasticsearch.packaging.test.DefaultTarTests;
25+
import org.elasticsearch.packaging.test.DefaultZipTests;
2326

24-
/**
25-
* This class doesn't have any tests yet
26-
*/
27-
public class PackagingTests {
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.Suite;
29+
import org.junit.runners.Suite.SuiteClasses;
2830

29-
@Test
30-
public void testDummy() {}
31-
}
31+
@RunWith(Suite.class)
32+
@SuiteClasses({
33+
DefaultTarTests.class,
34+
DefaultZipTests.class,
35+
OssTarTests.class,
36+
OssZipTests.class
37+
})
38+
public class PackagingTests {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.packaging;
21+
22+
import org.junit.runner.JUnitCore;
23+
24+
import java.nio.file.Files;
25+
import java.nio.file.Paths;
26+
27+
/**
28+
* Ensures that the current JVM is running on a virtual machine before delegating to {@link JUnitCore}. We just check for the existence
29+
* of a special file that we create during VM provisioning.
30+
*/
31+
public class VMTestRunner {
32+
public static void main(String[] args) {
33+
if (Files.exists(Paths.get("/is_vagrant_vm"))) {
34+
JUnitCore.main(args);
35+
} else {
36+
throw new RuntimeException("This filesystem does not have an expected marker file indicating it's a virtual machine. These " +
37+
"tests should only run in a virtual machine because they're destructive.");
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.packaging.test;
21+
22+
import org.junit.Before;
23+
import org.junit.BeforeClass;
24+
import org.junit.FixMethodOrder;
25+
import org.junit.Test;
26+
import org.junit.runners.MethodSorters;
27+
28+
import org.elasticsearch.packaging.util.Distribution;
29+
import org.elasticsearch.packaging.util.Installation;
30+
31+
import static org.elasticsearch.packaging.util.Cleanup.cleanEverything;
32+
import static org.elasticsearch.packaging.util.Archives.installArchive;
33+
import static org.elasticsearch.packaging.util.Archives.verifyArchiveInstallation;
34+
import static org.hamcrest.CoreMatchers.is;
35+
import static org.junit.Assume.assumeThat;
36+
37+
/**
38+
* Tests that apply to the archive distributions (tar, zip). To add a case for a distribution, subclass and
39+
* override {@link ArchiveTestCase#distribution()}. These tests should be the same across all archive distributions
40+
*/
41+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
42+
public abstract class ArchiveTestCase {
43+
44+
private static Installation installation;
45+
46+
/** The {@link Distribution} that should be tested in this case */
47+
protected abstract Distribution distribution();
48+
49+
@BeforeClass
50+
public static void cleanup() {
51+
installation = null;
52+
cleanEverything();
53+
}
54+
55+
@Before
56+
public void onlyCompatibleDistributions() {
57+
assumeThat(distribution().packaging.compatible, is(true));
58+
}
59+
60+
@Test
61+
public void test10Install() {
62+
installation = installArchive(distribution());
63+
verifyArchiveInstallation(installation, distribution());
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.packaging.test;
21+
22+
import org.elasticsearch.packaging.util.Distribution;
23+
24+
public class DefaultTarTests extends ArchiveTestCase {
25+
26+
@Override
27+
protected Distribution distribution() {
28+
return Distribution.DEFAULT_TAR;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.packaging.test;
21+
22+
import org.elasticsearch.packaging.util.Distribution;
23+
24+
public class DefaultZipTests extends ArchiveTestCase {
25+
26+
@Override
27+
protected Distribution distribution() {
28+
return Distribution.DEFAULT_ZIP;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.packaging.test;
21+
22+
import org.elasticsearch.packaging.util.Distribution;
23+
24+
public class OssTarTests extends ArchiveTestCase {
25+
26+
@Override
27+
protected Distribution distribution() {
28+
return Distribution.OSS_TAR;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.packaging.test;
21+
22+
import org.elasticsearch.packaging.util.Distribution;
23+
24+
public class OssZipTests extends ArchiveTestCase {
25+
26+
@Override
27+
protected Distribution distribution() {
28+
return Distribution.OSS_ZIP;
29+
}
30+
}

0 commit comments

Comments
 (0)