Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate HealthChecks.MongoDb tests to Testcontainers #2351

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 5 additions & 41 deletions .github/workflows/healthchecks_mongodb_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<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.MongoDb" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.MsSql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.Redis" Version="$(TestcontainersVersion)" />
<PackageVersion Include="xunit" Version="2.9.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace HealthChecks.MongoDb.Tests.Functional;

public class mongodb_healthcheck_should
public class mongodb_healthcheck_should(MongoDbContainerFixture mongoDbContainerFixture) : IClassFixture<MongoDbContainerFixture>
{
[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 =>
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -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<IMongoDatabase>(), tags: ["mongodb"]);
})
Expand All @@ -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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
<ProjectReference Include="..\..\src\HealthChecks.MongoDb\HealthChecks.MongoDb.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="TestContainers.MongoDb" />
</ItemGroup>
</Project>
37 changes: 37 additions & 0 deletions test/HealthChecks.MongoDb.Tests/MongoDbContainerFixture.cs
Original file line number Diff line number Diff line change
@@ -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<MongoDbContainer> CreateContainerAsync()
{
var container = new MongoDbBuilder()
.WithImage($"{Registry}/{Image}:{Tag}")
.WithUsername(null)
.WithPassword(null)
.Build();
await container.StartAsync();

return container;
}
}
Loading