From d8d88a06301f35140366cce8f0a5439b38346677 Mon Sep 17 00:00:00 2001 From: Alireza Baloochi Date: Thu, 19 Dec 2024 19:33:12 +0330 Subject: [PATCH] Migrate HealthChecks.MySql tests to Testcontainers (#2353) --- .github/workflows/healthchecks_mysql_ci.yml | 49 ++----------------- Directory.Packages.props | 1 + .../Functional/MySqlHealthCheckTests.cs | 6 +-- .../HealthChecks.MySql.Tests.csproj | 4 ++ .../MySqlContainerFixture.cs | 35 +++++++++++++ 5 files changed, 48 insertions(+), 47 deletions(-) create mode 100644 test/HealthChecks.MySql.Tests/MySqlContainerFixture.cs diff --git a/.github/workflows/healthchecks_mysql_ci.yml b/.github/workflows/healthchecks_mysql_ci.yml index 670ebc557a..99bf2c350b 100644 --- a/.github/workflows/healthchecks_mysql_ci.yml +++ b/.github/workflows/healthchecks_mysql_ci.yml @@ -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 diff --git a/Directory.Packages.props b/Directory.Packages.props index 7a3c077b65..0833e51097 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -105,6 +105,7 @@ + diff --git a/test/HealthChecks.MySql.Tests/Functional/MySqlHealthCheckTests.cs b/test/HealthChecks.MySql.Tests/Functional/MySqlHealthCheckTests.cs index 6f2859b233..5e644ae441 100644 --- a/test/HealthChecks.MySql.Tests/Functional/MySqlHealthCheckTests.cs +++ b/test/HealthChecks.MySql.Tests/Functional/MySqlHealthCheckTests.cs @@ -3,12 +3,12 @@ namespace HealthChecks.MySql.Tests.Functional; -public class mysql_healthcheck_should +public class mysql_healthcheck_should(MySqlContainerFixture mySqlContainerFixture) : IClassFixture { [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 => @@ -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 => diff --git a/test/HealthChecks.MySql.Tests/HealthChecks.MySql.Tests.csproj b/test/HealthChecks.MySql.Tests/HealthChecks.MySql.Tests.csproj index 416df1f116..fd909441b5 100644 --- a/test/HealthChecks.MySql.Tests/HealthChecks.MySql.Tests.csproj +++ b/test/HealthChecks.MySql.Tests/HealthChecks.MySql.Tests.csproj @@ -8,4 +8,8 @@ + + + + diff --git a/test/HealthChecks.MySql.Tests/MySqlContainerFixture.cs b/test/HealthChecks.MySql.Tests/MySqlContainerFixture.cs new file mode 100644 index 0000000000..0ac143143c --- /dev/null +++ b/test/HealthChecks.MySql.Tests/MySqlContainerFixture.cs @@ -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 CreateContainerAsync() + { + var container = new MySqlBuilder() + .WithImage($"{Registry}/{Image}:{Tag}") + .Build(); + await container.StartAsync(); + + return container; + } +}