Skip to content

Commit ae04b68

Browse files
authored
Migrate HealthChecks.Qdrant tests to Testcontainers (#2362)
1 parent 6f02752 commit ae04b68

File tree

5 files changed

+64
-46
lines changed

5 files changed

+64
-46
lines changed

.github/workflows/healthchecks_qdrant_ci.yml

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,8 @@ on:
2929

3030
jobs:
3131
build:
32-
runs-on: ubuntu-latest
33-
services:
34-
qdrant:
35-
image: qdrant/qdrant:v1.12.1
36-
ports:
37-
- 6333:6333
38-
- 6334:6334
39-
40-
steps:
41-
- uses: actions/checkout@v3
42-
- name: Setup .NET
43-
uses: actions/setup-dotnet@v4
44-
with:
45-
dotnet-version: |
46-
8.0.x
47-
9.0.x
48-
- name: Restore
49-
run: |
50-
dotnet restore ./src/HealthChecks.Qdrant/HealthChecks.Qdrant.csproj &&
51-
dotnet restore ./test/HealthChecks.Qdrant.Tests/HealthChecks.Qdrant.Tests.csproj
52-
- name: Check formatting
53-
run: |
54-
dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.Qdrant/HealthChecks.Qdrant.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) &&
55-
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)
56-
- name: Build
57-
run: |
58-
dotnet build --no-restore ./src/HealthChecks.Qdrant/HealthChecks.Qdrant.csproj &&
59-
dotnet build --no-restore ./test/HealthChecks.Qdrant.Tests/HealthChecks.Qdrant.Tests.csproj
60-
- name: Test
61-
run: >
62-
dotnet test
63-
./test/HealthChecks.Qdrant.Tests/HealthChecks.Qdrant.Tests.csproj
64-
--no-restore
65-
--no-build
66-
--collect "XPlat Code Coverage"
67-
--results-directory .coverage
68-
--
69-
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
70-
- name: Upload Coverage
71-
uses: codecov/codecov-action@v5
72-
with:
73-
flags: Qdrant
74-
directory: .coverage
32+
uses: ./.github/workflows/reusable_ci_workflow.yml
33+
with:
34+
PROJECT_PATH: ./src/HealthChecks.Qdrant/HealthChecks.Qdrant.csproj
35+
TEST_PROJECT_PATH: ./test/HealthChecks.Qdrant.Tests/HealthChecks.Qdrant.Tests.csproj
36+
CODECOV_FLAGS: Qdrant

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
103103
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
104104
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
105+
<PackageVersion Include="Testcontainers" Version="$(TestcontainersVersion)" />
105106
<PackageVersion Include="Testcontainers.Milvus" Version="$(TestcontainersVersion)" />
106107
<PackageVersion Include="Testcontainers.PostgreSql" Version="$(TestcontainersVersion)" />
107108
<PackageVersion Include="TestContainers.MongoDb" Version="$(TestcontainersVersion)" />

test/HealthChecks.Qdrant.Tests/Functional/QdrantHealthCheckTests.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33

44
namespace HealthChecks.Qdrant.Tests.Functional;
55

6-
public class qdrant_healthcheck_should
6+
public class qdrant_healthcheck_should(QdrantContainerFixture qdrantContainerFixture) : IClassFixture<QdrantContainerFixture>
77
{
88
[Fact]
99
public async Task be_healthy_when_qdrant_is_available_using_client_factory()
1010
{
11+
string connectionString = qdrantContainerFixture.GetConnectionString();
12+
1113
var webHostBuilder = new WebHostBuilder()
1214
.ConfigureServices(services =>
1315
{
1416
services
1517
.AddHealthChecks()
1618
.AddQdrant(
17-
clientFactory: sp => new QdrantClient("localhost"), tags: new string[] { "qdrant" });
19+
clientFactory: sp => new QdrantClient(new Uri(connectionString)), tags: new string[] { "qdrant" });
1820
})
1921
.Configure(app =>
2022
{
@@ -34,11 +36,13 @@ public async Task be_healthy_when_qdrant_is_available_using_client_factory()
3436
[Fact]
3537
public async Task be_healthy_when_qdrant_is_available_using_singleton()
3638
{
39+
string connectionString = qdrantContainerFixture.GetConnectionString();
40+
3741
var webHostBuilder = new WebHostBuilder()
3842
.ConfigureServices(services =>
3943
{
4044
services
41-
.AddSingleton(new QdrantClient("localhost"))
45+
.AddSingleton(new QdrantClient(new Uri(connectionString)))
4246
.AddHealthChecks()
4347
.AddQdrant(tags: new string[] { "qdrant" });
4448
})

test/HealthChecks.Qdrant.Tests/HealthChecks.Qdrant.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3+
<ItemGroup>
4+
<PackageReference Include="Testcontainers" />
5+
</ItemGroup>
6+
37
<ItemGroup>
48
<ProjectReference Include="..\..\src\HealthChecks.Qdrant\HealthChecks.Qdrant.csproj" />
59
</ItemGroup>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using DotNet.Testcontainers.Builders;
2+
using DotNet.Testcontainers.Containers;
3+
4+
namespace HealthChecks.Qdrant.Tests;
5+
6+
public sealed class QdrantContainerFixture : IAsyncLifetime
7+
{
8+
public const string Registry = "docker.io";
9+
10+
public const string Image = "qdrant/qdrant";
11+
12+
public const string Tag = "v1.12.1";
13+
14+
private const int GrpcPort = 6334;
15+
16+
public IContainer? Container { get; private set; }
17+
18+
public string GetConnectionString()
19+
{
20+
if (Container is null)
21+
{
22+
throw new InvalidOperationException("The test container was not initialized.");
23+
}
24+
string endpoint = new UriBuilder("http", Container.Hostname, Container.GetMappedPublicPort(GrpcPort)).ToString();
25+
return endpoint;
26+
}
27+
28+
public async Task InitializeAsync() => Container = await CreateContainerAsync();
29+
30+
public async Task DisposeAsync()
31+
{
32+
if (Container is not null)
33+
await Container.DisposeAsync();
34+
}
35+
36+
public static async Task<IContainer> CreateContainerAsync()
37+
{
38+
var container = new ContainerBuilder()
39+
.WithImage($"{Registry}/{Image}:{Tag}")
40+
.WithPortBinding(GrpcPort, true)
41+
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(GrpcPort))
42+
.Build();
43+
await container.StartAsync();
44+
45+
return container;
46+
}
47+
}

0 commit comments

Comments
 (0)