Skip to content

Commit

Permalink
Migrate HealthChecks.Npgsql tests to Testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
Alirexaa committed Dec 17, 2024
1 parent f54852f commit 9ad3389
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/healthchecks_npgsql_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ 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
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.PostgreSql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.Redis" Version="$(TestcontainersVersion)" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.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 9ad3389

Please sign in to comment.