f02067d295
The tests probe a free port (bind 0, release) and start the SDK server later; under a highly parallel run another test can take the port in between and the SDK fails with 'Failed to establish tcp listener sockets for Ipv4 and IPv6.'. OpcUaHostStart.StartAsync builds a fresh host + server on a fresh port per attempt (up to 5) when the failure is a bind failure; all 45 start sites in OpcUaServer.Tests, OpcUaServer.IntegrationTests and Runtime.Tests use it. Per-attempt GUID ApplicationUris are hoisted out of the host factory because a changed URI against a reused PKI store fails with 'Cannot access private key'. Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
313 lines
15 KiB
C#
313 lines
15 KiB
C#
using Opc.Ua;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
|
using Shouldly;
|
|
using Xunit;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
|
|
|
|
/// <summary>
|
|
/// R2-07 Phase 2 (T8/T9/T10) — surgical IN-PLACE removal on the node manager: a single value variable
|
|
/// (<see cref="OtOpcUaNodeManager.RemoveVariableNode"/>), a single Part 9 alarm condition
|
|
/// (<see cref="OtOpcUaNodeManager.RemoveAlarmConditionNode"/>), and an equipment folder + its whole
|
|
/// descendant subtree with notifier demotion (<see cref="OtOpcUaNodeManager.RemoveEquipmentSubtree"/>).
|
|
/// Each removal detaches only the scoped node(s), cleans the matching maps, and raises a Part 3
|
|
/// NodeDeleted model-change; an unknown id returns false (the caller falls back to a full rebuild).
|
|
/// </summary>
|
|
public sealed class NodeManagerSurgicalRemoveTests : IDisposable
|
|
{
|
|
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
|
|
|
private readonly string _pkiRoot = Path.Combine(
|
|
Path.GetTempPath(),
|
|
$"otopcua-surgical-remove-{Guid.NewGuid():N}");
|
|
|
|
// ---------- T8: RemoveVariableNode ----------
|
|
|
|
/// <summary>Ensure a variable then remove it: it disappears from the maps (TryGetVariable null,
|
|
/// VariableCount decremented), its historized-tagname registration is dropped, and the call returns
|
|
/// true.</summary>
|
|
[Trait("Category", "Unit")]
|
|
[Fact]
|
|
public async Task RemoveVariableNode_drops_variable_and_historian_registration()
|
|
{
|
|
var (host, server) = await BootAsync();
|
|
var nm = server.NodeManager!;
|
|
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1", realm: AddressSpaceRealm.Uns);
|
|
nm.EnsureVariable("eq-1/A", "eq-1", "A", "Float", writable: false, historianTagname: "Hist.A", realm: AddressSpaceRealm.Uns);
|
|
nm.EnsureVariable("eq-1/B", "eq-1", "B", "Float", writable: false, realm: AddressSpaceRealm.Uns);
|
|
var countBefore = nm.VariableCount;
|
|
nm.TryGetHistorizedTagname("eq-1/A", out _).ShouldBeTrue();
|
|
|
|
nm.RemoveVariableNode("eq-1/A", realm: AddressSpaceRealm.Uns).ShouldBeTrue();
|
|
|
|
nm.TryGetVariable("eq-1/A").ShouldBeNull();
|
|
nm.VariableCount.ShouldBe(countBefore - 1);
|
|
nm.TryGetHistorizedTagname("eq-1/A", out _).ShouldBeFalse();
|
|
// The sibling variable is untouched.
|
|
nm.TryGetVariable("eq-1/B").ShouldNotBeNull();
|
|
|
|
await host.DisposeAsync();
|
|
}
|
|
|
|
/// <summary>Removing an unknown variable id returns false (map drift ⇒ caller rebuilds).</summary>
|
|
[Trait("Category", "Unit")]
|
|
[Fact]
|
|
public async Task RemoveVariableNode_unknown_id_returns_false()
|
|
{
|
|
var (host, server) = await BootAsync();
|
|
var nm = server.NodeManager!;
|
|
|
|
nm.RemoveVariableNode("eq-1/nope", realm: AddressSpaceRealm.Uns).ShouldBeFalse();
|
|
|
|
await host.DisposeAsync();
|
|
}
|
|
|
|
/// <summary>The built removed-node event announces the deleted node with verb NodeDeleted.</summary>
|
|
[Trait("Category", "Unit")]
|
|
[Fact]
|
|
public async Task Built_removed_event_announces_the_deleted_node_with_NodeDeleted_verb()
|
|
{
|
|
var (host, server) = await BootAsync();
|
|
var nm = server.NodeManager!;
|
|
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1", realm: AddressSpaceRealm.Uns);
|
|
nm.EnsureVariable("eq-1/A", "eq-1", "A", "Float", writable: false, realm: AddressSpaceRealm.Uns);
|
|
|
|
var e = nm.BuildNodesRemovedModelChange("eq-1/A");
|
|
|
|
e.ShouldNotBeNull();
|
|
e.Changes.ShouldNotBeNull();
|
|
var changes = e.Changes.Value;
|
|
changes.Length.ShouldBe(1);
|
|
changes[0].Verb.ShouldBe((byte)ModelChangeStructureVerbMask.NodeDeleted);
|
|
|
|
await host.DisposeAsync();
|
|
}
|
|
|
|
// ---------- T9: RemoveAlarmConditionNode ----------
|
|
|
|
/// <summary>Materialise a native alarm condition then remove it: it disappears from the condition map and
|
|
/// the native flag is cleared; unknown id returns false.</summary>
|
|
[Trait("Category", "Unit")]
|
|
[Fact]
|
|
public async Task RemoveAlarmConditionNode_drops_condition_and_native_flag()
|
|
{
|
|
var (host, server) = await BootAsync();
|
|
var nm = server.NodeManager!;
|
|
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1", realm: AddressSpaceRealm.Uns);
|
|
nm.MaterialiseAlarmCondition("eq-1/OverTemp", "eq-1", "OverTemp", "OffNormalAlarm", 700, isNative: true, realm: AddressSpaceRealm.Uns);
|
|
nm.TryGetAlarmCondition("eq-1/OverTemp").ShouldNotBeNull();
|
|
nm.IsNativeAlarmNode("eq-1/OverTemp").ShouldBeTrue();
|
|
|
|
nm.RemoveAlarmConditionNode("eq-1/OverTemp", realm: AddressSpaceRealm.Uns).ShouldBeTrue();
|
|
|
|
nm.TryGetAlarmCondition("eq-1/OverTemp").ShouldBeNull();
|
|
nm.IsNativeAlarmNode("eq-1/OverTemp").ShouldBeFalse();
|
|
|
|
nm.RemoveAlarmConditionNode("eq-1/OverTemp", realm: AddressSpaceRealm.Uns).ShouldBeFalse(); // already gone ⇒ false
|
|
|
|
await host.DisposeAsync();
|
|
}
|
|
|
|
/// <summary>A scripted condition removes cleanly too (no native flag was set).</summary>
|
|
[Trait("Category", "Unit")]
|
|
[Fact]
|
|
public async Task RemoveAlarmConditionNode_removes_scripted_condition()
|
|
{
|
|
var (host, server) = await BootAsync();
|
|
var nm = server.NodeManager!;
|
|
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1", realm: AddressSpaceRealm.Uns);
|
|
nm.MaterialiseAlarmCondition("alm-1", "eq-1", "HighTemp", "OffNormalAlarm", 500, isNative: false, realm: AddressSpaceRealm.Uns);
|
|
|
|
nm.RemoveAlarmConditionNode("alm-1", realm: AddressSpaceRealm.Uns).ShouldBeTrue();
|
|
nm.TryGetAlarmCondition("alm-1").ShouldBeNull();
|
|
|
|
await host.DisposeAsync();
|
|
}
|
|
|
|
// ---------- T10: RemoveEquipmentSubtree ----------
|
|
|
|
/// <summary>Remove an equipment folder carrying a sub-folder, variables (one historized), and a condition:
|
|
/// every descendant disappears from every map, the notifier registration is demoted, and a SIBLING
|
|
/// equipment is fully intact.</summary>
|
|
[Trait("Category", "Unit")]
|
|
[Fact]
|
|
public async Task RemoveEquipmentSubtree_removes_all_descendants_and_leaves_siblings_intact()
|
|
{
|
|
var (host, server) = await BootAsync();
|
|
var nm = server.NodeManager!;
|
|
|
|
// Target equipment eq-1 with a sub-folder, two variables (one historized), and a native condition.
|
|
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1", realm: AddressSpaceRealm.Uns);
|
|
nm.EnsureFolder("eq-1/Diag", parentNodeId: "eq-1", displayName: "Diag", realm: AddressSpaceRealm.Uns);
|
|
nm.EnsureVariable("eq-1/A", "eq-1", "A", "Float", writable: false, historianTagname: "Hist.A", realm: AddressSpaceRealm.Uns);
|
|
nm.EnsureVariable("eq-1/Diag/T", "eq-1/Diag", "T", "Float", writable: false, realm: AddressSpaceRealm.Uns);
|
|
nm.MaterialiseAlarmCondition("eq-1/OverTemp", "eq-1", "OverTemp", "OffNormalAlarm", 700, isNative: true, realm: AddressSpaceRealm.Uns);
|
|
|
|
// Sibling equipment eq-2 that must survive untouched.
|
|
nm.EnsureFolder("eq-2", parentNodeId: null, displayName: "Equipment 2", realm: AddressSpaceRealm.Uns);
|
|
nm.EnsureVariable("eq-2/S", "eq-2", "S", "Float", writable: false, realm: AddressSpaceRealm.Uns);
|
|
|
|
nm.RemoveEquipmentSubtree("eq-1", realm: AddressSpaceRealm.Uns).ShouldBeTrue();
|
|
|
|
// Every eq-1 descendant is gone from every map.
|
|
nm.TryGetFolder("eq-1").ShouldBeNull();
|
|
nm.TryGetFolder("eq-1/Diag").ShouldBeNull();
|
|
nm.TryGetVariable("eq-1/A").ShouldBeNull();
|
|
nm.TryGetVariable("eq-1/Diag/T").ShouldBeNull();
|
|
nm.TryGetAlarmCondition("eq-1/OverTemp").ShouldBeNull();
|
|
nm.TryGetHistorizedTagname("eq-1/A", out _).ShouldBeFalse();
|
|
nm.IsNativeAlarmNode("eq-1/OverTemp").ShouldBeFalse();
|
|
|
|
// Sibling eq-2 is fully intact.
|
|
nm.TryGetFolder("eq-2").ShouldNotBeNull();
|
|
nm.TryGetVariable("eq-2/S").ShouldNotBeNull();
|
|
|
|
// Re-materialising an alarm under eq-2 still works (the notifier machinery was not corrupted by the
|
|
// eq-1 demotion) — proves no orphaned root-notifier ref broke the event path.
|
|
Should.NotThrow(() => nm.MaterialiseAlarmCondition("eq-2/Alm", "eq-2", "Alm", "OffNormalAlarm", 300, isNative: false, realm: AddressSpaceRealm.Uns));
|
|
|
|
await host.DisposeAsync();
|
|
}
|
|
|
|
/// <summary>Removing an unknown equipment id returns false (map drift ⇒ caller rebuilds).</summary>
|
|
[Trait("Category", "Unit")]
|
|
[Fact]
|
|
public async Task RemoveEquipmentSubtree_unknown_id_returns_false()
|
|
{
|
|
var (host, server) = await BootAsync();
|
|
var nm = server.NodeManager!;
|
|
|
|
nm.RemoveEquipmentSubtree("eq-nope", realm: AddressSpaceRealm.Uns).ShouldBeFalse();
|
|
|
|
await host.DisposeAsync();
|
|
}
|
|
|
|
// ---------- Gitea #599 (archreview 03/P5 + 03/S20): equipment-with-references removal ----------
|
|
|
|
/// <summary>
|
|
/// S20 — the subtree removal severs the inverse half of every cross-tree <c>Organizes</c> edge a removed
|
|
/// variable holds. The raw node survives a UNS-scoped removal, so without this it keeps a browsable,
|
|
/// unresolvable reference to a node that no longer exists — the hazard <c>RemoveVariableNode</c> was
|
|
/// already fixed for (2026-08-15 review I3), left open on the subtree path.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
[Fact]
|
|
public async Task RemoveEquipmentSubtree_severs_the_inverse_Organizes_edge_on_the_surviving_raw_node()
|
|
{
|
|
var (host, server) = await BootAsync();
|
|
try
|
|
{
|
|
var nm = server.NodeManager!;
|
|
nm.EnsureVariable("Plant/Modbus/dev1/temp", null, "temp", "Float", writable: false, realm: AddressSpaceRealm.Raw);
|
|
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1", realm: AddressSpaceRealm.Uns);
|
|
nm.EnsureVariable("eq-1/Temp", "eq-1", "Temp", "Float", writable: false, realm: AddressSpaceRealm.Uns);
|
|
nm.AddReference("eq-1/Temp", AddressSpaceRealm.Uns, "Plant/Modbus/dev1/temp", AddressSpaceRealm.Raw);
|
|
var mirror = nm.TryGetVariable("eq-1/Temp", AddressSpaceRealm.Uns)!.NodeId;
|
|
InverseOrganizesTo(nm, mirror).ShouldBeTrue(); // precondition: the edge really is there
|
|
|
|
nm.RemoveEquipmentSubtree("eq-1", AddressSpaceRealm.Uns).ShouldBeTrue();
|
|
|
|
nm.TryGetVariable("Plant/Modbus/dev1/temp", AddressSpaceRealm.Raw).ShouldNotBeNull();
|
|
InverseOrganizesTo(nm, mirror).ShouldBeFalse();
|
|
}
|
|
finally
|
|
{
|
|
await host.DisposeAsync();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// P5 — removing an equipment that holds UNS references is a scoped in-place removal, NOT a full rebuild,
|
|
/// and leaves the surviving raw node with no dangling inverse edge. Run for both id shapes: a reference
|
|
/// whose NodeId sits under the equipment id (so the subtree removal reaches it — before the fix the
|
|
/// reference loop then found it gone and ratcheted to a rebuild, severing every subscription on the
|
|
/// server), and the production shape, an <c>Area/Line/Equipment/Name</c> NodeId parented to an
|
|
/// id-named equipment folder (which the subtree's id-prefix scope does not reach).
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
[Theory]
|
|
[InlineData("eq-1/Temp")]
|
|
[InlineData("Area/Line/Eq/Temp")]
|
|
public async Task Removing_equipment_with_references_is_surgical_and_leaves_no_dangling_edge(string mirrorId)
|
|
{
|
|
var (host, server) = await BootAsync();
|
|
try
|
|
{
|
|
var nm = server.NodeManager!;
|
|
nm.EnsureVariable("Plant/Modbus/dev1/temp", null, "temp", "Float", writable: false, realm: AddressSpaceRealm.Raw);
|
|
nm.EnsureFolder("eq-1", parentNodeId: null, displayName: "Equipment 1", realm: AddressSpaceRealm.Uns);
|
|
nm.EnsureVariable(mirrorId, "eq-1", "Temp", "Float", writable: false, realm: AddressSpaceRealm.Uns);
|
|
nm.AddReference(mirrorId, AddressSpaceRealm.Uns, "Plant/Modbus/dev1/temp", AddressSpaceRealm.Raw);
|
|
// A bystander whose value must survive (a rebuild would drop it).
|
|
nm.EnsureFolder("eq-2", parentNodeId: null, displayName: "Equipment 2", realm: AddressSpaceRealm.Uns);
|
|
nm.EnsureVariable("eq-2/S", "eq-2", "S", "Float", writable: false, realm: AddressSpaceRealm.Uns);
|
|
var mirror = nm.TryGetVariable(mirrorId, AddressSpaceRealm.Uns)!.NodeId;
|
|
|
|
var plan = new AddressSpacePlan(
|
|
AddedEquipment: Array.Empty<EquipmentNode>(),
|
|
RemovedEquipment: new[] { new EquipmentNode("eq-1", "Equipment 1", "") },
|
|
ChangedEquipment: Array.Empty<AddressSpacePlan.EquipmentDelta>(),
|
|
AddedDrivers: Array.Empty<DriverInstancePlan>(),
|
|
RemovedDrivers: Array.Empty<DriverInstancePlan>(),
|
|
ChangedDrivers: Array.Empty<AddressSpacePlan.DriverDelta>(),
|
|
AddedAlarms: Array.Empty<ScriptedAlarmPlan>(),
|
|
RemovedAlarms: Array.Empty<ScriptedAlarmPlan>(),
|
|
ChangedAlarms: Array.Empty<AddressSpacePlan.AlarmDelta>())
|
|
{
|
|
RemovedUnsReferenceVariables = new[]
|
|
{
|
|
new UnsReferenceVariable("ref-1", "eq-1", mirrorId, "Temp", "Plant/Modbus/dev1/temp", "Float", Writable: false),
|
|
},
|
|
};
|
|
|
|
var outcome = new AddressSpaceApplier(new SdkAddressSpaceSink(nm),
|
|
Microsoft.Extensions.Logging.Abstractions.NullLogger<AddressSpaceApplier>.Instance).Apply(plan);
|
|
|
|
outcome.RebuildCalled.ShouldBeFalse();
|
|
nm.TryGetVariable(mirrorId, AddressSpaceRealm.Uns).ShouldBeNull();
|
|
nm.TryGetFolder("eq-1").ShouldBeNull();
|
|
nm.TryGetVariable("eq-2/S", AddressSpaceRealm.Uns).ShouldNotBeNull(); // positive control: no rebuild
|
|
nm.TryGetVariable("Plant/Modbus/dev1/temp", AddressSpaceRealm.Raw).ShouldNotBeNull();
|
|
InverseOrganizesTo(nm, mirror).ShouldBeFalse();
|
|
}
|
|
finally
|
|
{
|
|
await host.DisposeAsync();
|
|
}
|
|
}
|
|
|
|
private static bool InverseOrganizesTo(OtOpcUaNodeManager nm, NodeId mirror) =>
|
|
nm.GetNodeReferences("Plant/Modbus/dev1/temp", AddressSpaceRealm.Raw)
|
|
.Any(r => r.ReferenceTypeId == ReferenceTypeIds.Organizes && r.IsInverse && r.TargetId == mirror);
|
|
|
|
private async Task<(OpcUaApplicationHost Host, OtOpcUaSdkServer Server)> BootAsync()
|
|
{
|
|
var applicationUri = $"urn:OtOpcUa.SurgicalRemoveTest:{Guid.NewGuid():N}";
|
|
var started = await OpcUaHostStart.StartAsync(
|
|
port => new OpcUaApplicationHost(
|
|
new OpcUaApplicationHostOptions
|
|
{
|
|
ApplicationName = "OtOpcUa.SurgicalRemoveTest",
|
|
ApplicationUri = applicationUri,
|
|
OpcUaPort = port,
|
|
PublicHostname = "localhost",
|
|
PkiStoreRoot = _pkiRoot,
|
|
},
|
|
Microsoft.Extensions.Logging.Abstractions.NullLogger<OpcUaApplicationHost>.Instance),
|
|
() => new OtOpcUaSdkServer(), Ct);
|
|
var host = started.Host;
|
|
var server = started.Server;
|
|
return (host, server);
|
|
}
|
|
|
|
/// <summary>Cleans up the PKI root directory.</summary>
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(_pkiRoot))
|
|
{
|
|
try { Directory.Delete(_pkiRoot, recursive: true); }
|
|
catch { /* best-effort cleanup */ }
|
|
}
|
|
}
|
|
}
|