Skip to content

Commit d82593a

Browse files
authored
Migrate HealthChecks.ClickHouse tests to Testcontainers (#2412)
1 parent 59c9f73 commit d82593a

File tree

5 files changed

+67
-57
lines changed

5 files changed

+67
-57
lines changed

.github/workflows/healthchecks_clickhouse_ci.yml

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

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

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
104104
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
105105
<PackageVersion Include="Testcontainers" Version="$(TestcontainersVersion)" />
106+
<PackageVersion Include="Testcontainers.ClickHouse" Version="$(TestcontainersVersion)" />
106107
<PackageVersion Include="Testcontainers.Kafka" Version="$(TestcontainersVersion)" />
107108
<PackageVersion Include="Testcontainers.Milvus" Version="$(TestcontainersVersion)" />
108109
<PackageVersion Include="Testcontainers.PostgreSql" Version="$(TestcontainersVersion)" />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Testcontainers.ClickHouse;
2+
3+
namespace HealthChecks.ClickHouse.Tests;
4+
5+
public class ClickHouseContainerFixture : IAsyncLifetime
6+
{
7+
private const string Registry = "docker.io";
8+
9+
private const string Image = "library/clickhouse";
10+
11+
private const string Tag = "25.7.2";
12+
13+
public ClickHouseContainer? Container { get; private set; }
14+
15+
public string GetConnectionString()
16+
{
17+
if (Container is null)
18+
{
19+
throw new InvalidOperationException("The test container was not initialized.");
20+
}
21+
22+
return Container.GetConnectionString();
23+
}
24+
25+
public async Task InitializeAsync() => Container = await CreateContainerAsync();
26+
27+
public Task DisposeAsync() => Container?.DisposeAsync().AsTask() ?? Task.CompletedTask;
28+
29+
private async Task<ClickHouseContainer?> CreateContainerAsync()
30+
{
31+
var container = new ClickHouseBuilder()
32+
.WithImage($"{Registry}/{Image}:{Tag}")
33+
.Build();
34+
35+
await container.StartAsync();
36+
37+
return container;
38+
}
39+
}

test/HealthChecks.ClickHouse.Tests/Functional/ClickHouseHealthCheckTests.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ public class DBConfigSetting
1010
public string ConnectionString { get; set; } = null!;
1111
}
1212

13-
public class ClickHouse_healthcheck_should
13+
public class ClickHouse_healthcheck_should(ClickHouseContainerFixture clickHouseFixture) : IClassFixture<ClickHouseContainerFixture>
1414
{
15-
private const string ConnectionString = "Host=127.0.0.1;Port=8123;Database=default;Username=default;Password=Password12!;";
15+
private readonly string _connectionString = clickHouseFixture.GetConnectionString();
1616

1717
[Fact]
1818
public async Task be_healthy_if_ClickHouse_is_available()
1919
{
2020
var webHostBuilder = new WebHostBuilder()
21-
.ConfigureServices(static services =>
21+
.ConfigureServices(services =>
2222
{
2323
services.AddHealthChecks()
24-
.AddClickHouse(static _ => new ClickHouseConnection(ConnectionString), tags: new string[] { "ClickHouse" });
24+
.AddClickHouse(_ => new ClickHouseConnection(_connectionString), tags: new string[] { "ClickHouse" });
2525
})
2626
.Configure(static app =>
2727
{
@@ -42,10 +42,10 @@ public async Task be_healthy_if_ClickHouse_is_available()
4242
public async Task be_unhealthy_if_sql_query_is_not_valid()
4343
{
4444
var webHostBuilder = new WebHostBuilder()
45-
.ConfigureServices(static services =>
45+
.ConfigureServices(services =>
4646
{
4747
services.AddHealthChecks()
48-
.AddClickHouse(static _ => new ClickHouseConnection(ConnectionString), "SELECT 1 FROM InvalidDB", tags: new string[] { "ClickHouse" });
48+
.AddClickHouse(_ => new ClickHouseConnection(_connectionString), "SELECT 1 FROM InvalidDB", tags: new string[] { "ClickHouse" });
4949
})
5050
.Configure(static app =>
5151
{
@@ -68,8 +68,11 @@ public async Task be_unhealthy_if_ClickHouse_is_not_available()
6868
var webHostBuilder = new WebHostBuilder()
6969
.ConfigureServices(services =>
7070
{
71-
services.AddHealthChecks()
72-
.AddClickHouse(static _ => new ClickHouseConnection("Host=200.0.0.1;Port=8123;Database=default;Username=default;Password=Password12!;"), tags: new string[] { "ClickHouse" });
71+
services
72+
.AddHealthChecks()
73+
.AddClickHouse(static _ => new ClickHouseConnection("Host=200.0.0.1;Port=8123;Database=default;Username=default;Password=Password12!;"),
74+
tags: ["ClickHouse"],
75+
timeout: TimeSpan.FromSeconds(15));
7376
})
7477
.Configure(app =>
7578
{
@@ -94,7 +97,7 @@ public async Task be_healthy_if_ClickHouse_is_available_by_iServiceProvider_regi
9497
{
9598
services.AddSingleton(new DBConfigSetting
9699
{
97-
ConnectionString = ConnectionString
100+
ConnectionString = _connectionString
98101
});
99102

100103
services.AddHealthChecks()
@@ -126,8 +129,12 @@ public async Task be_unhealthy_if_ClickHouse_is_not_available_registered()
126129
ConnectionString = "Server=200.0.0.1;Port=8010;User ID=postgres;Password=Password12!;database=postgres"
127130
});
128131

129-
services.AddHealthChecks()
130-
.AddClickHouse(static sp => new ClickHouseConnection(sp.GetRequiredService<DBConfigSetting>().ConnectionString), tags: new string[] { "ClickHouse" });
132+
services
133+
.AddHealthChecks()
134+
.AddClickHouse(
135+
static sp => new ClickHouseConnection(sp.GetRequiredService<DBConfigSetting>().ConnectionString),
136+
tags: ["ClickHouse"],
137+
timeout: TimeSpan.FromSeconds(15));
131138
})
132139
.Configure(app =>
133140
{

test/HealthChecks.ClickHouse.Tests/HealthChecks.ClickHouse.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
<ProjectReference Include="..\..\src\HealthChecks.ClickHouse\HealthChecks.ClickHouse.csproj" />
55
</ItemGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Testcontainers.ClickHouse" />
9+
</ItemGroup>
10+
711
</Project>

0 commit comments

Comments
 (0)