From 9a175e7ed7c5ec3d9cb277d7476eec52cbd333b5 Mon Sep 17 00:00:00 2001 From: prateek-kumar-improving Date: Tue, 10 Sep 2024 16:55:43 -0700 Subject: [PATCH] Fetch server version using info command (#2258) * Fetch server version using info command Signed-off-by: Prateek Kumar --- CHANGELOG.md | 1 + .../test/java/glide/TestConfiguration.java | 21 ++++++++++++++- .../src/test/java/glide/TestUtilities.java | 27 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e04e86a7df..1c1a9789a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ #### Changes +* Java: Fetch server version using info command ([#2258](https://github.com/valkey-io/valkey-glide/pull/2258)) * Node: Added binary variant for commands which have `Record` as input or output ([#2207](https://github.com/valkey-io/valkey-glide/pull/2207)) * Node: Renamed `ReturnType` to `GlideReturnType` ([#2241](https://github.com/valkey-io/valkey-glide/pull/2241)) * Node, Python: Rename `stop` to `end` in sorted set queries ([#2214](https://github.com/valkey-io/valkey-glide/pull/2214)) diff --git a/java/integTest/src/test/java/glide/TestConfiguration.java b/java/integTest/src/test/java/glide/TestConfiguration.java index 7bf788746e..a57c1928b7 100644 --- a/java/integTest/src/test/java/glide/TestConfiguration.java +++ b/java/integTest/src/test/java/glide/TestConfiguration.java @@ -1,14 +1,33 @@ /** Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */ package glide; +import static glide.TestUtilities.commonClientConfig; + import com.vdurmont.semver4j.Semver; +import glide.api.GlideClient; import java.util.Arrays; public final class TestConfiguration { // All servers are hosted on localhost public static final int[] STANDALONE_PORTS = getPortsFromProperty("test.server.standalone.ports"); public static final int[] CLUSTER_PORTS = getPortsFromProperty("test.server.cluster.ports"); - public static final Semver SERVER_VERSION = new Semver(System.getProperty("test.server.version")); + public static final Semver SERVER_VERSION; + + static { + try { + String serverVersion = + TestUtilities.getServerVersion( + GlideClient.createClient(commonClientConfig().build()).get()); + if (serverVersion != null) { + SERVER_VERSION = new Semver(serverVersion); + } else { + throw new Exception("Failed to get server version"); + } + + } catch (Exception e) { + throw new RuntimeException("Failed to get server version", e); + } + } private static int[] getPortsFromProperty(String propName) { return Arrays.stream(System.getProperty(propName).split(",")) diff --git a/java/integTest/src/test/java/glide/TestUtilities.java b/java/integTest/src/test/java/glide/TestUtilities.java index 97c5222ba7..40371498f7 100644 --- a/java/integTest/src/test/java/glide/TestUtilities.java +++ b/java/integTest/src/test/java/glide/TestUtilities.java @@ -14,6 +14,7 @@ import glide.api.GlideClusterClient; import glide.api.models.ClusterValue; import glide.api.models.GlideString; +import glide.api.models.commands.InfoOptions; import glide.api.models.configuration.GlideClientConfiguration; import glide.api.models.configuration.GlideClusterClientConfiguration; import glide.api.models.configuration.NodeAddress; @@ -25,10 +26,17 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import lombok.NonNull; +import lombok.SneakyThrows; import lombok.experimental.UtilityClass; @UtilityClass public class TestUtilities { + /** Key names for versions returned in info command. */ + private static final String VALKEY_VERSION_KEY = "valkey_version"; + + private static final String REDIS_VERSION_KEY = "redis_version"; + /** Extract integer parameter value from INFO command output */ public static long getValueFromInfo(String data, String value) { for (var line : data.split("\r\n")) { @@ -371,4 +379,23 @@ public static void waitForNotBusy(BaseClient client) { } } while (isBusy); } + + /** + * This method returns the server version using a glide client. + * + * @param glideClient Glide client to be used for running the info command. + * @return String The server version number. + */ + @SneakyThrows + public static String getServerVersion(@NonNull final GlideClient glideClient) { + String infoResponse = + glideClient.info(InfoOptions.builder().section(InfoOptions.Section.SERVER).build()).get(); + Map infoResponseMap = parseInfoResponseToMap(infoResponse); + if (infoResponseMap.containsKey(VALKEY_VERSION_KEY)) { + return infoResponseMap.get(VALKEY_VERSION_KEY); + } else if (infoResponseMap.containsKey(REDIS_VERSION_KEY)) { + return infoResponseMap.get(REDIS_VERSION_KEY); + } + return null; + } }