From f54852f39646fb5e1615c42278a051fef9181ec3 Mon Sep 17 00:00:00 2001 From: Alireza Baloochi Date: Tue, 17 Dec 2024 15:01:58 +0330 Subject: [PATCH] Migrate Redis tests to Testcontainers (#2345) * Migrate Redis tests to Testcontainers * Address PR feedback --- .github/workflows/healthchecks_redis_ci.yml | 5 --- Directory.Packages.props | 1 + build/versions.props | 4 +++ .../Functional/RedisHealthCheckTests.cs | 14 +++++--- .../HealthChecks.Redis.Tests.csproj | 4 +++ .../RedisContainerFixture.cs | 35 +++++++++++++++++++ 6 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 test/HealthChecks.Redis.Tests/RedisContainerFixture.cs diff --git a/.github/workflows/healthchecks_redis_ci.yml b/.github/workflows/healthchecks_redis_ci.yml index 2c1a230907..9ddf3ae16b 100644 --- a/.github/workflows/healthchecks_redis_ci.yml +++ b/.github/workflows/healthchecks_redis_ci.yml @@ -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 diff --git a/Directory.Packages.props b/Directory.Packages.props index 5af2c85ee4..7653922744 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -102,6 +102,7 @@ + diff --git a/build/versions.props b/build/versions.props index 0fcbb066bf..a3b3e22049 100644 --- a/build/versions.props +++ b/build/versions.props @@ -75,4 +75,8 @@ 9.0.0 + + 4.1.0 + + diff --git a/test/HealthChecks.Redis.Tests/Functional/RedisHealthCheckTests.cs b/test/HealthChecks.Redis.Tests/Functional/RedisHealthCheckTests.cs index 3e6b19a0af..ec9dd8a9fe 100644 --- a/test/HealthChecks.Redis.Tests/Functional/RedisHealthCheckTests.cs +++ b/test/HealthChecks.Redis.Tests/Functional/RedisHealthCheckTests.cs @@ -5,12 +5,12 @@ namespace HealthChecks.Redis.Tests.Functional; -public class redis_healthcheck_should +public class redis_healthcheck_should(RedisContainerFixture redisContainerFixture) : IClassFixture { [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 => @@ -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 => @@ -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 => @@ -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 => diff --git a/test/HealthChecks.Redis.Tests/HealthChecks.Redis.Tests.csproj b/test/HealthChecks.Redis.Tests/HealthChecks.Redis.Tests.csproj index ed1690452b..e530a7a1a1 100644 --- a/test/HealthChecks.Redis.Tests/HealthChecks.Redis.Tests.csproj +++ b/test/HealthChecks.Redis.Tests/HealthChecks.Redis.Tests.csproj @@ -1,5 +1,9 @@ + + + + diff --git a/test/HealthChecks.Redis.Tests/RedisContainerFixture.cs b/test/HealthChecks.Redis.Tests/RedisContainerFixture.cs new file mode 100644 index 0000000000..4a20e79079 --- /dev/null +++ b/test/HealthChecks.Redis.Tests/RedisContainerFixture.cs @@ -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 CreateContainerAsync() + { + var container = new RedisBuilder() + .WithImage($"{Registry}/{Image}:{Tag}") + .Build(); + await container.StartAsync(); + + return container; + } +}