diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f4771c0..027e776 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -163,8 +163,13 @@ jobs: if command -v busybox >/dev/null; then WARDEN_TEST_SH="busybox sh" bash qemu/tests/init-rig-bus-test.sh fi - - name: rig control bridge waits out a slow UI (flare-edge#1448) - run: bash qemu/tests/ctl-bridge-test.sh + - name: rig control bridge waits out a slow UI and reaches the test hook (flare-edge#1448, #2054) + # Once more under busybox sh, the rig's own shell. + run: | + bash qemu/tests/ctl-bridge-test.sh + if command -v busybox >/dev/null; then + WARDEN_TEST_SH="busybox sh" bash qemu/tests/ctl-bridge-test.sh + fi - name: mkimage.sh SEED_DIR regression test # Only ui-drive.sh --seed (a real VM boot) exercises this hook # otherwise; this builds the same unprivileged mkfs.ext4 image and diff --git a/qemu/README.md b/qemu/README.md index 995a3ec..3038e59 100644 --- a/qemu/README.md +++ b/qemu/README.md @@ -77,6 +77,11 @@ A `mosquitto` in the payload starts the panel's device bus first (socket `/run/warden/bus/bus.sock`, one credential file per bus user under `/run/warden/bus/cred/`), as S10bus does before every bus client on a panel; see `payload/README.md`. The rig broker takes any user and loads no plugin. +With a test-flavor `warden-ui` and `wthctl` (flare-edge `tools/wthctl.c`, the +test hook client) in the payload as well, a flow's `override` step pins a +state document inside the UI: qmp.py hands the request to the control bridge +(`@hookdoc` pieces, then `@hook override`), which sends it to `test/req/ui` +as bus user `testhook` and answers the hook's reply. The VT cursor is kept off (`vt.global_cursor_default=0`): fbcon shares the virtio-gpu framebuffer with warden-ui and its blinking cursor would otherwise @@ -111,7 +116,7 @@ All take the virt-fragment ``; `FLARE_EDGE=` where noted. | `real-image-boot.sh` | matched `rootfs.img` + `oem.img` | an ACTUAL flare-edge build (placed by `mkimage.sh --rootfs-image/--oem-image`) boots its own init chain to getty; binaries predating known fixes reproduce their bugs faithfully, a time machine for field issues | | `test-ui-drive-rs485.sh` | - | offline, no VM: a fake `run.sh` and (for one case) a fake `socat` stand in so `ui-drive.sh --rs485-devices`'s startup fails closed instead of printing a false "== rs485 simulator: ..." over a bus nothing is serving, for each of a qemu rs.sock that never appears, a socat that never links rs.pty, and an mbsim.py that dies before opening rs.ctl; also proves `cleanup()` escalates to SIGKILL for a sim process that ignores SIGTERM | | `run-sh-args-test.sh` | - | offline, no VM: a fake `qemu-system-arm` captures run.sh's own argv and pins the ctl/rs485 pci-serial ordering contract rootfs/sbin/init's ttyS0-vs-ttyS1 alias depends on -- the ctl device always enumerates before rs485, warden.ctl only lands on the cmdline when --ctl is given, and an rs485 pci-serial device (real or null-backed) is always present so the port count never shifts | -| `ctl-bridge-test.sh` | - | offline, no VM: runs the control bridge (`warden_ctl_bridge` in rootfs/etc/warden-lib.sh) against a fake warden-ui that answers some commands late. A reply slower than the old 5 s bound still arrives (the bound is now 30 s, `WARDEN_CTL_WAIT_S`), a timeout answers `bridge: no reply from warden-ui ...` (qmp.py raises it as a rig fault), and a timed-out command's late reply is drained instead of being read as the next command's answer (flare-edge #1448) | +| `ctl-bridge-test.sh` | - | offline, no VM: runs the control bridge (`warden_ctl_bridge` in rootfs/etc/warden-lib.sh) against a fake warden-ui that answers some commands late. A reply slower than the old 5 s bound still arrives (the bound is now 30 s, `WARDEN_CTL_WAIT_S`), a timeout answers `bridge: no reply from warden-ui ...` (qmp.py raises it as a rig fault), and a timed-out command's late reply is drained instead of being read as the next command's answer (flare-edge #1448). A fake `wthctl` stands in for the hook client behind `@hookdoc`/`@hook` (flare-edge #2054): the pieces join into one request whose args are used once, only plain words reach the client, and a missing client or a request with no reply answers a `bridge:` line. CI runs it under bash's `sh` and busybox `sh` | | `init-rig-bus-test.sh` | - | offline, no VM: `warden_rig_bus` in rootfs/etc/warden-lib.sh against stub brokers. No broker in the payload creates nothing; a broker that listens gets the panel's layout (the `bus.sock` link, a 0400 64-hex credential per bus user, a config without password file, ACL or plugin); a broker that exits or never listens is reported within the bound; init starts the bus before the payload daemons. `WARDEN_TEST_MOSQUITTO_DIR` adds a real mosquitto carrying one message between two users logged in by credential file (flare-edge #1754) | | watchdog (`run.sh --watchdog`) | - | arm `/dev/watchdog`, don't pet: the VM resets ~30 s later (verified) | diff --git a/qemu/rootfs/etc/warden-lib.sh b/qemu/rootfs/etc/warden-lib.sh index ac617b0..2dc5130 100644 --- a/qemu/rootfs/etc/warden-lib.sh +++ b/qemu/rootfs/etc/warden-lib.sh @@ -86,10 +86,24 @@ warden_idle_forever() { # reply and discards it. A reply slower than 5 s is logged on stderr (the # console) with its time, for the host-speed baselines. # -# WARDEN_CTL_FIFO and WARDEN_CTL_REPLY exist for the host test only. +# `@hook WORDS` is answered here too: one request to the UI's test hook on +# the device bus (test/req/ui, flare-edge docs/bus-test-hook.md), sent by the +# payload's hook client (wthctl, as bus user testhook) and answered with its +# reply JSON. WORDS are the client's own words, the verb last (`override`, +# or `--to flared VERB` for flared's hook); the request's args JSON is the +# text the `@hookdoc TEXT` / `@hookdoc+ TEXT` lines before it wrote (the first +# starts it, each `+` appends), or `{}` when there were none. The args come in +# pieces because the ctl port is a tty in cooked mode, which cuts a line at +# 4095 bytes, and an override document may be 16 KiB. A hook request that +# never reached the hook (no client in the payload, no bus, no reply) answers +# one "bridge: ..." line, which qmp.py treats as a rig fault, not a verdict. +# +# WARDEN_CTL_FIFO, WARDEN_CTL_REPLY, WARDEN_CTL_HOOKDOC, WARDEN_WTHCTL and +# WARDEN_BUS_RUN exist for the host test only. warden_ctl_bridge() { _wcb_fifo="${WARDEN_CTL_FIFO:-/tmp/warden-ui.ctl}" _wcb_reply="${WARDEN_CTL_REPLY:-/tmp/warden-ui.dbg}" + _wcb_hookdoc="${WARDEN_CTL_HOOKDOC:-/tmp/warden-ctl-hook.args}" _wcb_limit=$(( ${WARDEN_CTL_WAIT_S:-30} * 20 )) _wcb_stale=0 while IFS= read -r cmd; do @@ -107,6 +121,21 @@ warden_ctl_bridge() { echo "bridge: no such file: $path" fi ;; + "@hookdoc "*) + printf '%s' "${cmd#@hookdoc }" > "$_wcb_hookdoc" + echo "hookdoc: $(wc -c < "$_wcb_hookdoc") bytes" + ;; + "@hookdoc+ "*) + if [ -f "$_wcb_hookdoc" ]; then + printf '%s' "${cmd#@hookdoc+ }" >> "$_wcb_hookdoc" + echo "hookdoc: $(wc -c < "$_wcb_hookdoc") bytes" + else + echo "bridge: @hookdoc+ without a @hookdoc before it" + fi + ;; + "@hook "*) + _wcb_hook "${cmd#@hook }" + ;; "@procs") # Every process's stat line, for the driver's stall probe # (qmp.py stall_report, flare-edge #1528); needs no UI. @@ -138,6 +167,41 @@ warden_ctl_bridge() { done } +# One test hook request for `@hook WORDS` (see warden_ctl_bridge). The args +# file is used once: removed before the client runs, so a later request +# never inherits it. +_wcb_hook() { + _wch_words="$1" + _wch_args='{}' + if [ -f "$_wcb_hookdoc" ]; then + _wch_args="$(cat "$_wcb_hookdoc")" + rm -f "$_wcb_hookdoc" + fi + # The words reach the client unquoted (one argument each), so only plain + # verb and flag characters pass: no glob or quote can reach the shell. + if [ -z "$(printf '%s' "$_wch_words" | tr -d ' ')" ] || + [ -n "$(printf '%s' "$_wch_words" | tr -d 'A-Za-z0-9_ -')" ]; then + echo "bridge: bad hook request: $_wch_words" + return 0 + fi + _wch_client="${WARDEN_WTHCTL:-/usr/bin/wthctl}" + _wch_bus="${WARDEN_BUS_RUN:-/run/warden/bus}" + if [ ! -x "$_wch_client" ]; then + echo "bridge: no hook client in the payload ($_wch_client)" + return 0 + fi + # shellcheck disable=SC2086 # one argument per checked word, see above + _wch_out="$("$_wch_client" "$_wch_bus/bus.sock" "$_wch_bus/cred/testhook" $_wch_words "$_wch_args" 2>&1)" + _wch_rc=$? + # wthctl: 0 ok:true, 1 ok:false (both are the hook's answer), 2 no + # connection or no reply. + if [ "$_wch_rc" -le 1 ]; then + printf '%s\n' "$_wch_out" + else + echo "bridge: hook request got no answer (client exit $_wch_rc): $(printf '%s' "$_wch_out" | tr '\n' ' ')" + fi +} + # Polls for a non-empty reply file every 0.05 s, up to the bridge's bound; # leaves the poll count in _wcb_n. _wcb_wait_reply() { @@ -166,7 +230,9 @@ _wcb_wait_reply() { # fresh random secret all the same, so a client finds what it expects. # # WARDEN_RIG_BUS_USERS lists the bus users that get a credential file: the -# panel's /etc/warden/bus/users and bundled-users (flare-edge busgen output). +# panel's /etc/warden/bus/users and bundled-users (flare-edge busgen output), +# plus testhook, the user the control bridge's `@hook` client logs in as (a +# test-flavor panel has it too). # WARDEN_RIG_BROKER, WARDEN_BUS_RUN, WARDEN_RIG_BUS_LOG and # WARDEN_RIG_BUS_WAIT_S exist for the host test only. warden_rig_bus() { @@ -174,7 +240,7 @@ warden_rig_bus() { [ -x "$_wrb_broker" ] || return 0 _wrb_dir="${WARDEN_BUS_RUN:-/run/warden/bus}" _wrb_log="${WARDEN_RIG_BUS_LOG:-/tmp/mosquitto.log}" - _wrb_users="${WARDEN_RIG_BUS_USERS:-flared ui io modbus appd telemetry automation.main}" + _wrb_users="${WARDEN_RIG_BUS_USERS:-flared ui io modbus appd telemetry automation.main testhook}" if ! mkdir -p "$_wrb_dir/cred" "$_wrb_dir/sock"; then echo "init: rig bus FAILED: cannot create $_wrb_dir" return 1 diff --git a/qemu/tests/ctl-bridge-test.sh b/qemu/tests/ctl-bridge-test.sh index 9d8ff47..40e327b 100755 --- a/qemu/tests/ctl-bridge-test.sh +++ b/qemu/tests/ctl-bridge-test.sh @@ -4,7 +4,9 @@ # the FIFO and renames each reply into place, as warden_debug.c does; some # commands answer late. Checks: a reply slower than the old 5 s bound still # arrives, a timeout answers the explicit no-reply line, and the late reply -# of a timed-out command is never handed to the next command. +# of a timed-out command is never handed to the next command. A fake hook +# client stands in for wthctl behind `@hookdoc` / `@hook` (the test hook +# over the device bus, flare-edge #2054). set -uo pipefail # Job control: the fake UI leads its own process group, so one kill stops # its pending sleep too. @@ -12,6 +14,7 @@ set -m HERE="$(cd "$(dirname "$0")" && pwd)" LIB="$HERE/../rootfs/etc/warden-lib.sh" +# Unquoted where it runs: WARDEN_TEST_SH may be "busybox sh", the rig's shell. SH="${WARDEN_TEST_SH:-sh}" WORK="$(mktemp -d)" || exit 1 UI_PID= @@ -58,10 +61,12 @@ start_ui() { run_bridge() { # run_bridge local name="$1" wait="$2" shift 2 + # $SH unquoted (it may be "busybox sh"); the quoted script is meant literally. + # shellcheck disable=SC2086,SC2016 printf '%s\n' "$@" | env WARDEN_CTL_FIFO="$WORK/ctl" WARDEN_CTL_REPLY="$WORK/dbg" \ ${wait:+WARDEN_CTL_WAIT_S="$wait"} \ - "$SH" -c ". '$LIB'; warden_ctl_bridge" > "$WORK/$name.out" 2> "$WORK/$name.err" + $SH -c '. "$1"; warden_ctl_bridge' sh "$LIB" > "$WORK/$name.out" 2> "$WORK/$name.err" } same() { # same @@ -111,6 +116,65 @@ procs_ok() { run_bridge procs 1 @procs check "@procs lists process stat lines without the UI" procs_ok +# @hookdoc / @hookdoc+ / @hook: the fake client logs its argv one per line +# and answers by verb (ok:true, ok:false with exit 1, or no reply, exit 2). +cat > "$WORK/wthctl" <<'FAKE' +#!/bin/sh +log="$(dirname "$0")/hook.log" +printf '%s\n' "$@" "--" >> "$log" +for last; do :; done +case "$*" in + *" refuse "*) echo '{"v":1,"ok":false,"err":{"code":"E_NOT_FOUND"}}'; exit 1 ;; + *" silent "*) echo "wthctl: no reply (-3)" >&2; exit 2 ;; +esac +printf '{"v":1,"ok":true,"data":{"args":%s}}\n' "$last" +FAKE +chmod +x "$WORK/wthctl" +run_hook() { # run_hook + local name="$1" client="$2" + shift 2 + rm -f "$WORK/hook.log" "$WORK/hook.args" + # $SH unquoted (it may be "busybox sh"); the quoted script is meant literally. + # shellcheck disable=SC2086,SC2016 + printf '%s\n' "$@" | + env WARDEN_CTL_FIFO="$WORK/ctl" WARDEN_CTL_REPLY="$WORK/dbg" WARDEN_CTL_WAIT_S=1 \ + WARDEN_CTL_HOOKDOC="$WORK/hook.args" WARDEN_WTHCTL="$client" WARDEN_BUS_RUN="$WORK/bus" \ + $SH -c '. "$1"; warden_ctl_bridge' sh "$LIB" > "$WORK/$name.out" 2> "$WORK/$name.err" +} + +run_hook pieces "$WORK/wthctl" '@hookdoc {"topic":"io/state/x",' '@hookdoc+ "doc":{"why":"two words"}}' \ + '@hook override' '@hook override' +check "@hookdoc pieces join into one request; the args are used once" \ + same "$WORK/pieces.out" 'hookdoc: 22 bytes' '<>' 'hookdoc: 49 bytes' '<>' \ + '{"v":1,"ok":true,"data":{"args":{"topic":"io/state/x","doc":{"why":"two words"}}}}' '<>' \ + '{"v":1,"ok":true,"data":{"args":{}}}' '<>' +check "the client gets the bus socket, the testhook credential, the verb and the args" \ + same "$WORK/hook.log" "$WORK/bus/bus.sock" "$WORK/bus/cred/testhook" override \ + '{"topic":"io/state/x","doc":{"why":"two words"}}' -- \ + "$WORK/bus/bus.sock" "$WORK/bus/cred/testhook" override '{}' -- + +run_hook words "$WORK/wthctl" '@hook --to flared guard_evidence' '@hook override;touch x' \ + '@hook ov*' '@hook ' '@hookdoc+ {}' +check "client words pass one by one; a word with other characters is refused" \ + same "$WORK/words.out" '{"v":1,"ok":true,"data":{"args":{}}}' '<>' \ + 'bridge: bad hook request: override;touch x' '<>' 'bridge: bad hook request: ov*' '<>' \ + 'bridge: bad hook request: ' '<>' \ + 'bridge: @hookdoc+ without a @hookdoc before it' '<>' +check "the refused words never ran the client" \ + same "$WORK/hook.log" "$WORK/bus/bus.sock" "$WORK/bus/cred/testhook" --to flared guard_evidence '{}' -- + +run_hook answers "$WORK/wthctl" '@hook refuse' '@hook silent' +check "ok:false is the hook's answer; no reply is a bridge line" \ + same "$WORK/answers.out" '{"v":1,"ok":false,"err":{"code":"E_NOT_FOUND"}}' '<>' \ + 'bridge: hook request got no answer (client exit 2): wthctl: no reply (-3)' '<>' + +run_hook noclient "$WORK/absent" '@hookdoc {}' '@hook override' '@hook override' +check "no client in the payload answers a bridge line and drops the args" \ + same "$WORK/noclient.out" 'hookdoc: 2 bytes' '<>' \ + "bridge: no hook client in the payload ($WORK/absent)" '<>' \ + "bridge: no hook client in the payload ($WORK/absent)" '<>' +check "the args file is gone after a request" test ! -e "$WORK/hook.args" + if [ "$FAIL" = 0 ]; then echo "ctl-bridge tests: OK ($CHECKS checks)" else diff --git a/qemu/tests/qmp.py b/qemu/tests/qmp.py index d7897cb..322d4ac 100755 --- a/qemu/tests/qmp.py +++ b/qemu/tests/qmp.py @@ -42,6 +42,18 @@ a real panel, so these verbs mean the same thing on the rig and on hardware: address, answers nothing) or put it back; needs --rs485-control (ui-drive.sh --rs485-devices) ctl WORDS... raw passthrough for anything the channel grows + override TOPIC DOC|null + pin the state document TOPIC inside warden-ui + (DOC: one compact JSON object or array) or unpin + it (null), through the UI's test hook on the + device bus (flare-edge docs/bus-test-hook.md), + which the bridge's `@hook` reaches with the + payload's wthctl. ok when the hook answers + ok:true, fail on its ok:false (the err is the + detail), fatal when the request never reached + the hook (no bus, no wthctl, no reply). Needs a + broker, wthctl and a test-flavor warden-ui in the + payload assert_page MENU/TAB the active page is exactly this assert_hit X Y CLASS [box=X1,Y1,WxH] [TEXT...] a real tap at X,Y lands on an object of CLASS, @@ -1158,6 +1170,59 @@ def verb_rs485(ctx, lineno, cmd, args, line): return ("ok" if reply.startswith("ok") else "fail"), reply +# The bridge's `@hookdoc` piece size: the ctl port is a tty in cooked mode, +# which cuts a line at 4095 bytes, and an override document may be 16 KiB. +HOOK_PIECE = 2000 +# A concrete topic: no wildcard, no empty level, no leading slash, nothing a +# drive line could not carry as one word. +HOOK_TOPIC = re.compile(r"^[A-Za-z0-9_.%-]+(/[A-Za-z0-9_.%-]+)*$") + + +def hook_request(ctl, words, args): + """One request to a test hook through the bridge (`@hookdoc` pieces, then + `@hook WORDS`) -> the reply text. A refused piece is a channel fault.""" + text = json.dumps(args, separators=(",", ":"), ensure_ascii=True) + for at in range(0, len(text), HOOK_PIECE): + op = "@hookdoc" if at == 0 else "@hookdoc+" + reply = ctl.send(f"{op} {text[at:at + HOOK_PIECE]}") + if not reply.startswith("hookdoc: "): + raise RuntimeError(f"control channel: hook args refused: {reply}") + return ctl.send(f"@hook {words}") + + +def judge_hook_reply(reply): + """(status, detail) for a hook reply: ok:true passes, ok:false fails with + the hook's err, and anything else (a bridge line, no JSON) is fatal: the + step never reached a hook that could judge it.""" + if reply.startswith("bridge: "): + return "fatal", reply + try: + doc = json.loads(reply) + except ValueError: + return "fatal", f"hook reply is not JSON: {reply!r}" + if not isinstance(doc, dict) or not isinstance(doc.get("ok"), bool): + return "fatal", f"hook reply has no ok member: {reply!r}" + if doc["ok"]: + return "ok", "" + return "fail", json.dumps(doc.get("err"), sort_keys=True) + + +def verb_override(ctx, lineno, cmd, args, line): + # override TOPIC DOC|null (flowc.py action_override). The document is + # the rest of the line, which may hold spaces inside its strings. + ctx.need_ctl(lineno, cmd) + topic, _, doc_text = line[len(cmd):].strip().partition(" ") + if not HOOK_TOPIC.match(topic): + return "fatal", f"override topic {topic!r} is not one concrete topic" + try: + doc = json.loads(doc_text) + except ValueError: + return "fatal", f"override document is not JSON: {doc_text!r}" + if doc is not None and not isinstance(doc, (dict, list)): + return "fatal", "override document must be an object, an array or null" + return judge_hook_reply(hook_request(ctx.ctl, "override", {"topic": topic, "doc": doc})) + + def verb_assert_page(ctx, lineno, cmd, args, line): ctx.need_ctl(lineno, cmd) want = " ".join(args) @@ -1388,6 +1453,7 @@ VERBS = { "wait_region": verb_wait_region, "assert_ocr": verb_assert_ocr, "expect_reboot": verb_expect_reboot, + "override": verb_override, } diff --git a/qemu/tests/test_qmp_drive.py b/qemu/tests/test_qmp_drive.py index 1dea034..3a57f05 100755 --- a/qemu/tests/test_qmp_drive.py +++ b/qemu/tests/test_qmp_drive.py @@ -847,6 +847,115 @@ class DriveVerbs(unittest.TestCase): self.assertEqual([r["status"] for r in rows], ["ok"]) +class HookCtl(FakeCtl): + """The bridge's `@hookdoc` / `@hook` commands (warden_ctl_bridge) over a + fake test hook: the pieces are joined as the bridge joins them, and the + hook's answer is whatever ANSWERS maps the joined args to.""" + + sent = [] + answer = staticmethod(lambda words, args: '{"v":1,"ok":true,"data":{}}') + refuse_pieces = False + + def __init__(self, path, timeout=15.0): + super().__init__(path, timeout) + self.doc = None + + def send(self, cmd): + HookCtl.sent.append(cmd) + if cmd.startswith("@hookdoc+ "): + self.doc += cmd[len("@hookdoc+ "):] + return f"hookdoc: {len(self.doc)} bytes" + if cmd.startswith("@hookdoc "): + if HookCtl.refuse_pieces: + return "bridge: something else" + self.doc = cmd[len("@hookdoc "):] + return f"hookdoc: {len(self.doc)} bytes" + if cmd.startswith("@hook "): + args, self.doc = json.loads(self.doc or "{}"), None + return HookCtl.answer(cmd[len("@hook "):], args) + return super().send(cmd) + + +class OverrideVerb(unittest.TestCase): + def setUp(self): + HookCtl.sent = [] + HookCtl.answer = staticmethod(lambda words, args: '{"v":1,"ok":true,"data":{}}') + HookCtl.refuse_pieces = False + + def test_pin_and_unpin_reach_the_hook_as_one_request_each(self): + seen = [] + + def answer(words, args): + seen.append((words, args)) + return '{"v":1,"ok":true,"data":{"pinned":1}}' + HookCtl.answer = staticmethod(answer) + rc, by, rows = run_script( + 'override io/state/failsafe {"last_trip":{"why":"two words"},"ack":false}\n' + "override io/state/failsafe null\n", ctl_cls=HookCtl) + self.assertIsNone(rc, rows) + self.assertEqual([r["status"] for r in rows], ["ok", "ok"]) + self.assertEqual(seen, [ + ("override", {"topic": "io/state/failsafe", + "doc": {"last_trip": {"why": "two words"}, "ack": False}}), + ("override", {"topic": "io/state/failsafe", "doc": None}), + ]) + + def test_a_large_document_goes_in_pieces_under_the_tty_line_limit(self): + seen = [] + + def answer(words, args): + seen.append(args) + return '{"v":1,"ok":true}' + HookCtl.answer = staticmethod(answer) + doc = {"rows": ["x" * 100] * 120} + rc, by, rows = run_script( + "override cfg/state/big " + json.dumps(doc, separators=(",", ":")) + "\n", + ctl_cls=HookCtl) + self.assertEqual(rows[0]["status"], "ok", rows) + self.assertEqual(seen, [{"topic": "cfg/state/big", "doc": doc}]) + pieces = [c for c in HookCtl.sent if c.startswith("@hookdoc")] + self.assertGreater(len(pieces), 5) + self.assertTrue(pieces[0].startswith("@hookdoc {")) + self.assertTrue(all(c.startswith("@hookdoc+ ") for c in pieces[1:])) + self.assertTrue(all(len(c) < 4095 for c in HookCtl.sent), "a line over the tty limit") + self.assertEqual(HookCtl.sent[-1], "@hook override") + + def test_the_hooks_refusal_is_a_fail_with_its_err(self): + HookCtl.answer = staticmethod(lambda words, args: json.dumps( + {"v": 1, "ok": False, "err": {"code": "E_NOT_FOUND", "msg": "no filter covers it"}})) + rc, by, rows = run_script('override io/state/nope {"a":1}\n', ctl_cls=HookCtl) + self.assertEqual(rc, 1) + self.assertEqual(rows[0]["status"], "fail") + self.assertIn("E_NOT_FOUND", rows[0]["detail"]) + + def test_a_request_that_never_reached_the_hook_is_fatal(self): + for reply in ("bridge: no hook client in the payload (/usr/bin/wthctl)", + "bridge: hook request got no answer (client exit 2): wthctl: no reply (-3)", + "not json", '{"v":1}', "[true]"): + HookCtl.answer = staticmethod(lambda words, args, r=reply: r) + rc, by, rows = run_script('override io/state/x {"a":1}\n', ctl_cls=HookCtl) + self.assertEqual(rows[0]["status"], "fatal", reply) + + def test_a_refused_piece_is_fatal_for_the_step_only(self): + HookCtl.refuse_pieces = True + rc, by, rows = run_script('override io/state/x {"a":1}\nwake\n', ctl_cls=HookCtl) + self.assertEqual([r["status"] for r in rows], ["fatal", "ok"]) + self.assertIn("hook args refused", rows[0]["detail"]) + self.assertNotIn("@hook override", HookCtl.sent) + + def test_a_malformed_line_is_fatal_and_sends_nothing(self): + rc, by, rows = run_script( + 'override io/state/+ {"a":1}\n' + 'override /io/state {"a":1}\n' + "override io//state null\n" + "override io/state/x {broken\n" + "override io/state/x 5\n" + 'override io/state/x "text"\n' + "override io/state/x\n", ctl_cls=HookCtl) + self.assertEqual([r["status"] for r in rows], ["fatal"] * 7, rows) + self.assertEqual(HookCtl.sent, []) + + class ConsoleWatchTests(unittest.TestCase): """ConsoleWatch replaced a ui_exited() that reread and rescanned the WHOLE console log on every one of drive()'s per-step checks -- O(steps *