Skip to content

Commit

Permalink
Migrate HealthChecks.Npgsql tests to Testcontainers (#2349)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alirexaa authored Dec 18, 2024
1 parent 69fa668 commit 2b47b15
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 49 deletions.
49 changes: 5 additions & 44 deletions .github/workflows/healthchecks_npgsql_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.PostgreSql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.MsSql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.Redis" Version="$(TestcontainersVersion)" />
<PackageVersion Include="xunit" Version="2.9.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostgreSQLContainerFixture>
{
[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 =>
Expand All @@ -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 =>
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<ItemGroup>
<PackageReference Include="Npgsql.DependencyInjection" />
<PackageReference Include="Testcontainers.PostgreSql" />
</ItemGroup>

<ItemGroup>
Expand Down
35 changes: 35 additions & 0 deletions test/HealthChecks.Npgsql.Tests/PostgreSQLContainerFixture.cs
Original file line number Diff line number Diff line change
@@ -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<PostgreSqlContainer> CreateContainerAsync()
{
var container = new PostgreSqlBuilder()
.WithImage($"{Registry}/{Image}:{Tag}")
.Build();
await container.StartAsync();

return container;
}
}

0 comments on commit 2b47b15

Please sign in to comment.