diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java index 3aada7837d8ae..1f569c2b36156 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java @@ -128,7 +128,7 @@ public void test30AbortWhenJavaMissing() { }); Platforms.onLinux(() -> { - final String javaPath = sh.run("which java").stdout.trim(); + final String javaPath = sh.run("command -v java").stdout.trim(); try { sh.run("chmod -x '" + javaPath + "'"); diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java index 1990840db7201..8a21381cbdfe5 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java @@ -31,6 +31,8 @@ import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -38,11 +40,13 @@ import static junit.framework.TestCase.assertTrue; import static org.elasticsearch.packaging.util.Cleanup.cleanEverything; import static org.elasticsearch.packaging.util.FileUtils.assertPathsDontExist; +import static org.elasticsearch.packaging.util.FileUtils.mv; import static org.elasticsearch.packaging.util.Packages.SYSTEMD_SERVICE; import static org.elasticsearch.packaging.util.Packages.assertInstalled; import static org.elasticsearch.packaging.util.Packages.assertRemoved; import static org.elasticsearch.packaging.util.Packages.install; import static org.elasticsearch.packaging.util.Packages.remove; +import static org.elasticsearch.packaging.util.Packages.runInstallCommand; import static org.elasticsearch.packaging.util.Packages.startElasticsearch; import static org.elasticsearch.packaging.util.Packages.verifyPackageInstallation; import static org.elasticsearch.packaging.util.Platforms.getOsRelease; @@ -77,6 +81,21 @@ public void onlyCompatibleDistributions() { assumeTrue("only compatible distributions", distribution().packaging.compatible); } + public void test05InstallFailsWhenJavaMissing() { + final Shell sh = new Shell(); + final Result java = sh.run("command -v java"); + + final Path originalJavaPath = Paths.get(java.stdout.trim()); + final Path relocatedJavaPath = originalJavaPath.getParent().resolve("java.relocated"); + try { + mv(originalJavaPath, relocatedJavaPath); + final Result installResult = runInstallCommand(distribution()); + assertThat(installResult.exitCode, is(1)); + } finally { + mv(relocatedJavaPath, originalJavaPath); + } + } + public void test10InstallPackage() { assertRemoved(distribution()); installation = install(distribution()); diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java index 6e80d9e027df2..be7edc5e8f9e4 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java @@ -67,7 +67,10 @@ public static void assertRemoved(Distribution distribution) { Platforms.onDPKG(() -> { assertThat(status.exitCode, anyOf(is(0), is(1))); if (status.exitCode == 0) { - assertTrue(Pattern.compile("(?m)^Status:.+deinstall ok").matcher(status.stdout).find()); + assertTrue("an uninstalled status should be indicated: " + status.stdout, + Pattern.compile("(?m)^Status:.+deinstall ok").matcher(status.stdout).find() || + Pattern.compile("(?m)^Status:.+ok not-installed").matcher(status.stdout).find() + ); } }); } @@ -90,13 +93,27 @@ public static Installation install(Distribution distribution) { } public static Installation install(Distribution distribution, String version) { + final Result result = runInstallCommand(distribution, version); + if (result.exitCode != 0) { + throw new RuntimeException("Installing distribution " + distribution + " version " + version + " failed: " + result); + } + + return Installation.ofPackage(distribution.packaging); + } + + public static Result runInstallCommand(Distribution distribution) { + return runInstallCommand(distribution, getCurrentVersion()); + } + + public static Result runInstallCommand(Distribution distribution, String version) { final Shell sh = new Shell(); final Path distributionFile = getDistributionFile(distribution, version); - Platforms.onRPM(() -> sh.run("rpm -i " + distributionFile)); - Platforms.onDPKG(() -> sh.run("dpkg -i " + distributionFile)); - - return Installation.ofPackage(distribution.packaging); + if (Platforms.isRPM()) { + return sh.runIgnoreExitCode("rpm -i " + distributionFile); + } else { + return sh.runIgnoreExitCode("dpkg -i " + distributionFile); + } } public static void remove(Distribution distribution) {