From b167c2afcf7598c5cb499c693b6d8e3e262376d3 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Tue, 21 Jul 2026 18:48:39 +0000 Subject: [PATCH] fix: share one PostgreSQL Testcontainer across all test projects Create Heimdall.TestCommon project with a single PostgreSqlContainerFixture shared by both Infrastructure and Integration tests. This prevents Docker resource conflicts from starting two PostgreSQL containers and ensures both test suites connect to the same database instance. - Move shared fixture to tests/Heimdall.TestCommon - Remove duplicate fixtures from each test project - Keep test isolation (truncation) and FK fix from previous commit - Revert DATABASE_URL env var check (Testcontainers works in CI) --- .../CameraAssignmentServiceTests.cs | 2 + .../CameraEventServiceTests.cs | 2 + .../Heimdall.Infrastructure.Tests.csproj | 2 +- .../PostgreSqlContainerFixture.cs | 56 ----------------- .../PostgresInfraCollection.cs | 12 ++++ .../Controllers/GlobalUsings.cs | 1 + .../Controllers/PostgresCollection.cs | 12 ++++ .../Controllers/PostgresTestBase.cs | 3 +- .../Factories/PostgreSqlContainerFixture.cs | 62 ------------------- .../Heimdall.IntegrationTests.csproj | 3 +- .../Heimdall.TestCommon.csproj | 17 +++++ .../PostgreSqlContainerFixture.cs | 33 ++++++++++ 12 files changed, 83 insertions(+), 122 deletions(-) delete mode 100644 tests/Heimdall.Infrastructure.Tests/PostgreSqlContainerFixture.cs create mode 100644 tests/Heimdall.Infrastructure.Tests/PostgresInfraCollection.cs create mode 100644 tests/Heimdall.IntegrationTests/Controllers/GlobalUsings.cs create mode 100644 tests/Heimdall.IntegrationTests/Controllers/PostgresCollection.cs delete mode 100644 tests/Heimdall.IntegrationTests/Factories/PostgreSqlContainerFixture.cs create mode 100644 tests/Heimdall.TestCommon/Heimdall.TestCommon.csproj create mode 100644 tests/Heimdall.TestCommon/PostgreSqlContainerFixture.cs diff --git a/tests/Heimdall.Infrastructure.Tests/CameraAssignmentServiceTests.cs b/tests/Heimdall.Infrastructure.Tests/CameraAssignmentServiceTests.cs index fb2dc77..f85178c 100644 --- a/tests/Heimdall.Infrastructure.Tests/CameraAssignmentServiceTests.cs +++ b/tests/Heimdall.Infrastructure.Tests/CameraAssignmentServiceTests.cs @@ -9,6 +9,8 @@ using Microsoft.Extensions.Logging; using NSubstitute; using Shouldly; +using Heimdall.TestCommon; + namespace Heimdall.Infrastructure.Tests; [Collection("PostgresInfra")] diff --git a/tests/Heimdall.Infrastructure.Tests/CameraEventServiceTests.cs b/tests/Heimdall.Infrastructure.Tests/CameraEventServiceTests.cs index 2831244..cefc7e9 100644 --- a/tests/Heimdall.Infrastructure.Tests/CameraEventServiceTests.cs +++ b/tests/Heimdall.Infrastructure.Tests/CameraEventServiceTests.cs @@ -10,6 +10,8 @@ using Microsoft.Extensions.Logging; using NSubstitute; using Shouldly; +using Heimdall.TestCommon; + namespace Heimdall.Infrastructure.Tests; [Collection("PostgresInfra")] diff --git a/tests/Heimdall.Infrastructure.Tests/Heimdall.Infrastructure.Tests.csproj b/tests/Heimdall.Infrastructure.Tests/Heimdall.Infrastructure.Tests.csproj index 8335317..89164d3 100644 --- a/tests/Heimdall.Infrastructure.Tests/Heimdall.Infrastructure.Tests.csproj +++ b/tests/Heimdall.Infrastructure.Tests/Heimdall.Infrastructure.Tests.csproj @@ -12,7 +12,6 @@ - @@ -23,6 +22,7 @@ + \ No newline at end of file diff --git a/tests/Heimdall.Infrastructure.Tests/PostgreSqlContainerFixture.cs b/tests/Heimdall.Infrastructure.Tests/PostgreSqlContainerFixture.cs deleted file mode 100644 index d1569ca..0000000 --- a/tests/Heimdall.Infrastructure.Tests/PostgreSqlContainerFixture.cs +++ /dev/null @@ -1,56 +0,0 @@ -using Testcontainers.PostgreSql; - -namespace Heimdall.Infrastructure.Tests; - -/// -/// xUnit collection fixture that manages a PostgreSQL Testcontainer. -/// One container per test collection — shared across all test classes. -/// In CI mode (DATABASE_URL env var), uses the CI-provided PostgreSQL service -/// instead of starting a Testcontainer. -/// -public class PostgreSqlContainerFixture : IAsyncLifetime -{ - private readonly PostgreSqlContainer? _container; - private readonly string _connectionString; - - public string ConnectionString => _connectionString; - - public PostgreSqlContainerFixture() - { - var envUrl = Environment.GetEnvironmentVariable("DATABASE_URL"); - if (!string.IsNullOrEmpty(envUrl)) - { - // CI mode — use the CI-provided PostgreSQL service - _connectionString = envUrl; - _container = null; - } - else - { - // Local dev — start a Testcontainer - _container = new PostgreSqlBuilder("postgres:17-alpine") - .WithDatabase("heimdall_test") - .WithUsername("heimdall") - .WithPassword("heimdall_test_pwd") - .WithCleanUp(true) - .Build(); - _connectionString = _container.GetConnectionString(); - } - } - - public async Task InitializeAsync() - { - if (_container is not null) - await _container.StartAsync(); - } - - public async Task DisposeAsync() - { - if (_container is not null) - await _container.DisposeAsync(); - } -} - -[CollectionDefinition("PostgresInfra")] -public class PostgresInfraFixture : ICollectionFixture -{ -} diff --git a/tests/Heimdall.Infrastructure.Tests/PostgresInfraCollection.cs b/tests/Heimdall.Infrastructure.Tests/PostgresInfraCollection.cs new file mode 100644 index 0000000..628ee03 --- /dev/null +++ b/tests/Heimdall.Infrastructure.Tests/PostgresInfraCollection.cs @@ -0,0 +1,12 @@ +using Heimdall.TestCommon; + +namespace Heimdall.Infrastructure.Tests; + +/// +/// Collection definition for Infrastructure tests that need PostgreSQL. +/// Uses the shared from Heimdall.TestCommon. +/// +[CollectionDefinition("PostgresInfra")] +public sealed class PostgresInfraDefinition : ICollectionFixture +{ +} diff --git a/tests/Heimdall.IntegrationTests/Controllers/GlobalUsings.cs b/tests/Heimdall.IntegrationTests/Controllers/GlobalUsings.cs new file mode 100644 index 0000000..17fb09d --- /dev/null +++ b/tests/Heimdall.IntegrationTests/Controllers/GlobalUsings.cs @@ -0,0 +1 @@ +global using Heimdall.TestCommon; diff --git a/tests/Heimdall.IntegrationTests/Controllers/PostgresCollection.cs b/tests/Heimdall.IntegrationTests/Controllers/PostgresCollection.cs new file mode 100644 index 0000000..1dc2d6c --- /dev/null +++ b/tests/Heimdall.IntegrationTests/Controllers/PostgresCollection.cs @@ -0,0 +1,12 @@ +using Heimdall.TestCommon; + +namespace Heimdall.IntegrationTests.Controllers; + +/// +/// Collection definition for Integration tests that need PostgreSQL. +/// Uses the shared from Heimdall.TestCommon. +/// +[CollectionDefinition("Postgres")] +public sealed class PostgresDefinition : ICollectionFixture +{ +} diff --git a/tests/Heimdall.IntegrationTests/Controllers/PostgresTestBase.cs b/tests/Heimdall.IntegrationTests/Controllers/PostgresTestBase.cs index c83d5e5..835ea74 100644 --- a/tests/Heimdall.IntegrationTests/Controllers/PostgresTestBase.cs +++ b/tests/Heimdall.IntegrationTests/Controllers/PostgresTestBase.cs @@ -1,6 +1,7 @@ using Heimdall.Infrastructure.Data; using Heimdall.Infrastructure.Data.Interceptors; using Heimdall.IntegrationTests.Factories; +using Heimdall.TestCommon; using Microsoft.EntityFrameworkCore; namespace Heimdall.IntegrationTests.Controllers; @@ -8,7 +9,7 @@ namespace Heimdall.IntegrationTests.Controllers; /// /// Base class for integration tests that need a real PostgreSQL database /// via Testcontainers. All test classes in the "Postgres" collection share -/// the same PostgreSQL container. +/// the same PostgreSQL database. /// Resets the database before each test to guarantee isolation. /// [Collection("Postgres")] diff --git a/tests/Heimdall.IntegrationTests/Factories/PostgreSqlContainerFixture.cs b/tests/Heimdall.IntegrationTests/Factories/PostgreSqlContainerFixture.cs deleted file mode 100644 index 7754bf0..0000000 --- a/tests/Heimdall.IntegrationTests/Factories/PostgreSqlContainerFixture.cs +++ /dev/null @@ -1,62 +0,0 @@ -using Testcontainers.PostgreSql; - -namespace Heimdall.IntegrationTests.Factories; - -/// -/// xUnit collection fixture that manages a PostgreSQL Testcontainer -/// for integration tests. One container per test collection (shared -/// across all test classes in the same collection). -/// In CI mode (DATABASE_URL env var), uses the CI-provided PostgreSQL -/// service instead of starting a Testcontainer. -/// -public class PostgreSqlContainerFixture : IAsyncLifetime -{ - private readonly PostgreSqlContainer? _container; - private readonly string _connectionString; - - /// Connection string for the running PostgreSQL instance. - public string ConnectionString => _connectionString; - - public PostgreSqlContainerFixture() - { - var envUrl = Environment.GetEnvironmentVariable("DATABASE_URL"); - if (!string.IsNullOrEmpty(envUrl)) - { - // CI mode — use the CI-provided PostgreSQL service - _connectionString = envUrl; - _container = null; - } - else - { - // Local dev — start a Testcontainer - _container = new PostgreSqlBuilder("postgres:17-alpine") - .WithDatabase("heimdall_test") - .WithUsername("heimdall") - .WithPassword("heimdall_test_pwd") - .WithCleanUp(true) - .Build(); - _connectionString = _container.GetConnectionString(); - } - } - - public async Task InitializeAsync() - { - if (_container is not null) - await _container.StartAsync(); - } - - public async Task DisposeAsync() - { - if (_container is not null) - await _container.DisposeAsync(); - } -} - -/// -/// Defines the "Postgres" test collection. All test classes in this -/// collection share the same PostgreSQL container via the fixture. -/// -[CollectionDefinition("Postgres")] -public class PostgresCollectionFixture : ICollectionFixture -{ -} diff --git a/tests/Heimdall.IntegrationTests/Heimdall.IntegrationTests.csproj b/tests/Heimdall.IntegrationTests/Heimdall.IntegrationTests.csproj index 3e10e2e..dd728e9 100644 --- a/tests/Heimdall.IntegrationTests/Heimdall.IntegrationTests.csproj +++ b/tests/Heimdall.IntegrationTests/Heimdall.IntegrationTests.csproj @@ -10,11 +10,9 @@ - - @@ -26,6 +24,7 @@ + \ No newline at end of file diff --git a/tests/Heimdall.TestCommon/Heimdall.TestCommon.csproj b/tests/Heimdall.TestCommon/Heimdall.TestCommon.csproj new file mode 100644 index 0000000..608d705 --- /dev/null +++ b/tests/Heimdall.TestCommon/Heimdall.TestCommon.csproj @@ -0,0 +1,17 @@ + + + + net10.0 + enable + enable + false + + + + + + + + + + diff --git a/tests/Heimdall.TestCommon/PostgreSqlContainerFixture.cs b/tests/Heimdall.TestCommon/PostgreSqlContainerFixture.cs new file mode 100644 index 0000000..88106d0 --- /dev/null +++ b/tests/Heimdall.TestCommon/PostgreSqlContainerFixture.cs @@ -0,0 +1,33 @@ +using Testcontainers.PostgreSql; +using Xunit; + +namespace Heimdall.TestCommon; + +/// +/// xUnit collection fixture that manages ONE PostgreSQL Testcontainer +/// for ALL database-dependent tests across test projects. +/// +public sealed class PostgreSqlContainerFixture : IAsyncLifetime +{ + private readonly PostgreSqlContainer _container = new PostgreSqlBuilder("postgres:17-alpine") + .WithDatabase("heimdall_test") + .WithUsername("heimdall") + .WithPassword("heimdall_test_pwd") + .WithCleanUp(true) + .Build(); + + public string ConnectionString => _container.GetConnectionString(); + + public async Task InitializeAsync() + { + await _container.StartAsync(); + } + + public async Task DisposeAsync() + { + await _container.DisposeAsync(); + } +} + +// Collection definitions are in each test project assembly +// (xUnit requires [CollectionDefinition] in the same assembly as the test).