forked from dotnet/aspire
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Aspire.Hosting.Valkey.Tests project
Contributes to dotnet#3185 Contributes to dotnet#4294
- Loading branch information
Showing
13 changed files
with
230 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
tests/Aspire.Hosting.Valkey.Tests/Aspire.Hosting.Valkey.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(NetCurrent)</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Aspire.Hosting.Valkey\Aspire.Hosting.Valkey.csproj" /> | ||
<ProjectReference Include="..\..\src\Components\Aspire.StackExchange.Redis\Aspire.StackExchange.Redis.csproj" /> | ||
<ProjectReference Include="..\Aspire.Hosting.Tests\Aspire.Hosting.Tests.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="$(RepoRoot)src\Aspire.Hosting.Valkey\ValkeyContainerImageTags.cs" /> | ||
<Compile Include="$(SharedDir)VolumeNameGenerator.cs" Link="Utils\VolumeNameGenerator.cs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
197 changes: 197 additions & 0 deletions
197
tests/Aspire.Hosting.Valkey.Tests/ValkeyFunctionalTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
// 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.Valkey.Tests; | ||
|
||
public class ValkeyFunctionalTests(ITestOutputHelper testOutputHelper) | ||
{ | ||
[Fact] | ||
[RequiresDocker] | ||
public async Task VerifyValkeyResource() | ||
{ | ||
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)); | ||
|
||
var builder = CreateDistributedApplicationBuilder(); | ||
|
||
var valkey = builder.AddValkey("valkey"); | ||
|
||
using var app = builder.Build(); | ||
|
||
await app.StartAsync(); | ||
|
||
var hb = Host.CreateApplicationBuilder(); | ||
|
||
hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> | ||
{ | ||
[$"ConnectionStrings:{valkey.Resource.Name}"] = await valkey.Resource.ConnectionStringExpression.GetValueAsync(default) | ||
}); | ||
|
||
hb.AddRedisClient(valkey.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); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
[RequiresDocker] | ||
public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume) | ||
{ | ||
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)); | ||
string? volumeName = null; | ||
string? bindMountPath = null; | ||
|
||
try | ||
{ | ||
var builder1 = CreateDistributedApplicationBuilder(); | ||
var valkey1 = builder1.AddValkey("valkey"); | ||
|
||
if (useVolume) | ||
{ | ||
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails | ||
volumeName = VolumeNameGenerator.CreateVolumeName(valkey1, nameof(WithDataShouldPersistStateBetweenUsages)); | ||
|
||
// if the volume already exists (because of a crashing previous run), try to delete it | ||
DockerUtils.AttemptDeleteDockerVolume(volumeName); | ||
valkey1.WithDataVolume(volumeName); | ||
} | ||
else | ||
{ | ||
bindMountPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
valkey1.WithDataBindMount(bindMountPath); | ||
} | ||
|
||
using (var app = builder1.Build()) | ||
{ | ||
await app.StartAsync(); | ||
try | ||
{ | ||
var hb = Host.CreateApplicationBuilder(); | ||
|
||
// BGSAVE is only available in admin mode, enable it for this instance | ||
hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> | ||
{ | ||
[$"ConnectionStrings:{valkey1.Resource.Name}"] = $"{await valkey1.Resource.ConnectionStringExpression.GetValueAsync(default)},allowAdmin=true" | ||
}); | ||
|
||
hb.AddRedisClient(valkey1.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); | ||
} | ||
} | ||
finally | ||
{ | ||
// Stops the container, or the Volume/mount would still be in use | ||
await app.StopAsync(); | ||
} | ||
} | ||
|
||
var builder2 = CreateDistributedApplicationBuilder(); | ||
var valkey2 = builder2.AddValkey("valkey"); | ||
|
||
if (useVolume) | ||
{ | ||
valkey2.WithDataVolume(volumeName); | ||
} | ||
else | ||
{ | ||
valkey2.WithDataBindMount(bindMountPath!); | ||
} | ||
|
||
using (var app = builder2.Build()) | ||
{ | ||
await app.StartAsync(); | ||
try | ||
{ | ||
var hb = Host.CreateApplicationBuilder(); | ||
|
||
hb.Configuration.AddInMemoryCollection(new Dictionary<string, string?> | ||
{ | ||
[$"ConnectionStrings:{valkey2.Resource.Name}"] = await valkey2.Resource.ConnectionStringExpression.GetValueAsync(default) | ||
}); | ||
|
||
hb.AddRedisClient(valkey2.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); | ||
} | ||
} | ||
finally | ||
{ | ||
// Stops the container, or the Volume/mount would still be in use | ||
await app.StopAsync(); | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
if (volumeName is not null) | ||
{ | ||
DockerUtils.AttemptDeleteDockerVolume(volumeName); | ||
} | ||
|
||
if (bindMountPath is not null) | ||
{ | ||
try | ||
{ | ||
File.Delete(bindMountPath); | ||
} | ||
catch | ||
{ | ||
// Don't fail test if we can't clean the temporary folder | ||
} | ||
} | ||
} | ||
} | ||
|
||
private TestDistributedApplicationBuilder CreateDistributedApplicationBuilder() | ||
{ | ||
var builder = TestDistributedApplicationBuilder.CreateWithTestContainerRegistry(); | ||
builder.Services.AddXunitLogging(testOutputHelper); | ||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.