diff --git a/.github/workflows/healthchecks_eventstore_ci.yml b/.github/workflows/healthchecks_eventstore_ci.yml index 9301c75cc1..4cec84f918 100644 --- a/.github/workflows/healthchecks_eventstore_ci.yml +++ b/.github/workflows/healthchecks_eventstore_ci.yml @@ -28,49 +28,8 @@ on: jobs: build: - runs-on: ubuntu-latest - services: - eventstore: - image: eventstore/eventstore:22.10.5-jammy - ports: - - "2113:2113" - - "1113:1113" - env: - EVENTSTORE_INSECURE: true - EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP: true - EVENTSTORE_ENABLE_EXTERNAL_TCP: true - 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.EventStore/HealthChecks.EventStore.csproj && - dotnet restore ./test/HealthChecks.EventStore.Tests/HealthChecks.EventStore.Tests.csproj - - name: Check formatting - run: | - dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.EventStore/HealthChecks.EventStore.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) && - dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.EventStore.Tests/HealthChecks.EventStore.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) - - name: Build - run: | - dotnet build --no-restore ./src/HealthChecks.EventStore/HealthChecks.EventStore.csproj && - dotnet build --no-restore ./test/HealthChecks.EventStore.Tests/HealthChecks.EventStore.Tests.csproj - - name: Test - run: > - dotnet test - ./test/HealthChecks.EventStore.Tests/HealthChecks.EventStore.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: EventStore - directory: .coverage + uses: ./.github/workflows/reusable_ci_workflow.yml + with: + PROJECT_PATH: ./src/HealthChecks.EventStore/HealthChecks.EventStore.csproj + TEST_PROJECT_PATH: ./test/HealthChecks.EventStore.Tests/HealthChecks.EventStore.Tests.csproj + CODECOV_FLAGS: EventStore diff --git a/.github/workflows/healthchecks_eventstore_grpc_ci.yml b/.github/workflows/healthchecks_eventstore_grpc_ci.yml index ee1c233c6f..ec839e52f8 100644 --- a/.github/workflows/healthchecks_eventstore_grpc_ci.yml +++ b/.github/workflows/healthchecks_eventstore_grpc_ci.yml @@ -29,47 +29,8 @@ on: jobs: build: - runs-on: ubuntu-latest - services: - eventstore: - image: eventstore/eventstore:22.10.5-jammy - ports: - - "1113:1113" - - "2113:2113" - env: - EVENTSTORE_INSECURE: true - 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.EventStore.gRPC/HealthChecks.EventStore.gRPC.csproj && - dotnet restore ./test/HealthChecks.EventStore.gRPC.Tests/HealthChecks.EventStore.gRPC.Tests.csproj - - name: Check formatting - run: | - dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.EventStore.gRPC/HealthChecks.EventStore.gRPC.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) && - dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.EventStore.gRPC.Tests/HealthChecks.EventStore.gRPC.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) - - name: Build - run: | - dotnet build --no-restore ./src/HealthChecks.EventStore.gRPC/HealthChecks.EventStore.gRPC.csproj && - dotnet build --no-restore ./test/HealthChecks.EventStore.gRPC.Tests/HealthChecks.EventStore.gRPC.Tests.csproj - - name: Test - run: > - dotnet test - ./test/HealthChecks.EventStore.gRPC.Tests/HealthChecks.EventStore.gRPC.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: EventStore.gRPC - directory: .coverage + uses: ./.github/workflows/reusable_ci_workflow.yml + with: + PROJECT_PATH: ./src/HealthChecks.EventStore.gRPC/HealthChecks.EventStore.gRPC.csproj + TEST_PROJECT_PATH: ./test/HealthChecks.EventStore.gRPC.Tests/HealthChecks.EventStore.gRPC.Tests.csproj + CODECOV_FLAGS: EventStore.gRPC diff --git a/Directory.Packages.props b/Directory.Packages.props index 2c2207765a..5b23131f43 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -104,6 +104,7 @@ + @@ -119,4 +120,4 @@ - + \ No newline at end of file diff --git a/test/HealthChecks.EventStore.Tests/EventStoreDbContainerFixture.cs b/test/HealthChecks.EventStore.Tests/EventStoreDbContainerFixture.cs new file mode 100644 index 0000000000..cfefae8062 --- /dev/null +++ b/test/HealthChecks.EventStore.Tests/EventStoreDbContainerFixture.cs @@ -0,0 +1,55 @@ +using System.Data.Common; +using Testcontainers.EventStoreDb; + +namespace HealthChecks.EventStore.Tests; + +public class EventStoreDbContainerFixture : IAsyncLifetime +{ + private const string Registry = "docker.io"; + + private const string Image = "eventstore/eventstore"; + + private const string Tag = "22.10.5-bookworm-slim"; + + public const int TcpPort = 1113; + + public EventStoreDbContainer? Container { get; private set; } + + public string GetConnectionString() + { + if (Container is null) + { + throw new InvalidOperationException("The test container was not initialized."); + } + + var uriBuilder = new UriBuilder( + "tcp", + Container.Hostname, + Container.GetMappedPublicPort(TcpPort)); + + var dbConnectionStringBuilder = new DbConnectionStringBuilder + { + { "ConnectTo", uriBuilder.ToString() }, + { "UseSslConnection", false } + }; + + return dbConnectionStringBuilder.ConnectionString; + } + + public async Task InitializeAsync() => Container = await CreateContainerAsync(); + + public Task DisposeAsync() => Container?.DisposeAsync().AsTask() ?? Task.CompletedTask; + + private static async Task CreateContainerAsync() + { + var container = new EventStoreDbBuilder() + .WithImage($"{Registry}/{Image}:{Tag}") + .WithEnvironment("EVENTSTORE_ENABLE_EXTERNAL_TCP", "true") + .WithPortBinding(TcpPort, true) + .Build(); + + await container.StartAsync(); + + return container; + } +} diff --git a/test/HealthChecks.EventStore.Tests/Functional/EventStoreHealthCheckTests.cs b/test/HealthChecks.EventStore.Tests/Functional/EventStoreHealthCheckTests.cs index c26656ad6e..a6a6480488 100644 --- a/test/HealthChecks.EventStore.Tests/Functional/EventStoreHealthCheckTests.cs +++ b/test/HealthChecks.EventStore.Tests/Functional/EventStoreHealthCheckTests.cs @@ -3,16 +3,18 @@ namespace HealthChecks.EventStore.Tests.Functional; -public class eventstore_healthcheck_should +public class eventstore_healthcheck_should(EventStoreDbContainerFixture eventStoreDbFixture) : IClassFixture { [Fact] public async Task be_healthy_if_eventstore_is_available_with_uri_format() { + string connectionString = eventStoreDbFixture.GetConnectionString(); + var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => { services.AddHealthChecks() - .AddEventStore("ConnectTo=tcp://localhost:1113; UseSslConnection=false", tags: ["eventstore"]); + .AddEventStore(connectionString, tags: ["eventstore"]); }) .Configure(app => { @@ -33,11 +35,13 @@ public async Task be_healthy_if_eventstore_is_available_with_uri_format() [Fact] public async Task be_healthy_if_eventstore_is_available() { + string connectionString = eventStoreDbFixture.GetConnectionString(); + var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => { services.AddHealthChecks() - .AddEventStore("ConnectTo=tcp://localhost:1113; UseSslConnection=false; HeartBeatTimeout=500", tags: ["eventstore"]); + .AddEventStore(connectionString, tags: ["eventstore"]); }) .Configure(app => { diff --git a/test/HealthChecks.EventStore.Tests/HealthChecks.EventStore.Tests.csproj b/test/HealthChecks.EventStore.Tests/HealthChecks.EventStore.Tests.csproj index d155db1da3..6feeade03f 100644 --- a/test/HealthChecks.EventStore.Tests/HealthChecks.EventStore.Tests.csproj +++ b/test/HealthChecks.EventStore.Tests/HealthChecks.EventStore.Tests.csproj @@ -4,4 +4,8 @@ + + + + diff --git a/test/HealthChecks.EventStore.gRPC.Tests/EventStoreDbContainerFixture.cs b/test/HealthChecks.EventStore.gRPC.Tests/EventStoreDbContainerFixture.cs new file mode 100644 index 0000000000..c954286d3f --- /dev/null +++ b/test/HealthChecks.EventStore.gRPC.Tests/EventStoreDbContainerFixture.cs @@ -0,0 +1,39 @@ +using Testcontainers.EventStoreDb; + +namespace HealthChecks.EventStore.gRPC.Tests; + +public class EventStoreDbContainerFixture : IAsyncLifetime +{ + private const string Registry = "docker.io"; + + private const string Image = "eventstore/eventstore"; + + private const string Tag = "24.10.6"; + + public EventStoreDbContainer? Container { get; private set; } + + public string GetConnectionString() + { + if (Container is null) + { + throw new InvalidOperationException("The test container was not initialized."); + } + + return Container.GetConnectionString(); + } + + public async Task InitializeAsync() => Container = await CreateContainerAsync(); + + public Task DisposeAsync() => Container?.DisposeAsync().AsTask() ?? Task.CompletedTask; + + private static async Task CreateContainerAsync() + { + var container = new EventStoreDbBuilder() + .WithImage($"{Registry}/{Image}:{Tag}") + .Build(); + + await container.StartAsync(); + + return container; + } +} diff --git a/test/HealthChecks.EventStore.gRPC.Tests/Functional/EventStoreHealthCheckTests.cs b/test/HealthChecks.EventStore.gRPC.Tests/Functional/EventStoreHealthCheckTests.cs index 09479b1ac6..462cedd965 100644 --- a/test/HealthChecks.EventStore.gRPC.Tests/Functional/EventStoreHealthCheckTests.cs +++ b/test/HealthChecks.EventStore.gRPC.Tests/Functional/EventStoreHealthCheckTests.cs @@ -2,16 +2,18 @@ namespace HealthChecks.EventStore.gRPC.Tests.Functional; -public class eventstore_healthcheck_should +public class eventstore_healthcheck_should(EventStoreDbContainerFixture eventStoreDbFixture) : IClassFixture { [Fact] public async Task be_healthy_if_eventstore_is_available() { + string connectionString = eventStoreDbFixture.GetConnectionString(); + var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => { services.AddHealthChecks() - .AddEventStore("esdb://localhost:2113?tls=false", tags: ["eventstore"]); + .AddEventStore(connectionString, tags: ["eventstore"]); }) .Configure(app => { diff --git a/test/HealthChecks.EventStore.gRPC.Tests/HealthChecks.EventStore.gRPC.Tests.csproj b/test/HealthChecks.EventStore.gRPC.Tests/HealthChecks.EventStore.gRPC.Tests.csproj index 7f3f6f2307..da5bf52f5f 100644 --- a/test/HealthChecks.EventStore.gRPC.Tests/HealthChecks.EventStore.gRPC.Tests.csproj +++ b/test/HealthChecks.EventStore.gRPC.Tests/HealthChecks.EventStore.gRPC.Tests.csproj @@ -4,4 +4,8 @@ + + + +