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:
@@ -5,6 +5,7 @@ using System.Security.Authentication;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text.Json;
|
||||
using Google.Protobuf;
|
||||
using Grpc.Core;
|
||||
using Grpc.Net.Client;
|
||||
using GrpcDataProbe;
|
||||
@@ -25,7 +26,7 @@ try
|
||||
using var manifest = JsonDocument.Parse(await File.ReadAllTextAsync(manifestPath));
|
||||
var root = manifest.RootElement;
|
||||
var scenario = arguments.GetValueOrDefault("--scenario", "all");
|
||||
if (scenario is not ("all" or "D13" or "wire")) throw new ArgumentException("Unsupported wire scenario.");
|
||||
if (scenario is not ("all" or "D13" or "wire" or "history")) throw new ArgumentException("Unsupported wire scenario.");
|
||||
var nodes = root.GetProperty("nodes");
|
||||
var endpoints = root.GetProperty("endpoints");
|
||||
var keyPath = root.GetProperty("credentials").GetProperty("data_key_files").GetProperty("source").GetString()!;
|
||||
@@ -122,7 +123,118 @@ try
|
||||
}
|
||||
}
|
||||
}
|
||||
if (scenario != "D13")
|
||||
if (scenario == "history")
|
||||
{
|
||||
// H2 smoke (opt-in history fixture): raw origin RPCs on each SOURCE data listener.
|
||||
failurePhase = ProbeFailurePhase.WireOperation;
|
||||
var fixture = root.GetProperty("fixture");
|
||||
var rawUri = fixture.GetProperty("namespace_uris").GetProperty("raw").GetString()!;
|
||||
var paths = fixture.GetProperty("history");
|
||||
var historized = paths.GetProperty("historized").GetProperty("raw").GetString()!;
|
||||
var unhistorized = paths.GetProperty("unhistorized").GetProperty("raw").GetString()!;
|
||||
var otherKeyPath = root.GetProperty("credentials").GetProperty("data_key_files").GetProperty("source_nohistory").GetString()!;
|
||||
Restricted(otherKeyPath);
|
||||
var otherKey = (await File.ReadAllTextAsync(otherKeyPath)).Trim();
|
||||
W.View View(string path) => new() { Address = new() { OriginClusterId = "SOURCE", NamespaceUri = rawUri, IdentifierKind = "string", Identifier = path },
|
||||
Realm = (int)DataRealm.Raw, Subject = path };
|
||||
W.Budget HistoryBudget() => new() { TimeoutTicks = TimeSpan.FromSeconds(20).Ticks, MaxItems = 8, MaxBytes = 4 * 1024 * 1024 };
|
||||
static string Hex(uint status) => "0x" + status.ToString("X8");
|
||||
static object Samples(W.HistoryReadResult item) => item.Samples.Select(sample => new
|
||||
{
|
||||
value = sample.Value?.ToString(), statusCode = Hex(sample.StatusCode),
|
||||
sourceTimestampUtc = sample.SourceTimestamp?.Value?.ToDateTimeOffset().ToString("O"),
|
||||
serverTimestampUtc = sample.ServerTimestamp?.Value?.ToDateTimeOffset().ToString("O"),
|
||||
}).ToArray();
|
||||
static byte[] Shape(W.HistoryReadResult item) { var copy = item.Clone(); copy.CorrelationId = ""; return copy.ToByteArray(); }
|
||||
var windowStart = arguments.TryGetValue("--history-start", out var startText) ? DateTimeOffset.Parse(startText, System.Globalization.CultureInfo.InvariantCulture) : (DateTimeOffset?)null;
|
||||
var windowEnd = arguments.TryGetValue("--history-end", out var endText) ? DateTimeOffset.Parse(endText, System.Globalization.CultureInfo.InvariantCulture) : (DateTimeOffset?)null;
|
||||
foreach (var node in sourceNodes)
|
||||
{
|
||||
var name = node.Name;
|
||||
failureTarget = name;
|
||||
var nodeId = node.Value.GetProperty("node_id").GetString()!;
|
||||
Metadata With(string credential, bool feature)
|
||||
{
|
||||
var headers = Headers(credential, "Node", nodeId);
|
||||
if (feature) headers.Add("x-otopcua-data-features", "history");
|
||||
return headers;
|
||||
}
|
||||
using var handler = security.Handler(name);
|
||||
using var channel = GrpcChannel.ForAddress($"https://{name}:4057", new() { HttpHandler = handler });
|
||||
var client = new W.DataConnectionService.DataConnectionServiceClient(channel);
|
||||
var deadline = () => DateTime.UtcNow.AddSeconds(20);
|
||||
// 1. Capabilities with and without the feature header.
|
||||
var plain = await client.GetCapabilitiesAsync(new() { ProtocolMajor = 1, ProtocolMinor = 3 }, With(key, false), deadline());
|
||||
var featured = await client.GetCapabilitiesAsync(new() { ProtocolMajor = 1, ProtocolMinor = 3 }, With(key, true), deadline());
|
||||
var plainV1Shape = plain.History is null && (plain.Operations & 32) == 0;
|
||||
var featuredShape = featured.History is not null && (featured.Operations & 32) == 32;
|
||||
var sameOtherwise = plain.Operations == (featured.Operations & ~32u) && plain.ClusterId == featured.ClusterId;
|
||||
results.Add(new("history-capabilities-" + name, "history-capabilities", plainV1Shape && featuredShape && sameOtherwise, 3, new()
|
||||
{
|
||||
["withoutHeader"] = new { operations = plain.Operations, historyPresent = plain.History is not null, bytes = plain.CalculateSize() },
|
||||
["withHeader"] = new { operations = featured.Operations, history = featured.History?.ToString(), bytes = featured.CalculateSize() },
|
||||
["serving"] = featured.Serving.ToString(),
|
||||
}));
|
||||
// 2. Raw end=now, 5 values, newest first + continuation; resume once; resume the same token again.
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
W.HistoryReadRequest Raw(W.View view, ByteString? continuation, DateTimeOffset? start = null, DateTimeOffset? end = null, uint count = 5)
|
||||
{
|
||||
var request = new W.HistoryReadRequest { Budget = HistoryBudget(), Raw = new() { NumValuesPerNode = count, ReturnBounds = false } };
|
||||
if (start is { } s) request.Raw.Start = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTimeOffset(s);
|
||||
request.Raw.End = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTimeOffset(end ?? now);
|
||||
var item = new W.HistoryReadItem { CorrelationId = "one", View = view };
|
||||
if (continuation is not null) item.Continuation = continuation;
|
||||
request.Items.Add(item);
|
||||
return request;
|
||||
}
|
||||
var page1 = (await client.HistoryReadAsync(Raw(View(historized), null), With(key, true), deadline())).Items.Single();
|
||||
var times1 = page1.Samples.Select(x => x.SourceTimestamp.Value.ToDateTimeOffset()).ToArray();
|
||||
var page1Ok = page1.StatusCode >> 30 == 0 && page1.Samples.Count is > 0 and <= 5 && page1.HasContinuation
|
||||
&& times1.Zip(times1.Skip(1)).All(pair => pair.First > pair.Second);
|
||||
var page2 = page1.HasContinuation ? (await client.HistoryReadAsync(Raw(View(historized), page1.Continuation), With(key, true), deadline())).Items.Single() : null;
|
||||
var times2 = page2?.Samples.Select(x => x.SourceTimestamp.Value.ToDateTimeOffset()).ToArray() ?? [];
|
||||
var page2Ok = page2 is not null && page2.StatusCode >> 30 == 0 && page2.Samples.Count is > 0 and <= 5
|
||||
&& times2.Zip(times2.Skip(1)).All(pair => pair.First > pair.Second) && times2[0] < times1[^1];
|
||||
var replay = page1.HasContinuation ? (await client.HistoryReadAsync(Raw(View(historized), page1.Continuation), With(key, true), deadline())).Items.Single() : null;
|
||||
var replayOk = replay is not null && replay.StatusCode == 0x804A0000u && replay.Samples.Count == 0;
|
||||
if (page2 is not null && page2.HasContinuation)
|
||||
await client.HistoryReadAsync(new W.HistoryReadRequest { Budget = HistoryBudget(), Raw = new() { NumValuesPerNode = 5 }, ReleaseContinuations = true,
|
||||
Items = { new W.HistoryReadItem { CorrelationId = "one", View = View(historized), Continuation = page2.Continuation } } }, With(key, true), deadline());
|
||||
results.Add(new("history-raw-paging-" + name, "history-raw", page1Ok && page2Ok && replayOk, 3, new()
|
||||
{
|
||||
["end"] = now.ToString("O"),
|
||||
["page1"] = new { statusCode = Hex(page1.StatusCode), hasContinuation = page1.HasContinuation, samples = Samples(page1) },
|
||||
["page2"] = page2 is null ? null : new { statusCode = Hex(page2.StatusCode), hasContinuation = page2.HasContinuation, samples = Samples(page2) },
|
||||
["replaySameToken"] = replay is null ? null : new { statusCode = Hex(replay.StatusCode), samples = replay.Samples.Count, hasContinuation = replay.HasContinuation },
|
||||
["serving"] = featured.Serving.ToString(),
|
||||
}));
|
||||
// 3. Unhistorized tag and the no-HistoryRead principal answer the same bytes; no header ⇒ FAILED_PRECONDITION.
|
||||
var unhist = (await client.HistoryReadAsync(Raw(View(unhistorized), null), With(key, true), deadline())).Items.Single();
|
||||
var denied = (await client.HistoryReadAsync(Raw(View(historized), null), With(otherKey, true), deadline())).Items.Single();
|
||||
var identical = Shape(unhist).AsSpan().SequenceEqual(Shape(denied));
|
||||
string? precondition = null;
|
||||
try { await client.HistoryReadAsync(Raw(View(historized), null), With(key, false), deadline()); precondition = "OK"; }
|
||||
catch (RpcException error) { precondition = error.StatusCode.ToString(); }
|
||||
results.Add(new("history-refusals-" + name, "history-refusals",
|
||||
unhist.StatusCode == 0x80720000u && denied.StatusCode == 0x80720000u && identical && precondition == "FailedPrecondition", 4, new()
|
||||
{
|
||||
["unhistorized"] = new { statusCode = Hex(unhist.StatusCode), unhist.Samples.Count, unhist.HasContinuation, unhist.HasDiagnostic, bytes = Convert.ToHexString(Shape(unhist)) },
|
||||
["deniedPrincipal"] = new { statusCode = Hex(denied.StatusCode), denied.Samples.Count, denied.HasContinuation, denied.HasDiagnostic, bytes = Convert.ToHexString(Shape(denied)) },
|
||||
["byteIdentical"] = identical,
|
||||
["withoutFeatureHeader"] = precondition,
|
||||
}));
|
||||
// 4. Optional fixed window (forward, all values) for comparison with an OPC UA historyread.
|
||||
if (windowStart is { } ws && windowEnd is { } we)
|
||||
{
|
||||
var window = (await client.HistoryReadAsync(Raw(View(historized), null, ws, we, 0), With(key, true), deadline())).Items.Single();
|
||||
results.Add(new("history-window-" + name, "history-window", window.StatusCode >> 30 == 0 && window.Samples.Count > 0 && !window.HasContinuation, 1, new()
|
||||
{
|
||||
["start"] = ws.ToString("O"), ["end"] = we.ToString("O"), ["statusCode"] = Hex(window.StatusCode), ["samples"] = Samples(window),
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (scenario is not ("D13" or "history"))
|
||||
{
|
||||
var fixture = root.GetProperty("fixture");
|
||||
var rawUri = fixture.GetProperty("namespace_uris").GetProperty("raw").GetString()!;
|
||||
|
||||
Reference in New Issue
Block a user