feat: add AuthStoreHealthCheck readiness probe
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ZB.MOM.WW.MxGateway.Server.Configuration;
|
||||
using ZB.MOM.WW.MxGateway.Server.Diagnostics;
|
||||
using ZB.MOM.WW.MxGateway.Server.Security.Authentication;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Tests.Diagnostics;
|
||||
|
||||
public sealed class AuthStoreHealthCheckTests
|
||||
{
|
||||
private static AuthSqliteConnectionFactory FactoryFor(string sqlitePath)
|
||||
{
|
||||
// GatewayOptions.Authentication and AuthenticationOptions.SqlitePath are both
|
||||
// init-only, so populate them through object initializers.
|
||||
var options = new GatewayOptions
|
||||
{
|
||||
Authentication = new AuthenticationOptions { SqlitePath = sqlitePath },
|
||||
};
|
||||
return new AuthSqliteConnectionFactory(Options.Create(options));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Healthy_WhenStoreReachable()
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"authcheck-{Guid.NewGuid():N}.db");
|
||||
try
|
||||
{
|
||||
var check = new AuthStoreHealthCheck(FactoryFor(path));
|
||||
var result = await check.CheckHealthAsync(new HealthCheckContext());
|
||||
Assert.Equal(HealthStatus.Healthy, result.Status);
|
||||
}
|
||||
finally { if (File.Exists(path)) File.Delete(path); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Unhealthy_WhenPathUnusable()
|
||||
{
|
||||
// A regular file used as a parent directory forces the open to fail.
|
||||
var bogus = Path.Combine(Path.GetTempPath(), $"authcheck-{Guid.NewGuid():N}");
|
||||
await File.WriteAllTextAsync(bogus, "x");
|
||||
try
|
||||
{
|
||||
var check = new AuthStoreHealthCheck(FactoryFor(Path.Combine(bogus, "store.db")));
|
||||
var result = await check.CheckHealthAsync(new HealthCheckContext());
|
||||
Assert.Equal(HealthStatus.Unhealthy, result.Status);
|
||||
}
|
||||
finally { if (File.Exists(bogus)) File.Delete(bogus); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user