2323import org .apache .http .client .fluent .Request ;
2424import org .elasticsearch .packaging .util .FileUtils ;
2525import org .elasticsearch .packaging .util .Shell .Result ;
26- import org .hamcrest .CoreMatchers ;
2726import org .junit .BeforeClass ;
2827
2928import java .nio .charset .StandardCharsets ;
4746import static org .elasticsearch .packaging .util .Packages .SYSTEMD_SERVICE ;
4847import static org .elasticsearch .packaging .util .Packages .assertInstalled ;
4948import static org .elasticsearch .packaging .util .Packages .assertRemoved ;
49+ import static org .elasticsearch .packaging .util .Packages .clearJournal ;
5050import static org .elasticsearch .packaging .util .Packages .installPackage ;
5151import static org .elasticsearch .packaging .util .Packages .remove ;
5252import static org .elasticsearch .packaging .util .Packages .restartElasticsearch ;
5353import static org .elasticsearch .packaging .util .Packages .startElasticsearch ;
54+ import static org .elasticsearch .packaging .util .Packages .startElasticsearchIgnoringFailure ;
5455import static org .elasticsearch .packaging .util .Packages .stopElasticsearch ;
5556import static org .elasticsearch .packaging .util .Packages .verifyPackageInstallation ;
5657import static org .elasticsearch .packaging .util .Platforms .getOsRelease ;
6061import static org .hamcrest .CoreMatchers .equalTo ;
6162import static org .hamcrest .CoreMatchers .not ;
6263import static org .hamcrest .Matchers .containsString ;
63- import static org .hamcrest .Matchers .isEmptyString ;
64+ import static org .hamcrest .Matchers .emptyString ;
6465import static org .hamcrest .core .Is .is ;
6566import static org .junit .Assume .assumeThat ;
6667import static org .junit .Assume .assumeTrue ;
@@ -79,8 +80,8 @@ public void test10InstallPackage() throws Exception {
7980 verifyPackageInstallation (installation , distribution (), sh );
8081 }
8182
82- public void test20PluginsCommandWhenNoPlugins () throws Exception {
83- assertThat (sh .run (installation .bin ("elasticsearch-plugin" ) + " list" ).stdout , isEmptyString ( ));
83+ public void test20PluginsCommandWhenNoPlugins () {
84+ assertThat (sh .run (installation .bin ("elasticsearch-plugin" ) + " list" ).stdout , is ( emptyString () ));
8485 }
8586
8687 public void test30DaemonIsNotEnabledOnRestart () {
@@ -95,7 +96,7 @@ public void test31InstallDoesNotStartServer() {
9596 assertThat (sh .run ("ps aux" ).stdout , not (containsString ("org.elasticsearch.bootstrap.Elasticsearch" )));
9697 }
9798
98- public void assertRunsWithJavaHome () throws Exception {
99+ private void assertRunsWithJavaHome () throws Exception {
99100 byte [] originalEnvFile = Files .readAllBytes (installation .envFile );
100101 try {
101102 Files .write (installation .envFile , ("JAVA_HOME=" + systemJavaHome + "\n " ).getBytes (StandardCharsets .UTF_8 ),
@@ -287,53 +288,17 @@ public void test80DeletePID_DIRandRestart() throws Exception {
287288 }
288289
289290 public void test81CustomPathConfAndJvmOptions () throws Exception {
290- assumeTrue (isSystemd ());
291-
292- assertPathsExist (installation .envFile );
293-
294- stopElasticsearch (sh );
295-
296- // The custom config directory is not under /tmp or /var/tmp because
297- // systemd's private temp directory functionally means different
298- // processes can have different views of what's in these directories
299- String randomName = RandomStrings .randomAsciiAlphanumOfLength (getRandom (), 10 );
300- sh .run ("mkdir /etc/" +randomName );
301- final Path tempConf = Paths .get ("/etc/" +randomName );
302-
303- try {
304- mkdir (tempConf );
305- cp (installation .config ("elasticsearch.yml" ), tempConf .resolve ("elasticsearch.yml" ));
306- cp (installation .config ("log4j2.properties" ), tempConf .resolve ("log4j2.properties" ));
307-
308- // we have to disable Log4j from using JMX lest it will hit a security
309- // manager exception before we have configured logging; this will fail
310- // startup since we detect usages of logging before it is configured
311- final String jvmOptions =
312- "-Xms512m\n " +
313- "-Xmx512m\n " +
314- "-Dlog4j2.disable.jmx=true\n " ;
315- append (tempConf .resolve ("jvm.options" ), jvmOptions );
316-
317- sh .runIgnoreExitCode ("chown -R elasticsearch:elasticsearch " + tempConf );
318-
319- cp (installation .envFile , tempConf .resolve ("elasticsearch.bk" ));//backup
320- append (installation .envFile , "ES_PATH_CONF=" + tempConf + "\n " );
291+ withCustomConfig (tempConf -> {
321292 append (installation .envFile , "ES_JAVA_OPTS=-XX:-UseCompressedOops" );
322293
323294 startElasticsearch (sh , installation );
324295
325296 final String nodesResponse = makeRequest (Request .Get ("http://localhost:9200/_nodes" ));
326- assertThat (nodesResponse , CoreMatchers . containsString ("\" heap_init_in_bytes\" :536870912" ));
327- assertThat (nodesResponse , CoreMatchers . containsString ("\" using_compressed_ordinary_object_pointers\" :\" false\" " ));
297+ assertThat (nodesResponse , containsString ("\" heap_init_in_bytes\" :536870912" ));
298+ assertThat (nodesResponse , containsString ("\" using_compressed_ordinary_object_pointers\" :\" false\" " ));
328299
329300 stopElasticsearch (sh );
330-
331- } finally {
332- rm (installation .envFile );
333- cp (tempConf .resolve ("elasticsearch.bk" ), installation .envFile );
334- rm (tempConf );
335- cleanup ();
336- }
301+ });
337302 }
338303
339304 public void test82SystemdMask () throws Exception {
@@ -375,4 +340,63 @@ public void test83serviceFileSetsLimits() throws Exception {
375340
376341 stopElasticsearch (sh );
377342 }
343+
344+ public void test90DoNotCloseStderrWhenQuiet () throws Exception {
345+ withCustomConfig (tempConf -> {
346+ // Create a startup problem by adding an invalid YAML line to the config
347+ append (tempConf .resolve ("elasticsearch.yml" ), "discovery.zen.ping.unicast.hosts:15172.30.5.3416172.30.5.35, 172.30.5.17]\n " );
348+
349+ // Make sure we don't pick up the journal entries for previous ES instances.
350+ clearJournal (sh );
351+ startElasticsearchIgnoringFailure (sh );
352+
353+ final Result logs = sh .run ("journalctl -u elasticsearch.service" );
354+
355+ assertThat (logs .stdout , containsString ("Failed to load settings from [elasticsearch.yml]" ));
356+ });
357+ }
358+
359+ @ FunctionalInterface
360+ private interface CustomConfigConsumer {
361+ void accept (Path path ) throws Exception ;
362+ }
363+
364+ private void withCustomConfig (CustomConfigConsumer pathConsumer ) throws Exception {
365+ assumeTrue (isSystemd ());
366+
367+ assertPathsExist (installation .envFile );
368+
369+ stopElasticsearch (sh );
370+
371+ // The custom config directory is not under /tmp or /var/tmp because
372+ // systemd's private temp directory functionally means different
373+ // processes can have different views of what's in these directories
374+ String randomName = RandomStrings .randomAsciiAlphanumOfLength (getRandom (), 10 );
375+ sh .run ("mkdir /etc/" + randomName );
376+ final Path tempConf = Paths .get ("/etc/" + randomName );
377+
378+ try {
379+ mkdir (tempConf );
380+ cp (installation .config ("elasticsearch.yml" ), tempConf .resolve ("elasticsearch.yml" ));
381+ cp (installation .config ("log4j2.properties" ), tempConf .resolve ("log4j2.properties" ));
382+
383+ // we have to disable Log4j from using JMX lest it will hit a security
384+ // manager exception before we have configured logging; this will fail
385+ // startup since we detect usages of logging before it is configured
386+ final String jvmOptions = "-Xms512m\n -Xmx512m\n -Dlog4j2.disable.jmx=true\n " ;
387+ append (tempConf .resolve ("jvm.options" ), jvmOptions );
388+
389+ sh .runIgnoreExitCode ("chown -R elasticsearch:elasticsearch " + tempConf );
390+
391+ cp (installation .envFile , tempConf .resolve ("elasticsearch.bk" ));// backup
392+ append (installation .envFile , "ES_PATH_CONF=" + tempConf + "\n " );
393+
394+ pathConsumer .accept (tempConf );
395+ } finally {
396+ rm (installation .envFile );
397+ cp (tempConf .resolve ("elasticsearch.bk" ), installation .envFile );
398+ rm (tempConf );
399+ cleanup ();
400+ }
401+ }
378402}
0 commit comments