diff --git a/scripts/dogfood-installer.sh b/scripts/dogfood-installer.sh index 0854aaca..c367f505 100644 --- a/scripts/dogfood-installer.sh +++ b/scripts/dogfood-installer.sh @@ -440,16 +440,20 @@ hold_console() { } # type_keys : type on the guest's keyboard through the -# QEMU monitor (sendkey); a newline is Enter. +# QEMU monitor (sendkey); a newline is Enter. Widen the per-key gap while +# console noise is active: the injected errors can make the guest miss a +# scancode, and a 16-character password typed at the default gap lost one +# keystroke under noise, so the two password prompts did not match (#600). type_keys() { - python3 - "${state}/$1.mon" "$2" <<'PY' + python3 - "${state}/$1.mon" "$2" "${console_noise:-0}" <<'PY' import socket, sys, time keys = {" ": "spc", "\n": "ret", "-": "minus", ".": "dot", "/": "slash", "=": "equal", ",": "comma"} +gap = 0.35 if sys.argv[3] == "1" else 0.15 with socket.socket(socket.AF_UNIX) as s: s.connect(sys.argv[1]) for ch in sys.argv[2]: s.sendall(f"sendkey {keys.get(ch, ch)}\n".encode()) - time.sleep(0.15) + time.sleep(gap) time.sleep(0.5) PY } diff --git a/tests/unit/test_dogfood_installer.py b/tests/unit/test_dogfood_installer.py index 891e2fd3..7175cadd 100644 --- a/tests/unit/test_dogfood_installer.py +++ b/tests/unit/test_dogfood_installer.py @@ -10,11 +10,15 @@ These tests run the script's own ``boot_wait`` against a stand-in QEMU that never exits, without booting anything. """ +from __future__ import annotations - +import os import re import shlex +import socket import subprocess +import threading +import time from pathlib import Path import pytest @@ -103,3 +107,75 @@ def test_the_journal_mirror_opens_after_the_flush(script: str) -> None: lines = unit[1].splitlines() assert "ExecStart=journalctl -b -f --no-pager -o short-monotonic" in lines assert "After=systemd-journal-flush.service" in lines + + +class _FakeMonitor: + """A stand-in QEMU monitor: a Unix socket that accepts and drains the + sendkeys type_keys types on, so the script never blocks sending. Under + console noise type_keys widens its per-key gap, so typing the whole + string takes longer (#600).""" + + def __init__(self, path: Path) -> None: + if path.exists(): + path.unlink() + self._srv = socket.socket(socket.AF_UNIX) + self._srv.bind(str(path)) + self._srv.listen(1) + self._thread = threading.Thread(target=self._serve, daemon=True) + + def __enter__(self) -> _FakeMonitor: + self._thread.start() + return self + + def __exit__(self, *_exc) -> None: + try: + self._srv.close() + except OSError: + pass + + def _serve(self) -> None: + try: + conn, _ = self._srv.accept() + except OSError: + return + while conn.recv(4096): + pass + conn.close() + + +def type_keys_elapsed(tmp_path: Path, text: str, console_noise: str) -> float: + """Seconds type_keys takes to type ; the noise-widened gap is + roughly twice the quiet one over the same string.""" + with _FakeMonitor(tmp_path / "t.mon"): + script = ( + "set -euo pipefail\n" + f'state="{tmp_path}"\n' + + function("type_keys") + + 'console_noise="$TK_NOISE"\n' + + 'type_keys "t" "$TK_TEXT"\n' + ) + env = os.environ.copy() + env["TK_TEXT"] = text + env["TK_NOISE"] = console_noise + env.pop("COVERAGE_PROCESS_CONFIG", None) + start = time.monotonic() + result = subprocess.run( + ["bash", "-c", script, "type_keys"], + env=env, + capture_output=True, + text=True, + timeout=60, + ) + elapsed = time.monotonic() - start + assert result.returncode == 0, result.stderr + return elapsed + + +def test_type_keys_widens_its_gap_under_console_noise(tmp_path: Path) -> None: + text = "1234567890\n" + quiet = type_keys_elapsed(tmp_path, text, "0") + noisy = type_keys_elapsed(tmp_path, text, "1") + # Eleven keys at 0.15s vs 0.35s are about 2.2s apart; this separates the + # two without depending on per-key scheduling jitter. + assert quiet < 3.0, f"quiet gap unexpectedly large: {quiet:.2f}s" + assert noisy > 3.5, f"noise gap unexpectedly small: {noisy:.2f}s"