6c4d29902e
v2-ci / unit-tests (push) Has been cancelled
#899 — EnsureTags is an upsert, so binding a node to an existing AVEVA tag via historianTagname re-typed/re-described it on every add and host start. - TagConfig `historianReadOnly: true` (TagConfigIntent -> RawTagPlan.HistorianReadOnly): HistoryRead from an existing historian tag that OtOpcUa never provisions and never records into (AddressSpaceApplier.IsOwnedHistorizedValue gates both the EnsureTags hook and the recorder feed). Node-inert: surgical, not mirrored on UNS refs, in the RawTagPlan equality/hash. A non-bool value is the deploy error HistorianReadOnlyInvalid. AdminUI: CSV column HistorianReadOnly (strict), TagHistorizeConfig reads via the runtime parse. - ServerHistorian:ProvisionTags = CreateOrUpdate (default, unchanged) | CreateOnly (Gateway only: TagExistsAsync first, existing tags left alone, failed lookup not sent) | Off. Unknown value / CreateOnly on a SQL backend fail host start. - Default for an explicit historianTagname stays OWNED: overrides pin owned history across renames, a read-only default would silently stop provisioning + recording. #898 — the hook parsed Tag.DataType (an OPC UA name) as DriverDataType, so historized Float/Double/Byte/SByte tags were never provisioned. New shared HistorianDataTypeNames.TryResolve: Float->Float32, Double->Float64, Byte/SByte->Int16 (lossless, provisioned as Int2); unmapped names logged at Information. Gateway now skips Int64/UInt64 (HistorianGateway cannot create Int8/UInt8: lossless-cast-or-reject). UPGRADE: the first host start after this provisions (upserts) every existing historized Float/Double/Byte/SByte tag. Mark foreign-tag bindings historianReadOnly, or set ServerHistorian:ProvisionTags=CreateOnly/Off, before upgrading (docs/Historian.md). Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
320 lines
17 KiB
C#
320 lines
17 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
|
|
|
|
/// <summary>
|
|
/// v3 Batch 4 (B4-WP1) — the planner diffs per realm. Raw containers + raw tags diff by RawPath
|
|
/// (NodeId), so a rename is remove(old)+add(new); UNS reference variables diff by the stable
|
|
/// <c>UnsTagReferenceId</c>, so a backing-tag rename is a re-point (BackingRawPath moves, NodeId stable)
|
|
/// and a display-name-override edit is a UNS-only change with no raw delta.
|
|
/// <para><b>The pinned invariant:</b> a raw-tag rename = remove+add in Raw AND re-point in UNS.</para>
|
|
/// </summary>
|
|
public sealed class AddressSpacePlannerDualNamespaceTests
|
|
{
|
|
private const string RawFolderName = "Plant";
|
|
private const string DriverName = "Modbus1";
|
|
private const string DeviceName = "PLC-A";
|
|
|
|
// Compose a fixture with one raw tag (given name) under Plant/Modbus1/PLC-A and one UNS reference to it
|
|
// (with an optional display-name override so the effective name can be held stable across a raw rename).
|
|
private static AddressSpaceComposition Compose(string tagName, string? displayOverride, string tagConfig = "{}")
|
|
{
|
|
var folder = new RawFolder { RawFolderId = "raw-plant", ClusterId = "c1", Name = RawFolderName };
|
|
var driver = new DriverInstance
|
|
{
|
|
DriverInstanceId = "drv-modbus", ClusterId = "c1", RawFolderId = "raw-plant",
|
|
Name = DriverName, DriverType = "Modbus", DriverConfig = "{}",
|
|
};
|
|
var device = new Device { DeviceId = "dev-1", DriverInstanceId = "drv-modbus", Name = DeviceName, DeviceConfig = "{}" };
|
|
var tag = new Tag
|
|
{
|
|
TagId = "t-speed", DeviceId = "dev-1", Name = tagName,
|
|
DataType = "Float", AccessLevel = TagAccessLevel.ReadWrite, TagConfig = tagConfig,
|
|
};
|
|
var area = new UnsArea { UnsAreaId = "area-1", ClusterId = "c1", Name = "filling" };
|
|
var line = new UnsLine { UnsLineId = "line-1", UnsAreaId = "area-1", Name = "line-1" };
|
|
var equip = new Equipment { EquipmentId = "eq-1", UnsLineId = "line-1", Name = "station-1", MachineCode = "STATION_001" };
|
|
var reference = new UnsTagReference
|
|
{
|
|
UnsTagReferenceId = "ref-1", EquipmentId = "eq-1", TagId = "t-speed",
|
|
DisplayNameOverride = displayOverride,
|
|
};
|
|
|
|
return AddressSpaceComposer.Compose(
|
|
new[] { area }, new[] { line }, new[] { equip },
|
|
new[] { driver }, Array.Empty<ScriptedAlarm>(),
|
|
unsTagReferences: new[] { reference },
|
|
tags: new[] { tag },
|
|
rawFolders: new[] { folder },
|
|
devices: new[] { device });
|
|
}
|
|
|
|
/// <summary>A newly-added raw tag surfaces in AddedRawTags (keyed by RawPath); the plan is non-empty.</summary>
|
|
[Fact]
|
|
public void Added_raw_tag_goes_to_AddedRawTags()
|
|
{
|
|
var prev = Compose("Speed", displayOverride: null);
|
|
var next = Compose("Speed", displayOverride: null);
|
|
// prev without the tag: strip RawTags/UNS by composing an empty raw side.
|
|
var empty = AddressSpaceComposer.Compose(
|
|
Array.Empty<UnsArea>(), Array.Empty<UnsLine>(), Array.Empty<Equipment>(),
|
|
Array.Empty<DriverInstance>(), Array.Empty<ScriptedAlarm>());
|
|
|
|
var plan = AddressSpacePlanner.Compute(empty, next);
|
|
|
|
plan.IsEmpty.ShouldBeFalse();
|
|
plan.AddedRawTags.Select(t => t.NodeId).ShouldContain("Plant/Modbus1/PLC-A/Speed");
|
|
plan.RemovedRawTags.ShouldBeEmpty();
|
|
_ = prev;
|
|
}
|
|
|
|
/// <summary>A disappeared raw tag surfaces in RemovedRawTags.</summary>
|
|
[Fact]
|
|
public void Removed_raw_tag_goes_to_RemovedRawTags()
|
|
{
|
|
var prev = Compose("Speed", displayOverride: null);
|
|
var empty = AddressSpaceComposer.Compose(
|
|
Array.Empty<UnsArea>(), Array.Empty<UnsLine>(), Array.Empty<Equipment>(),
|
|
Array.Empty<DriverInstance>(), Array.Empty<ScriptedAlarm>());
|
|
|
|
var plan = AddressSpacePlanner.Compute(prev, empty);
|
|
|
|
plan.RemovedRawTags.Select(t => t.NodeId).ShouldContain("Plant/Modbus1/PLC-A/Speed");
|
|
plan.AddedRawTags.ShouldBeEmpty();
|
|
}
|
|
|
|
/// <summary>A newly-added UNS reference surfaces in AddedUnsReferenceVariables.</summary>
|
|
[Fact]
|
|
public void Added_uns_reference_goes_to_AddedUnsReferenceVariables()
|
|
{
|
|
var empty = AddressSpaceComposer.Compose(
|
|
Array.Empty<UnsArea>(), Array.Empty<UnsLine>(), Array.Empty<Equipment>(),
|
|
Array.Empty<DriverInstance>(), Array.Empty<ScriptedAlarm>());
|
|
var next = Compose("Speed", displayOverride: null);
|
|
|
|
var plan = AddressSpacePlanner.Compute(empty, next);
|
|
|
|
plan.AddedUnsReferenceVariables.Single().UnsTagReferenceId.ShouldBe("ref-1");
|
|
plan.RemovedUnsReferenceVariables.ShouldBeEmpty();
|
|
}
|
|
|
|
/// <summary>A disappeared UNS reference surfaces in RemovedUnsReferenceVariables.</summary>
|
|
[Fact]
|
|
public void Removed_uns_reference_goes_to_RemovedUnsReferenceVariables()
|
|
{
|
|
var prev = Compose("Speed", displayOverride: null);
|
|
var empty = AddressSpaceComposer.Compose(
|
|
Array.Empty<UnsArea>(), Array.Empty<UnsLine>(), Array.Empty<Equipment>(),
|
|
Array.Empty<DriverInstance>(), Array.Empty<ScriptedAlarm>());
|
|
|
|
var plan = AddressSpacePlanner.Compute(prev, empty);
|
|
|
|
plan.RemovedUnsReferenceVariables.Single().UnsTagReferenceId.ShouldBe("ref-1");
|
|
plan.AddedUnsReferenceVariables.ShouldBeEmpty();
|
|
}
|
|
|
|
/// <summary><b>THE PIN:</b> a raw-tag rename = remove+add in Raw AND re-point in UNS. With a display-name
|
|
/// override holding the effective name stable, the reference row is unchanged (same UnsTagReferenceId,
|
|
/// same UNS NodeId) but its backing NodeId moved ⇒ the UNS variable's Organizes target (BackingRawPath)
|
|
/// re-points via a ChangedUnsReferenceVariables delta.</summary>
|
|
[Fact]
|
|
public void Raw_rename_is_remove_add_in_raw_and_repoint_in_uns()
|
|
{
|
|
var prev = Compose("Speed", displayOverride: "Spd");
|
|
var next = Compose("Velocity", displayOverride: "Spd"); // raw tag renamed; effective name held stable
|
|
|
|
var plan = AddressSpacePlanner.Compute(prev, next);
|
|
|
|
// Raw realm: remove the old RawPath, add the new one (immutable NodeId ⇒ not a Changed delta).
|
|
plan.RemovedRawTags.Select(t => t.NodeId).ShouldBe(new[] { "Plant/Modbus1/PLC-A/Speed" });
|
|
plan.AddedRawTags.Select(t => t.NodeId).ShouldBe(new[] { "Plant/Modbus1/PLC-A/Velocity" });
|
|
plan.ChangedRawTags.ShouldBeEmpty();
|
|
|
|
// UNS realm: the reference row is unchanged in identity + NodeId, but re-points to the new RawPath.
|
|
plan.AddedUnsReferenceVariables.ShouldBeEmpty();
|
|
plan.RemovedUnsReferenceVariables.ShouldBeEmpty();
|
|
var repoint = plan.ChangedUnsReferenceVariables.ShouldHaveSingleItem();
|
|
repoint.Previous.UnsTagReferenceId.ShouldBe("ref-1");
|
|
repoint.Current.UnsTagReferenceId.ShouldBe("ref-1");
|
|
repoint.Previous.NodeId.ShouldBe(repoint.Current.NodeId); // effective name stable ⇒ NodeId stable
|
|
repoint.Previous.BackingRawPath.ShouldBe("Plant/Modbus1/PLC-A/Speed");
|
|
repoint.Current.BackingRawPath.ShouldBe("Plant/Modbus1/PLC-A/Velocity");
|
|
|
|
// The raw container topology (Folder/Driver/Device) is untouched by a tag rename.
|
|
plan.AddedRawContainers.ShouldBeEmpty();
|
|
plan.RemovedRawContainers.ShouldBeEmpty();
|
|
plan.ChangedRawContainers.ShouldBeEmpty();
|
|
}
|
|
|
|
/// <summary>A display-name-override change is a UNS-only change (no raw delta): same reference row id,
|
|
/// the effective name + UNS NodeId move, the backing RawPath is unchanged.</summary>
|
|
[Fact]
|
|
public void Display_name_override_change_is_uns_only_no_raw_change()
|
|
{
|
|
var prev = Compose("Speed", displayOverride: "Spd");
|
|
var next = Compose("Speed", displayOverride: "Speedy"); // only the override changed
|
|
|
|
var plan = AddressSpacePlanner.Compute(prev, next);
|
|
|
|
// No raw-side change whatsoever.
|
|
plan.AddedRawTags.ShouldBeEmpty();
|
|
plan.RemovedRawTags.ShouldBeEmpty();
|
|
plan.ChangedRawTags.ShouldBeEmpty();
|
|
plan.AddedRawContainers.ShouldBeEmpty();
|
|
plan.RemovedRawContainers.ShouldBeEmpty();
|
|
plan.ChangedRawContainers.ShouldBeEmpty();
|
|
|
|
// UNS: same reference row id, effective name + NodeId changed, backing RawPath unchanged.
|
|
var delta = plan.ChangedUnsReferenceVariables.ShouldHaveSingleItem();
|
|
delta.Previous.UnsTagReferenceId.ShouldBe("ref-1");
|
|
delta.Previous.EffectiveName.ShouldBe("Spd");
|
|
delta.Current.EffectiveName.ShouldBe("Speedy");
|
|
delta.Previous.NodeId.ShouldNotBe(delta.Current.NodeId);
|
|
delta.Previous.BackingRawPath.ShouldBe(delta.Current.BackingRawPath);
|
|
plan.AddedUnsReferenceVariables.ShouldBeEmpty();
|
|
plan.RemovedUnsReferenceVariables.ShouldBeEmpty();
|
|
}
|
|
|
|
/// <summary>An attribute-only raw-tag edit (historize toggle) keeps the same RawPath ⇒ a
|
|
/// ChangedRawTags delta (not remove+add).</summary>
|
|
[Fact]
|
|
public void Raw_tag_attribute_edit_keeps_nodeid_and_routes_to_ChangedRawTags()
|
|
{
|
|
var prev = Compose("Speed", displayOverride: null);
|
|
// next: same identity + name, but toggle historize on the raw tag.
|
|
var folder = new RawFolder { RawFolderId = "raw-plant", ClusterId = "c1", Name = RawFolderName };
|
|
var driver = new DriverInstance
|
|
{
|
|
DriverInstanceId = "drv-modbus", ClusterId = "c1", RawFolderId = "raw-plant",
|
|
Name = DriverName, DriverType = "Modbus", DriverConfig = "{}",
|
|
};
|
|
var device = new Device { DeviceId = "dev-1", DriverInstanceId = "drv-modbus", Name = DeviceName, DeviceConfig = "{}" };
|
|
var tag = new Tag
|
|
{
|
|
TagId = "t-speed", DeviceId = "dev-1", Name = "Speed",
|
|
DataType = "Float", AccessLevel = TagAccessLevel.ReadWrite, TagConfig = "{\"isHistorized\":true}",
|
|
};
|
|
var next = AddressSpaceComposer.Compose(
|
|
Array.Empty<UnsArea>(), Array.Empty<UnsLine>(), Array.Empty<Equipment>(),
|
|
new[] { driver }, Array.Empty<ScriptedAlarm>(),
|
|
tags: new[] { tag }, rawFolders: new[] { folder }, devices: new[] { device });
|
|
|
|
var prevRawOnly = AddressSpaceComposer.Compose(
|
|
Array.Empty<UnsArea>(), Array.Empty<UnsLine>(), Array.Empty<Equipment>(),
|
|
new[] { driver }, Array.Empty<ScriptedAlarm>(),
|
|
tags: new[] { new Tag { TagId = "t-speed", DeviceId = "dev-1", Name = "Speed", DataType = "Float", AccessLevel = TagAccessLevel.ReadWrite, TagConfig = "{}" } },
|
|
rawFolders: new[] { folder }, devices: new[] { device });
|
|
|
|
var plan = AddressSpacePlanner.Compute(prevRawOnly, next);
|
|
|
|
plan.AddedRawTags.ShouldBeEmpty();
|
|
plan.RemovedRawTags.ShouldBeEmpty();
|
|
var changed = plan.ChangedRawTags.ShouldHaveSingleItem();
|
|
changed.Previous.NodeId.ShouldBe(changed.Current.NodeId);
|
|
changed.Previous.IsHistorized.ShouldBeFalse();
|
|
changed.Current.IsHistorized.ShouldBeTrue();
|
|
_ = prev;
|
|
}
|
|
|
|
/// <summary>lmxopcua#899: a redeploy whose ONLY change is <c>historianReadOnly</c> must reach the applier as a
|
|
/// ChangedRawTags delta — otherwise (the field missing from RawTagPlan's hand-written equality) the flip would diff
|
|
/// empty and a tag marked read-only would keep being provisioned and recorded until something else about it changed.</summary>
|
|
[Theory]
|
|
[InlineData(false, true)]
|
|
[InlineData(true, false)]
|
|
public void Historian_read_only_flip_alone_routes_to_ChangedRawTags(bool before, bool after)
|
|
{
|
|
var folder = new RawFolder { RawFolderId = "raw-plant", ClusterId = "c1", Name = RawFolderName };
|
|
var driver = new DriverInstance
|
|
{
|
|
DriverInstanceId = "drv-modbus", ClusterId = "c1", RawFolderId = "raw-plant",
|
|
Name = DriverName, DriverType = "Modbus", DriverConfig = "{}",
|
|
};
|
|
var device = new Device { DeviceId = "dev-1", DriverInstanceId = "drv-modbus", Name = DeviceName, DeviceConfig = "{}" };
|
|
AddressSpaceComposition ComposeWith(bool readOnly) => AddressSpaceComposer.Compose(
|
|
Array.Empty<UnsArea>(), Array.Empty<UnsLine>(), Array.Empty<Equipment>(),
|
|
new[] { driver }, Array.Empty<ScriptedAlarm>(),
|
|
tags: new[]
|
|
{
|
|
new Tag
|
|
{
|
|
TagId = "t-speed", DeviceId = "dev-1", Name = "Speed", DataType = "Int32", AccessLevel = TagAccessLevel.Read,
|
|
TagConfig = $"{{\"isHistorized\":true,\"historianTagname\":\"SysTimeSec\",\"historianReadOnly\":{(readOnly ? "true" : "false")}}}",
|
|
},
|
|
},
|
|
rawFolders: new[] { folder }, devices: new[] { device });
|
|
|
|
var plan = AddressSpacePlanner.Compute(ComposeWith(before), ComposeWith(after));
|
|
|
|
var changed = plan.ChangedRawTags.ShouldHaveSingleItem();
|
|
changed.Previous.HistorianReadOnly.ShouldBe(before);
|
|
changed.Current.HistorianReadOnly.ShouldBe(after);
|
|
AddressSpaceChangeClassifier.Classify(plan).ShouldBe(AddressSpaceChangeKind.AttributeOnly); // in place, no rebuild
|
|
}
|
|
|
|
/// <summary>
|
|
/// <b>THE PIN (2026-08-15 surgical-deploys review):</b> a RAW-side edit to the backing tag's historian
|
|
/// intent or array shape must produce a <c>ChangedRawTags</c> delta AND a <b>coincident</b>
|
|
/// <c>ChangedUnsReferenceVariables</c> delta on every reference to it. The coincident delta is the ONLY
|
|
/// repair trigger for the UNS mirror node: the raw node is updated surgically in place and
|
|
/// <c>EnsureVariable</c> early-returns on the existing UNS node, so without this delta the mirror keeps
|
|
/// the OLD historian tagname (HistoryRead via the UNS NodeId serves a different series than via the raw
|
|
/// NodeId) and the OLD shape (a scalar→array flip leaves it scalar AND writable). Nothing about the
|
|
/// reference ROW changed here — only the backing tag's TagConfig — so the delta exists purely because
|
|
/// the four <c>Backing*</c> fields are on <see cref="UnsReferenceVariable"/>.
|
|
/// </summary>
|
|
[Theory]
|
|
[InlineData("{\"isHistorized\":true,\"historianTagname\":\"WW.Old\"}", "{\"isHistorized\":true,\"historianTagname\":\"WW.New\"}")]
|
|
[InlineData("{\"isHistorized\":true}", "{}")]
|
|
[InlineData("{}", "{\"isArray\":true,\"arrayLength\":8}")]
|
|
[InlineData("{\"isArray\":true,\"arrayLength\":8}", "{\"isArray\":true,\"arrayLength\":16}")]
|
|
public void Backing_tag_historian_or_shape_edit_produces_a_coincident_uns_reference_delta(string before, string after)
|
|
{
|
|
var prev = Compose("Speed", displayOverride: null, tagConfig: before);
|
|
var next = Compose("Speed", displayOverride: null, tagConfig: after);
|
|
|
|
var plan = AddressSpacePlanner.Compute(prev, next);
|
|
|
|
// Raw side: an attribute edit on a stable RawPath ⇒ a Changed delta (not remove+add).
|
|
var rawDelta = plan.ChangedRawTags.ShouldHaveSingleItem();
|
|
rawDelta.Previous.NodeId.ShouldBe(rawDelta.Current.NodeId);
|
|
|
|
// UNS side: the SAME reference row, same NodeId + BackingRawPath — flagged Changed ONLY because the
|
|
// inherited backing historian/shape moved. This is what drives the applier's remove+recreate repair.
|
|
var unsDelta = plan.ChangedUnsReferenceVariables.ShouldHaveSingleItem();
|
|
unsDelta.Previous.UnsTagReferenceId.ShouldBe("ref-1");
|
|
unsDelta.Previous.NodeId.ShouldBe(unsDelta.Current.NodeId);
|
|
unsDelta.Previous.BackingRawPath.ShouldBe(unsDelta.Current.BackingRawPath);
|
|
unsDelta.Previous.ShouldNotBe(unsDelta.Current);
|
|
|
|
// And the inherited values on the CURRENT side match the backing tag's new intent, so the recreate
|
|
// materialises the mirror from the same truth the raw node was updated with.
|
|
unsDelta.Current.BackingIsHistorized.ShouldBe(rawDelta.Current.IsHistorized);
|
|
unsDelta.Current.BackingHistorianTagname.ShouldBe(rawDelta.Current.HistorianTagname);
|
|
unsDelta.Current.BackingIsArray.ShouldBe(rawDelta.Current.IsArray);
|
|
unsDelta.Current.BackingArrayLength.ShouldBe(rawDelta.Current.ArrayLength);
|
|
|
|
// Neither realm sees a structural add/remove — this stays a surgical, subscription-preserving deploy.
|
|
plan.AddedRawTags.ShouldBeEmpty();
|
|
plan.RemovedRawTags.ShouldBeEmpty();
|
|
plan.AddedUnsReferenceVariables.ShouldBeEmpty();
|
|
plan.RemovedUnsReferenceVariables.ShouldBeEmpty();
|
|
}
|
|
|
|
/// <summary>Identical compositions diff to an empty plan across both realms (fresh list instances must
|
|
/// not spuriously flag RawTags/UnsReferenceVariables as changed).</summary>
|
|
[Fact]
|
|
public void Identical_dual_namespace_compositions_diff_to_empty()
|
|
{
|
|
var prev = Compose("Speed", displayOverride: "Spd");
|
|
var next = Compose("Speed", displayOverride: "Spd");
|
|
|
|
var plan = AddressSpacePlanner.Compute(prev, next);
|
|
|
|
plan.IsEmpty.ShouldBeTrue();
|
|
}
|
|
}
|