fix(tests): restore DATABASE_URL fallback for CI in shared PostgreSqlContainerFixture
The shared fixture (Heimdall.TestCommon) was refactored from per-project fixtures but lost the DATABASE_URL env var check. Without it, the fixture always starts a Testcontainers PostgreSQL container, which fails in CI when Docker disk space is exhausted (initdb: No space left on device). Now checks DATABASE_URL first — if set (CI mode), skips Testcontainers and uses the CI-provided PostgreSQL service. Falls back to Testcontainers for local development.
This commit is contained in:
1 parent
2d0202b706
commit
0ef4642f6d
1 file changed
+39
-9
@@ -8,21 +8,51 @@ namespace Heimdall.TestCommon;
|
||||
/// 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.
|
||||
///
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public sealed class PostgreSqlContainerFixture : IAsyncLifetime
|
||||
{
|
||||
private static readonly PostgreSqlContainer Container = new PostgreSqlBuilder("postgres:17-alpine")
|
||||
.WithDatabase("heimdall_test")
|
||||
.WithUsername("heimdall")
|
||||
.WithPassword("heimdall_test_pwd")
|
||||
.WithCleanUp(true)
|
||||
.Build();
|
||||
/// <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;
|
||||
|
||||
private static readonly object RefLock = new();
|
||||
private static int _refCount;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <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>
|
||||
#pragma warning disable CA1822 // Member does not access instance data
|
||||
public string ConnectionString => Container.GetConnectionString();
|
||||
public string ConnectionString => CiConnectionString ?? Container!.GetConnectionString();
|
||||
#pragma warning restore CA1822
|
||||
|
||||
public async Task InitializeAsync()
|
||||
@@ -34,7 +64,7 @@ public sealed class PostgreSqlContainerFixture : IAsyncLifetime
|
||||
shouldStart = _refCount == 1;
|
||||
}
|
||||
|
||||
if (shouldStart)
|
||||
if (shouldStart && Container is not null)
|
||||
await Container.StartAsync();
|
||||
}
|
||||
|
||||
@@ -47,7 +77,7 @@ public sealed class PostgreSqlContainerFixture : IAsyncLifetime
|
||||
shouldStop = _refCount == 0;
|
||||
}
|
||||
|
||||
if (shouldStop)
|
||||
if (shouldStop && Container is not null)
|
||||
await Container.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user