Skip to content

Commit

Permalink
Migrate HealthChecks.Qdrant tests to Testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
Alirexaa committed Jan 9, 2025
1 parent 3aee551 commit dff08fe
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 46 deletions.
48 changes: 5 additions & 43 deletions .github/workflows/healthchecks_qdrant_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,8 @@ on:

jobs:
build:
runs-on: ubuntu-latest
services:
qdrant:
image: qdrant/qdrant:v1.12.1
ports:
- 6333:6333
- 6334:6334

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.Qdrant/HealthChecks.Qdrant.csproj &&
dotnet restore ./test/HealthChecks.Qdrant.Tests/HealthChecks.Qdrant.Tests.csproj
- name: Check formatting
run: |
dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.Qdrant/HealthChecks.Qdrant.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) &&
dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.Qdrant.Tests/HealthChecks.Qdrant.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1)
- name: Build
run: |
dotnet build --no-restore ./src/HealthChecks.Qdrant/HealthChecks.Qdrant.csproj &&
dotnet build --no-restore ./test/HealthChecks.Qdrant.Tests/HealthChecks.Qdrant.Tests.csproj
- name: Test
run: >
dotnet test
./test/HealthChecks.Qdrant.Tests/HealthChecks.Qdrant.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: Qdrant
directory: .coverage
uses: ./.github/workflows/reusable_ci_workflow.yml
with:
PROJECT_PATH: ./src/HealthChecks.Qdrant/HealthChecks.Qdrant.csproj
TEST_PROJECT_PATH: ./test/HealthChecks.Qdrant.Tests/HealthChecks.Qdrant.Tests.csproj
CODECOV_FLAGS: Qdrant
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
<PackageVersion Include="Testcontainers" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.Milvus" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="TestContainers.MongoDb" Version="$(TestcontainersVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@

namespace HealthChecks.Qdrant.Tests.Functional;

public class qdrant_healthcheck_should
public class qdrant_healthcheck_should(QdrantContainerFixture qdrantContainerFixture) : IClassFixture<QdrantContainerFixture>
{
[Fact]
public async Task be_healthy_when_qdrant_is_available_using_client_factory()
{
string connectionString = qdrantContainerFixture.GetConnectionString();

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services
.AddHealthChecks()
.AddQdrant(
clientFactory: sp => new QdrantClient("localhost"), tags: new string[] { "qdrant" });
clientFactory: sp => new QdrantClient(new Uri(connectionString)), tags: new string[] { "qdrant" });
})
.Configure(app =>
{
Expand All @@ -34,11 +36,13 @@ public async Task be_healthy_when_qdrant_is_available_using_client_factory()
[Fact]
public async Task be_healthy_when_qdrant_is_available_using_singleton()
{
string connectionString = qdrantContainerFixture.GetConnectionString();

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services
.AddSingleton(new QdrantClient("localhost"))
.AddSingleton(new QdrantClient(new Uri(connectionString)))
.AddHealthChecks()
.AddQdrant(tags: new string[] { "qdrant" });
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="Testcontainers" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\HealthChecks.Qdrant\HealthChecks.Qdrant.csproj" />
</ItemGroup>
Expand Down
47 changes: 47 additions & 0 deletions test/HealthChecks.Qdrant.Tests/QdrantContainerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;

namespace HealthChecks.Qdrant.Tests;

public sealed class QdrantContainerFixture : IAsyncLifetime
{
public const string Registry = "docker.io";

public const string Image = "qdrant/qdrant";

public const string Tag = "v1.12.1";

private const int GrpcPort = 6334;

public IContainer? Container { get; private set; }

public string GetConnectionString()
{
if (Container is null)
{
throw new InvalidOperationException("The test container was not initialized.");
}
string endpoint = new UriBuilder("http", Container.Hostname, Container.GetMappedPublicPort(GrpcPort)).ToString();
return endpoint;
}

public async Task InitializeAsync() => Container = await CreateContainerAsync();

public async Task DisposeAsync()
{
if (Container is not null)
await Container.DisposeAsync();
}

public static async Task<IContainer> CreateContainerAsync()
{
var container = new ContainerBuilder()
.WithImage($"{Registry}/{Image}:{Tag}")
.WithPortBinding(GrpcPort, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(GrpcPort))
.Build();
await container.StartAsync();

return container;
}
}

0 comments on commit dff08fe

Please sign in to comment.