Skip to content
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

Migrate Redis tests to Testcontainers #2345

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .github/workflows/healthchecks_redis_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ on:
jobs:
build:
runs-on: ubuntu-latest
services:
redis:
image: redis:alpine
ports:
- 6379:6379
steps:
- uses: actions/checkout@v3
- name: Setup .NET
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
<PackageVersion Include="Testcontainers.Redis" Version="$(TestcontainersVersion)" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions build/versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@
<HealthCheckKeyVaultSecrets>9.0.0</HealthCheckKeyVaultSecrets>
</PropertyGroup>

<PropertyGroup>
<TestcontainersVersion>4.1.0</TestcontainersVersion>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

namespace HealthChecks.Redis.Tests.Functional;

public class redis_healthcheck_should
public class redis_healthcheck_should(RedisContainerFixture redisContainerFixture) : IClassFixture<RedisContainerFixture>
{
[Fact]
public async Task be_healthy_if_redis_is_available_with_connection_string()
{
var connectionString = "localhost:6379,allowAdmin=true";
var connectionString = $"{redisContainerFixture.GetConnectionString()},allowAdmin=true";

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand All @@ -36,7 +36,7 @@ public async Task be_healthy_if_redis_is_available_with_connection_string()
[Fact]
public async Task be_healthy_if_multiple_redis_are_available_with_connection_string()
{
var connectionString = "localhost:6379,allowAdmin=true";
var connectionString = $"{redisContainerFixture.GetConnectionString()},allowAdmin=true";

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand All @@ -63,8 +63,10 @@ public async Task be_healthy_if_multiple_redis_are_available_with_connection_str
[Fact]
public async Task be_healthy_if_redis_is_available_with_connection_multiplexer()
{
var connectionString = $"{redisContainerFixture.GetConnectionString()},allowAdmin=true";

var connectionMultiplexer = await ConnectionMultiplexer
.ConnectAsync("localhost:6379,allowAdmin=true");
.ConnectAsync(connectionString);

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand All @@ -90,8 +92,10 @@ public async Task be_healthy_if_redis_is_available_with_connection_multiplexer()
[Fact]
public async Task be_healthy_if_multiple_redis_are_available_with_connection_multiplexer()
{
var connectionString = $"{redisContainerFixture.GetConnectionString()},allowAdmin=true";

var connectionMultiplexer = await ConnectionMultiplexer
.ConnectAsync("localhost:6379,allowAdmin=true");
.ConnectAsync(connectionString);

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand Down
4 changes: 4 additions & 0 deletions test/HealthChecks.Redis.Tests/HealthChecks.Redis.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="Testcontainers.Redis" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\HealthChecks.Redis\HealthChecks.Redis.csproj" />
</ItemGroup>
Expand Down
35 changes: 35 additions & 0 deletions test/HealthChecks.Redis.Tests/RedisContainerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Testcontainers.Redis;

namespace HealthChecks.Redis.Tests;

public sealed class RedisContainerFixture : IAsyncLifetime
{
public const string Registry = "docker.io";

public const string Image = "library/redis";

public const string Tag = "7.4";

public RedisContainer? Container { get; private set; }

public string GetConnectionString() => Container?.GetConnectionString() ??
throw new InvalidOperationException("The test container was not initialized.");

public async Task InitializeAsync() => Container = await CreateContainerAsync();

public async Task DisposeAsync()
{
if (Container is not null)
await Container.DisposeAsync();
}

public static async Task<RedisContainer> CreateContainerAsync()
{
var container = new RedisBuilder()
.WithImage($"{Registry}/{Image}:{Tag}")
.Build();
await container.StartAsync();

return container;
}
}
Loading