From 5665fdcaf3e2c7d18915f5e18e912fe9b2536a48 Mon Sep 17 00:00:00 2001 From: Oleksandr Kyselov Date: Thu, 8 Aug 2024 16:55:00 +0200 Subject: [PATCH] table storage healthchecks --- .../TableServiceHealthCheckTests.cs | 107 ++++++++++-------- 1 file changed, 57 insertions(+), 50 deletions(-) diff --git a/test/HealthChecks.Azure.Data.Tables.Tests/TableServiceHealthCheckTests.cs b/test/HealthChecks.Azure.Data.Tables.Tests/TableServiceHealthCheckTests.cs index 6606790eb3..5eaf6dc68b 100644 --- a/test/HealthChecks.Azure.Data.Tables.Tests/TableServiceHealthCheckTests.cs +++ b/test/HealthChecks.Azure.Data.Tables.Tests/TableServiceHealthCheckTests.cs @@ -2,6 +2,7 @@ using Azure; using Azure.Data.Tables; using Azure.Data.Tables.Models; +using Microsoft.AspNetCore.Mvc.RazorPages; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -55,14 +56,10 @@ public async Task return_healthy_when_only_checking_healthy_service() } [Fact] - public async Task return_healthy_when_checking_healthy_service_and_table() + public async Task return_healthy_when_checking_healthy_service_table() { using var tokenSource = new CancellationTokenSource(); - _tableServiceClient - .QueryAsync(filter: "false", cancellationToken: tokenSource.Token) - .Returns(AsyncPageable.FromPages(Array.Empty>())); - _tableClient .QueryAsync(filter: "false", cancellationToken: tokenSource.Token) .Returns(AsyncPageable.FromPages(Array.Empty>())); @@ -70,10 +67,6 @@ public async Task return_healthy_when_checking_healthy_service_and_table() _options.TableName = TableName; var actual = await _healthCheck.CheckHealthAsync(_context, tokenSource.Token); - _tableServiceClient - .Received(1) - .QueryAsync(filter: "false", cancellationToken: tokenSource.Token); - _tableClient .Received(1) .QueryAsync(filter: "false", cancellationToken: tokenSource.Token); @@ -81,10 +74,8 @@ public async Task return_healthy_when_checking_healthy_service_and_table() actual.Status.ShouldBe(HealthStatus.Healthy); } - [Theory] - [InlineData(false)] - [InlineData(true)] - public async Task return_unhealthy_when_checking_unhealthy_service(bool checkTable) + [Fact] + public async Task return_unhealthy_when_checking_unhealthy_service() { using var tokenSource = new CancellationTokenSource(); @@ -103,7 +94,6 @@ public async Task return_unhealthy_when_checking_unhealthy_service(bool checkTab .MoveNextAsync() .ThrowsAsync(new RequestFailedException((int)HttpStatusCode.Unauthorized, "Unable to authorize access.")); - _options.TableName = checkTable ? TableName : null; var actual = await _healthCheck.CheckHealthAsync(_context, tokenSource.Token); _tableServiceClient @@ -129,47 +119,44 @@ await enumerator } [Fact] - public async Task return_unhealthy_when_checking_unhealthy_container() + public async Task return_unhealthy_when_checking_unhealthy_service_queue() { using var tokenSource = new CancellationTokenSource(); - var pageable = Substitute.For>(); - var enumerator = Substitute.For>(); - - _tableServiceClient - .QueryAsync(filter: "false", cancellationToken: tokenSource.Token) - .Returns(AsyncPageable.FromPages(Array.Empty>())); - _tableClient .QueryAsync(filter: "false", cancellationToken: tokenSource.Token) - .Returns(pageable); + .Throws(new RequestFailedException((int)HttpStatusCode.Unauthorized, "Unable to authorize access.")); - pageable - .GetAsyncEnumerator(tokenSource.Token) - .Returns(enumerator); - - enumerator - .MoveNextAsync() - .ThrowsAsync(new RequestFailedException((int)HttpStatusCode.NotFound, "Table not found")); _options.TableName = TableName; var actual = await _healthCheck.CheckHealthAsync(_context, tokenSource.Token); - _tableServiceClient - .Received(1) - .QueryAsync(filter: "false", cancellationToken: tokenSource.Token); - _tableClient .Received(1) .QueryAsync(filter: "false", cancellationToken: tokenSource.Token); - pageable - .Received(1) - .GetAsyncEnumerator(tokenSource.Token); + actual.Status.ShouldBe(HealthStatus.Unhealthy); + actual + .Exception!.ShouldBeOfType() + .Status.ShouldBe((int)HttpStatusCode.Unauthorized); + } - await enumerator + [Fact] + public async Task return_unhealthy_when_checking_unhealthy_table() + { + using var tokenSource = new CancellationTokenSource(); + + _tableClient + .QueryAsync(filter: "false", cancellationToken: tokenSource.Token) + .Throws(new RequestFailedException((int)HttpStatusCode.NotFound, "Table not found")); + + _options.TableName = TableName; + var actual = await _healthCheck.CheckHealthAsync(_context, tokenSource.Token); + + _tableClient .Received(1) - .MoveNextAsync(); + .QueryAsync(filter: "false", cancellationToken: tokenSource.Token); + actual.Status.ShouldBe(HealthStatus.Unhealthy); actual @@ -184,19 +171,15 @@ public async Task return_unhealthy_when_invoked_from_healthcheckservice() .AddSingleton(_tableServiceClient) .AddLogging() .AddHealthChecks() - .AddAzureTable(optionsFactory: _ => new AzureTableServiceHealthCheckOptions() { TableName = TableName }, name: HealthCheckName) + .AddAzureTable(optionsFactory: _ => new AzureTableServiceHealthCheckOptions(), name: HealthCheckName) .Services .BuildServiceProvider(); - var pageable = Substitute.For>(); - var enumerator = Substitute.For>(); + var pageable = Substitute.For>(); + var enumerator = Substitute.For>(); _tableServiceClient .QueryAsync(filter: "false", cancellationToken: Arg.Any()) - .Returns(AsyncPageable.FromPages(Array.Empty>())); - - _tableClient - .QueryAsync(filter: "false", cancellationToken: Arg.Any()) .Returns(pageable); pageable @@ -214,10 +197,6 @@ public async Task return_unhealthy_when_invoked_from_healthcheckservice() .Received(1) .QueryAsync(filter: "false", cancellationToken: Arg.Any()); - _tableClient - .Received(1) - .QueryAsync(filter: "false", cancellationToken: Arg.Any()); - pageable .Received(1) .GetAsyncEnumerator(Arg.Any()); @@ -230,4 +209,32 @@ await enumerator actual.Status.ShouldBe(HealthStatus.Unhealthy); actual.Exception!.ShouldBeOfType(); } + + + [Fact] + public async Task return_unhealthy_when_invoked_from_healthcheckservice_for_table() + { + using var provider = new ServiceCollection() + .AddSingleton(_tableServiceClient) + .AddLogging() + .AddHealthChecks() + .AddAzureTable(optionsFactory: _ => new AzureTableServiceHealthCheckOptions() { TableName = TableName }, name: HealthCheckName) + .Services + .BuildServiceProvider(); + + _tableClient + .QueryAsync(filter: "false", cancellationToken: Arg.Any()) + .Throws(new RequestFailedException((int)HttpStatusCode.NotFound, "Table not found")); + + var service = provider.GetRequiredService(); + var report = await service.CheckHealthAsync(); + + _tableClient + .Received(1) + .QueryAsync(filter: "false", cancellationToken: Arg.Any()); + + var actual = report.Entries[HealthCheckName]; + actual.Status.ShouldBe(HealthStatus.Unhealthy); + actual.Exception!.ShouldBeOfType(); + } }