133 lines
6.5 KiB
Python
133 lines
6.5 KiB
Python
"""A3 consumer-condition live smoke on a `rig.py up --conditions` run (plan §9 A3 exit; the full gate is A4).
|
|
|
|
Raises the SOURCE scripted alarm `ScalarHigh` (scalar register 20 := 150) through the device control API, then:
|
|
|
|
1. starts the CLI `alarms --refresh` on the SOURCE Primary (the origin's OWN subscriber) and on the CONSUMER Primary (whose
|
|
Server-object item opens condition demand, so the consumer imports ScalarHigh over its data connection);
|
|
2. waits for the IMPORTED `ScalarHigh` (Active, Unacknowledged) on the consumer, with its consumer-minted EventId and its
|
|
`mapped/conditions` ConditionNodeId;
|
|
3. refuses first: a bogus EventId must answer `BadEventIdUnknown` (nothing sent);
|
|
4. acknowledges it THROUGH THE CONSUMER with the CLI `ack` (§5.6: claimed by the node manager, dispatched to the origin's
|
|
`CallConditionMethod`, never applied locally);
|
|
5. requires the origin's own subscriber to see EXACTLY ONE `Active, Acknowledged` ScalarHigh event, and the consumer to
|
|
see the acknowledgement come back (the origin's record re-imported) — exactly one consumer `Active, Acknowledged` event;
|
|
6. clears the alarm.
|
|
|
|
Evidence lands in the run's restricted `evidence/` folder (`A3-*.txt`, `A3-*.process.json`, `A3-conditions-consumer.json`).
|
|
"""
|
|
import argparse
|
|
import json
|
|
import os
|
|
import re
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
import scenarios
|
|
|
|
|
|
def watch(runner, node, name):
|
|
log = runner.folder / name
|
|
descriptor = os.open(log, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
process = subprocess.Popen(["dotnet", str(runner.cli_dll), "alarms", "-u", runner.endpoint(node), "-U", "apikey", "-P", runner.key,
|
|
"--refresh", "-i", "250"], stdout=descriptor, stderr=subprocess.STDOUT, start_new_session=True)
|
|
os.close(descriptor)
|
|
return process, log
|
|
|
|
|
|
def stop(process):
|
|
process.send_signal(signal.SIGINT)
|
|
try:
|
|
process.wait(timeout=20)
|
|
except subprocess.TimeoutExpired:
|
|
process.kill()
|
|
process.wait(timeout=10)
|
|
|
|
|
|
def blocks(log, condition="ScalarHigh"):
|
|
found = []
|
|
for block in log.read_text(errors="replace").split("\n\n"):
|
|
if f"Condition: {condition}" not in block:
|
|
continue
|
|
fields = {}
|
|
for line in block.splitlines():
|
|
match = re.match(r"\s*(State|ConditionNodeId|EventId|Message):\s*(.*)$", line)
|
|
if match:
|
|
fields[match.group(1)] = match.group(2).strip()
|
|
found.append(fields)
|
|
return found
|
|
|
|
|
|
def wait_for(predicate, timeout, step=1.0):
|
|
deadline = time.monotonic() + timeout
|
|
while time.monotonic() < deadline:
|
|
value = predicate()
|
|
if value:
|
|
return value
|
|
time.sleep(step)
|
|
return predicate()
|
|
|
|
|
|
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)
|
|
parser.add_argument("--import-timeout", type=int, default=120)
|
|
arguments = parser.parse_args(argv)
|
|
runner = scenarios.ScenarioRunner(arguments.run_id)
|
|
runner.active, runner.deadline = "A3", time.monotonic() + 900
|
|
runner.publish()
|
|
runner.device("set", address=20, value=150)
|
|
time.sleep(arguments.settle_seconds)
|
|
source, consumer = runner.primary("source"), runner.primary("consumer")
|
|
origin_watch, origin_log = watch(runner, source, "A3-origin-alarms.txt")
|
|
consumer_watch, consumer_log = watch(runner, consumer, "A3-consumer-alarms.txt")
|
|
report = {"source_primary": source, "consumer_primary": consumer, "checks": {}}
|
|
try:
|
|
imported = wait_for(lambda: next((b for b in blocks(consumer_log) if b.get("State") == "Active, Unacknowledged"
|
|
and b.get("EventId") and b.get("ConditionNodeId")), None), arguments.import_timeout)
|
|
report["checks"]["imported_active_unacked"] = bool(imported)
|
|
if not imported:
|
|
raise scenarios.ScenarioError("the consumer never showed the imported ScalarHigh (Active, Unacknowledged)")
|
|
report["imported"] = {"condition_node_id": imported["ConditionNodeId"], "event_id_len": len(imported["EventId"])}
|
|
endpoint = runner.endpoint(consumer)
|
|
base = ["dotnet", str(runner.cli_dll), "ack", "-u", endpoint, "-U", "apikey", "-P", runner.key, "-n", imported["ConditionNodeId"]]
|
|
|
|
bogus = runner.process(base + ["-e", "00" * 16, "-c", "A3 bogus"], "A3-ack-bogus-eventid", timeout=60)
|
|
bogus_text = bogus.stdout + bogus.stderr
|
|
report["checks"]["bogus_eventid_refused"] = ("BadEventIdUnknown" in bogus_text or "809A0000" in bogus_text.upper()) \
|
|
and "successful" not in bogus_text
|
|
report["bogus"] = bogus_text[-300:]
|
|
time.sleep(3)
|
|
report["checks"]["nothing_acked_after_bogus"] = all(b.get("State") != "Active, Acknowledged" for b in blocks(origin_log))
|
|
|
|
started = time.monotonic()
|
|
ack = runner.process(base + ["-e", imported["EventId"], "-c", "A3 consumer ack"], "A3-ack-consumer", timeout=60)
|
|
report["ack"] = {"returncode": ack.returncode, "elapsed_ms": round((time.monotonic() - started) * 1000),
|
|
"stdout": ack.stdout[-400:]}
|
|
report["checks"]["ack_good"] = ack.returncode == 0 and "Acknowledge successful" in ack.stdout
|
|
|
|
acked_back = wait_for(lambda: any(b.get("State") == "Active, Acknowledged" for b in blocks(consumer_log)), 30)
|
|
report["checks"]["consumer_sees_ack_back"] = bool(acked_back)
|
|
time.sleep(6) # let any duplicate arrive before counting
|
|
finally:
|
|
stop(consumer_watch)
|
|
stop(origin_watch)
|
|
runner.device("set", address=20, value=0)
|
|
|
|
origin_states = [b.get("State") for b in blocks(origin_log)]
|
|
consumer_states = [b.get("State") for b in blocks(consumer_log)]
|
|
report["origin_subscriber"] = {"states": origin_states, "active_acknowledged_events": origin_states.count("Active, Acknowledged")}
|
|
report["consumer_subscriber"] = {"states": consumer_states, "active_acknowledged_events": consumer_states.count("Active, Acknowledged")}
|
|
report["checks"]["origin_exactly_one_ack_event"] = report["origin_subscriber"]["active_acknowledged_events"] == 1
|
|
report["checks"]["consumer_exactly_one_ack_event"] = report["consumer_subscriber"]["active_acknowledged_events"] == 1
|
|
report["passed"] = all(report["checks"].values())
|
|
runner.save("A3-conditions-consumer.json", report)
|
|
print(json.dumps(report, indent=2)[:20000])
|
|
return 0 if report["passed"] else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|