Skip to content

Commit

Permalink
Introduce forceRedisServerVersion into test
Browse files Browse the repository at this point in the history
If set will assume this version instead of resolving it from tested server instaces.

Used to force running tests agains older server versions
  • Loading branch information
ggivo committed Nov 19, 2024
1 parent cfea6ab commit e1c3608
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/test/java/io/redis/test/utils/RedisVersionRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@

import java.lang.reflect.Method;

import static io.redis.test.utils.RedisVersionUtil.FORCE_REDIS_SERVER_VERSION_ENV;

public class RedisVersionRule implements TestRule {
private static final Logger logger = LoggerFactory.getLogger(RedisVersionRule.class);

private final HostAndPort hostPort;
private final JedisClientConfig config;
private static final RedisVersion forcedVersion = System.getenv(FORCE_REDIS_SERVER_VERSION_ENV) != null
? RedisVersion.of(System.getenv(FORCE_REDIS_SERVER_VERSION_ENV))
: null;


public RedisVersionRule(EndpointConfig endpoint) {
this.hostPort = endpoint.getHostAndPort();
Expand Down Expand Up @@ -56,9 +62,19 @@ public void evaluate() throws Throwable {
}
}
private void checkRedisVersion(Jedis jedisClient, SinceRedisVersion versionAnnotation) {

// Check if the environment variable is set
RedisVersion currentVersion;

if (forcedVersion != null) {
logger.info("Using forced Redis server version from environment variable: " + forcedVersion);
currentVersion = forcedVersion;
} else {
RedisInfo info = RedisInfo.parseInfoServer(jedisClient.info("server"));
currentVersion = RedisVersion.of(info.getRedisVersion());
}

RedisVersion minRequiredVersion = RedisVersion.of(versionAnnotation.value());
RedisInfo info = RedisInfo.parseInfoServer(jedisClient.info("server"));
RedisVersion currentVersion = RedisVersion.of(info.getRedisVersion());
if (currentVersion.isLessThan(minRequiredVersion)) {
Assume.assumeTrue("Test requires Redis version " + minRequiredVersion + " or later, but found " + currentVersion, false);
}
Expand All @@ -80,7 +96,6 @@ private SinceRedisVersion getMethodAnnotation(Description description) {
}
}
} catch (Exception e) {
// Handle any potential exceptions here
throw new RuntimeException("Could not resolve EnabledOnCommand annotation", e);
}
return null;
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/io/redis/test/utils/RedisVersionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@

public class RedisVersionUtil {

static final String FORCE_REDIS_SERVER_VERSION_ENV = "forceRedisServerVersion";
private static final RedisVersion forcedVersion = System.getenv(FORCE_REDIS_SERVER_VERSION_ENV) != null
? RedisVersion.of(System.getenv(FORCE_REDIS_SERVER_VERSION_ENV))
: null;

public static RedisVersion getRedisVersion(Connection conn) {
if (forcedVersion != null) {
return forcedVersion;
}

try (Jedis jedis = new Jedis(conn)) {
Object response = SafeEncoder.encodeObject(jedis.sendCommand(Protocol.Command.INFO, "server"));
Expand All @@ -20,19 +27,29 @@ public static RedisVersion getRedisVersion(Connection conn) {
}

public static RedisVersion getRedisVersion(UnifiedJedis jedis) {
if (forcedVersion != null) {
return forcedVersion;
}

Object response = SafeEncoder.encodeObject(jedis.sendCommand(Protocol.Command.INFO, "server"));
RedisInfo info = RedisInfo.parseInfoServer(response.toString());
return RedisVersion.of(info.getRedisVersion());
}

public static RedisVersion getRedisVersion(Jedis jedis) {
if (forcedVersion != null) {
return forcedVersion;
}

RedisInfo info = RedisInfo.parseInfoServer(jedis.info("server"));
return RedisVersion.of(info.getRedisVersion());
}

public static RedisVersion getRedisVersion(EndpointConfig endpoint) {
if (forcedVersion != null) {
return forcedVersion;
}

DefaultJedisClientConfig.Builder builder = endpoint.getClientConfigBuilder();
if (endpoint.isTls()) {
builder.sslSocketFactory(createTrustAllSslSocketFactory());
Expand Down

0 comments on commit e1c3608

Please sign in to comment.