diff --git a/.github/workflows/healthchecks_openidconnectserver_ci.yml b/.github/workflows/healthchecks_openidconnectserver_ci.yml index bb95f3a3fd..717024b85b 100644 --- a/.github/workflows/healthchecks_openidconnectserver_ci.yml +++ b/.github/workflows/healthchecks_openidconnectserver_ci.yml @@ -29,44 +29,8 @@ on: jobs: build: - runs-on: ubuntu-latest - services: - idsvr: - image: nakah/identityserver4 - ports: - - 8888:80 - 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.OpenIdConnectServer/HealthChecks.OpenIdConnectServer.csproj && - dotnet restore ./test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.Tests.csproj - - name: Check formatting - run: | - dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.OpenIdConnectServer/HealthChecks.OpenIdConnectServer.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) && - dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) - - name: Build - run: | - dotnet build --no-restore ./src/HealthChecks.OpenIdConnectServer/HealthChecks.OpenIdConnectServer.csproj && - dotnet build --no-restore ./test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.Tests.csproj - - name: Test - run: > - dotnet test - ./test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.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: OpenIdConnectServer - directory: .coverage + uses: ./.github/workflows/reusable_ci_workflow.yml + with: + PROJECT_PATH: ./src/HealthChecks.OpenIdConnectServer/HealthChecks.OpenIdConnectServer.csproj + TEST_PROJECT_PATH: ./test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.Tests.csproj + CODECOV_FLAGS: OpenIdConnectServer diff --git a/Directory.Packages.props b/Directory.Packages.props index 2c2207765a..29bd8bcac7 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -105,6 +105,7 @@ + @@ -119,4 +120,4 @@ - + \ No newline at end of file diff --git a/test/HealthChecks.OpenIdConnectServer.Tests/Functional/OpenIdConnectServerHealthCheckTests.cs b/test/HealthChecks.OpenIdConnectServer.Tests/Functional/OpenIdConnectServerHealthCheckTests.cs index 4e95735d7e..27e7cf5e2b 100644 --- a/test/HealthChecks.OpenIdConnectServer.Tests/Functional/OpenIdConnectServerHealthCheckTests.cs +++ b/test/HealthChecks.OpenIdConnectServer.Tests/Functional/OpenIdConnectServerHealthCheckTests.cs @@ -2,7 +2,7 @@ namespace HealthChecks.OpenIdConnectServer.Tests.Functional; -public class oidc_server_healthcheck_should +public class oidc_server_healthcheck_should(KeycloakContainerFixture keycloakFixture) : IClassFixture { [Fact] public async Task be_unhealthy_if_oidc_server_is_unavailable() @@ -32,12 +32,14 @@ public async Task be_unhealthy_if_oidc_server_is_unavailable() [Fact] public async Task be_healthy_if_oidc_server_is_available() { + string baseAddress = keycloakFixture.GetBaseAddress(); + var webHostBuilder = new WebHostBuilder() .ConfigureServices(services => { services .AddHealthChecks() - .AddOpenIdConnectServer(new Uri("http://localhost:8888"), tags: ["oidcserver"]); + .AddOpenIdConnectServer(new Uri(baseAddress), tags: ["oidcserver"]); }) .Configure(app => { diff --git a/test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.Tests.csproj b/test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.Tests.csproj index 32e4c9dee0..62932e215b 100644 --- a/test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.Tests.csproj +++ b/test/HealthChecks.OpenIdConnectServer.Tests/HealthChecks.OpenIdConnectServer.Tests.csproj @@ -4,4 +4,8 @@ + + + + diff --git a/test/HealthChecks.OpenIdConnectServer.Tests/KeycloakContainerFixture.cs b/test/HealthChecks.OpenIdConnectServer.Tests/KeycloakContainerFixture.cs new file mode 100644 index 0000000000..20662aa39c --- /dev/null +++ b/test/HealthChecks.OpenIdConnectServer.Tests/KeycloakContainerFixture.cs @@ -0,0 +1,44 @@ +using Testcontainers.Keycloak; + +namespace HealthChecks.OpenIdConnectServer.Tests; + +public class KeycloakContainerFixture : IAsyncLifetime +{ + private const string Registry = "quay.io"; + + private const string Image = "keycloak/keycloak"; + + private const string Tag = "26.3.2"; + + public KeycloakContainer? Container { get; private set; } + + public string GetBaseAddress() + { + if (Container is null) + { + throw new InvalidOperationException("The test container was not initialized."); + } + + var uriBuilder = new UriBuilder(Container.GetBaseAddress()) + { + Path = "/realms/master/" + }; + + return uriBuilder.ToString(); + } + + public async Task InitializeAsync() => Container = await CreateContainerAsync(); + + public Task DisposeAsync() => Container?.DisposeAsync().AsTask() ?? Task.CompletedTask; + + private static async Task CreateContainerAsync() + { + var container = new KeycloakBuilder() + .WithImage($"{Registry}/{Image}:{Tag}") + .Build(); + + await container.StartAsync(); + + return container; + } +}