Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] package pre-install java check #32259

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,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 @@ -30,16 +30,20 @@

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 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 @@ -75,6 +79,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()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the more I see of this, the more I think that it's worth extending the onDPKG type of API to be able to produce values so reading trough the code is more consistent.

Copy link
Contributor Author

@andyb-elastic andyb-elastic Jul 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The version of that I tried looked like this

Result result = Platforms.onRpm("...")
if (result == null) {
  result = Platforms.onDpkg("...")
}
return result;

because the onFoo methods still have to return something when they don't run their action. Which doesn't seem much more readable to me. Doing it with an Optional is a little better. Is there a better one that I'm missing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

   Result result = Platforms.packagingChoice()
        .onRpm("...")
        .onDpkg()
        .get()

The packagingChoice() is optional, only if you don't want to keep the Platforms class growing.
The onRpm() etc type of methods will an instance, say ResultTracker<Result> and depending of the choice to be made will or will not add a result to it. get() will just return the result or throw an exception. We could also have a ResultTracker<Void> and a decide() method instead of get() that doesn't return anything. Maybe PackagingDecisionMaker is a better name than ResultTracker. Since this is for testing, we could have all the result trackers/decision makers register into some static context in Platforms perhaps, and add a check after each test to make sure that all of them are "decided" to make sure that one doesn't Platforms.onmRpm().onDpkg() without forgetting to call decide().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right we had something similar in a discussion on another PR, thanks. I'll likely open another one to make that change since we have a few places that would benefit from it now

return sh.runIgnoreExitCode("rpm -i " + distributionFile);
} else {
return sh.runIgnoreExitCode("dpkg -i " + distributionFile);
}
}

public static void remove(Distribution distribution) {
Expand Down