From 2b47b154cb48f1cdf7a7708b3708e8187dc09502 Mon Sep 17 00:00:00 2001 From: Alireza Baloochi Date: Wed, 18 Dec 2024 19:20:37 +0330 Subject: [PATCH] Migrate HealthChecks.Npgsql tests to Testcontainers (#2349) --- .github/workflows/healthchecks_npgsql_ci.yml | 49 ++----------------- Directory.Packages.props | 1 + .../Functional/NpgsqlHealthCheckTests.cs | 12 +++-- .../HealthChecks.Npgsql.Tests.csproj | 1 + .../PostgreSQLContainerFixture.cs | 35 +++++++++++++ 5 files changed, 49 insertions(+), 49 deletions(-) create mode 100644 test/HealthChecks.Npgsql.Tests/PostgreSQLContainerFixture.cs diff --git a/.github/workflows/healthchecks_npgsql_ci.yml b/.github/workflows/healthchecks_npgsql_ci.yml index c2b8ffb2f2..11bd649683 100644 --- a/.github/workflows/healthchecks_npgsql_ci.yml +++ b/.github/workflows/healthchecks_npgsql_ci.yml @@ -29,47 +29,8 @@ on: jobs: build: - runs-on: ubuntu-latest - services: - npgsql: - image: postgres - ports: - - 8010:5432 - env: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: Password12! - steps: - - uses: actions/checkout@v3 - - name: Setup .NET - uses: actions/setup-dotnet@v4 - with: - dotnet-version: | - 8.0.x - 9.0.x - - name: Restore - run: | - dotnet restore ./src/HealthChecks.NpgSql/HealthChecks.NpgSql.csproj && - dotnet restore ./test/HealthChecks.Npgsql.Tests/HealthChecks.Npgsql.Tests.csproj - - name: Check formatting - run: | - dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.NpgSql/HealthChecks.NpgSql.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) && - dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.Npgsql.Tests/HealthChecks.Npgsql.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) - - name: Build - run: | - dotnet build --no-restore ./src/HealthChecks.NpgSql/HealthChecks.NpgSql.csproj && - dotnet build --no-restore ./test/HealthChecks.Npgsql.Tests/HealthChecks.Npgsql.Tests.csproj - - name: Test - run: > - dotnet test - ./test/HealthChecks.Npgsql.Tests/HealthChecks.Npgsql.Tests.csproj - --no-restore - --no-build - --collect "XPlat Code Coverage" - --results-directory .coverage - -- - DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover - - name: Upload Coverage - uses: codecov/codecov-action@v5 - with: - flags: Npgsql - directory: .coverage + uses: ./.github/workflows/reusable_ci_workflow.yml + with: + PROJECT_PATH: ./src/HealthChecks.NpgSql/HealthChecks.NpgSql.csproj + TEST_PROJECT_PATH: ./test/HealthChecks.Npgsql.Tests/HealthChecks.Npgsql.Tests.csproj + CODECOV_FLAGS: Npgsql diff --git a/Directory.Packages.props b/Directory.Packages.props index ce5cab912e..6732ccd3d2 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -102,6 +102,7 @@ + diff --git a/test/HealthChecks.Npgsql.Tests/Functional/NpgsqlHealthCheckTests.cs b/test/HealthChecks.Npgsql.Tests/Functional/NpgsqlHealthCheckTests.cs index 93f5f9238a..0b624ce5df 100644 --- a/test/HealthChecks.Npgsql.Tests/Functional/NpgsqlHealthCheckTests.cs +++ b/test/HealthChecks.Npgsql.Tests/Functional/NpgsqlHealthCheckTests.cs @@ -9,12 +9,12 @@ public class DBConfigSetting public string ConnectionString { get; set; } = null!; } -public class npgsql_healthcheck_should +public class npgsql_healthcheck_should(PostgreSQLContainerFixture postgreSQLContainerFixture) : IClassFixture { [Fact] public async Task be_healthy_if_npgsql_is_available() { - var connectionString = "Server=127.0.0.1;Port=8010;User ID=postgres;Password=Password12!;database=postgres"; + var connectionString = postgreSQLContainerFixture.GetConnectionString(); var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => @@ -40,7 +40,7 @@ public async Task be_healthy_if_npgsql_is_available() [Fact] public async Task be_unhealthy_if_sql_query_is_not_valid() { - var connectionString = "Server=127.0.0.1;Port=8010;User ID=postgres;Password=Password12!;database=postgres"; + var connectionString = postgreSQLContainerFixture.GetConnectionString(); var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => @@ -90,12 +90,14 @@ public async Task be_unhealthy_if_npgsql_is_not_available() [Fact] public async Task be_healthy_if_npgsql_is_available_by_iServiceProvider_registered() { + var connectionString = postgreSQLContainerFixture.GetConnectionString(); + var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => { services.AddSingleton(new DBConfigSetting { - ConnectionString = "Server=127.0.0.1;Port=8010;User ID=postgres;Password=Password12!;database=postgres" + ConnectionString = connectionString }); services.AddHealthChecks() @@ -148,7 +150,7 @@ public async Task be_unhealthy_if_npgsql_is_not_available_registered() [Fact] public async Task unhealthy_check_log_detailed_messages() { - var connectionString = "Server=127.0.0.1;Port=8010;User ID=postgres;Password=Password12!;database=postgres"; + var connectionString = postgreSQLContainerFixture.GetConnectionString(); var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => diff --git a/test/HealthChecks.Npgsql.Tests/HealthChecks.Npgsql.Tests.csproj b/test/HealthChecks.Npgsql.Tests/HealthChecks.Npgsql.Tests.csproj index cd6620228f..fd7560cc54 100644 --- a/test/HealthChecks.Npgsql.Tests/HealthChecks.Npgsql.Tests.csproj +++ b/test/HealthChecks.Npgsql.Tests/HealthChecks.Npgsql.Tests.csproj @@ -2,6 +2,7 @@ + diff --git a/test/HealthChecks.Npgsql.Tests/PostgreSQLContainerFixture.cs b/test/HealthChecks.Npgsql.Tests/PostgreSQLContainerFixture.cs new file mode 100644 index 0000000000..04ec080837 --- /dev/null +++ b/test/HealthChecks.Npgsql.Tests/PostgreSQLContainerFixture.cs @@ -0,0 +1,35 @@ +using Testcontainers.PostgreSql; + +namespace HealthChecks.Npgsql.Tests; + +public sealed class PostgreSQLContainerFixture : IAsyncLifetime +{ + public const string Registry = "docker.io"; + + public const string Image = "library/postgres"; + + public const string Tag = "17.0"; + + public PostgreSqlContainer? 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 PostgreSqlBuilder() + .WithImage($"{Registry}/{Image}:{Tag}") + .Build(); + await container.StartAsync(); + + return container; + } +}