Skip to content

Commit 52eb1e1

Browse files
Alirexaaadamsitnik
andauthored
Migrate HealthChecks.Kafka tests to Testcontainers (#2365)
* Migrate HealthChecks.Kafka tests to Testcontainers * Apply suggestions from code review --------- Co-authored-by: Adam Sitnik <[email protected]>
1 parent d38c3b6 commit 52eb1e1

File tree

6 files changed

+48
-73
lines changed

6 files changed

+48
-73
lines changed

.github/workflows/healthchecks_kafka_ci.yml

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

3030
jobs:
3131
build:
32-
runs-on: ubuntu-latest
33-
services:
34-
zookeeper:
35-
image: confluentinc/cp-zookeeper:latest
36-
ports:
37-
- 2181:2181
38-
env:
39-
ZOOKEEPER_CLIENT_PORT: 2181
40-
ZOOKEEPER_TICK_TIME: 2000
41-
kafka:
42-
image: confluentinc/cp-kafka:latest
43-
ports:
44-
- 9092:9092
45-
- 29092:29092
46-
env:
47-
KAFKA_ADVERTISED_HOST_NAME: localhost
48-
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
49-
KAFKA_BROKER_ID: 1
50-
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,PLAINTEXT_HOST://localhost:29092
51-
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
52-
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
53-
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
54-
steps:
55-
- uses: actions/checkout@v3
56-
- name: Setup .NET
57-
uses: actions/setup-dotnet@v4
58-
with:
59-
dotnet-version: |
60-
8.0.x
61-
9.0.x
62-
- name: Restore
63-
run: |
64-
dotnet restore ./src/HealthChecks.Kafka/HealthChecks.Kafka.csproj &&
65-
dotnet restore ./test/HealthChecks.Kafka.Tests/HealthChecks.Kafka.Tests.csproj
66-
- name: Check formatting
67-
run: |
68-
dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.Kafka/HealthChecks.Kafka.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) &&
69-
dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.Kafka.Tests/HealthChecks.Kafka.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1)
70-
- name: Build
71-
run: |
72-
dotnet build --no-restore ./src/HealthChecks.Kafka/HealthChecks.Kafka.csproj &&
73-
dotnet build --no-restore ./test/HealthChecks.Kafka.Tests/HealthChecks.Kafka.Tests.csproj
74-
- name: Test
75-
run: >
76-
dotnet test
77-
./test/HealthChecks.Kafka.Tests/HealthChecks.Kafka.Tests.csproj
78-
--no-restore
79-
--no-build
80-
--collect "XPlat Code Coverage"
81-
--results-directory .coverage
82-
--
83-
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
84-
- name: Upload Coverage
85-
uses: codecov/codecov-action@v5
86-
with:
87-
flags: Kafka
88-
directory: .coverage
32+
uses: ./.github/workflows/reusable_ci_workflow.yml
33+
with:
34+
PROJECT_PATH: ./src/HealthChecks.Kafka/HealthChecks.Kafka.csproj
35+
TEST_PROJECT_PATH: ./test/HealthChecks.Kafka.Tests/HealthChecks.Kafka.Tests.csproj
36+
CODECOV_FLAGS: Kafka

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.Kafka" Version="$(TestcontainersVersion)" />
106107
<PackageVersion Include="Testcontainers.Milvus" Version="$(TestcontainersVersion)" />
107108
<PackageVersion Include="Testcontainers.PostgreSql" Version="$(TestcontainersVersion)" />
108109
<PackageVersion Include="TestContainers.MongoDb" Version="$(TestcontainersVersion)" />

docker-compose.yml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,12 @@ services:
5757
- MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
5858
ports:
5959
- ${MYSQL_PORT}:3306
60-
zookeeper:
61-
image: confluent/zookeeper
62-
ports:
63-
- ${ZOOKEEPER_PORT}:2181
6460
raven:
6561
image: ravendb/ravendb
6662
ports:
6763
- ${RAVENDB_PORT}:8080
6864
environment:
6965
- RAVEN_ARGS=--Setup.Mode=None
70-
kafka:
71-
image: confluent/kafka
72-
environment:
73-
- KAFKA_ADVERTISED_HOST_NAME=localhost
74-
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
75-
- KAFKA_ADVERTISED_PORT=9092
76-
ports:
77-
- ${KAFKA_PORT}:9092
78-
links:
79-
- zookeeper
8066
rabbitmq:
8167
image: rabbitmq
8268
ports:

test/HealthChecks.Kafka.Tests/Functional/KafkaHealthCheckTests.cs

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

44
namespace HealthChecks.Kafka.Tests.Functional;
55

6-
public class kafka_healthcheck_should
6+
public class kafka_healthcheck_should(KafkaContainerFixture kafkaContainerFixture) : IClassFixture<KafkaContainerFixture>
77
{
88
[Fact]
99
public async Task be_unhealthy_if_kafka_is_unavailable()
@@ -41,9 +41,11 @@ public async Task be_unhealthy_if_kafka_is_unavailable()
4141
[Fact]
4242
public async Task be_healthy_if_kafka_is_available()
4343
{
44+
string connectionString = kafkaContainerFixture.GetConnectionString();
45+
4446
var configuration = new ProducerConfig()
4547
{
46-
BootstrapServers = "localhost:29092",
48+
BootstrapServers = connectionString,
4749
MessageSendMaxRetries = 0
4850
};
4951

test/HealthChecks.Kafka.Tests/HealthChecks.Kafka.Tests.csproj

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

7+
<ItemGroup>
8+
<PackageReference Include="Testcontainers.Kafka" />
9+
</ItemGroup>
710
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Testcontainers.Kafka;
2+
3+
namespace HealthChecks.Kafka.Tests;
4+
5+
public sealed class KafkaContainerFixture : IAsyncLifetime
6+
{
7+
public const string Registry = "docker.io";
8+
9+
public const string Image = "confluentinc/cp-kafka";
10+
11+
public const string Tag = "7.8.0";
12+
13+
public KafkaContainer? Container { get; private set; }
14+
15+
public string GetConnectionString() => Container?.GetBootstrapAddress() ??
16+
throw new InvalidOperationException("The test container was not initialized.");
17+
18+
public async Task InitializeAsync() => Container = await CreateContainerAsync();
19+
20+
public async Task DisposeAsync()
21+
{
22+
if (Container is not null)
23+
await Container.DisposeAsync();
24+
}
25+
26+
public static async Task<KafkaContainer> CreateContainerAsync()
27+
{
28+
var container = new KafkaBuilder()
29+
.WithImage($"{Registry}/{Image}:{Tag}")
30+
.Build();
31+
await container.StartAsync();
32+
33+
return container;
34+
}
35+
}

0 commit comments

Comments
 (0)