-
Notifications
You must be signed in to change notification settings - Fork 739
Extract Aspire.Hosting.Valkey.Tests project #4930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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)); | ||
eerhardt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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); | ||
eerhardt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.