Skip to content

Commit

Permalink
Migrate HealthChecks.MySql tests to Testcontainers (#2353)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alirexaa authored Dec 19, 2024
1 parent 3f4ad9e commit d8d88a0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 47 deletions.
49 changes: 5 additions & 44 deletions .github/workflows/healthchecks_mysql_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,10 @@ on:
- Directory.Build.props
- Directory.Build.targets
- Directory.Packages.props

jobs:
build:
runs-on: ubuntu-latest
services:
mysql:
image: mysql
ports:
- 3306:3306
env:
MYSQL_ROOT_PASSWORD: Password12!
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.MySql/HealthChecks.MySql.csproj &&
dotnet restore ./test/HealthChecks.MySql.Tests/HealthChecks.MySql.Tests.csproj
- name: Check formatting
run: |
dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.MySql/HealthChecks.MySql.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) &&
dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.MySql.Tests/HealthChecks.MySql.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1)
- name: Build
run: |
dotnet build --no-restore ./src/HealthChecks.MySql/HealthChecks.MySql.csproj &&
dotnet build --no-restore ./test/HealthChecks.MySql.Tests/HealthChecks.MySql.Tests.csproj
- name: Test
run: >
dotnet test
./test/HealthChecks.MySql.Tests/HealthChecks.MySql.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: MySql
directory: .coverage
uses: ./.github/workflows/reusable_ci_workflow.yml
with:
PROJECT_PATH: ./src/HealthChecks.MySql/HealthChecks.MySql.csproj
TEST_PROJECT_PATH: ./test/HealthChecks.MySql.Tests/HealthChecks.MySql.Tests.csproj
CODECOV_FLAGS: MySql
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.MongoDb" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.MsSql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.MySql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.Redis" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.RabbitMq" Version="$(TestcontainersVersion)" />
<PackageVersion Include="xunit" Version="2.9.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace HealthChecks.MySql.Tests.Functional;

public class mysql_healthcheck_should
public class mysql_healthcheck_should(MySqlContainerFixture mySqlContainerFixture) : IClassFixture<MySqlContainerFixture>
{
[Fact]
public async Task be_healthy_when_mysql_server_is_available_using_data_source()
{
var connectionString = "server=localhost;port=3306;database=information_schema;uid=root;password=Password12!";
var connectionString = mySqlContainerFixture.GetConnectionString();

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand All @@ -35,7 +35,7 @@ public async Task be_healthy_when_mysql_server_is_available_using_data_source()
[Fact]
public async Task be_healthy_when_mysql_server_is_available_using_connection_string()
{
var connectionString = "server=localhost;port=3306;database=information_schema;uid=root;password=Password12!";
var connectionString = mySqlContainerFixture.GetConnectionString();

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand Down
4 changes: 4 additions & 0 deletions test/HealthChecks.MySql.Tests/HealthChecks.MySql.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
<ProjectReference Include="..\..\src\HealthChecks.MySql\HealthChecks.MySql.csproj" />
</ItemGroup>

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

</Project>
35 changes: 35 additions & 0 deletions test/HealthChecks.MySql.Tests/MySqlContainerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Testcontainers.MySql;

namespace HealthChecks.MySql.Tests;

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

public const string Image = "library/mysql";

public const string Tag = "9.1";

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

return container;
}
}

0 comments on commit d8d88a0

Please sign in to comment.