Skip to content

Commit 07e830e

Browse files
[test] java tests for archive packaging (#30734)
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.
1 parent 621a26b commit 07e830e

19 files changed

+1313
-17
lines changed

TESTING.asciidoc

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,9 @@ into it
512512
vagrant ssh debian-9
513513
--------------------------------------------
514514

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

517519
--------------------------------------------
518520
cd $PACKAGING_ARCHIVES
@@ -524,18 +526,36 @@ sudo bats $BATS_TESTS/*.bats
524526
sudo bats $BATS_TESTS/20_tar_package.bats $BATS_TESTS/25_tar_plugins.bats
525527
--------------------------------------------
526528

527-
To run the Java packaging tests, again inside the VM
529+
The new packaging tests are written in Java and run on both linux and windows. On
530+
linux (again, inside the VM)
528531

529532
--------------------------------------------
530-
bash $PACKAGING_TESTS/run-tests.sh
533+
# run the full suite
534+
sudo bash $PACKAGING_TESTS/run-tests.sh
535+
536+
# run specific test cases
537+
sudo bash $PACKAGING_TESTS/run-tests.sh \
538+
org.elasticsearch.packaging.test.DefaultZipTests \
539+
org.elasticsearch.packaging.test.OssZipTests
531540
--------------------------------------------
532541

533-
or on Windows
542+
or on Windows, from a terminal running as Administrator
534543

535544
--------------------------------------------
545+
# run the full suite
536546
powershell -File $Env:PACKAGING_TESTS/run-tests.ps1
547+
548+
# run specific test cases
549+
powershell -File $Env:PACKAGING_TESTS/run-tests.ps1 `
550+
org.elasticsearch.packaging.test.DefaultZipTests `
551+
org.elasticsearch.packaging.test.OssZipTests
537552
--------------------------------------------
538553

554+
Note that on Windows boxes when running from inside the GUI, you may have to log out and
555+
back in to the `vagrant` user (password `vagrant`) for the environment variables that
556+
locate the packaging tests and distributions to take effect, due to how vagrant provisions
557+
Windows machines.
558+
539559
When you've made changes you want to test, keep the VM up and reload the tests and
540560
distributions inside by running (on the host)
541561

Vagrantfile

Lines changed: 1 addition & 0 deletions
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

Lines changed: 20 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 15 additions & 8 deletions
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 {}
Lines changed: 40 additions & 0 deletions
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+
}
Lines changed: 65 additions & 0 deletions
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+
}
Lines changed: 30 additions & 0 deletions
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+
}
Lines changed: 30 additions & 0 deletions
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+
}
Lines changed: 30 additions & 0 deletions
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+
}
Lines changed: 30 additions & 0 deletions
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)