2026-07-21 18:48:39 +00:00
|
|
|
using Testcontainers.PostgreSql;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.TestCommon;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// xUnit collection fixture that manages ONE PostgreSQL Testcontainer
|
2026-07-21 19:05:54 +00:00
|
|
|
/// shared across ALL test projects via a static container with ref-counting.
|
|
|
|
|
/// Only the first fixture to initialize starts the container; only the last
|
|
|
|
|
/// to dispose stops it.
|
2026-07-21 19:59:27 +00:00
|
|
|
///
|
|
|
|
|
/// In CI mode (DATABASE_URL env var), skips Testcontainers entirely and
|
|
|
|
|
/// uses the CI-provided PostgreSQL service instead — avoids Docker disk
|
|
|
|
|
/// space issues and container startup time on CI runners.
|
2026-07-21 18:48:39 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public sealed class PostgreSqlContainerFixture : IAsyncLifetime
|
|
|
|
|
{
|
2026-07-21 19:59:27 +00:00
|
|
|
/// <summary>Null in CI mode (DATABASE_URL env var); non-null in local dev.</summary>
|
|
|
|
|
private static readonly PostgreSqlContainer? Container;
|
|
|
|
|
/// <summary>Set only in CI mode. Null in local dev — use Container.GetConnectionString().</summary>
|
|
|
|
|
private static readonly string? CiConnectionString;
|
2026-07-21 18:48:39 +00:00
|
|
|
|
2026-07-21 19:05:54 +00:00
|
|
|
private static readonly object RefLock = new();
|
|
|
|
|
private static int _refCount;
|
|
|
|
|
|
2026-07-21 19:59:27 +00:00
|
|
|
static PostgreSqlContainerFixture()
|
|
|
|
|
{
|
|
|
|
|
var envUrl = Environment.GetEnvironmentVariable("DATABASE_URL");
|
|
|
|
|
if (!string.IsNullOrEmpty(envUrl))
|
|
|
|
|
{
|
|
|
|
|
// CI mode — use the CI-provided PostgreSQL service
|
|
|
|
|
CiConnectionString = envUrl;
|
|
|
|
|
Container = null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Local dev — build a Testcontainer (NOT started yet — StartAsync in InitializeAsync)
|
|
|
|
|
Container = new PostgreSqlBuilder("postgres:17-alpine")
|
|
|
|
|
.WithDatabase("heimdall_test")
|
|
|
|
|
.WithUsername("heimdall")
|
|
|
|
|
.WithPassword("heimdall_test_pwd")
|
|
|
|
|
.WithCleanUp(true)
|
|
|
|
|
.Build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 09:09:14 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates whether the PostgreSQL container started successfully.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsAvailable { get; private set; } = true;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reason the container is unavailable (for test skip messages).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string? SkipReason { get; private set; }
|
|
|
|
|
|
2026-07-21 19:59:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Connection string for the PostgreSQL instance.
|
|
|
|
|
/// In CI mode returns the DATABASE_URL env var directly.
|
|
|
|
|
/// In local dev returns the Testcontainer connection string.
|
|
|
|
|
/// WARNING: In local dev, the container must be started (via InitializeAsync)
|
|
|
|
|
/// before accessing this property.
|
|
|
|
|
/// </summary>
|
2026-07-21 19:05:54 +00:00
|
|
|
#pragma warning disable CA1822 // Member does not access instance data
|
2026-07-21 19:59:27 +00:00
|
|
|
public string ConnectionString => CiConnectionString ?? Container!.GetConnectionString();
|
2026-07-21 19:05:54 +00:00
|
|
|
#pragma warning restore CA1822
|
2026-07-21 18:48:39 +00:00
|
|
|
|
|
|
|
|
public async Task InitializeAsync()
|
|
|
|
|
{
|
2026-07-21 19:05:54 +00:00
|
|
|
bool shouldStart;
|
|
|
|
|
lock (RefLock)
|
|
|
|
|
{
|
|
|
|
|
_refCount++;
|
|
|
|
|
shouldStart = _refCount == 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 19:59:27 +00:00
|
|
|
if (shouldStart && Container is not null)
|
2026-07-27 09:09:14 +00:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
|
|
|
|
|
await Container.StartAsync(cts.Token);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) when (ex is InvalidOperationException or TimeoutException or TaskCanceledException)
|
|
|
|
|
{
|
|
|
|
|
IsAvailable = false;
|
|
|
|
|
SkipReason = $"PostgreSQL TestContainers unavailable (Docker not running?): {ex.Message}";
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
IsAvailable = false;
|
|
|
|
|
SkipReason = $"PostgreSQL TestContainers failed: {ex.Message}";
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-21 18:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task DisposeAsync()
|
|
|
|
|
{
|
2026-07-21 19:05:54 +00:00
|
|
|
bool shouldStop;
|
|
|
|
|
lock (RefLock)
|
|
|
|
|
{
|
|
|
|
|
_refCount--;
|
|
|
|
|
shouldStop = _refCount == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 09:09:14 +00:00
|
|
|
if (shouldStop && Container is not null && IsAvailable)
|
2026-07-21 19:05:54 +00:00
|
|
|
await Container.DisposeAsync();
|
2026-07-21 18:48:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Collection definitions are in each test project assembly
|
|
|
|
|
// (xUnit requires [CollectionDefinition] in the same assembly as the test).
|