Skip to content

Commit

Permalink
[test] package pre-install java check (elastic#32259)
Browse files Browse the repository at this point in the history
This recreates a test that was added to the bats packaging tests
in elastic#31343 but didn't make it over to the java project during when the
linux package tests were ported in elastic#31943

When packages are installed but can not locate the java executable, they
should fail with a descriptive message
  • Loading branch information
andyb-elastic committed Jul 24, 2018
1 parent 1c32896 commit 223b84c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@

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;

import static junit.framework.TestCase.assertFalse;
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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
});
}
Expand All @@ -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) {
Expand Down

0 comments on commit 223b84c

Please sign in to comment.