Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate HealthChecks.MySql tests to Testcontainers #2353

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -104,6 +104,7 @@
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.MsSql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.MySql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.Redis" Version="$(TestcontainersVersion)" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.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;
}
}
Loading