From 6f897420bd61a58319ee8b23d91f902382a9749a Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Thu, 28 Mar 2019 13:07:55 +0100 Subject: [PATCH 1/3] Migrate systemd packaging tests from bats to java backport(#39954) Migrating systemd bats tests from bats to java dsl. This also covers partially the sysv, but more must be added relates #32143 --- .../packaging/test/PackageTestCase.java | 207 +++++++++++++- .../packaging/util/FileUtils.java | 27 ++ .../packaging/util/Packages.java | 20 +- .../elasticsearch/packaging/util/Shell.java | 5 + .../resources/packaging/tests/60_systemd.bats | 257 ------------------ 5 files changed, 242 insertions(+), 274 deletions(-) delete mode 100644 qa/vagrant/src/test/resources/packaging/tests/60_systemd.bats diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java index e306e7c63ce5a..458359b299e75 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java @@ -20,31 +20,45 @@ package org.elasticsearch.packaging.test; import com.carrotsearch.randomizedtesting.annotations.TestCaseOrdering; +import org.apache.http.client.fluent.Request; +import org.elasticsearch.packaging.util.FileUtils; import org.elasticsearch.packaging.util.Shell; import org.elasticsearch.packaging.util.Shell.Result; +import org.hamcrest.CoreMatchers; import org.junit.Before; 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.nio.file.StandardOpenOption; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static org.elasticsearch.packaging.util.FileUtils.append; import static org.elasticsearch.packaging.util.FileUtils.assertPathsDontExist; +import static org.elasticsearch.packaging.util.FileUtils.assertPathsExist; +import static org.elasticsearch.packaging.util.FileUtils.cp; +import static org.elasticsearch.packaging.util.FileUtils.fileWithGlobExist; +import static org.elasticsearch.packaging.util.FileUtils.mkdir; import static org.elasticsearch.packaging.util.FileUtils.mv; +import static org.elasticsearch.packaging.util.FileUtils.rm; +import static org.elasticsearch.packaging.util.FileUtils.slurp; 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.restartElasticsearch; import static org.elasticsearch.packaging.util.Packages.startElasticsearch; import static org.elasticsearch.packaging.util.Packages.stopElasticsearch; import static org.elasticsearch.packaging.util.Packages.verifyPackageInstallation; import static org.elasticsearch.packaging.util.Platforms.getOsRelease; import static org.elasticsearch.packaging.util.Platforms.isSystemd; +import static org.elasticsearch.packaging.util.ServerUtils.makeRequest; import static org.elasticsearch.packaging.util.ServerUtils.runElasticsearchTests; +import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.Matchers.containsString; @@ -55,42 +69,50 @@ @TestCaseOrdering(TestCaseOrdering.AlphabeticOrder.class) public abstract class PackageTestCase extends PackagingTestCase { + private Shell sh; @Before public void onlyCompatibleDistributions() { assumeTrue("only compatible distributions", distribution().packaging.compatible); + sh = newShell(); } public void test10InstallPackage() throws IOException { assertRemoved(distribution()); installation = install(distribution()); assertInstalled(distribution()); - verifyPackageInstallation(installation, distribution(), newShell()); + verifyPackageInstallation(installation, distribution(), sh); } public void test20PluginsCommandWhenNoPlugins() { assumeThat(installation, is(notNullValue())); - assertThat(newShell().run(installation.bin("elasticsearch-plugin") + " list").stdout, isEmptyString()); + assertThat(sh.run(installation.bin("elasticsearch-plugin") + " list").stdout, isEmptyString()); } - public void test30InstallDoesNotStartServer() { + public void test30DaemonIsNotEnabledOnRestart() { + if (isSystemd()) { + sh.run("systemctl daemon-reload"); + String isEnabledOutput = sh.runIgnoreExitCode("systemctl is-enabled elasticsearch.service").stdout.trim(); + assertThat(isEnabledOutput, equalTo("disabled")); + } + } + + public void test31InstallDoesNotStartServer() { assumeThat(installation, is(notNullValue())); - assertThat(newShell().run("ps aux").stdout, not(containsString("org.elasticsearch.bootstrap.Elasticsearch"))); + assertThat(sh.run("ps aux").stdout, not(containsString("org.elasticsearch.bootstrap.Elasticsearch"))); } public void assertRunsWithJavaHome() throws IOException { - Shell sh = newShell(); - String systemJavaHome = sh.run("echo $SYSTEM_JAVA_HOME").stdout.trim(); byte[] originalEnvFile = Files.readAllBytes(installation.envFile); try { Files.write(installation.envFile, ("JAVA_HOME=" + systemJavaHome + "\n").getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND); - startElasticsearch(); + startElasticsearch(sh); runElasticsearchTests(); - stopElasticsearch(); + stopElasticsearch(sh); } finally { Files.write(installation.envFile, originalEnvFile); } @@ -99,7 +121,7 @@ public void assertRunsWithJavaHome() throws IOException { assertThat(new String(Files.readAllBytes(log), StandardCharsets.UTF_8), containsString(systemJavaHome)); } - public void test31JavaHomeOverride() throws IOException { + public void test32JavaHomeOverride() throws IOException { assumeThat(installation, is(notNullValue())); // we always run with java home when no bundled jdk is included, so this test would be repetitive assumeThat(distribution().hasJdk, is(true)); @@ -121,11 +143,20 @@ public void test42BundledJdkRemoved() throws IOException { } public void test40StartServer() throws IOException { + String start = sh.runIgnoreExitCode("date ").stdout.trim(); assumeThat(installation, is(notNullValue())); - startElasticsearch(); + startElasticsearch(sh); + + String journalEntries = sh.runIgnoreExitCode("journalctl _SYSTEMD_UNIT=elasticsearch.service " + + "--since \"" + start + "\" --output cat | wc -l").stdout.trim(); + assertThat(journalEntries, equalTo("0")); + + assertPathsExist(installation.pidDir.resolve("elasticsearch.pid")); + assertPathsExist(installation.logs.resolve("elasticsearch_server.json")); + runElasticsearchTests(); - verifyPackageInstallation(installation, distribution(), newShell()); // check startup script didn't change permissions + verifyPackageInstallation(installation, distribution(), sh); // check startup script didn't change permissions } public void test50Remove() { @@ -134,7 +165,6 @@ public void test50Remove() { remove(distribution()); // removing must stop the service - final Shell sh = newShell(); assertThat(sh.run("ps aux").stdout, not(containsString("org.elasticsearch.bootstrap.Elasticsearch"))); if (isSystemd()) { @@ -184,9 +214,160 @@ public void test60Reinstall() throws IOException { installation = install(distribution()); assertInstalled(distribution()); - verifyPackageInstallation(installation, distribution(), newShell()); + verifyPackageInstallation(installation, distribution(), sh); remove(distribution()); assertRemoved(distribution()); } + + public void test70RestartServer() throws IOException { + try { + installation = install(distribution()); + assertInstalled(distribution()); + + startElasticsearch(sh); + restartElasticsearch(sh); + runElasticsearchTests(); + stopElasticsearch(sh); + } finally { + cleanup(); + } + } + + + public void test72TestRuntimeDirectory() throws IOException { + try { + installation = install(distribution()); + FileUtils.rm(installation.pidDir); + startElasticsearch(sh); + assertPathsExist(installation.pidDir); + stopElasticsearch(sh); + } finally { + cleanup(); + } + } + + public void test73gcLogsExist() throws IOException { + installation = install(distribution()); + startElasticsearch(sh); + // it can be gc.log or gc.log.0.current + assertThat(installation.logs, fileWithGlobExist("gc.log*")); + stopElasticsearch(sh); + } + + // TEST CASES FOR SYSTEMD ONLY + + + /** + * # Simulates the behavior of a system restart: + * # the PID directory is deleted by the operating system + * # but it should not block ES from starting + * # see https://github.com/elastic/elasticsearch/issues/11594 + */ + public void test80DeletePID_DIRandRestart() throws IOException { + assumeTrue(isSystemd()); + + rm(installation.pidDir); + + sh.run("systemd-tmpfiles --create"); + + startElasticsearch(sh); + + final Path pidFile = installation.pidDir.resolve("elasticsearch.pid"); + + assertTrue(Files.exists(pidFile)); + + stopElasticsearch(sh); + } + + public void test81CustomPathConfAndJvmOptions() throws IOException { + assumeTrue(isSystemd()); + + assumeThat(installation, is(notNullValue())); + assertPathsExist(installation.envFile); + + stopElasticsearch(sh); + + // The custom config directory is not under /tmp or /var/tmp because + // systemd's private temp directory functionally means different + // processes can have different views of what's in these directories + String temp = sh.runIgnoreExitCode("mktemp -p /etc -d").stdout.trim(); + final Path tempConf = Paths.get(temp); + + try { + mkdir(tempConf); + cp(installation.config("elasticsearch.yml"), tempConf.resolve("elasticsearch.yml")); + cp(installation.config("log4j2.properties"), tempConf.resolve("log4j2.properties")); + + // we have to disable Log4j from using JMX lest it will hit a security + // manager exception before we have configured logging; this will fail + // startup since we detect usages of logging before it is configured + final String jvmOptions = + "-Xms512m\n" + + "-Xmx512m\n" + + "-Dlog4j2.disable.jmx=true\n"; + append(tempConf.resolve("jvm.options"), jvmOptions); + + sh.runIgnoreExitCode("chown -R elasticsearch:elasticsearch " + tempConf); + + final Shell serverShell = newShell(); + cp(installation.envFile, tempConf.resolve("elasticsearch.bk"));//backup + append(installation.envFile, "ES_PATH_CONF=" + tempConf + "\n"); + append(installation.envFile, "ES_JAVA_OPTS=-XX:-UseCompressedOops"); + + startElasticsearch(serverShell); + + final String nodesResponse = makeRequest(Request.Get("http://localhost:9200/_nodes")); + assertThat(nodesResponse, CoreMatchers.containsString("\"heap_init_in_bytes\":536870912")); + assertThat(nodesResponse, CoreMatchers.containsString("\"using_compressed_ordinary_object_pointers\":\"false\"")); + + stopElasticsearch(serverShell); + + } finally { + rm(installation.envFile); + cp(tempConf.resolve("elasticsearch.bk"), installation.envFile); + rm(tempConf); + cleanup(); + } + } + + public void test82SystemdMask() throws IOException { + try { + assumeTrue(isSystemd()); + + sh.run("systemctl mask systemd-sysctl.service"); + + installation = install(distribution()); + + sh.run("systemctl unmask systemd-sysctl.service"); + } finally { + cleanup(); + } + } + + public void test83serviceFileSetsLimits() throws IOException { + // Limits are changed on systemd platforms only + assumeTrue(isSystemd()); + + installation = install(distribution()); + + startElasticsearch(sh); + + final Path pidFile = installation.pidDir.resolve("elasticsearch.pid"); + assertTrue(Files.exists(pidFile)); + String pid = slurp(pidFile).trim(); + String maxFileSize = sh.run("cat /proc/%s/limits | grep \"Max file size\" | awk '{ print $4 }'", pid).stdout.trim(); + assertThat(maxFileSize, equalTo("unlimited")); + + String maxProcesses = sh.run("cat /proc/%s/limits | grep \"Max processes\" | awk '{ print $3 }'", pid).stdout.trim(); + assertThat(maxProcesses, equalTo("4096")); + + String maxOpenFiles = sh.run("cat /proc/%s/limits | grep \"Max open files\" | awk '{ print $4 }'", pid).stdout.trim(); + assertThat(maxOpenFiles, equalTo("65535")); + + String maxAddressSpace = sh.run("cat /proc/%s/limits | grep \"Max address space\" | awk '{ print $4 }'", pid).stdout.trim(); + assertThat(maxAddressSpace, equalTo("unlimited")); + + stopElasticsearch(sh); + } } diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/FileUtils.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/FileUtils.java index 10d1b3ee6b6de..efbf0bd74a354 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/FileUtils.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/FileUtils.java @@ -20,6 +20,8 @@ package org.elasticsearch.packaging.util; import org.elasticsearch.core.internal.io.IOUtils; +import org.hamcrest.FeatureMatcher; +import org.hamcrest.Matcher; import java.io.BufferedWriter; import java.io.IOException; @@ -34,9 +36,11 @@ import java.nio.file.attribute.PosixFileAttributes; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.emptyIterable; import static org.hamcrest.core.IsNot.not; import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString; import static org.junit.Assert.assertFalse; @@ -69,6 +73,15 @@ public static void rm(Path... paths) { } } + public static Path mktempDir(Path path) { + try { + return Files.createTempDirectory(path,"tmp"); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static Path mkdir(Path path) { try { return Files.createDirectories(path); @@ -176,6 +189,20 @@ public static void assertPathsExist(Path... paths) { Arrays.stream(paths).forEach(path -> assertTrue(path + " should exist", Files.exists(path))); } + public static Matcher fileWithGlobExist(String glob) throws IOException { + return new FeatureMatcher>(not(emptyIterable()),"File with pattern exist", "file with pattern"){ + + @Override + protected Iterable featureValueOf(Path actual) { + try { + return Files.newDirectoryStream(actual,glob); + } catch (IOException e) { + return Collections.emptyList(); + } + } + }; + } + public static void assertPathsDontExist(Path... paths) { Arrays.stream(paths).forEach(path -> assertFalse(path + " should not exist", Files.exists(path))); } diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java index 7014a627a7aee..afa7e371c2c55 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java @@ -270,8 +270,7 @@ private static void verifyDefaultInstallation(Installation es) { ).forEach(configFile -> assertThat(es.config(configFile), file(File, "root", "elasticsearch", p660))); } - public static void startElasticsearch() throws IOException { - final Shell sh = new Shell(); + public static void startElasticsearch(Shell sh) throws IOException { if (isSystemd()) { sh.run("systemctl daemon-reload"); sh.run("systemctl enable elasticsearch.service"); @@ -281,6 +280,10 @@ public static void startElasticsearch() throws IOException { sh.run("service elasticsearch start"); } + assertElasticsearchStarted(sh); + } + + public static void assertElasticsearchStarted(Shell sh) throws IOException { waitForElasticsearch(); if (isSystemd()) { @@ -291,12 +294,21 @@ public static void startElasticsearch() throws IOException { } } - public static void stopElasticsearch() throws IOException { - final Shell sh = new Shell(); + public static void stopElasticsearch(Shell sh) throws IOException { if (isSystemd()) { sh.run("systemctl stop elasticsearch.service"); } else { sh.run("service elasticsearch stop"); } } + + public static void restartElasticsearch(Shell sh) throws IOException { + if (isSystemd()) { + sh.run("systemctl restart elasticsearch.service"); + } else { + sh.run("service elasticsearch restart"); + } + + waitForElasticsearch(); + } } diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Shell.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Shell.java index b437438130552..dc490de05b9c8 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Shell.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Shell.java @@ -27,6 +27,7 @@ import java.io.InputStreamReader; import java.nio.file.Path; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.stream.Stream; @@ -67,6 +68,10 @@ public Result runIgnoreExitCode(String script) { return runScriptIgnoreExitCode(getScriptCommand(script)); } + public Result run( String command, Object... args) { + String formattedCommand = String.format(Locale.ROOT, command, args); + return run(formattedCommand); + } private String[] getScriptCommand(String script) { if (Platforms.WINDOWS) { return powershellCommand(script); diff --git a/qa/vagrant/src/test/resources/packaging/tests/60_systemd.bats b/qa/vagrant/src/test/resources/packaging/tests/60_systemd.bats deleted file mode 100644 index 8baa75f38f5bc..0000000000000 --- a/qa/vagrant/src/test/resources/packaging/tests/60_systemd.bats +++ /dev/null @@ -1,257 +0,0 @@ -#!/usr/bin/env bats - -# This file is used to test the elasticsearch Systemd setup. - -# WARNING: This testing file must be executed as root and can -# dramatically change your system. It should only be executed -# in a throw-away VM like those made by the Vagrantfile at -# the root of the Elasticsearch source code. This should -# cause the script to fail if it is executed any other way: -[ -f /etc/is_vagrant_vm ] || { - >&2 echo "must be run on a vagrant VM" - exit 1 -} - -# The test case can be executed with the Bash Automated -# Testing System tool available at https://github.com/sstephenson/bats -# Thanks to Sam Stephenson! - -# Licensed to Elasticsearch under one or more contributor -# license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright -# ownership. Elasticsearch licenses this file to you under -# the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -# Load test utilities -load $BATS_UTILS/utils.bash -load $BATS_UTILS/packages.bash -load $BATS_UTILS/plugins.bash - -# Cleans everything for the 1st execution -setup() { - skip_not_systemd - skip_not_dpkg_or_rpm - export_elasticsearch_paths -} - -@test "[SYSTEMD] install elasticsearch" { - clean_before_test - install_package -} - -@test "[SYSTEMD] daemon reload after install" { - systemctl daemon-reload -} - -@test "[SYSTEMD] daemon isn't enabled on restart" { - # Rather than restart the VM we just ask systemd if it plans on starting - # elasticsearch on restart. Not as strong as a restart but much much - # faster. - run systemctl is-enabled elasticsearch.service - [ "$output" = "disabled" ] -} - -@test "[SYSTEMD] enable" { - systemctl enable elasticsearch.service - - systemctl is-enabled elasticsearch.service -} - -@test "[SYSTEMD] start" { - # Capture the current epoch in millis - run date +%s - epoch="$output" - - # The OpenJDK packaged for CentOS and OEL both override the default value (false) for the JVM option "AssumeMP". - # - # Because it is forced to "true" by default for these packages, the following warning message is printed to the - # standard output when the Vagrant box has only 1 CPU: - # OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure - # the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N - # - # This message will then fail the next test where we check if no entries have been added to the journal. - # - # This message appears since with java-1.8.0-openjdk-1.8.0.111-1.b15.el7_2.x86_64 because of the commit: - # 2016-10-10 - Andrew Hughes - 1:1.8.0.111-1.b15 - Turn debug builds on for all JIT architectures. - # Always AssumeMP on RHEL. - # - Resolves: rhbz#1381990 - # - if [ -x "$(command -v lsb_release)" ]; then - # Here we set the "-XX:-AssumeMP" option to false again: - lsb_release=$(lsb_release -i) - if [[ "$lsb_release" =~ "CentOS" ]] || [[ "$lsb_release" =~ "OracleServer" ]]; then - echo "-XX:-AssumeMP" >> $ESCONFIG/jvm.options - fi - fi - - systemctl start elasticsearch.service - wait_for_elasticsearch_status - assert_file_exist "/var/run/elasticsearch/elasticsearch.pid" - assert_file_exist "/var/log/elasticsearch/elasticsearch_server.json" - - # Converts the epoch back in a human readable format - run date --date=@$epoch "+%Y-%m-%d %H:%M:%S" - since="$output" - - # Verifies that no new entries in journald have been added - # since the last start - result="$(journalctl _SYSTEMD_UNIT=elasticsearch.service --since "$since" --output cat | wc -l)" - [ "$result" -eq "0" ] || { - echo "Expected no entries in journalctl for the Elasticsearch service but found:" - journalctl _SYSTEMD_UNIT=elasticsearch.service --since "$since" - false - } -} - -@test "[SYSTEMD] start (running)" { - systemctl start elasticsearch.service -} - -@test "[SYSTEMD] is active (running)" { - run systemctl is-active elasticsearch.service - [ "$status" -eq 0 ] - [ "$output" = "active" ] -} - -@test "[SYSTEMD] status (running)" { - systemctl status elasticsearch.service -} - -################################## -# Check that Elasticsearch is working -################################## -@test "[SYSTEMD] test elasticsearch" { - run_elasticsearch_tests -} - -@test "[SYSTEMD] restart" { - systemctl restart elasticsearch.service - - wait_for_elasticsearch_status - - service elasticsearch status -} - -@test "[SYSTEMD] stop (running)" { - systemctl stop elasticsearch.service -} - -@test "[SYSTEMD] status (stopping)" { - run systemctl status elasticsearch.service - # I'm not sure why suse exits 0 here, but it does - if [ ! -e /etc/SuSE-release ]; then - [ "$status" -eq 3 ] || "Expected exit code 3 meaning stopped but got $status" - fi - echo "$output" | grep "Active:" | grep "inactive" -} - -@test "[SYSTEMD] stop (stopped)" { - systemctl stop elasticsearch.service -} - -@test "[SYSTEMD] status (stopped)" { - run systemctl status elasticsearch.service - # I'm not sure why suse exits 0 here, but it does - if [ ! -e /etc/SuSE-release ]; then - [ "$status" -eq 3 ] || "Expected exit code 3 meaning stopped but got $status" - fi - echo "$output" | grep "Active:" | grep "inactive" -} - -# Simulates the behavior of a system restart: -# the PID directory is deleted by the operating system -# but it should not block ES from starting -# see https://github.com/elastic/elasticsearch/issues/11594 -@test "[SYSTEMD] delete PID_DIR and restart" { - rm -rf /var/run/elasticsearch - - systemd-tmpfiles --create - - systemctl start elasticsearch.service - - wait_for_elasticsearch_status - - assert_file_exist "/var/run/elasticsearch/elasticsearch.pid" - - systemctl stop elasticsearch.service -} - -@test "[SYSTEMD] start Elasticsearch with custom JVM options" { - assert_file_exist $ESENVFILE - # The custom config directory is not under /tmp or /var/tmp because - # systemd's private temp directory functionally means different - # processes can have different views of what's in these directories - local temp=`mktemp -p /etc -d` - cp "$ESCONFIG"/elasticsearch.yml "$temp" - cp "$ESCONFIG"/log4j2.properties "$temp" - touch "$temp/jvm.options" - chown -R elasticsearch:elasticsearch "$temp" - echo "-Xms512m" >> "$temp/jvm.options" - echo "-Xmx512m" >> "$temp/jvm.options" - # we have to disable Log4j from using JMX lest it will hit a security - # manager exception before we have configured logging; this will fail - # startup since we detect usages of logging before it is configured - echo "-Dlog4j2.disable.jmx=true" >> "$temp/jvm.options" - cp $ESENVFILE "$temp/elasticsearch" - echo "ES_PATH_CONF=\"$temp\"" >> $ESENVFILE - echo "ES_JAVA_OPTS=\"-XX:-UseCompressedOops\"" >> $ESENVFILE - service elasticsearch start - wait_for_elasticsearch_status - curl -s -XGET localhost:9200/_nodes | fgrep '"heap_init_in_bytes":536870912' - curl -s -XGET localhost:9200/_nodes | fgrep '"using_compressed_ordinary_object_pointers":"false"' - service elasticsearch stop - cp "$temp/elasticsearch" $ESENVFILE -} - -@test "[SYSTEMD] masking systemd-sysctl" { - clean_before_test - - systemctl mask systemd-sysctl.service - install_package - - systemctl unmask systemd-sysctl.service -} - -@test "[SYSTEMD] service file sets limits" { - clean_before_test - install_package - systemctl start elasticsearch.service - wait_for_elasticsearch_status - local pid=$(cat /var/run/elasticsearch/elasticsearch.pid) - local max_file_size=$(cat /proc/$pid/limits | grep "Max file size" | awk '{ print $4 }') - [ "$max_file_size" == "unlimited" ] - local max_processes=$(cat /proc/$pid/limits | grep "Max processes" | awk '{ print $3 }') - [ "$max_processes" == "4096" ] - local max_open_files=$(cat /proc/$pid/limits | grep "Max open files" | awk '{ print $4 }') - [ "$max_open_files" == "65535" ] - local max_address_space=$(cat /proc/$pid/limits | grep "Max address space" | awk '{ print $4 }') - [ "$max_address_space" == "unlimited" ] - systemctl stop elasticsearch.service -} - -@test "[SYSTEMD] test runtime directory" { - clean_before_test - install_package - sudo rm -rf /var/run/elasticsearch - systemctl start elasticsearch.service - wait_for_elasticsearch_status - [ -d /var/run/elasticsearch ] - systemctl stop elasticsearch.service -} - -@test "[SYSTEMD] GC logs exist" { - start_elasticsearch_service - assert_file_exist /var/log/elasticsearch/gc.log.0.current - stop_elasticsearch_service -} From 4fddb2e55f55f3d7a1d8dbcbafd66e184b820114 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Tue, 2 Apr 2019 12:54:09 +0200 Subject: [PATCH 2/3] Bat scripts to work with JAVA_HOME with parentheses (#39712) the elasticsearch.bat and elasticsearch-env.bat won't work if JAVA contains parentheses. This seems to be the limitation of FOR /F IN (command) DO syntax. The JAVA variable present in a command contains a path to a binary to start elasticsearch (with spaces & parens). We can workaround the problem of spaces and parentheses in this path by referring this variable with a CALL command. Note that executing binaries with CALL is an undocumented behaviour (but works) closes #38578 closes #38624 closes #33405 closes #30606 --- distribution/src/bin/elasticsearch-env.bat | 2 +- distribution/src/bin/elasticsearch.bat | 4 +- .../packaging/test/ArchiveTestCase.java | 92 +++++++++++++++---- .../test/DebPreservationTestCase.java | 9 +- .../packaging/test/PackageTestCase.java | 40 ++++---- .../packaging/test/PackagingTestCase.java | 4 +- .../test/RpmPreservationTestCase.java | 9 +- .../test/WindowsServiceTestCase.java | 2 +- .../packaging/util/Archives.java | 14 ++- .../elasticsearch/packaging/util/Cleanup.java | 2 +- .../packaging/util/Packages.java | 6 +- .../packaging/util/Platforms.java | 10 +- 12 files changed, 126 insertions(+), 68 deletions(-) diff --git a/distribution/src/bin/elasticsearch-env.bat b/distribution/src/bin/elasticsearch-env.bat index 1264d116b6a72..395a66878c42c 100644 --- a/distribution/src/bin/elasticsearch-env.bat +++ b/distribution/src/bin/elasticsearch-env.bat @@ -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%" diff --git a/distribution/src/bin/elasticsearch.bat b/distribution/src/bin/elasticsearch.bat index 7df6f19fc0765..ecbbad826e797 100644 --- a/distribution/src/bin/elasticsearch.bat +++ b/distribution/src/bin/elasticsearch.bat @@ -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" ( diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java index af22861a90aa8..ba0484046adfd 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java @@ -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; @@ -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; @@ -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(); @@ -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(); @@ -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(); @@ -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 @@ -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(() -> { @@ -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)); @@ -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: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 del /F /Q '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)); @@ -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"); @@ -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"); @@ -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(); @@ -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(); @@ -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(); @@ -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); diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/DebPreservationTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/DebPreservationTestCase.java index 1b2b891da4513..12597ae8b4de2 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/DebPreservationTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/DebPreservationTestCase.java @@ -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; @@ -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(); } @@ -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()); @@ -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(); diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java index 458359b299e75..c664e28931087 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackageTestCase.java @@ -20,6 +20,7 @@ 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.FileUtils; import org.elasticsearch.packaging.util.Shell; @@ -27,7 +28,6 @@ import org.hamcrest.CoreMatchers; import org.junit.Before; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -36,6 +36,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import static com.carrotsearch.randomizedtesting.RandomizedTest.getRandom; import static org.elasticsearch.packaging.util.FileUtils.append; import static org.elasticsearch.packaging.util.FileUtils.assertPathsDontExist; import static org.elasticsearch.packaging.util.FileUtils.assertPathsExist; @@ -72,19 +73,19 @@ public abstract class PackageTestCase extends PackagingTestCase { private Shell sh; @Before - public void onlyCompatibleDistributions() { + public void onlyCompatibleDistributions() throws Exception { assumeTrue("only compatible distributions", distribution().packaging.compatible); sh = newShell(); } - public void test10InstallPackage() throws IOException { + public void test10InstallPackage() throws Exception { assertRemoved(distribution()); installation = install(distribution()); assertInstalled(distribution()); verifyPackageInstallation(installation, distribution(), sh); } - public void test20PluginsCommandWhenNoPlugins() { + public void test20PluginsCommandWhenNoPlugins() throws Exception { assumeThat(installation, is(notNullValue())); assertThat(sh.run(installation.bin("elasticsearch-plugin") + " list").stdout, isEmptyString()); @@ -104,7 +105,7 @@ public void test31InstallDoesNotStartServer() { assertThat(sh.run("ps aux").stdout, not(containsString("org.elasticsearch.bootstrap.Elasticsearch"))); } - public void assertRunsWithJavaHome() throws IOException { + public void assertRunsWithJavaHome() throws Exception { String systemJavaHome = sh.run("echo $SYSTEM_JAVA_HOME").stdout.trim(); byte[] originalEnvFile = Files.readAllBytes(installation.envFile); try { @@ -121,7 +122,7 @@ public void assertRunsWithJavaHome() throws IOException { assertThat(new String(Files.readAllBytes(log), StandardCharsets.UTF_8), containsString(systemJavaHome)); } - public void test32JavaHomeOverride() throws IOException { + public void test32JavaHomeOverride() throws Exception { assumeThat(installation, is(notNullValue())); // we always run with java home when no bundled jdk is included, so this test would be repetitive assumeThat(distribution().hasJdk, is(true)); @@ -129,7 +130,7 @@ public void test32JavaHomeOverride() throws IOException { assertRunsWithJavaHome(); } - public void test42BundledJdkRemoved() throws IOException { + public void test42BundledJdkRemoved() throws Exception { assumeThat(installation, is(notNullValue())); assumeThat(distribution().hasJdk, is(true)); @@ -142,7 +143,7 @@ public void test42BundledJdkRemoved() throws IOException { } } - public void test40StartServer() throws IOException { + public void test40StartServer() throws Exception { String start = sh.runIgnoreExitCode("date ").stdout.trim(); assumeThat(installation, is(notNullValue())); @@ -159,7 +160,7 @@ public void test40StartServer() throws IOException { verifyPackageInstallation(installation, distribution(), sh); // check startup script didn't change permissions } - public void test50Remove() { + public void test50Remove() throws Exception { assumeThat(installation, is(notNullValue())); remove(distribution()); @@ -209,7 +210,7 @@ public void test50Remove() { assertFalse(Files.exists(SYSTEMD_SERVICE)); } - public void test60Reinstall() throws IOException { + public void test60Reinstall() throws Exception { assumeThat(installation, is(notNullValue())); installation = install(distribution()); @@ -220,7 +221,7 @@ public void test60Reinstall() throws IOException { assertRemoved(distribution()); } - public void test70RestartServer() throws IOException { + public void test70RestartServer() throws Exception { try { installation = install(distribution()); assertInstalled(distribution()); @@ -235,7 +236,7 @@ public void test70RestartServer() throws IOException { } - public void test72TestRuntimeDirectory() throws IOException { + public void test72TestRuntimeDirectory() throws Exception { try { installation = install(distribution()); FileUtils.rm(installation.pidDir); @@ -247,7 +248,7 @@ public void test72TestRuntimeDirectory() throws IOException { } } - public void test73gcLogsExist() throws IOException { + public void test73gcLogsExist() throws Exception { installation = install(distribution()); startElasticsearch(sh); // it can be gc.log or gc.log.0.current @@ -264,7 +265,7 @@ public void test73gcLogsExist() throws IOException { * # but it should not block ES from starting * # see https://github.com/elastic/elasticsearch/issues/11594 */ - public void test80DeletePID_DIRandRestart() throws IOException { + public void test80DeletePID_DIRandRestart() throws Exception { assumeTrue(isSystemd()); rm(installation.pidDir); @@ -280,7 +281,7 @@ public void test80DeletePID_DIRandRestart() throws IOException { stopElasticsearch(sh); } - public void test81CustomPathConfAndJvmOptions() throws IOException { + public void test81CustomPathConfAndJvmOptions() throws Exception { assumeTrue(isSystemd()); assumeThat(installation, is(notNullValue())); @@ -291,8 +292,9 @@ public void test81CustomPathConfAndJvmOptions() throws IOException { // The custom config directory is not under /tmp or /var/tmp because // systemd's private temp directory functionally means different // processes can have different views of what's in these directories - String temp = sh.runIgnoreExitCode("mktemp -p /etc -d").stdout.trim(); - final Path tempConf = Paths.get(temp); + String randomName = RandomStrings.randomAsciiAlphanumOfLength(getRandom(), 10); + sh.run("mkdir /etc/"+randomName); + final Path tempConf = Paths.get("/etc/"+randomName); try { mkdir(tempConf); @@ -331,7 +333,7 @@ public void test81CustomPathConfAndJvmOptions() throws IOException { } } - public void test82SystemdMask() throws IOException { + public void test82SystemdMask() throws Exception { try { assumeTrue(isSystemd()); @@ -345,7 +347,7 @@ public void test82SystemdMask() throws IOException { } } - public void test83serviceFileSetsLimits() throws IOException { + public void test83serviceFileSetsLimits() throws Exception { // Limits are changed on systemd platforms only assumeTrue(isSystemd()); diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackagingTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackagingTestCase.java index 7cb860e617eb0..bd7738aeac4ac 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackagingTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/PackagingTestCase.java @@ -64,7 +64,7 @@ public void setup() { protected static Installation installation; @BeforeClass - public static void cleanup() { + public static void cleanup() throws Exception { installation = null; cleanEverything(); } @@ -72,7 +72,7 @@ public static void cleanup() { /** The {@link Distribution} that should be tested in this case */ protected abstract Distribution distribution(); - protected Shell newShell() { + protected Shell newShell() throws Exception { Shell sh = new Shell(); if (distribution().hasJdk == false) { Platforms.onLinux(() -> { diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/RpmPreservationTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/RpmPreservationTestCase.java index 5cfc10b110afb..7b6ac039fc55c 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/RpmPreservationTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/RpmPreservationTestCase.java @@ -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.Path; import java.util.stream.Stream; @@ -56,7 +55,7 @@ public abstract class RpmPreservationTestCase extends PackagingTestCase { protected abstract Distribution distribution(); @BeforeClass - public static void cleanup() { + public static void cleanup() throws Exception { installation = null; cleanEverything(); } @@ -67,14 +66,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()); @@ -89,7 +88,7 @@ public void test20Remove() { assertFalse(Files.exists(installation.envFile)); } - public void test30PreserveConfig() throws IOException { + public void test30PreserveConfig() throws Exception { final Shell sh = new Shell(); installation = install(distribution()); diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/WindowsServiceTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/WindowsServiceTestCase.java index b9536f86184fb..13ada93e6bef7 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/WindowsServiceTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/WindowsServiceTestCase.java @@ -103,7 +103,7 @@ private void assertCommand(String script) { } } - public void test10InstallArchive() { + public void test10InstallArchive() throws Exception { installation = installArchive(distribution()); verifyArchiveInstallation(installation, distribution()); serviceScript = installation.bin("elasticsearch-service.bat").toString(); diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Archives.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Archives.java index c8ddda2dc4f37..e557b47fb8912 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Archives.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Archives.java @@ -19,7 +19,6 @@ package org.elasticsearch.packaging.util; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -37,15 +36,14 @@ import static org.elasticsearch.packaging.util.FileUtils.getDefaultArchiveInstallPath; import static org.elasticsearch.packaging.util.FileUtils.getDistributionFile; import static org.elasticsearch.packaging.util.FileUtils.lsGlob; - import static org.elasticsearch.packaging.util.FileUtils.mv; import static org.elasticsearch.packaging.util.FileUtils.slurp; import static org.elasticsearch.packaging.util.Platforms.isDPKG; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.isEmptyOrNullString; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.hamcrest.collection.IsEmptyCollection.empty; +import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.assertTrue; @@ -59,11 +57,11 @@ public class Archives { ? "vagrant" : "elasticsearch"; - public static Installation installArchive(Distribution distribution) { + public static Installation installArchive(Distribution distribution) throws Exception { return installArchive(distribution, getDefaultArchiveInstallPath(), getCurrentVersion()); } - public static Installation installArchive(Distribution distribution, Path fullInstallPath, String version) { + public static Installation installArchive(Distribution distribution, Path fullInstallPath, String version) throws Exception { final Shell sh = new Shell(); final Path distributionFile = getDistributionFile(distribution); @@ -255,7 +253,7 @@ private static void verifyDefaultInstallation(Installation es, Distribution dist ).forEach(configFile -> assertThat(es.config(configFile), file(File, owner, owner, p660))); } - public static void runElasticsearch(Installation installation, Shell sh) throws IOException { + public static void runElasticsearch(Installation installation, Shell sh) throws Exception { final Path pidFile = installation.home.resolve("elasticsearch.pid"); final Installation.Executables bin = installation.executables(); @@ -305,7 +303,7 @@ public static void runElasticsearch(Installation installation, Shell sh) throws Platforms.onWindows(() -> sh.run("Get-Process -Id " + pid)); } - public static void stopElasticsearch(Installation installation) { + public static void stopElasticsearch(Installation installation) throws Exception { Path pidFile = installation.home.resolve("elasticsearch.pid"); assertTrue(Files.exists(pidFile)); String pid = slurp(pidFile).trim(); diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Cleanup.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Cleanup.java index fda61e9fb36e5..f9b98d58ccacc 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Cleanup.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Cleanup.java @@ -50,7 +50,7 @@ public class Cleanup { // todo private static final List ELASTICSEARCH_FILES_WINDOWS = Collections.emptyList(); - public static void cleanEverything() { + public static void cleanEverything() throws Exception { final Shell sh = new Shell(); // kill elasticsearch processes diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java index afa7e371c2c55..c5dcc34af882f 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Packages.java @@ -54,14 +54,14 @@ public class Packages { public static final Path SYSVINIT_SCRIPT = Paths.get("/etc/init.d/elasticsearch"); public static final Path SYSTEMD_SERVICE = Paths.get("/usr/lib/systemd/system/elasticsearch.service"); - public static void assertInstalled(Distribution distribution) { + public static void assertInstalled(Distribution distribution) throws Exception { final Result status = packageStatus(distribution); assertThat(status.exitCode, is(0)); Platforms.onDPKG(() -> assertFalse(Pattern.compile("(?m)^Status:.+deinstall ok").matcher(status.stdout).find())); } - public static void assertRemoved(Distribution distribution) { + public static void assertRemoved(Distribution distribution) throws Exception { final Result status = packageStatus(distribution); Platforms.onRPM(() -> assertThat(status.exitCode, is(1))); @@ -133,7 +133,7 @@ public static Result runInstallCommand(Distribution distribution, String version } } - public static void remove(Distribution distribution) { + public static void remove(Distribution distribution) throws Exception { final Shell sh = new Shell(); Platforms.onRPM(() -> { diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Platforms.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Platforms.java index dbac9c88d26c9..6258c1336b2fc 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Platforms.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/util/Platforms.java @@ -65,25 +65,25 @@ public static boolean isSysVInit() { return new Shell().runIgnoreExitCode("which service").isSuccess(); } - public static void onWindows(PlatformAction action) { + public static void onWindows(PlatformAction action) throws Exception { if (WINDOWS) { action.run(); } } - public static void onLinux(PlatformAction action) { + public static void onLinux(PlatformAction action) throws Exception { if (LINUX) { action.run(); } } - public static void onRPM(PlatformAction action) { + public static void onRPM(PlatformAction action) throws Exception { if (isRPM()) { action.run(); } } - public static void onDPKG(PlatformAction action) { + public static void onDPKG(PlatformAction action) throws Exception { if (isDPKG()) { action.run(); } @@ -94,6 +94,6 @@ public static void onDPKG(PlatformAction action) { */ @FunctionalInterface public interface PlatformAction { - void run(); + void run() throws Exception; } } From 7fe0faab7381483eab17c906124377a12d17091d Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 3 Apr 2019 23:00:55 -0700 Subject: [PATCH 3/3] Link to SYSTEM_JAVA_HOME on windows (#40806) We don't always have java home defined in packaging tests, as we want to use the bundled jdk most of the time. This commit fixes the java home with special characters test to link to SYSTEM_JAVA_HOME on windows. closes #40797 --- .../org/elasticsearch/packaging/test/ArchiveTestCase.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java index ba0484046adfd..531ae1e3c4a82 100644 --- a/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java +++ b/qa/vagrant/src/main/java/org/elasticsearch/packaging/test/ArchiveTestCase.java @@ -203,7 +203,7 @@ public void test53JavaHomeWithSpecialCharacters() throws Exception { 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:JAVA_HOME"); + 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"); @@ -218,7 +218,7 @@ public void test53JavaHomeWithSpecialCharacters() throws Exception { } finally { //clean up sym link - sh.run("cmd /c del /F /Q 'C:\\Program Files (x86)\\java' "); + sh.run("cmd /c rmdir 'C:\\Program Files (x86)\\java' "); } });