Skip to content

Commit

Permalink
Handle special chars in JAVA_HOME in elasticsearch-service.bat (#52676)…
Browse files Browse the repository at this point in the history
… (#53057)

* Handle special chars in JAVA_HOME in elasticsearch-service.bat (#52676)

* Test case for windows service where JAVA_HOME path contains spaces (#53028)

Co-authored-by: Muhammad Shaheer Akram <[email protected]>
  • Loading branch information
williamrandolph and shaheerakr authored Mar 3, 2020
1 parent 6cece3a commit d3a8ac6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion distribution/src/bin/elasticsearch-service.bat
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ rem - fourth, ergonomic JVM options are applied
if not "%ES_JAVA_OPTS%" == "" set ES_JAVA_OPTS=%ES_JAVA_OPTS: =;%

@setlocal
for /F "usebackq delims=" %%a in (`"%JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.JvmOptionsParser" "!ES_PATH_CONF!" || echo jvm_options_parser_failed"`) do set ES_JAVA_OPTS=%%a
for /F "usebackq delims=" %%a in (`CALL %JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.JvmOptionsParser" "!ES_PATH_CONF!" ^|^| echo jvm_options_parser_failed`) do set ES_JAVA_OPTS=%%a
@endlocal & set "MAYBE_JVM_OPTIONS_PARSER_FAILED=%ES_JAVA_OPTS%" & set ES_JAVA_OPTS=%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 @@ -52,6 +52,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeThat;
import static org.junit.Assume.assumeTrue;
Expand Down Expand Up @@ -104,12 +105,29 @@ public void test31BadJavaHome() throws Exception {
sh.getEnv().put("JAVA_HOME", "doesnotexist");

// ask for elasticsearch version to quickly exit if java is actually found (ie test failure)
final Result runResult = sh.runIgnoreExitCode(bin.elasticsearch.toString() + " -v");
final Result runResult = sh.runIgnoreExitCode(bin.elasticsearch.toString() + " -V");
assertThat(runResult.exitCode, is(1));
assertThat(runResult.stderr, containsString("could not find java in JAVA_HOME"));

}

public void test32SpecialCharactersInJdkPath() throws Exception {
final Installation.Executables bin = installation.executables();
assumeTrue("Only run this test when we know where the JDK is.", distribution().hasJdk);

final Path relocatedJdk = installation.bundledJdk.getParent().resolve("a (special) path");
sh.getEnv().put("JAVA_HOME", relocatedJdk.toString());

try {
mv(installation.bundledJdk, relocatedJdk);
// ask for elasticsearch version to avoid starting the app
final Result runResult = sh.run(bin.elasticsearch.toString() + " -V");
assertThat(runResult.stdout, startsWith("Version: "));
} finally {
mv(relocatedJdk, installation.bundledJdk);
}
}

public void test50StartAndStop() throws Exception {
// cleanup from previous test
rm(installation.config("elasticsearch.keystore"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ public void test15RemoveNotInstalled() {
assertThat(result.stdout, containsString("Failed removing '" + DEFAULT_ID + "' service"));
}

public void test16InstallSpecialCharactersInJdkPath() throws IOException {
assumeTrue("Only run this test when we know where the JDK is.", distribution().hasJdk);
final Path relocatedJdk = installation.bundledJdk.getParent().resolve("a (special) jdk");
sh.getEnv().put("JAVA_HOME", relocatedJdk.toString());

try {
mv(installation.bundledJdk, relocatedJdk);
Result result = sh.run(serviceScript + " install");
assertThat(result.stdout, containsString("The service 'elasticsearch-service-x64' has been installed."));
} finally {
sh.runIgnoreExitCode(serviceScript + " remove");
mv(relocatedJdk, installation.bundledJdk);
}
}

public void test20CustomizeServiceId() {
String serviceId = "my-es-service";
String displayName = DEFAULT_DISPLAY_NAME.replace(DEFAULT_ID, serviceId);
Expand Down

0 comments on commit d3a8ac6

Please sign in to comment.