From 223b84cf27405508dc341b6d08aeb2039d30d780 Mon Sep 17 00:00:00 2001 From: Andy Bristol Date: Mon, 23 Jul 2018 11:45:04 -0700 Subject: [PATCH] [test] package pre-install java check (#32259) This recreates a test that was added to the bats packaging tests in #31343 but didn't make it over to the java project during when the linux package tests were ported in #31943 When packages are installed but can not locate the java executable, they should fail with a descriptive message --- .../packaging/test/ArchiveTestCase.java | 2 +- .../packaging/test/PackageTestCase.java | 19 +++++++++++++ .../packaging/util/Packages.java | 27 +++++++++++++++---- 3 files changed, 42 insertions(+), 6 deletions(-) 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) {