513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
203 lines
9.4 KiB
C#
203 lines
9.4 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Health;
|
|
using ZB.MOM.WW.ScadaBridge.HealthMonitoring;
|
|
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Persistence;
|
|
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Secrets;
|
|
using ZB.MOM.WW.ScadaBridge.TestSupport;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.Secrets;
|
|
|
|
/// <summary>
|
|
/// <see cref="SecretMirrorStatusReporter"/> forwards every node's
|
|
/// replicated <c>secret_mirror_status</c> row onto the site health report via the collector.
|
|
/// </summary>
|
|
public class SecretMirrorStatusReporterTests : IAsyncLifetime, IDisposable
|
|
{
|
|
private readonly TestLocalDb _localDb = TestLocalDb.CreateTemp("secret-mirror-status");
|
|
private SiteStorageService _storage = null!;
|
|
|
|
/// <summary>Creates the site storage service and initializes its schema.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
public async Task InitializeAsync()
|
|
{
|
|
_storage = new SiteStorageService(_localDb.Db, NullLogger<SiteStorageService>.Instance);
|
|
await _storage.InitializeAsync();
|
|
}
|
|
|
|
/// <summary>Completes immediately; the synchronous <c>Dispose</c> releases the resources.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
public Task DisposeAsync() => Task.CompletedTask;
|
|
|
|
/// <summary>Releases the resources held by this test class.</summary>
|
|
public void Dispose()
|
|
{
|
|
var path = _localDb.Path;
|
|
_localDb.Dispose();
|
|
TestLocalDb.DeleteFiles(path);
|
|
}
|
|
|
|
/// <summary>Verifies that a probe pushes every node's status row onto the next health report.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Probe_PushesEveryNodesRow_OntoTheNextReport()
|
|
{
|
|
var at = new DateTimeOffset(2026, 9, 28, 6, 0, 0, TimeSpan.Zero);
|
|
await _storage.SetSecretMirrorStatusAsync("node-a", [], at);
|
|
await _storage.SetSecretMirrorStatusAsync(
|
|
"node-b", [new SecretMirrorFailure("OPC-PWD", "did not decrypt on this node")], at);
|
|
var collector = new SiteHealthCollector();
|
|
var reporter = new SecretMirrorStatusReporter(
|
|
collector, _storage, NullLogger<SecretMirrorStatusReporter>.Instance,
|
|
time: new FixedTime(at.AddMinutes(1)));
|
|
|
|
await reporter.ProbeAsync();
|
|
var report = collector.CollectReport("site-a");
|
|
|
|
Assert.NotNull(report.SecretMirrorStatuses);
|
|
Assert.Equal(["node-a", "node-b"], report.SecretMirrorStatuses!.Select(s => s.NodeName));
|
|
Assert.Equal("OPC-PWD", Assert.Single(report.SecretMirrorStatuses[1].Failures).SecretName);
|
|
}
|
|
|
|
/// <summary>Verifies that the report field is null before any probe has run.</summary>
|
|
[Fact]
|
|
public void BeforeAnyProbe_TheReportFieldIsNull()
|
|
{
|
|
var report = new SiteHealthCollector().CollectReport("site-a");
|
|
|
|
Assert.Null(report.SecretMirrorStatuses);
|
|
}
|
|
|
|
/// <summary>Verifies that a probe with no rows yet pushes an empty list rather than null.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Probe_NoRowsYet_PushesAnEmptyList_NotNull()
|
|
{
|
|
var collector = new SiteHealthCollector();
|
|
var reporter = new SecretMirrorStatusReporter(
|
|
collector, _storage, NullLogger<SecretMirrorStatusReporter>.Instance);
|
|
|
|
await reporter.ProbeAsync();
|
|
|
|
Assert.Empty(collector.CollectReport("site-a").SecretMirrorStatuses!);
|
|
}
|
|
|
|
/// <summary>Verifies that a read failure during a probe keeps the previous snapshot and does not throw.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Probe_ReadFailure_KeepsThePreviousSnapshot_AndDoesNotThrow()
|
|
{
|
|
await _storage.SetSecretMirrorStatusAsync("node-a", [], DateTimeOffset.UtcNow);
|
|
var collector = new SiteHealthCollector();
|
|
var reporter = new SecretMirrorStatusReporter(
|
|
collector, _storage, NullLogger<SecretMirrorStatusReporter>.Instance);
|
|
await reporter.ProbeAsync();
|
|
|
|
await using (var connection = _storage.CreateConnection())
|
|
await using (var drop = connection.CreateCommand())
|
|
{
|
|
drop.CommandText = "DROP TABLE secret_mirror_status";
|
|
await drop.ExecuteNonQueryAsync();
|
|
}
|
|
|
|
await reporter.ProbeAsync();
|
|
|
|
// Point-in-time: not drained on collect, and not cleared by a failed read.
|
|
Assert.Single(collector.CollectReport("site-a").SecretMirrorStatuses!);
|
|
Assert.Single(collector.CollectReport("site-a").SecretMirrorStatuses!);
|
|
}
|
|
|
|
// ── Stale rows (decommissioned / renamed nodes) ──
|
|
|
|
/// <summary>Verifies that a row older than the stale-after window is left off the report and deleted.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Probe_RowOlderThanStaleAfter_IsLeftOffTheReport_AndDeleted()
|
|
{
|
|
var now = new DateTimeOffset(2026, 9, 29, 12, 0, 0, TimeSpan.Zero);
|
|
await _storage.SetSecretMirrorStatusAsync("node-a", [], now.AddMinutes(-5));
|
|
await _storage.SetSecretMirrorStatusAsync(
|
|
"node-old", [new SecretMirrorFailure("OPC-PWD", "did not decrypt on this node")], now.AddHours(-2));
|
|
var collector = new SiteHealthCollector();
|
|
var reporter = new SecretMirrorStatusReporter(
|
|
collector, _storage, NullLogger<SecretMirrorStatusReporter>.Instance,
|
|
staleAfter: TimeSpan.FromHours(1), time: new FixedTime(now));
|
|
|
|
await reporter.ProbeAsync();
|
|
|
|
Assert.Equal(["node-a"], collector.CollectReport("site-a").SecretMirrorStatuses!.Select(s => s.NodeName));
|
|
Assert.Equal(["node-a"], (await _storage.GetSecretMirrorStatusesAsync()).Select(s => s.NodeName));
|
|
}
|
|
|
|
/// <summary>Verifies that a row exactly at the stale cutoff is kept.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Probe_RowExactlyAtTheCutoff_IsKept()
|
|
{
|
|
var now = new DateTimeOffset(2026, 9, 29, 12, 0, 0, TimeSpan.Zero);
|
|
await _storage.SetSecretMirrorStatusAsync("node-b", [], now.AddHours(-1));
|
|
var collector = new SiteHealthCollector();
|
|
var reporter = new SecretMirrorStatusReporter(
|
|
collector, _storage, NullLogger<SecretMirrorStatusReporter>.Instance,
|
|
staleAfter: TimeSpan.FromHours(1), time: new FixedTime(now));
|
|
|
|
await reporter.ProbeAsync();
|
|
|
|
Assert.Single(collector.CollectReport("site-a").SecretMirrorStatuses!);
|
|
Assert.Single(await _storage.GetSecretMirrorStatusesAsync());
|
|
}
|
|
|
|
/// <summary>Verifies that a probe prunes nothing during the start-up grace period and prunes once it has passed.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Probe_DuringTheStartUpGrace_PrunesNothing_ThenPrunesAfterIt()
|
|
{
|
|
var time = new MutableTime(new DateTimeOffset(2026, 9, 29, 12, 0, 0, TimeSpan.Zero));
|
|
await _storage.SetSecretMirrorStatusAsync("node-b", [], time.Now.AddHours(-2));
|
|
var collector = new SiteHealthCollector();
|
|
var reporter = new SecretMirrorStatusReporter(
|
|
collector, _storage, NullLogger<SecretMirrorStatusReporter>.Instance,
|
|
staleAfter: TimeSpan.FromHours(1), time: time, pruneGrace: TimeSpan.FromMinutes(10));
|
|
|
|
time.Now = time.Now.AddMinutes(9);
|
|
await reporter.ProbeAsync();
|
|
Assert.Single(collector.CollectReport("site-a").SecretMirrorStatuses!);
|
|
Assert.Single(await _storage.GetSecretMirrorStatusesAsync());
|
|
|
|
time.Now = time.Now.AddMinutes(1);
|
|
await reporter.ProbeAsync();
|
|
Assert.Empty(collector.CollectReport("site-a").SecretMirrorStatuses!);
|
|
Assert.Empty(await _storage.GetSecretMirrorStatusesAsync());
|
|
}
|
|
|
|
/// <summary>Verifies that a conditional delete does not remove a row the node rewrote after it was read.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ConditionalDelete_DoesNotRemoveARowTheNodeRewroteAfterTheRead()
|
|
{
|
|
// The reporter read a stale checked_at, but the node came back and re-wrote its row
|
|
// before the delete ran: the fresh row must survive.
|
|
var stale = new DateTimeOffset(2026, 9, 29, 9, 0, 0, TimeSpan.Zero);
|
|
await _storage.SetSecretMirrorStatusAsync("node-b", [], stale);
|
|
await _storage.SetSecretMirrorStatusAsync("node-b", [], stale.AddHours(3));
|
|
|
|
Assert.False(await _storage.DeleteSecretMirrorStatusIfUnchangedAsync("node-b", stale));
|
|
Assert.Single(await _storage.GetSecretMirrorStatusesAsync());
|
|
Assert.True(await _storage.DeleteSecretMirrorStatusIfUnchangedAsync("node-b", stale.AddHours(3)));
|
|
Assert.Empty(await _storage.GetSecretMirrorStatusesAsync());
|
|
}
|
|
|
|
private sealed class MutableTime(DateTimeOffset now) : TimeProvider
|
|
{
|
|
/// <summary>Gets or sets the instant that the clock reports.</summary>
|
|
public DateTimeOffset Now { get; set; } = now;
|
|
/// <inheritdoc />
|
|
public override DateTimeOffset GetUtcNow() => Now;
|
|
}
|
|
|
|
private sealed class FixedTime(DateTimeOffset now) : TimeProvider
|
|
{
|
|
/// <inheritdoc />
|
|
public override DateTimeOffset GetUtcNow() => now;
|
|
}
|
|
}
|