test(grpc-data): opt-in H2 history profile for the standalone rig (SqlServer historian overlay, history seed grants, probe HistoryRead, smoke runner)

This commit is contained in:
Joseph Doherty
2026-11-23 23:13:31 -05:00
parent abc6a1b068
commit eb29360d47
6 changed files with 344 additions and 11 deletions
+19 -4
View File
@@ -25,6 +25,10 @@ var options = new DbContextOptionsBuilder<OtOpcUaConfigDbContext>().UseSqlServer
IDbContextFactory<OtOpcUaConfigDbContext> factory = new PooledDbContextFactory<OtOpcUaConfigDbContext>(options);
string[] nodes = ["control-1", "control-2", "source-1", "source-2", "consumer-1", "consumer-2"];
const DataOperations operations = DataOperations.Browse | DataOperations.Metadata | DataOperations.Read | DataOperations.Write | DataOperations.Subscribe;
// H2 smoke profile (opt-in, `rig.py up --history`): SOURCE historizes `counter`, the consumer's connection and
// exports gain HistoryRead, and a second principal's export lacks it. Unset ⇒ the v1 fixture, unchanged.
var history = Environment.GetEnvironmentVariable("GRPC_DATA_HISTORY") == "1";
var grantedOperations = history ? operations | DataOperations.HistoryRead : operations;
try
{
if (args.Length == 0) return 2;
@@ -335,7 +339,9 @@ try
foreach (var (name, address, writable) in new[] { ("counter", 0, false), ("scalar", 20, true), ("array0", 10, false), ("array1", 11, false), ("array2", 12, false) })
db.Tags.Add(new() { TagRowId = Guid.NewGuid(), TagId = "source-" + name, DeviceId = "source-device", Name = name, DataType = "UInt16",
AccessLevel = writable ? TagAccessLevel.ReadWrite : TagAccessLevel.Read, WriteIdempotent = false, PollGroupId = "source-fast",
TagConfig = JsonSerializer.Serialize(new { region = "HoldingRegisters", address, dataType = "UInt16", writable }) });
TagConfig = history && name == "counter"
? JsonSerializer.Serialize(new { region = "HoldingRegisters", address, dataType = "UInt16", writable, isHistorized = true })
: JsonSerializer.Serialize(new { region = "HoldingRegisters", address, dataType = "UInt16", writable }) });
db.Tags.Add(new() { TagRowId = Guid.NewGuid(), TagId = "source-vector", DeviceId = "source-device", Name = "vector", DataType = "UInt16",
AccessLevel = TagAccessLevel.ReadWrite, WriteIdempotent = false, PollGroupId = "source-fast",
TagConfig = """{"region":"HoldingRegisters","address":24,"dataType":"UInt16","isArray":true,"arrayLength":3,"writable":true}""" });
@@ -354,7 +360,7 @@ try
db.DataConnections.Add(new() { DataConnectionRowId = Guid.NewGuid(), DataConnectionId = "consumer-source", ClusterId = "CONSUMER", Enabled = true,
ConsumerNodeIds = ["consumer-1:4053", "consumer-2:4053"], TargetMode = DataTargetMode.Cluster, ExpectedClusterId = "SOURCE",
EligibleTargetNodeIds = ["source-1:4053", "source-2:4053"], Endpoints = ["https://source-1:4057", "https://source-2:4057"],
SecretReference = "secret:data/source", SecretScopeClusterId = "CONSUMER", AllowedOperations = operations,
SecretReference = "secret:data/source", SecretScopeClusterId = "CONSUMER", AllowedOperations = grantedOperations,
TimeoutMilliseconds = 30000 });
foreach (var realm in new[] { DataRealm.Raw, DataRealm.Uns })
{
@@ -362,18 +368,26 @@ try
var rootKind = realm == DataRealm.Raw ? "RawDevice" : "Equipment";
var entity = realm == DataRealm.Raw ? "source-device" : equipment;
db.DataExports.Add(new() { DataExportRowId = Guid.NewGuid(), DataExportId = "export-" + name, ClusterId = "SOURCE", Enabled = true,
Realm = realm, RootKind = rootKind, RootEntityId = entity, PrincipalId = "fixture-consumer", AllowedOperations = operations });
Realm = realm, RootKind = rootKind, RootEntityId = entity, PrincipalId = "fixture-consumer", AllowedOperations = grantedOperations });
if (history)
db.DataExports.Add(new() { DataExportRowId = Guid.NewGuid(), DataExportId = "export-" + name + "-nohistory", ClusterId = "SOURCE", Enabled = true,
Realm = realm, RootKind = rootKind, RootEntityId = entity, PrincipalId = "fixture-nohistory", AllowedOperations = operations });
db.RemoteNamespaceMappings.Add(new() { RemoteNamespaceMappingRowId = Guid.NewGuid(), RemoteNamespaceMappingId = "map-" + name,
ClusterId = "CONSUMER", DataConnectionId = "consumer-source", Enabled = true, ConsumerNodeIds = ["consumer-1:4053", "consumer-2:4053"],
Realm = realm, RemoteRealm = realm, LocalParentKind = "Cluster", LocalParentId = "CONSUMER", BranchName = "remote-source",
RemoteRootEntityId = entity, RemoteNamespaceUri = realm == DataRealm.Raw ? V3NodeIds.RawNamespaceUri : V3NodeIds.UnsNamespaceUri,
RemoteIdentifierKind = "string", RemoteIdentifier = realm == DataRealm.Raw ? "modbus/plc" : equipment, AllowedOperations = operations, WriteThrough = true });
}
if (history)
db.NodeAcls.Add(new() { NodeAclRowId = Guid.NewGuid(), NodeAclId = "source-peer-nohistory", ClusterId = "SOURCE", Principal = "fixture-nohistory",
ScopeKind = NodeAclScopeKind.Cluster, PermissionFlags = NodePermissions.Admin });
await db.SaveChangesAsync();
}
var seedAdmin = new ApiKeyAdminService(factory, pepper);
await SaveText("cli.key", await seedAdmin.CreateAsync("fixture-cli", "fixture-cli", null, "owned local fixture", default));
await SaveText("source.key", await seedAdmin.CreateAsync("source-issued-consumer", "fixture-consumer", null, "SOURCE grants for CONSUMER", default));
if (history)
await SaveText("source-nohistory.key", await seedAdmin.CreateAsync("source-issued-nohistory", "fixture-nohistory", null, "SOURCE export without HistoryRead", default));
string unsBrowseRoot;
await using (var db = await factory.CreateDbContextAsync())
{
@@ -386,7 +400,8 @@ try
namespace_uris = new { raw = V3NodeIds.RawNamespaceUri, uns = V3NodeIds.UnsNamespaceUri, mapped_raw = ProjectedDataIds.RawNamespaceUri, mapped_uns = ProjectedDataIds.UnsNamespaceUri },
browse_roots = new { raw = "modbus/plc", uns = unsBrowseRoot },
browse_subjects = new { raw = "modbus/plc", uns = "area/line/plc" },
origin_paths = new { raw = "modbus/plc/scalar", uns = "area/line/plc/scalar", raw_array = "modbus/plc/vector", uns_array = "area/line/plc/vector", raw_string = "modbus/plc/text", uns_string = "area/line/plc/text" }, aliases = new[] { "raw", "uns" } });
origin_paths = new { raw = "modbus/plc/scalar", uns = "area/line/plc/scalar", raw_array = "modbus/plc/vector", uns_array = "area/line/plc/vector", raw_string = "modbus/plc/text", uns_string = "area/line/plc/text" }, aliases = new[] { "raw", "uns" },
history = history ? new { historized = new { raw = "modbus/plc/counter", uns = "area/line/plc/counter" }, unhistorized = new { raw = "modbus/plc/scalar", uns = "area/line/plc/scalar" } } : null });
return 0;
}
catch (Exception error)