-
Notifications
You must be signed in to change notification settings - Fork 462
/
RedisFunctionalTests.cs
304 lines (212 loc) · 9.38 KB
/
RedisFunctionalTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Aspire.Components.Common.Tests;
using Aspire.Hosting.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using Xunit;
using Xunit.Abstractions;
namespace Aspire.Hosting.Redis.Tests;
public class RedisFunctionalTests(ITestOutputHelper testOutputHelper)
{
[Fact]
[RequiresDocker]
public async Task VerifyRedisResource()
{
var builder = CreateDistributedApplicationBuilder();
var redis = builder.AddRedis("redis");
using var app = builder.Build();
await app.StartAsync();
var hb = Host.CreateApplicationBuilder();
hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
[$"ConnectionStrings:{redis.Resource.Name}"] = await redis.Resource.GetConnectionStringAsync()
});
hb.AddRedisClient(redis.Resource.Name);
using var host = hb.Build();
await host.StartAsync();
var redisClient = host.Services.GetRequiredService<IConnectionMultiplexer>();
var db = redisClient.GetDatabase();
await db.StringSetAsync("key", "value");
var value = await db.StringGetAsync("key");
Assert.Equal("value", value);
}
[Fact]
[RequiresDocker]
public async Task WithDataVolumeShouldPersistStateBetweenUsages()
{
// Use a volume to do a snapshot save
var builder1 = CreateDistributedApplicationBuilder();
var redis1 = builder1.AddRedis("redis");
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
var volumeName = VolumeNameGenerator.CreateVolumeName(redis1, nameof(WithDataVolumeShouldPersistStateBetweenUsages));
redis1.WithDataVolume(volumeName);
using (var app = builder1.Build())
{
await app.StartAsync();
var hb = Host.CreateApplicationBuilder();
// BGSAVE is only available in admin mode, enable it for this instance
hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
[$"ConnectionStrings:{redis1.Resource.Name}"] = $"{await redis1.Resource.GetConnectionStringAsync()},allowAdmin=true"
});
hb.AddRedisClient(redis1.Resource.Name);
using (var host = hb.Build())
{
await host.StartAsync();
var redisClient = host.Services.GetRequiredService<IConnectionMultiplexer>();
var db = redisClient.GetDatabase();
await db.StringSetAsync("key", "value");
// Force Redis to save the keys (snapshotting)
// c.f. https://redis.io/docs/latest/operate/oss_and_stack/management/persistence/
await redisClient.GetServers().First().SaveAsync(SaveType.BackgroundSave);
}
// Stops the container, or the Volume would still be in use
await app.StopAsync();
}
var builder2 = CreateDistributedApplicationBuilder();
var redis2 = builder2.AddRedis("redis").WithDataVolume(volumeName);
using (var app = builder2.Build())
{
await app.StartAsync();
var hb = Host.CreateApplicationBuilder();
hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
[$"ConnectionStrings:{redis2.Resource.Name}"] = await redis2.Resource.GetConnectionStringAsync()
});
hb.AddRedisClient(redis2.Resource.Name);
using (var host = hb.Build())
{
await host.StartAsync();
var redisClient = host.Services.GetRequiredService<IConnectionMultiplexer>();
var db = redisClient.GetDatabase();
var value = await db.StringGetAsync("key");
Assert.Equal("value", value);
}
// Stops the container, or the Volume would still be in use
await app.StopAsync();
}
DockerUtils.AttemptDeleteDockerVolume(volumeName);
}
[Fact]
[RequiresDocker]
public async Task WithDataBindMountShouldPersistStateBetweenUsages()
{
var bindMountPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
if (!Directory.Exists(bindMountPath))
{
Directory.CreateDirectory(bindMountPath);
}
// Use a bind mount to do a snapshot save
var builder1 = CreateDistributedApplicationBuilder();
var redis1 = builder1.AddRedis("redis").WithDataBindMount(bindMountPath);
using (var app = builder1.Build())
{
await app.StartAsync();
var hb = Host.CreateApplicationBuilder();
// BGSAVE is only available in admin mode, enable it for this instance
hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
[$"ConnectionStrings:{redis1.Resource.Name}"] = $"{await redis1.Resource.GetConnectionStringAsync()},allowAdmin=true"
});
hb.AddRedisClient(redis1.Resource.Name);
using (var host = hb.Build())
{
await host.StartAsync();
var redisClient = host.Services.GetRequiredService<IConnectionMultiplexer>();
var db = redisClient.GetDatabase();
await db.StringSetAsync("key", "value");
// Force Redis to save the keys (snapshotting)
// c.f. https://redis.io/docs/latest/operate/oss_and_stack/management/persistence/
await redisClient.GetServers().First().SaveAsync(SaveType.BackgroundSave);
}
await app.StopAsync();
}
var builder2 = CreateDistributedApplicationBuilder();
var redis2 = builder2.AddRedis("redis").WithDataBindMount(bindMountPath);
using (var app = builder2.Build())
{
await app.StartAsync();
var hb = Host.CreateApplicationBuilder();
hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
[$"ConnectionStrings:{redis2.Resource.Name}"] = await redis2.Resource.GetConnectionStringAsync()
});
hb.AddRedisClient(redis2.Resource.Name);
using (var host = hb.Build())
{
await host.StartAsync();
var redisClient = host.Services.GetRequiredService<IConnectionMultiplexer>();
var db = redisClient.GetDatabase();
var value = await db.StringGetAsync("key");
Assert.Equal("value", value);
}
await app.StopAsync();
}
try
{
Directory.Delete(bindMountPath, recursive: true);
}
catch
{
// Don't fail test if we can't clean the temporary folder
}
}
[Fact]
[RequiresDocker]
public async Task PersistenceIsDisabledByDefault()
{
// Checks that without enabling Redis Persistence the tests fail
var builder1 = CreateDistributedApplicationBuilder();
var redis1 = builder1.AddRedis("redis");
using (var app = builder1.Build())
{
await app.StartAsync();
var hb = Host.CreateApplicationBuilder();
// BGSAVE is only available in admin mode, enable it for this instance
hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
[$"ConnectionStrings:{redis1.Resource.Name}"] = $"{await redis1.Resource.GetConnectionStringAsync()},allowAdmin=true"
});
hb.AddRedisClient(redis1.Resource.Name);
using (var host = hb.Build())
{
await host.StartAsync();
var redisClient = host.Services.GetRequiredService<IConnectionMultiplexer>();
var db = redisClient.GetDatabase();
await db.StringSetAsync("key", "value");
}
await app.StopAsync();
}
var builder2 = CreateDistributedApplicationBuilder();
var redis2 = builder2.AddRedis("redis");
using (var app = builder2.Build())
{
await app.StartAsync();
var hb = Host.CreateApplicationBuilder();
hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
{
[$"ConnectionStrings:{redis2.Resource.Name}"] = await redis2.Resource.GetConnectionStringAsync()
});
hb.AddRedisClient(redis2.Resource.Name);
using (var host = hb.Build())
{
await host.StartAsync();
var redisClient = host.Services.GetRequiredService<IConnectionMultiplexer>();
var db = redisClient.GetDatabase();
var value = await db.StringGetAsync("key");
Assert.True(value.IsNull);
}
await app.StopAsync();
}
}
private TestDistributedApplicationBuilder CreateDistributedApplicationBuilder()
{
var builder = TestDistributedApplicationBuilder.CreateWithTestContainerRegistry();
builder.Services.AddXunitLogging(testOutputHelper);
return builder;
}
}