Skip to content

Commit 1f08fad

Browse files
uglideatakavci
andauthored
Load Redis test endpoints from config file or env vars (#294)
* Load Redis test endpoints from config file or env vars * Fix formatting * add IsTargetConnectionExist to help skipping some tests --------- Co-authored-by: atakavci <[email protected]> Co-authored-by: atakavci <[email protected]>
1 parent 0171221 commit 1f08fad

File tree

2 files changed

+87
-32
lines changed

2 files changed

+87
-32
lines changed

tests/NRedisStack.Tests/Core Commands/CoreTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public void TestBZMPopMultiplexerTimeout()
231231
var configurationOptions = new ConfigurationOptions();
232232
configurationOptions.SyncTimeout = 1000;
233233

234-
using var redis = redisFixture.CustomRedis(configurationOptions, out _);
234+
using var redis = redisFixture.GetConnectionById(configurationOptions, "standalone");
235235

236236
var db = redis.GetDatabase(null);
237237
db.Execute("FLUSHALL");
@@ -246,7 +246,7 @@ public async Task TestBZMPopMultiplexerTimeoutAsync()
246246
var configurationOptions = new ConfigurationOptions();
247247
configurationOptions.SyncTimeout = 1000;
248248

249-
await using var redis = redisFixture.CustomRedis(configurationOptions, out _);
249+
await using var redis = redisFixture.GetConnectionById(configurationOptions, "standalone");
250250

251251
var db = redis.GetDatabase(null);
252252
db.Execute("FLUSHALL");
+85-30
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,54 @@
11
using StackExchange.Redis;
2+
using System.Text.Json;
23

34
namespace NRedisStack.Tests;
45

6+
public class EndpointConfig
7+
{
8+
public List<string>? endpoints { get; set; }
9+
10+
public bool tls { get; set; }
11+
12+
public string? password { get; set; }
13+
14+
public int? bdb_id { get; set; }
15+
16+
public object? raw_endpoints { get; set; }
17+
18+
public ConnectionMultiplexer CreateConnection(ConfigurationOptions configurationOptions)
19+
{
20+
configurationOptions.EndPoints.Clear();
21+
22+
foreach (var endpoint in endpoints!)
23+
{
24+
configurationOptions.EndPoints.Add(endpoint);
25+
}
26+
27+
if (password != null)
28+
{
29+
configurationOptions.Password = password;
30+
}
31+
32+
// TODO(imalinovskiy): Add support for TLS
33+
// TODO(imalinovskiy): Add support for Discovery/Sentinel API
34+
35+
return ConnectionMultiplexer.Connect(configurationOptions);
36+
}
37+
}
38+
39+
540
public class RedisFixture : IDisposable
641
{
742
// Set the environment variable to specify your own alternate host and port:
843
private readonly string redisStandalone = Environment.GetEnvironmentVariable("REDIS") ?? "localhost:6379";
944
private readonly string? redisCluster = Environment.GetEnvironmentVariable("REDIS_CLUSTER");
1045
private readonly string? numRedisClusterNodesEnv = Environment.GetEnvironmentVariable("NUM_REDIS_CLUSTER_NODES");
46+
47+
private readonly string defaultEndpointId = Environment.GetEnvironmentVariable("REDIS_DEFAULT_ENDPOINT_ID") ?? "standalone";
48+
private readonly string? redisEndpointsPath = Environment.GetEnvironmentVariable("REDIS_ENDPOINTS_CONFIG_PATH");
49+
private Dictionary<string, EndpointConfig> redisEndpoints = new();
50+
51+
1152
public bool isEnterprise = Environment.GetEnvironmentVariable("IS_ENTERPRISE") == "true";
1253
public bool isOSSCluster;
1354

@@ -18,7 +59,42 @@ public RedisFixture()
1859
AsyncTimeout = 10000,
1960
SyncTimeout = 10000
2061
};
21-
Redis = Connect(clusterConfig, out isOSSCluster);
62+
63+
if (redisEndpointsPath != null && File.Exists(redisEndpointsPath))
64+
{
65+
string json = File.ReadAllText(redisEndpointsPath);
66+
var parsedEndpoints = JsonSerializer.Deserialize<Dictionary<string, EndpointConfig>>(json);
67+
68+
redisEndpoints = parsedEndpoints ?? throw new Exception("Failed to parse the Redis endpoints configuration.");
69+
}
70+
else
71+
{
72+
redisEndpoints.Add("standalone",
73+
new EndpointConfig { endpoints = new List<string> { redisStandalone } });
74+
75+
if (redisCluster != null)
76+
{
77+
string[] parts = redisCluster!.Split(':');
78+
string host = parts[0];
79+
int startPort = int.Parse(parts[1]);
80+
81+
var endpoints = new List<string>();
82+
int numRedisClusterNodes = int.Parse(numRedisClusterNodesEnv!);
83+
for (int i = 0; i < numRedisClusterNodes; i++)
84+
{
85+
endpoints.Add($"{host}:{startPort + i}");
86+
}
87+
88+
redisEndpoints.Add("cluster",
89+
new EndpointConfig { endpoints = endpoints });
90+
91+
// Set the default endpoint to the cluster to keep the tests consistent
92+
defaultEndpointId = "cluster";
93+
isOSSCluster = true;
94+
}
95+
}
96+
97+
Redis = GetConnectionById(clusterConfig, defaultEndpointId);
2298
}
2399

24100
public void Dispose()
@@ -28,39 +104,18 @@ public void Dispose()
28104

29105
public ConnectionMultiplexer Redis { get; }
30106

31-
public ConnectionMultiplexer CustomRedis(ConfigurationOptions configurationOptions, out bool isOssCluster)
107+
public ConnectionMultiplexer GetConnectionById(ConfigurationOptions configurationOptions, string id)
32108
{
33-
return Connect(configurationOptions, out isOssCluster);
34-
}
35-
36-
private ConnectionMultiplexer Connect(ConfigurationOptions configurationOptions, out bool isOssCluster)
37-
{
38-
// Redis Cluster
39-
if (redisCluster != null && numRedisClusterNodesEnv != null)
109+
if (!redisEndpoints.ContainsKey(id))
40110
{
41-
// Split to host and port
42-
string[] parts = redisCluster!.Split(':');
43-
string host = parts[0];
44-
int startPort = int.Parse(parts[1]);
45-
46-
var endpoints = new EndPointCollection(); // TODO: check if needed
47-
48-
configurationOptions.EndPoints.Clear();
49-
int numRedisClusterNodes = int.Parse(numRedisClusterNodesEnv!);
50-
for (int i = 0; i < numRedisClusterNodes; i++)
51-
{
52-
configurationOptions.EndPoints.Add(host, startPort + i);
53-
}
54-
55-
isOssCluster = true;
56-
return ConnectionMultiplexer.Connect(configurationOptions);
111+
throw new Exception($"The connection with id '{id}' is not configured.");
57112
}
58113

59-
// Redis Standalone
60-
configurationOptions.EndPoints.Clear();
61-
configurationOptions.EndPoints.Add($"{redisStandalone}");
114+
return redisEndpoints[id].CreateConnection(configurationOptions);
115+
}
62116

63-
isOssCluster = false;
64-
return ConnectionMultiplexer.Connect(configurationOptions);
117+
public bool IsTargetConnectionExist(string id)
118+
{
119+
return redisEndpoints.ContainsKey(id);
65120
}
66121
}

0 commit comments

Comments
 (0)