Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests/AlarmCommandVerdictTests.cs
T
Joseph Doherty 09aaabbeef fix(alarms): AdminUI /alerts ack/shelve reports the scripted engine's verdict (#950)
The /alerts buttons went central->node fire-and-forget and always showed
"Ack sent". AdminOperationsActor now spawns a per-request
AlarmCommandVerdictCollector (7 s window, under the client's 10 s ask) that
sends the command with AlarmCommand.VerdictRequested and gathers each node's
answer, finishing early once every node it sent to has answered.

- gRPC plane: CentralCommunicationActor tells the collector the dialled
  ClusterNode set and one verdict per node. A node handles a
  verdict_requested AlarmCommandMessage by asking its driver host
  (ScriptedAlarmHostActor.ExecuteAlarmCommand, the #937 path, no ask deadline,
  bounded 5 s) and returns it in the ADDITIVE ExecuteAck.alarm_verdict.
  Transport failure / timeout / empty ack (pre-#950 node) => that node unknown.
- DPS: published with the collector as sender; each ScriptedAlarmHostActor
  answers (non-owners NotOwned); expected set = Up driver members.
- Aggregation: any owner applied => Applied (partial noted); else any
  unknown/silent node => Outcome unknown (never "refused"); else owner
  refusal => Refused: <reason>; else no owner => Not applied.
- UI: green Applied / red Refused (reason) / amber Outcome unknown, per-node
  detail in the details pane; a verdict-less reply (validation, server fault,
  pre-#950 singleton) renders as before.

Wire is back-compatible both ways (new proto fields only; VerdictRequested and
NotOwned default false). Real-wire test through the real router, dialer,
interceptor and NodeCommandGrpcService (red without the node change).

Co-Authored-By: Claude <noreply@anthropic.com>
2026-12-01 19:45:17 -05:00

253 lines
12 KiB
C#

using Akka.Actor;
using Akka.Cluster.Tools.PublishSubscribe;
using Akka.TestKit;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Cluster;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Mesh;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
using ZB.MOM.WW.OtOpcUa.Commons.Types;
using ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations;
using ZB.MOM.WW.OtOpcUa.ControlPlane.Communication;
using ZB.MOM.WW.OtOpcUa.ControlPlane.Tests.Harness;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.ControlPlane.Tests;
/// <summary>
/// #950 — the AdminUI <c>/alerts</c> ack/shelve reports the scripted-alarm engine's verdict instead of "sent": the
/// per-request <see cref="AlarmCommandVerdictCollector"/>, the fleet aggregation, and both router legs.
/// </summary>
public sealed class AlarmCommandVerdictTests : ControlPlaneActorTestBase
{
private static readonly TimeSpan Window = TimeSpan.FromSeconds(7);
private static AlarmCommandOutcome Applied => new("alarm-1", "Acknowledge", AlarmCommandVerdict.Applied);
private static AlarmCommandOutcome Refused(string reason) =>
new("alarm-1", "Acknowledge", AlarmCommandVerdict.NotApplied, reason);
private static AlarmCommandOutcome NotOwned =>
new("alarm-1", "Acknowledge", AlarmCommandVerdict.NotApplied, "not loaded", NotOwned: true);
private static AlarmCommandOutcome Unknown => new("alarm-1", "Acknowledge", AlarmCommandVerdict.OutcomeUnknown, "store stalled", TimedOut: true);
private static AlarmCommandFleetOutcome Aggregate(
Dictionary<string, AlarmCommandOutcome> answers, string[]? unanswered = null, bool expectedKnown = true,
string? notDelivered = null) =>
AlarmCommandVerdictAggregator.Aggregate(answers, unanswered ?? [], expectedKnown, notDelivered, Window);
// ------------------------------------------------------------------ aggregation
[Fact]
public void Every_owner_applied_is_Applied()
{
var o = Aggregate(new() { ["a1"] = Applied, ["a2"] = Applied, ["b1"] = NotOwned });
o.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
o.Summary.ShouldBe("Applied");
o.Detail.ShouldContain("b1: does not own this alarm.");
}
[Fact]
public void One_owner_applied_and_its_peer_refused_is_a_partial_Applied()
{
var o = Aggregate(new() { ["a1"] = Applied, ["a2"] = Refused("last load failed") });
o.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
o.Summary.ShouldBe("Applied on 1 of 2 servers");
o.Detail.ShouldContain("a2: refused — last load failed");
}
[Fact]
public void An_owner_refusal_with_no_applier_and_no_unknown_is_Refused_with_its_reason()
{
var o = Aggregate(new() { ["a1"] = Refused("last load failed"), ["b1"] = NotOwned });
o.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
o.Summary.ShouldBe("Refused: last load failed");
}
[Fact]
public void A_refusal_beside_an_unknown_is_unknown_never_refused()
{
// The unknown node may still apply it: claiming "refused" would be the exact lie #937 removed from the OPC UA path.
var o = Aggregate(new() { ["a1"] = Refused("last load failed"), ["a2"] = Unknown });
o.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
o.Summary.ShouldBe("Outcome unknown");
}
[Fact]
public void A_silent_expected_node_is_unknown()
{
var o = Aggregate(new() { ["b1"] = NotOwned }, unanswered: ["a1"]);
o.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
o.Detail.ShouldContain("a1: no answer within 7 s");
}
[Fact]
public void No_owner_anywhere_is_NotApplied()
{
var o = Aggregate(new() { ["a1"] = NotOwned, ["b1"] = NotOwned });
o.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
o.Summary.ShouldBe("Not applied: no server owns this alarm");
}
[Fact]
public void No_owner_among_answers_from_an_unknown_node_set_is_unknown()
{
// DPS with no readable membership: a silent pre-#950 owner is indistinguishable from no owner.
var o = Aggregate(new() { ["b1"] = NotOwned }, expectedKnown: false);
o.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
}
[Fact]
public void A_command_that_reached_no_node_is_NotApplied_Not_delivered()
{
var o = Aggregate(new(), notDelivered: "no enabled, non-maintenance ClusterNode row carries a GrpcPort.");
o.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
o.Summary.ShouldBe("Not delivered");
o.Detail.ShouldContain("GrpcPort");
}
// ------------------------------------------------------------------ collector, via a router
private (IActorRef Admin, TestProbe Router) WireAdmin(TimeSpan? window = null)
{
var router = CreateTestProbe("router");
var admin = Sys.ActorOf(AdminOperationsActor.Props(
NewInMemoryDbFactory(), CreateTestProbe().Ref, Enumerable.Empty<IDriverProbe>(),
TagConfigValidationMode.Warn, router.Ref, alarmVerdictWindow: window ?? TimeSpan.FromSeconds(10)));
return (admin, router);
}
[Fact]
public void Ack_reports_Applied_as_soon_as_every_dialled_node_has_answered()
{
var (admin, router) = WireAdmin();
var correlation = CorrelationId.NewId();
admin.Tell(new AcknowledgeAlarmCommand("alarm-1", "alice", null, correlation));
var sent = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
sent.Topic.ShouldBe(AlarmCommandsTopic.Name);
sent.Message.ShouldBeOfType<AlarmCommand>().VerdictRequested.ShouldBeTrue();
var collector = sent.VerdictReplyTo.ShouldNotBeNull();
collector.Tell(new AlarmVerdictFanOut(["site-a-1", "site-a-2", "main-1"]));
collector.Tell(new AlarmNodeVerdict("site-a-1", Applied));
collector.Tell(new AlarmNodeVerdict("main-1", NotOwned));
ExpectNoMsg(TimeSpan.FromMilliseconds(200), TestContext.Current.CancellationToken); // site-a-2 still owed
collector.Tell(new AlarmNodeVerdict("site-a-2", Applied));
// Well inside the 10 s window: the reply does not wait it out once everyone answered.
var reply = ExpectMsg<AcknowledgeAlarmResult>(TimeSpan.FromSeconds(3), cancellationToken: TestContext.Current.CancellationToken);
reply.Ok.ShouldBeTrue();
reply.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
reply.Summary.ShouldBe("Applied");
reply.CorrelationId.ShouldBe(correlation);
}
[Fact]
public void Shelve_reports_the_owners_refusal_reason()
{
var (admin, router) = WireAdmin();
admin.Tell(new ShelveAlarmCommand("alarm-1", "alice", ShelveKind.OneShot, null, null, CorrelationId.NewId()));
var collector = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken).VerdictReplyTo!;
collector.Tell(new AlarmVerdictFanOut(["a1", "b1"]));
collector.Tell(new AlarmNodeVerdict("a1", Refused("The last configuration load failed; the command was not applied.")));
collector.Tell(new AlarmNodeVerdict("b1", NotOwned));
var reply = ExpectMsg<ShelveAlarmResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
reply.Ok.ShouldBeFalse();
reply.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
reply.Summary.ShouldBe("Refused: The last configuration load failed; the command was not applied.");
}
[Fact]
public void A_node_that_never_answers_makes_the_outcome_unknown_at_the_window()
{
var (admin, router) = WireAdmin(TimeSpan.FromSeconds(1));
admin.Tell(new AcknowledgeAlarmCommand("alarm-1", "alice", null, CorrelationId.NewId()));
var collector = router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken).VerdictReplyTo!;
collector.Tell(new AlarmVerdictFanOut(["a1", "a2"]));
collector.Tell(new AlarmNodeVerdict("a2", NotOwned));
var reply = ExpectMsg<AcknowledgeAlarmResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
reply.Ok.ShouldBeFalse();
reply.Verdict.ShouldBe(AlarmCommandVerdict.OutcomeUnknown);
reply.Message.ShouldNotBeNull().ShouldContain("a1: no answer within 1 s");
}
[Fact]
public void A_command_the_router_could_not_deliver_is_reported_at_once()
{
var (admin, router) = WireAdmin();
admin.Tell(new AcknowledgeAlarmCommand("alarm-1", "alice", null, CorrelationId.NewId()));
router.ExpectMsg<MeshCommand>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.VerdictReplyTo!.Tell(new AlarmVerdictFanOut([], "no enabled, non-maintenance ClusterNode row carries a GrpcPort."));
var reply = ExpectMsg<AcknowledgeAlarmResult>(TimeSpan.FromSeconds(3), cancellationToken: TestContext.Current.CancellationToken);
reply.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
reply.Summary.ShouldBe("Not delivered");
}
// ------------------------------------------------------------------ DistributedPubSub legs
/// <summary>Subscribes a probe that plays a node's ScriptedAlarmHostActor: answers <paramref name="answer"/> to the publish's sender.</summary>
private TestProbe SubscribeAnsweringHost(Func<AlarmCommand, AlarmCommandOutcome?> answer)
{
var host = CreateTestProbe("host");
host.SetAutoPilot(new DelegateAutoPilot((sender, msg) =>
{
if (msg is AlarmCommand cmd && answer(cmd) is { } outcome) sender.Tell(outcome, host.Ref);
return AutoPilot.KeepRunning;
}));
var mediator = DistributedPubSub.Get(Sys).Mediator;
var subscriber = CreateTestProbe();
mediator.Tell(new Subscribe(AlarmCommandsTopic.Name, host.Ref), subscriber.Ref);
subscriber.ExpectMsg<SubscribeAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
return host;
}
[Fact]
public void With_no_router_the_publish_carries_the_collector_as_sender_and_the_hosts_answer_counts()
{
AlarmCommand? seen = null;
SubscribeAnsweringHost(cmd =>
{
seen = cmd;
return cmd.VerdictRequested ? Applied : null;
});
var admin = Sys.ActorOf(AdminOperationsActor.Props(
NewInMemoryDbFactory(), CreateTestProbe().Ref, Enumerable.Empty<IDriverProbe>(),
alarmVerdictWindow: TimeSpan.FromSeconds(1)));
admin.Tell(new AcknowledgeAlarmCommand("alarm-1", "alice", null, CorrelationId.NewId()));
var reply = ExpectMsg<AcknowledgeAlarmResult>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
reply.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
seen.ShouldNotBeNull().VerdictRequested.ShouldBeTrue();
}
[Fact]
public void Dps_router_publishes_with_the_collector_as_sender()
{
SubscribeAnsweringHost(cmd => cmd.VerdictRequested ? Refused("nope") : null);
var router = Sys.ActorOf(CentralCommunicationActor.Props(
NewInMemoryDbFactory(), new MeshTransportOptions(), () => null));
var collector = CreateTestProbe("collector");
router.Tell(new MeshCommand(AlarmCommandsTopic.Name,
new AlarmCommand("alarm-1", "Acknowledge", "alice", null, null, VerdictRequested: true), collector.Ref));
collector.ExpectMsg<AlarmVerdictFanOut>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.NotDeliveredReason.ShouldBeNull();
collector.ExpectMsg<AlarmCommandOutcome>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.Reason.ShouldBe("nope");
}
}