From 1afc9afcacebf9fce50c8544df82b68240ace2ea Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 31 Jul 2017 11:45:06 +0900 Subject: [PATCH] Version option should display if snapshot We have a command-line flag -V or --version that can be used to display the version of Elasticsearch. However, the version that we display does not contain whether or not the version is a snapshot build. This commit changes the behavior here so that if the build is a snapshot, that is included in the version string. Relates #25970 --- core/src/main/java/org/elasticsearch/Version.java | 4 ++++ .../org/elasticsearch/bootstrap/Elasticsearch.java | 3 ++- core/src/main/java/org/elasticsearch/node/Node.java | 9 ++------- .../test/java/org/elasticsearch/VersionTests.java | 12 ++++++++++++ .../bootstrap/ElasticsearchCliTests.java | 2 +- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/Version.java b/core/src/main/java/org/elasticsearch/Version.java index c70ddeb1cc1..9664f42d68c 100644 --- a/core/src/main/java/org/elasticsearch/Version.java +++ b/core/src/main/java/org/elasticsearch/Version.java @@ -382,6 +382,10 @@ public class Version implements Comparable { return sb.toString(); } + public static String displayVersion(final Version version, final boolean isSnapshot) { + return version + (isSnapshot ? "-SNAPSHOT" : ""); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java b/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java index 9450a7bd868..62b01f3fe85 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java @@ -24,6 +24,7 @@ import joptsimple.OptionSpec; import joptsimple.OptionSpecBuilder; import joptsimple.util.PathConverter; import org.elasticsearch.Build; +import org.elasticsearch.Version; import org.elasticsearch.cli.EnvironmentAwareCommand; import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.Terminal; @@ -97,7 +98,7 @@ class Elasticsearch extends EnvironmentAwareCommand { throw new UserException(ExitCodes.USAGE, "Positional arguments not allowed, found " + options.nonOptionArguments()); } if (options.has(versionOption)) { - terminal.println("Version: " + org.elasticsearch.Version.CURRENT + terminal.println("Version: " + Version.displayVersion(Version.CURRENT, Build.CURRENT.isSnapshot()) + ", Build: " + Build.CURRENT.shortHash() + "/" + Build.CURRENT.date() + ", JVM: " + JvmInfo.jvmInfo().version()); return; diff --git a/core/src/main/java/org/elasticsearch/node/Node.java b/core/src/main/java/org/elasticsearch/node/Node.java index cae4fc85e97..9752cc4c2dd 100644 --- a/core/src/main/java/org/elasticsearch/node/Node.java +++ b/core/src/main/java/org/elasticsearch/node/Node.java @@ -277,7 +277,7 @@ public class Node implements Closeable { final JvmInfo jvmInfo = JvmInfo.jvmInfo(); logger.info( "version[{}], pid[{}], build[{}/{}], OS[{}/{}/{}], JVM[{}/{}/{}/{}]", - displayVersion(Version.CURRENT, Build.CURRENT.isSnapshot()), + Version.displayVersion(Version.CURRENT, Build.CURRENT.isSnapshot()), jvmInfo.pid(), Build.CURRENT.shortHash(), Build.CURRENT.date(), @@ -515,19 +515,14 @@ public class Node implements Closeable { } } - // visible for testing static void warnIfPreRelease(final Version version, final boolean isSnapshot, final Logger logger) { if (!version.isRelease() || isSnapshot) { logger.warn( "version [{}] is a pre-release version of Elasticsearch and is not suitable for production", - displayVersion(version, isSnapshot)); + Version.displayVersion(version, isSnapshot)); } } - private static String displayVersion(final Version version, final boolean isSnapshot) { - return version + (isSnapshot ? "-SNAPSHOT" : ""); - } - protected TransportService newTransportService(Settings settings, Transport transport, ThreadPool threadPool, TransportInterceptor interceptor, Function localNodeFactory, diff --git a/core/src/test/java/org/elasticsearch/VersionTests.java b/core/src/test/java/org/elasticsearch/VersionTests.java index f282f6f5e0c..5516ba94cc1 100644 --- a/core/src/test/java/org/elasticsearch/VersionTests.java +++ b/core/src/test/java/org/elasticsearch/VersionTests.java @@ -378,4 +378,16 @@ public class VersionTests extends ESTestCase { VersionTests.assertUnknownVersion(VERSION_5_1_0_UNRELEASED); } + public void testDisplayVersion() { + final Version version = randomVersion(random()); + { + final String displayVersion = Version.displayVersion(version, true); + assertThat(displayVersion, equalTo(version.toString() + "-SNAPSHOT")); + } + { + final String displayVersion = Version.displayVersion(version, false); + assertThat(displayVersion, equalTo(version.toString())); + } + } + } diff --git a/core/src/test/java/org/elasticsearch/bootstrap/ElasticsearchCliTests.java b/core/src/test/java/org/elasticsearch/bootstrap/ElasticsearchCliTests.java index 07c5a7e157f..137d7593edc 100644 --- a/core/src/test/java/org/elasticsearch/bootstrap/ElasticsearchCliTests.java +++ b/core/src/test/java/org/elasticsearch/bootstrap/ElasticsearchCliTests.java @@ -63,7 +63,7 @@ public class ElasticsearchCliTests extends ESElasticsearchCliTestCase { private void runTestThatVersionIsReturned(String... args) throws Exception { runTestVersion(ExitCodes.OK, output -> { - assertThat(output, containsString("Version: " + Version.CURRENT.toString())); + assertThat(output, containsString("Version: " + Version.displayVersion(Version.CURRENT, Build.CURRENT.isSnapshot()))); assertThat(output, containsString("Build: " + Build.CURRENT.shortHash() + "/" + Build.CURRENT.date())); assertThat(output, containsString("JVM: " + JvmInfo.jvmInfo().version())); }, args);