-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SQL Health Check] Handle HTTP exception (#902)
* Modifying health checks to return the failure reason as string. * Add HttpRequestException handling to Sql Health Check * Created new health status reason. * Test to validate new possible Health Check failure. * Adding more logs to components used by schema manager. * Addressing PR comments.
- Loading branch information
Showing
6 changed files
with
199 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/Microsoft.Health.SqlServer.UnitTests/Features/Health/SqlServerHealthCheckTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Health.Core.Features.Health; | ||
using Microsoft.Health.Encryption.Customer.Health; | ||
using Microsoft.Health.SqlServer.Configs; | ||
using Microsoft.Health.SqlServer.Features.Client; | ||
using Microsoft.Health.SqlServer.Features.Health; | ||
using Microsoft.Health.SqlServer.Features.Storage; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Microsoft.Health.SqlServer.UnitTests.Features.Health; | ||
|
||
public sealed class SqlServerHealthCheckTests | ||
{ | ||
private readonly ILogger<SqlServerHealthCheck> _logger; | ||
private readonly SqlTransactionHandler _sqlTransactionHandler; | ||
private readonly ISqlConnectionBuilder _sqlConnectionBuilder; | ||
private readonly SqlRetryLogicBaseProvider _sqlRetryLogicBaseProvider; | ||
private readonly IOptions<SqlServerDataStoreConfiguration> _sqlServerDataStoreConfiguration; | ||
private readonly ValueCache<CustomerKeyHealth> _cache; | ||
|
||
public SqlServerHealthCheckTests() | ||
{ | ||
_logger = Substitute.For<ILogger<SqlServerHealthCheck>>(); | ||
_sqlTransactionHandler = Substitute.For<SqlTransactionHandler>(); | ||
_sqlConnectionBuilder = Substitute.For<ISqlConnectionBuilder>(); | ||
_sqlRetryLogicBaseProvider = Substitute.For<SqlRetryLogicBaseProvider>(); | ||
|
||
_sqlServerDataStoreConfiguration = Substitute.For<IOptions<SqlServerDataStoreConfiguration>>(); | ||
_sqlServerDataStoreConfiguration.Value.Returns(new SqlServerDataStoreConfiguration()); | ||
|
||
_cache = new ValueCache<CustomerKeyHealth>(); | ||
_cache.Set(new CustomerKeyHealth() { IsHealthy = true }); | ||
} | ||
|
||
[Theory] | ||
[InlineData(HttpStatusCode.Forbidden)] | ||
[InlineData(HttpStatusCode.Unauthorized)] | ||
public async Task GivenASqlHealthCheck_WhenSqlConnectionWrapperThrowsAnInvalidAccess_ThenHandlesItProperlyAsDegraded(HttpStatusCode statusCode) | ||
{ | ||
HealthCheckResult healthCheckResult = await GetHealthCheckResultGivenAnErrorHttpStatusCodeAsync(statusCode); | ||
|
||
Assert.Equal(HealthStatus.Degraded, healthCheckResult.Status); | ||
Assert.Equal(HealthStatusReason.DataStoreConnectionDegraded.ToString(), healthCheckResult.Data["Reason"]); | ||
} | ||
|
||
[Theory] | ||
[InlineData(HttpStatusCode.NotFound)] | ||
public async Task GivenASqlHealthCheck_WhenSqlConnectionWrapperThrowsAnUnknownStatusCode_ThenHandlesItError(HttpStatusCode statusCode) | ||
{ | ||
HttpRequestException httpException = await Assert.ThrowsAsync<HttpRequestException>(() => GetHealthCheckResultGivenAnErrorHttpStatusCodeAsync(statusCode)); | ||
|
||
Assert.Equal(statusCode, httpException.StatusCode); | ||
} | ||
|
||
private async Task<HealthCheckResult> GetHealthCheckResultGivenAnErrorHttpStatusCodeAsync(HttpStatusCode statusCode) | ||
{ | ||
// Exception thrown by the SqlConnectionWrapperFactory.ObtainSqlConnectionWrapperAsync method. | ||
var httpRequestException = new HttpRequestException("error", inner: null, statusCode: statusCode); | ||
|
||
SqlConnectionWrapperFactory connectionWrapperFactory = Substitute.For<SqlConnectionWrapperFactory>( | ||
_sqlTransactionHandler, | ||
_sqlConnectionBuilder, | ||
_sqlRetryLogicBaseProvider, | ||
_sqlServerDataStoreConfiguration); | ||
|
||
// Setting up the ObtainSqlConnectionWrapperAsync method to throw an exception. | ||
connectionWrapperFactory.ObtainSqlConnectionWrapperAsync(Arg.Any<CancellationToken>()).Returns(Task.FromException<SqlConnectionWrapper>(httpRequestException)); | ||
|
||
var sqlHealthCheck = new SqlServerHealthCheck(connectionWrapperFactory, _cache, _logger); | ||
|
||
return await sqlHealthCheck.CheckHealthAsync(new HealthCheckContext(), CancellationToken.None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Microsoft.Health.SqlServer/Features/Storage/HttpErrorExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Net; | ||
using System.Net.Http; | ||
|
||
namespace Microsoft.Health.SqlServer.Features.Storage; | ||
|
||
public static class HttpErrorExtensions | ||
{ | ||
public static bool IsInvalidAccess(this HttpRequestException exception) | ||
{ | ||
return exception?.StatusCode is HttpStatusCode.Forbidden or HttpStatusCode.Unauthorized; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters