Skip to content

Commit

Permalink
Bat scripts to work with JAVA_HOME with parentheses Backports (#40770)
Browse files Browse the repository at this point in the history
backports :
Migrate systemd packaging tests from bats to java (#39954)
Bat scripts to work with JAVA_HOME with parentheses (#39712)
Link to SYSTEM_JAVA_HOME on windows (#40806)
  • Loading branch information
pgomulka authored Apr 4, 2019
1 parent d7a6772 commit e15e337
Show file tree
Hide file tree
Showing 15 changed files with 358 additions and 332 deletions.
2 changes: 1 addition & 1 deletion distribution/src/bin/elasticsearch-env.bat
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ set ES_DISTRIBUTION_TYPE=${es.distribution.type}
set ES_BUNDLED_JDK=${es.bundled_jdk}

if not defined ES_TMPDIR (
for /f "tokens=* usebackq" %%a in (`"%JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.TempDirectory""`) do set ES_TMPDIR=%%a
for /f "tokens=* usebackq" %%a in (`CALL %JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.TempDirectory"`) do set ES_TMPDIR=%%a
)

cd /d "%ES_HOME%"
4 changes: 2 additions & 2 deletions distribution/src/bin/elasticsearch.bat
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ IF ERRORLEVEL 1 (
EXIT /B %ERRORLEVEL%
)

set "ES_JVM_OPTIONS=%ES_PATH_CONF%\jvm.options"
set ES_JVM_OPTIONS=%ES_PATH_CONF%\jvm.options
@setlocal
for /F "usebackq delims=" %%a in (`"%JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.JvmOptionsParser" "!ES_JVM_OPTIONS!" || echo jvm_options_parser_failed"`) do set JVM_OPTIONS=%%a
for /F "usebackq delims=" %%a in (`CALL %JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.JvmOptionsParser" "!ES_JVM_OPTIONS!" ^|^| echo jvm_options_parser_failed`) do set JVM_OPTIONS=%%a
@endlocal & set "MAYBE_JVM_OPTIONS_PARSER_FAILED=%JVM_OPTIONS%" & set ES_JAVA_OPTS=%JVM_OPTIONS:${ES_TMPDIR}=!ES_TMPDIR!% %ES_JAVA_OPTS%

if "%MAYBE_JVM_OPTIONS_PARSER_FAILED%" == "jvm_options_parser_failed" (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@
package org.elasticsearch.packaging.test;

import com.carrotsearch.randomizedtesting.annotations.TestCaseOrdering;
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
import org.apache.http.client.fluent.Request;
import org.elasticsearch.packaging.util.Archives;
import org.elasticsearch.packaging.util.Distribution;
import org.elasticsearch.packaging.util.FileUtils;
import org.elasticsearch.packaging.util.Installation;
import org.elasticsearch.packaging.util.Platforms;
import org.elasticsearch.packaging.util.ServerUtils;
import org.elasticsearch.packaging.util.Shell;
import org.elasticsearch.packaging.util.Shell.Result;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

import static com.carrotsearch.randomizedtesting.RandomizedTest.getRandom;
import static org.elasticsearch.packaging.util.Archives.ARCHIVE_OWNER;
import static org.elasticsearch.packaging.util.Archives.installArchive;
import static org.elasticsearch.packaging.util.Archives.verifyArchiveInstallation;
Expand All @@ -49,6 +52,7 @@
import static org.elasticsearch.packaging.util.FileUtils.rm;
import static org.elasticsearch.packaging.util.ServerUtils.makeRequest;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
Expand All @@ -62,12 +66,12 @@
@TestCaseOrdering(TestCaseOrdering.AlphabeticOrder.class)
public abstract class ArchiveTestCase extends PackagingTestCase {

public void test10Install() {
public void test10Install() throws Exception {
installation = installArchive(distribution());
verifyArchiveInstallation(installation, distribution());
}

public void test20PluginsListWithNoPlugins() {
public void test20PluginsListWithNoPlugins() throws Exception {
assumeThat(installation, is(notNullValue()));

final Installation.Executables bin = installation.executables();
Expand All @@ -77,7 +81,7 @@ public void test20PluginsListWithNoPlugins() {
assertThat(r.stdout, isEmptyString());
}

public void test30NoJava() {
public void test30NoJava() throws Exception {
assumeThat(installation, is(notNullValue()));

final Installation.Executables bin = installation.executables();
Expand All @@ -101,7 +105,7 @@ public void test30NoJava() {
}
}

public void test40CreateKeystoreManually() {
public void test40CreateKeystoreManually() throws Exception {
assumeThat(installation, is(notNullValue()));

final Installation.Executables bin = installation.executables();
Expand Down Expand Up @@ -134,7 +138,7 @@ public void test40CreateKeystoreManually() {
});
}

public void test50StartAndStop() throws IOException {
public void test50StartAndStop() throws Exception {
assumeThat(installation, is(notNullValue()));

// cleanup from previous test
Expand All @@ -152,7 +156,7 @@ public void test50StartAndStop() throws IOException {
Archives.stopElasticsearch(installation);
}

public void assertRunsWithJavaHome() throws IOException {
public void assertRunsWithJavaHome() throws Exception {
Shell sh = newShell();

Platforms.onLinux(() -> {
Expand All @@ -173,13 +177,13 @@ public void assertRunsWithJavaHome() throws IOException {
assertThat(new String(Files.readAllBytes(log), StandardCharsets.UTF_8), containsString(systemJavaHome));
}

public void test51JavaHomeOverride() throws IOException {
public void test51JavaHomeOverride() throws Exception {
assumeThat(installation, is(notNullValue()));

assertRunsWithJavaHome();
}

public void test52BundledJdkRemoved() throws IOException {
public void test52BundledJdkRemoved() throws Exception {
assumeThat(installation, is(notNullValue()));
assumeThat(distribution().hasJdk, is(true));

Expand All @@ -192,7 +196,63 @@ public void test52BundledJdkRemoved() throws IOException {
}
}

public void test60AutoCreateKeystore() {
public void test53JavaHomeWithSpecialCharacters() throws Exception {
assumeThat(installation, is(notNullValue()));

Platforms.onWindows(() -> {
final Shell sh = new Shell();
try {
// once windows 2012 is no longer supported and powershell 5.0 is always available we can change this command
sh.run("cmd /c mklink /D 'C:\\Program Files (x86)\\java' $Env:SYSTEM_JAVA_HOME");

sh.getEnv().put("JAVA_HOME", "C:\\Program Files (x86)\\java");

//verify ES can start, stop and run plugin list
Archives.runElasticsearch(installation, sh);

Archives.stopElasticsearch(installation);

String pluginListCommand = installation.bin + "/elasticsearch-plugin list";
Result result = sh.run(pluginListCommand);
assertThat(result.exitCode, equalTo(0));

} finally {
//clean up sym link
sh.run("cmd /c rmdir 'C:\\Program Files (x86)\\java' ");
}
});

Platforms.onLinux(() -> {
final Shell sh = new Shell();
// Create temporary directory with a space and link to java binary.
// Use it as java_home
String nameWithSpace = RandomStrings.randomAsciiAlphanumOfLength(getRandom(), 10) + "java home";
String test_java_home = FileUtils.mkdir(Paths.get("/home",ARCHIVE_OWNER, nameWithSpace)).toAbsolutePath().toString();
try {
final String systemJavaHome = sh.run("echo $SYSTEM_JAVA_HOME").stdout.trim();
final String java = systemJavaHome + "/bin/java";

sh.run("mkdir -p \"" + test_java_home + "/bin\"");
sh.run("ln -s \"" + java + "\" \"" + test_java_home + "/bin/java\"");
sh.run("chown -R " + ARCHIVE_OWNER + ":" + ARCHIVE_OWNER + " \"" + test_java_home + "\"");

sh.getEnv().put("JAVA_HOME", test_java_home);

//verify ES can start, stop and run plugin list
Archives.runElasticsearch(installation, sh);

Archives.stopElasticsearch(installation);

String pluginListCommand = installation.bin + "/elasticsearch-plugin list";
Result result = sh.run(pluginListCommand);
assertThat(result.exitCode, equalTo(0));
} finally {
FileUtils.rm(Paths.get("\"" + test_java_home + "\""));
}
});
}

public void test60AutoCreateKeystore() throws Exception {
assumeThat(installation, is(notNullValue()));

assertThat(installation.config("elasticsearch.keystore"), file(File, ARCHIVE_OWNER, ARCHIVE_OWNER, p660));
Expand All @@ -211,7 +271,7 @@ public void test60AutoCreateKeystore() {
});
}

public void test70CustomPathConfAndJvmOptions() throws IOException {
public void test70CustomPathConfAndJvmOptions() throws Exception {
assumeThat(installation, is(notNullValue()));

final Path tempConf = getTempDir().resolve("esconf-alternate");
Expand Down Expand Up @@ -260,7 +320,7 @@ public void test70CustomPathConfAndJvmOptions() throws IOException {
}
}

public void test80RelativePathConf() throws IOException {
public void test80RelativePathConf() throws Exception {
assumeThat(installation, is(notNullValue()));

final Path temp = getTempDir().resolve("esconf-alternate");
Expand Down Expand Up @@ -304,7 +364,7 @@ public void test80RelativePathConf() throws IOException {
}
}

public void test90SecurityCliPackaging() {
public void test90SecurityCliPackaging() throws Exception {
assumeThat(installation, is(notNullValue()));

final Installation.Executables bin = installation.executables();
Expand All @@ -328,7 +388,7 @@ public void test90SecurityCliPackaging() {
}
}

public void test91ElasticsearchShardCliPackaging() {
public void test91ElasticsearchShardCliPackaging() throws Exception {
assumeThat(installation, is(notNullValue()));

final Installation.Executables bin = installation.executables();
Expand All @@ -345,7 +405,7 @@ public void test91ElasticsearchShardCliPackaging() {
}
}

public void test92ElasticsearchNodeCliPackaging() {
public void test92ElasticsearchNodeCliPackaging() throws Exception {
assumeThat(installation, is(notNullValue()));

final Installation.Executables bin = installation.executables();
Expand All @@ -363,7 +423,7 @@ public void test92ElasticsearchNodeCliPackaging() {
}
}

public void test93ElasticsearchNodeCustomDataPathAndNotEsHomeWorkDir() throws IOException {
public void test93ElasticsearchNodeCustomDataPathAndNotEsHomeWorkDir() throws Exception {
assumeThat(installation, is(notNullValue()));

Path relativeDataPath = installation.data.relativize(installation.home);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.junit.Before;
import org.junit.BeforeClass;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

Expand Down Expand Up @@ -54,7 +53,7 @@ public abstract class DebPreservationTestCase extends PackagingTestCase {
protected abstract Distribution distribution();

@BeforeClass
public static void cleanup() {
public static void cleanup() throws Exception {
installation = null;
cleanEverything();
}
Expand All @@ -65,14 +64,14 @@ public void onlyCompatibleDistributions() {
assumeTrue("only compatible distributions", distribution().packaging.compatible);
}

public void test10Install() throws IOException {
public void test10Install() throws Exception {
assertRemoved(distribution());
installation = install(distribution());
assertInstalled(distribution());
verifyPackageInstallation(installation, distribution(), newShell());
}

public void test20Remove() {
public void test20Remove() throws Exception {
assumeThat(installation, is(notNullValue()));

remove(distribution());
Expand Down Expand Up @@ -117,7 +116,7 @@ public void test20Remove() {
assertTrue(Files.exists(installation.envFile));
}

public void test30Purge() {
public void test30Purge() throws Exception {
assumeThat(installation, is(notNullValue()));

final Shell sh = new Shell();
Expand Down
Loading

0 comments on commit e15e337

Please sign in to comment.