Skip to content

Commit

Permalink
Migrate HealthChecks.Rabbitmq tests to Testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
Alirexaa committed Dec 19, 2024
1 parent 2b47b15 commit 2627710
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 79 deletions.
70 changes: 13 additions & 57 deletions .github/workflows/healthchecks_rabbitmq_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ on:
branches: [ master ]
paths:
- src/HealthChecks.Rabbitmq/**
- src/HealthChecks.Rabbitmq.v6/**
- test/HealthChecks.RabbitMQ.Tests/**
- test/HealthChecks.RabbitMQ.v6.Tests/**
- test/_SHARED/**
- .github/workflows/healthchecks_rabbitmq_ci.yml
- Directory.Build.props
Expand All @@ -31,60 +33,14 @@ on:

jobs:
build:
runs-on: ubuntu-latest
services:
rabbitmq:
image: rabbitmq
ports:
- 5672:5672
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.Rabbitmq/HealthChecks.Rabbitmq.csproj &&
dotnet restore ./src/HealthChecks.Rabbitmq.v6/HealthChecks.Rabbitmq.v6.csproj &&
dotnet restore ./test/HealthChecks.RabbitMQ.Tests/HealthChecks.RabbitMQ.Tests.csproj &&
dotnet restore ./test/HealthChecks.RabbitMQ.v6.Tests/HealthChecks.RabbitMQ.v6.Tests.csproj
- name: Check formatting
run: |
dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.Rabbitmq/HealthChecks.Rabbitmq.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) &&
dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.Rabbitmq.v6/HealthChecks.Rabbitmq.v6.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) &&
dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.RabbitMQ.Tests/HealthChecks.RabbitMQ.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) &&
dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.RabbitMQ.v6.Tests/HealthChecks.RabbitMQ.v6.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1)
- name: Build
run: |
dotnet build --no-restore ./src/HealthChecks.Rabbitmq/HealthChecks.Rabbitmq.csproj &&
dotnet build --no-restore ./src/HealthChecks.Rabbitmq.v6/HealthChecks.Rabbitmq.v6.csproj &&
dotnet build --no-restore ./test/HealthChecks.RabbitMQ.Tests/HealthChecks.RabbitMQ.Tests.csproj &&
dotnet build --no-restore ./test/HealthChecks.RabbitMQ.v6.Tests/HealthChecks.RabbitMQ.v6.Tests.csproj
- name: Test
run: >
dotnet test
./test/HealthChecks.RabbitMQ.Tests/HealthChecks.RabbitMQ.Tests.csproj
--no-restore
--no-build
--collect "XPlat Code Coverage"
--results-directory .coverage
--
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
- name: Test v6
run: >
dotnet test
./test/HealthChecks.RabbitMQ.v6.Tests/HealthChecks.RabbitMQ.v6.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: RabbitMQ
directory: .coverage
uses: ./.github/workflows/reusable_ci_workflow.yml
with:
PROJECT_PATH: ./src/HealthChecks.Rabbitmq/HealthChecks.Rabbitmq.csproj
TEST_PROJECT_PATH: ./test/HealthChecks.RabbitMQ.Tests/HealthChecks.RabbitMQ.Tests.csproj
CODECOV_FLAGS: RabbitMQ
build-v6:
uses: ./.github/workflows/reusable_ci_workflow.yml
with:
PROJECT_PATH: ./src/HealthChecks.Rabbitmq.v6/HealthChecks.Rabbitmq.v6.csproj
TEST_PROJECT_PATH: ./test/HealthChecks.RabbitMQ.v6.Tests/HealthChecks.RabbitMQ.v6.Tests.csproj
CODECOV_FLAGS: RabbitMQ
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<PackageVersion Include="Testcontainers.PostgreSql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.MsSql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.Redis" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.RabbitMq" Version="$(TestcontainersVersion)" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace HealthChecks.RabbitMQ.Tests.Functional;

public class rabbitmq_healthcheck_should
public class rabbitmq_healthcheck_should(RabbitMQContainerFixture rabbitMQContainerFixture) : IClassFixture<RabbitMQContainerFixture>
{
[Fact]
public async Task be_healthy_if_rabbitmq_is_available()
{
var connectionString = "amqp://localhost:5672";
var connectionString = rabbitMQContainerFixture.GetConnectionString();

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand Down Expand Up @@ -36,7 +36,7 @@ public async Task be_healthy_if_rabbitmq_is_available()
[Fact]
public async Task be_unhealthy_if_rabbitmq_is_not_available()
{
var connectionString = "amqp://localhost:6672";
var connectionString = "amqp://invalidhost:6672";

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand Down Expand Up @@ -64,7 +64,7 @@ public async Task be_unhealthy_if_rabbitmq_is_not_available()
[Fact]
public async Task be_healthy_if_rabbitmq_is_available_using_iconnection()
{
var connectionString = "amqp://localhost:5672";
var connectionString = rabbitMQContainerFixture.GetConnectionString();

var factory = new ConnectionFactory()
{
Expand Down Expand Up @@ -104,7 +104,7 @@ public async Task be_healthy_if_rabbitmq_is_available_using_iconnection()
[Fact]
public async Task be_healthy_if_rabbitmq_is_available_using_iconnection_in_serviceprovider()
{
var connectionString = "amqp://localhost:5672";
var connectionString = rabbitMQContainerFixture.GetConnectionString();

var factory = new ConnectionFactory()
{
Expand Down Expand Up @@ -149,9 +149,9 @@ public async Task be_not_crash_on_startup_when_rabbitmq_is_down_at_startup()
{
return new ConnectionFactory()
{
Uri = new Uri("amqp://localhost:3333"),
Uri = new Uri("amqp://invalidhost:3333"),
AutomaticRecoveryEnabled = true,
Ssl = new SslOption(serverName: "localhost", enabled: false)
Ssl = new SslOption(serverName: "invalidhost", enabled: false)
};
})
.AddHealthChecks()
Expand All @@ -176,8 +176,8 @@ public async Task be_not_crash_on_startup_when_rabbitmq_is_down_at_startup()
[Fact]
public async Task two_rabbitmq_health_check()
{
const string connectionString1 = "amqp://localhost:5672";
const string connectionString2 = "amqp://localhost:6672/";
var connectionString1 = rabbitMQContainerFixture.GetConnectionString();
const string connectionString2 = "amqp://invalidhost:6672/";

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\src\HealthChecks.Rabbitmq\HealthChecks.Rabbitmq.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Testcontainers.RabbitMq" />
</ItemGroup>
</Project>
35 changes: 35 additions & 0 deletions test/HealthChecks.RabbitMQ.Tests/RabbitMQContainerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Testcontainers.RabbitMq;

namespace HealthChecks.RabbitMQ.Tests;

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

public const string Image = "library/rabbitmq";

public const string Tag = "4.0";

public RabbitMqContainer? 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<RabbitMqContainer> CreateContainerAsync()
{
var container = new RabbitMqBuilder()
.WithImage($"{Registry}/{Image}:{Tag}")
.Build();
await container.StartAsync();

return container;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace HealthChecks.RabbitMQ.Tests.Functional;

public class rabbitmq_healthcheck_should
public class rabbitmq_healthcheck_should(RabbitMQContainerFixture rabbitMQContainerFixture) : IClassFixture<RabbitMQContainerFixture>
{
[Fact]
public async Task be_healthy_if_rabbitmq_is_available()
{
var connectionString = "amqp://localhost:5672";
var connectionString = rabbitMQContainerFixture.GetConnectionString();

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand All @@ -34,7 +34,7 @@ public async Task be_healthy_if_rabbitmq_is_available()
[Fact]
public async Task be_healthy_if_rabbitmq_is_available_using_ssloption()
{
var connectionString = "amqp://localhost:5672";
var connectionString = rabbitMQContainerFixture.GetConnectionString();

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand Down Expand Up @@ -64,7 +64,7 @@ public async Task be_unhealthy_if_rabbitmq_is_not_available()
.ConfigureServices(services =>
{
services.AddHealthChecks()
.AddRabbitMQ("amqp://localhost:6672", sslOption: new SslOption(serverName: "localhost", enabled: false), tags: ["rabbitmq"]);
.AddRabbitMQ("amqp://invalidlocalhost:6672", sslOption: new SslOption(serverName: "invalidlocalhost", enabled: false), tags: ["rabbitmq"]);
})
.Configure(app =>
{
Expand All @@ -84,7 +84,7 @@ public async Task be_unhealthy_if_rabbitmq_is_not_available()
[Fact]
public async Task be_healthy_if_rabbitmq_is_available_using_iconnectionfactory()
{
var connectionString = "amqp://localhost:5672";
var connectionString = rabbitMQContainerFixture.GetConnectionString();

var factory = new ConnectionFactory()
{
Expand Down Expand Up @@ -118,7 +118,7 @@ public async Task be_healthy_if_rabbitmq_is_available_using_iconnectionfactory()
[Fact]
public async Task be_healthy_if_rabbitmq_is_available_using_iconnection()
{
var connectionString = "amqp://localhost:5672";
var connectionString = rabbitMQContainerFixture.GetConnectionString();

var factory = new ConnectionFactory()
{
Expand Down Expand Up @@ -155,7 +155,7 @@ public async Task be_healthy_if_rabbitmq_is_available_using_iconnection()
[Fact]
public async Task be_healthy_if_rabbitmq_is_available_and_specify_default_ssloption()
{
var connectionString = "amqp://localhost:5672";
var connectionString = rabbitMQContainerFixture.GetConnectionString();

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand Down Expand Up @@ -189,7 +189,7 @@ public async Task be_not_crash_on_startup_when_rabbitmq_is_down_at_startup()
{
return new ConnectionFactory()
{
Uri = new Uri("amqp://localhost:3333"),
Uri = new Uri("amqp://invalidlocalhost:3333"),
AutomaticRecoveryEnabled = true,
Ssl = new SslOption(serverName: "localhost", enabled: false)
};
Expand All @@ -214,7 +214,7 @@ public async Task be_not_crash_on_startup_when_rabbitmq_is_down_at_startup()
[Fact]
public async Task be_healthy_if_rabbitmq_is_available_using_iServiceProvider()
{
var connectionString = "amqp://localhost:5672";
var connectionString = rabbitMQContainerFixture.GetConnectionString();

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand Down Expand Up @@ -242,8 +242,8 @@ public async Task be_healthy_if_rabbitmq_is_available_using_iServiceProvider()
[Fact]
public async Task two_rabbitmq_health_check()
{
const string connectionString1 = "amqp://localhost:5672";
const string connectionString2 = "amqp://localhost:6672/";
var connectionString1 = rabbitMQContainerFixture.GetConnectionString();
const string connectionString2 = "amqp://invalidhost:6672/";

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>HealthChecks.RabbitMQ.Tests</AssemblyName>
Expand All @@ -8,4 +8,12 @@
<ProjectReference Include="..\..\src\HealthChecks.Rabbitmq.v6\HealthChecks.Rabbitmq.v6.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\HealthChecks.RabbitMQ.Tests\RabbitMQContainerFixture.cs"></Compile>
</ItemGroup>

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

</Project>

0 comments on commit 2627710

Please sign in to comment.