Skip to content

Commit e15e337

Browse files
authored
Bat scripts to work with JAVA_HOME with parentheses Backports (#40770)
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)
1 parent d7a6772 commit e15e337

File tree

15 files changed

+358
-332
lines changed

15 files changed

+358
-332
lines changed

distribution/src/bin/elasticsearch-env.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ set ES_DISTRIBUTION_TYPE=${es.distribution.type}
5959
set ES_BUNDLED_JDK=${es.bundled_jdk}
6060

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

6565
cd /d "%ES_HOME%"

distribution/src/bin/elasticsearch.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ IF ERRORLEVEL 1 (
4141
EXIT /B %ERRORLEVEL%
4242
)
4343

44-
set "ES_JVM_OPTIONS=%ES_PATH_CONF%\jvm.options"
44+
set ES_JVM_OPTIONS=%ES_PATH_CONF%\jvm.options
4545
@setlocal
46-
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
46+
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
4747
@endlocal & set "MAYBE_JVM_OPTIONS_PARSER_FAILED=%JVM_OPTIONS%" & set ES_JAVA_OPTS=%JVM_OPTIONS:${ES_TMPDIR}=!ES_TMPDIR!% %ES_JAVA_OPTS%
4848

4949
if "%MAYBE_JVM_OPTIONS_PARSER_FAILED%" == "jvm_options_parser_failed" (

qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,24 @@
2020
package org.elasticsearch.packaging.test;
2121

2222
import com.carrotsearch.randomizedtesting.annotations.TestCaseOrdering;
23+
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
2324
import org.apache.http.client.fluent.Request;
2425
import org.elasticsearch.packaging.util.Archives;
2526
import org.elasticsearch.packaging.util.Distribution;
27+
import org.elasticsearch.packaging.util.FileUtils;
2628
import org.elasticsearch.packaging.util.Installation;
2729
import org.elasticsearch.packaging.util.Platforms;
2830
import org.elasticsearch.packaging.util.ServerUtils;
2931
import org.elasticsearch.packaging.util.Shell;
3032
import org.elasticsearch.packaging.util.Shell.Result;
3133

32-
import java.io.IOException;
3334
import java.nio.charset.StandardCharsets;
3435
import java.nio.file.Files;
3536
import java.nio.file.Path;
37+
import java.nio.file.Paths;
3638
import java.util.stream.Stream;
3739

40+
import static com.carrotsearch.randomizedtesting.RandomizedTest.getRandom;
3841
import static org.elasticsearch.packaging.util.Archives.ARCHIVE_OWNER;
3942
import static org.elasticsearch.packaging.util.Archives.installArchive;
4043
import static org.elasticsearch.packaging.util.Archives.verifyArchiveInstallation;
@@ -49,6 +52,7 @@
4952
import static org.elasticsearch.packaging.util.FileUtils.rm;
5053
import static org.elasticsearch.packaging.util.ServerUtils.makeRequest;
5154
import static org.hamcrest.CoreMatchers.containsString;
55+
import static org.hamcrest.CoreMatchers.equalTo;
5256
import static org.hamcrest.CoreMatchers.is;
5357
import static org.hamcrest.CoreMatchers.not;
5458
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -62,12 +66,12 @@
6266
@TestCaseOrdering(TestCaseOrdering.AlphabeticOrder.class)
6367
public abstract class ArchiveTestCase extends PackagingTestCase {
6468

65-
public void test10Install() {
69+
public void test10Install() throws Exception {
6670
installation = installArchive(distribution());
6771
verifyArchiveInstallation(installation, distribution());
6872
}
6973

70-
public void test20PluginsListWithNoPlugins() {
74+
public void test20PluginsListWithNoPlugins() throws Exception {
7175
assumeThat(installation, is(notNullValue()));
7276

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

80-
public void test30NoJava() {
84+
public void test30NoJava() throws Exception {
8185
assumeThat(installation, is(notNullValue()));
8286

8387
final Installation.Executables bin = installation.executables();
@@ -101,7 +105,7 @@ public void test30NoJava() {
101105
}
102106
}
103107

104-
public void test40CreateKeystoreManually() {
108+
public void test40CreateKeystoreManually() throws Exception {
105109
assumeThat(installation, is(notNullValue()));
106110

107111
final Installation.Executables bin = installation.executables();
@@ -134,7 +138,7 @@ public void test40CreateKeystoreManually() {
134138
});
135139
}
136140

137-
public void test50StartAndStop() throws IOException {
141+
public void test50StartAndStop() throws Exception {
138142
assumeThat(installation, is(notNullValue()));
139143

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

155-
public void assertRunsWithJavaHome() throws IOException {
159+
public void assertRunsWithJavaHome() throws Exception {
156160
Shell sh = newShell();
157161

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

176-
public void test51JavaHomeOverride() throws IOException {
180+
public void test51JavaHomeOverride() throws Exception {
177181
assumeThat(installation, is(notNullValue()));
178182

179183
assertRunsWithJavaHome();
180184
}
181185

182-
public void test52BundledJdkRemoved() throws IOException {
186+
public void test52BundledJdkRemoved() throws Exception {
183187
assumeThat(installation, is(notNullValue()));
184188
assumeThat(distribution().hasJdk, is(true));
185189

@@ -192,7 +196,63 @@ public void test52BundledJdkRemoved() throws IOException {
192196
}
193197
}
194198

195-
public void test60AutoCreateKeystore() {
199+
public void test53JavaHomeWithSpecialCharacters() throws Exception {
200+
assumeThat(installation, is(notNullValue()));
201+
202+
Platforms.onWindows(() -> {
203+
final Shell sh = new Shell();
204+
try {
205+
// once windows 2012 is no longer supported and powershell 5.0 is always available we can change this command
206+
sh.run("cmd /c mklink /D 'C:\\Program Files (x86)\\java' $Env:SYSTEM_JAVA_HOME");
207+
208+
sh.getEnv().put("JAVA_HOME", "C:\\Program Files (x86)\\java");
209+
210+
//verify ES can start, stop and run plugin list
211+
Archives.runElasticsearch(installation, sh);
212+
213+
Archives.stopElasticsearch(installation);
214+
215+
String pluginListCommand = installation.bin + "/elasticsearch-plugin list";
216+
Result result = sh.run(pluginListCommand);
217+
assertThat(result.exitCode, equalTo(0));
218+
219+
} finally {
220+
//clean up sym link
221+
sh.run("cmd /c rmdir 'C:\\Program Files (x86)\\java' ");
222+
}
223+
});
224+
225+
Platforms.onLinux(() -> {
226+
final Shell sh = new Shell();
227+
// Create temporary directory with a space and link to java binary.
228+
// Use it as java_home
229+
String nameWithSpace = RandomStrings.randomAsciiAlphanumOfLength(getRandom(), 10) + "java home";
230+
String test_java_home = FileUtils.mkdir(Paths.get("/home",ARCHIVE_OWNER, nameWithSpace)).toAbsolutePath().toString();
231+
try {
232+
final String systemJavaHome = sh.run("echo $SYSTEM_JAVA_HOME").stdout.trim();
233+
final String java = systemJavaHome + "/bin/java";
234+
235+
sh.run("mkdir -p \"" + test_java_home + "/bin\"");
236+
sh.run("ln -s \"" + java + "\" \"" + test_java_home + "/bin/java\"");
237+
sh.run("chown -R " + ARCHIVE_OWNER + ":" + ARCHIVE_OWNER + " \"" + test_java_home + "\"");
238+
239+
sh.getEnv().put("JAVA_HOME", test_java_home);
240+
241+
//verify ES can start, stop and run plugin list
242+
Archives.runElasticsearch(installation, sh);
243+
244+
Archives.stopElasticsearch(installation);
245+
246+
String pluginListCommand = installation.bin + "/elasticsearch-plugin list";
247+
Result result = sh.run(pluginListCommand);
248+
assertThat(result.exitCode, equalTo(0));
249+
} finally {
250+
FileUtils.rm(Paths.get("\"" + test_java_home + "\""));
251+
}
252+
});
253+
}
254+
255+
public void test60AutoCreateKeystore() throws Exception {
196256
assumeThat(installation, is(notNullValue()));
197257

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

214-
public void test70CustomPathConfAndJvmOptions() throws IOException {
274+
public void test70CustomPathConfAndJvmOptions() throws Exception {
215275
assumeThat(installation, is(notNullValue()));
216276

217277
final Path tempConf = getTempDir().resolve("esconf-alternate");
@@ -260,7 +320,7 @@ public void test70CustomPathConfAndJvmOptions() throws IOException {
260320
}
261321
}
262322

263-
public void test80RelativePathConf() throws IOException {
323+
public void test80RelativePathConf() throws Exception {
264324
assumeThat(installation, is(notNullValue()));
265325

266326
final Path temp = getTempDir().resolve("esconf-alternate");
@@ -304,7 +364,7 @@ public void test80RelativePathConf() throws IOException {
304364
}
305365
}
306366

307-
public void test90SecurityCliPackaging() {
367+
public void test90SecurityCliPackaging() throws Exception {
308368
assumeThat(installation, is(notNullValue()));
309369

310370
final Installation.Executables bin = installation.executables();
@@ -328,7 +388,7 @@ public void test90SecurityCliPackaging() {
328388
}
329389
}
330390

331-
public void test91ElasticsearchShardCliPackaging() {
391+
public void test91ElasticsearchShardCliPackaging() throws Exception {
332392
assumeThat(installation, is(notNullValue()));
333393

334394
final Installation.Executables bin = installation.executables();
@@ -345,7 +405,7 @@ public void test91ElasticsearchShardCliPackaging() {
345405
}
346406
}
347407

348-
public void test92ElasticsearchNodeCliPackaging() {
408+
public void test92ElasticsearchNodeCliPackaging() throws Exception {
349409
assumeThat(installation, is(notNullValue()));
350410

351411
final Installation.Executables bin = installation.executables();
@@ -363,7 +423,7 @@ public void test92ElasticsearchNodeCliPackaging() {
363423
}
364424
}
365425

366-
public void test93ElasticsearchNodeCustomDataPathAndNotEsHomeWorkDir() throws IOException {
426+
public void test93ElasticsearchNodeCustomDataPathAndNotEsHomeWorkDir() throws Exception {
367427
assumeThat(installation, is(notNullValue()));
368428

369429
Path relativeDataPath = installation.data.relativize(installation.home);

qa/vagrant/src/main/java/org/elasticsearch/packaging/test/DebPreservationTestCase.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.junit.Before;
2727
import org.junit.BeforeClass;
2828

29-
import java.io.IOException;
3029
import java.nio.file.Files;
3130
import java.nio.file.Paths;
3231

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

5655
@BeforeClass
57-
public static void cleanup() {
56+
public static void cleanup() throws Exception {
5857
installation = null;
5958
cleanEverything();
6059
}
@@ -65,14 +64,14 @@ public void onlyCompatibleDistributions() {
6564
assumeTrue("only compatible distributions", distribution().packaging.compatible);
6665
}
6766

68-
public void test10Install() throws IOException {
67+
public void test10Install() throws Exception {
6968
assertRemoved(distribution());
7069
installation = install(distribution());
7170
assertInstalled(distribution());
7271
verifyPackageInstallation(installation, distribution(), newShell());
7372
}
7473

75-
public void test20Remove() {
74+
public void test20Remove() throws Exception {
7675
assumeThat(installation, is(notNullValue()));
7776

7877
remove(distribution());
@@ -117,7 +116,7 @@ public void test20Remove() {
117116
assertTrue(Files.exists(installation.envFile));
118117
}
119118

120-
public void test30Purge() {
119+
public void test30Purge() throws Exception {
121120
assumeThat(installation, is(notNullValue()));
122121

123122
final Shell sh = new Shell();

0 commit comments

Comments
 (0)