Files
lmxopcua/scripts/validation/grpc-data/conditions_smoke.py
T

69 lines
3.8 KiB
Python

"""A2a condition-streaming live smoke on a `rig.py up --conditions` run (plan §9 A2; the full gate is A4).
Raises the SOURCE scripted alarm `ScalarHigh` (scalar register 20 := 150) through the device control API, then runs the
probe's `--scenario conditions` on both SOURCE data listeners: the Primary must stream a snapshot (total, records with
the active alarm, snapshot_complete) and, after the probe clears the alarm (scalar := 0), a delta record; the Secondary
must refuse the condition item (Primary-only). Evidence lands in the run's restricted `evidence/` folder.
A2b: on the Primary the probe also acknowledges the active alarm REMOTELY (`CallConditionMethod`, with a duplicate of the
same operation id and a header-less attempt) and requires the stream to carry exactly one acked delta before it clears the
alarm; the Secondary must answer RejectedBeforeDispatch. Meanwhile the CLI's `alarms` subscription on the SOURCE Primary's
OPC UA endpoint records the origin's OWN events: exactly one `Active, Acknowledged` event for ScalarHigh must appear.
"""
import argparse
import json
import os
import signal
import subprocess
import sys
import time
import scenarios
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--run-id", required=True)
parser.add_argument("--settle-seconds", type=int, default=8)
arguments = parser.parse_args(argv)
runner = scenarios.ScenarioRunner(arguments.run_id)
runner.active, runner.deadline = "A2a", time.monotonic() + 600
runner.publish()
runner.device("set", address=20, value=150)
time.sleep(arguments.settle_seconds)
# The origin's own subscriber: the CLI `alarms` (Server object, ConditionRefresh) on the SOURCE Primary.
primary = runner.primary("source")
alarms_log = runner.folder / "A2b-origin-alarms.txt"
descriptor = os.open(alarms_log, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
watcher = subprocess.Popen(["dotnet", str(runner.cli_dll), "alarms", "-u", runner.endpoint(primary), "-U", "apikey", "-P", runner.key,
"--refresh", "-i", "250"], stdout=descriptor, stderr=subprocess.STDOUT, start_new_session=True)
os.close(descriptor)
time.sleep(8)
output = runner.folder / "A2a-conditions-wire.json"
result = runner.process(["dotnet", str(runner.probe_dll), "--manifest", str(runner.manifest_path), "--scenario", "conditions",
# The UNS export root is the equipment, addressed by its catalog identifier (the EquipmentId).
"--equipment", runner.manifest["fixture"]["browse_roots"]["uns"],
"--subject", runner.manifest["fixture"]["browse_subjects"]["uns"],
"--output", str(output)], "A2a-conditions-wire", timeout=240)
wire = scenarios.restricted_json(output) if output.exists() else {"passed": False, "stderr": result.stderr[-4000:]}
time.sleep(4)
watcher.send_signal(signal.SIGINT)
try:
watcher.wait(timeout=20)
except subprocess.TimeoutExpired:
watcher.kill()
watcher.wait(timeout=10)
runner.device("set", address=20, value=0)
blocks = [b for b in alarms_log.read_text(errors="replace").split("\n\n") if "Condition: ScalarHigh" in b]
states = [line.split(":", 1)[1].strip() for b in blocks for line in b.splitlines() if line.strip().startswith("State:")]
origin = {"primary": primary, "scalar_high_events": len(blocks), "states": states,
"active_acknowledged_events": states.count("Active, Acknowledged")}
wire["origin_subscriber"] = origin
wire["passed"] = bool(wire.get("passed")) and origin["active_acknowledged_events"] == 1
print(json.dumps(wire, indent=2)[:20000])
return 0 if wire["passed"] else 1
if __name__ == "__main__":
sys.exit(main())