From 3e2cf425ccf281b9d76359452a2f1c73aff6cdad Mon Sep 17 00:00:00 2001 From: Alireza Baloochi Date: Thu, 19 Dec 2024 16:18:32 +0330 Subject: [PATCH] Migrate HealthChecks.MongoDb tests to Testcontainers --- .github/workflows/healthchecks_mongodb_ci.yml | 46 ++----------------- Directory.Packages.props | 1 + .../Functional/MongoDbHealthCheckTests.cs | 14 +++--- .../HealthChecks.MongoDb.Tests.csproj | 3 ++ .../MongoDbContainerFixture.cs | 37 +++++++++++++++ 5 files changed, 54 insertions(+), 47 deletions(-) create mode 100644 test/HealthChecks.MongoDb.Tests/MongoDbContainerFixture.cs diff --git a/.github/workflows/healthchecks_mongodb_ci.yml b/.github/workflows/healthchecks_mongodb_ci.yml index ea64d4d195..6411d58595 100644 --- a/.github/workflows/healthchecks_mongodb_ci.yml +++ b/.github/workflows/healthchecks_mongodb_ci.yml @@ -29,44 +29,8 @@ on: jobs: build: - runs-on: ubuntu-latest - services: - mongo: - image: mongo - ports: - - 27017:27017 - 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.MongoDb/HealthChecks.MongoDb.csproj && - dotnet restore ./test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.Tests.csproj - - name: Check formatting - run: | - dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.MongoDb/HealthChecks.MongoDb.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) && - dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) - - name: Build - run: | - dotnet build --no-restore ./src/HealthChecks.MongoDb/HealthChecks.MongoDb.csproj && - dotnet build --no-restore ./test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.Tests.csproj - - name: Test - run: > - dotnet test - ./test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.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: MongoDb - directory: .coverage + uses: ./.github/workflows/reusable_ci_workflow.yml + with: + PROJECT_PATH: ./src/HealthChecks.MongoDb/HealthChecks.MongoDb.csproj + TEST_PROJECT_PATH: ./test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.Tests.csproj + CODECOV_FLAGS: MongoDb diff --git a/Directory.Packages.props b/Directory.Packages.props index 6732ccd3d2..0833f4385c 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -103,6 +103,7 @@ + diff --git a/test/HealthChecks.MongoDb.Tests/Functional/MongoDbHealthCheckTests.cs b/test/HealthChecks.MongoDb.Tests/Functional/MongoDbHealthCheckTests.cs index bee1c28fbb..74ad2addf3 100644 --- a/test/HealthChecks.MongoDb.Tests/Functional/MongoDbHealthCheckTests.cs +++ b/test/HealthChecks.MongoDb.Tests/Functional/MongoDbHealthCheckTests.cs @@ -3,12 +3,12 @@ namespace HealthChecks.MongoDb.Tests.Functional; -public class mongodb_healthcheck_should +public class mongodb_healthcheck_should(MongoDbContainerFixture mongoDbContainerFixture) : IClassFixture { [Fact] public async Task be_healthy_listing_all_databases_if_mongodb_is_available() { - var connectionString = "mongodb://localhost:27017"; + var connectionString = mongoDbContainerFixture.GetConnectionString(); var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => @@ -36,7 +36,7 @@ public async Task be_healthy_listing_all_databases_if_mongodb_is_available() [Fact] public async Task be_healthy_on_specified_database_if_mongodb_is_available_and_database_exist() { - var connectionString = "mongodb://localhost:27017"; + var connectionString = mongoDbContainerFixture.GetConnectionString(); var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => @@ -64,7 +64,7 @@ public async Task be_healthy_on_specified_database_if_mongodb_is_available_and_d [Fact] public async Task be_healthy_on_connectionstring_specified_database_if_mongodb_is_available_and_database_exist() { - var connectionString = "mongodb://localhost:27017/local"; + var connectionString = $"{mongoDbContainerFixture.GetConnectionString()}local"; var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => @@ -92,11 +92,13 @@ public async Task be_healthy_on_connectionstring_specified_database_if_mongodb_i [Fact] public async Task be_healthy_on_connectionstring_specified_database_if_mongodb_is_available_and_database_exist_dbFactory() { + var connectionString = mongoDbContainerFixture.GetConnectionString(); + var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => { services - .AddSingleton(sp => new MongoClient("mongodb://localhost:27017").GetDatabase("namedDb")) + .AddSingleton(sp => new MongoClient(connectionString).GetDatabase("namedDb")) .AddHealthChecks() .AddMongoDb(dbFactory: sp => sp.GetRequiredService(), tags: ["mongodb"]); }) @@ -119,7 +121,7 @@ public async Task be_healthy_on_connectionstring_specified_database_if_mongodb_i public async Task be_healthy_on_connectionstring_specified_database_if_mongodb_is_available_and_database_not_exist() { // NOTE: with mongodb the database is created automatically the first time something is written to it - var connectionString = "mongodb://localhost:27017/nonexisting"; + var connectionString = $"{mongoDbContainerFixture.GetConnectionString()}nonexisting"; var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => diff --git a/test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.Tests.csproj b/test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.Tests.csproj index c7bf48044a..615da7dd68 100644 --- a/test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.Tests.csproj +++ b/test/HealthChecks.MongoDb.Tests/HealthChecks.MongoDb.Tests.csproj @@ -4,4 +4,7 @@ + + + diff --git a/test/HealthChecks.MongoDb.Tests/MongoDbContainerFixture.cs b/test/HealthChecks.MongoDb.Tests/MongoDbContainerFixture.cs new file mode 100644 index 0000000000..38f992dc33 --- /dev/null +++ b/test/HealthChecks.MongoDb.Tests/MongoDbContainerFixture.cs @@ -0,0 +1,37 @@ +using Testcontainers.MongoDb; + +namespace HealthChecks.MongoDb.Tests; + +public sealed class MongoDbContainerFixture : IAsyncLifetime +{ + public const string Registry = "docker.io"; + + public const string Image = "library/mongo"; + + public const string Tag = "8.0"; + + public MongoDbContainer? 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 CreateContainerAsync() + { + var container = new MongoDbBuilder() + .WithImage($"{Registry}/{Image}:{Tag}") + .WithUsername(null) + .WithPassword(null) + .Build(); + await container.StartAsync(); + + return container; + } +}