wake-alarm/wake_alarm/tests/test_rtc_hw.py
Krzysztof kuhy Rudnicki 899a2c5581
Some checks failed
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,free-days-interop) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / free-days-interop (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
release-apk / release (APK) (push) Has been cancelled
Fix RTC +1 s mismatch and busy-ring fixture
rtcwake -t shifts the alarm by the RTC/system clock offset (vmbox: +1 s),
so the exact epoch compare in arm()/disarm_ours() forgot our record and
left the alarm armed. Match within 60 s instead (a local-time RTC is off
by hours and still never matches).

_ring_main now emits ring_backend FAILED when pw-play dies mid-ring, not
only when it never starts.

vmbox: the busy-card holder ran as the ssh user (no ACL on a fresh card)
and was aplay, which handles TERM; it now holds the node as root from a
TERM-ignoring sleep and fails the case as a fixture when nothing holds.
Guest repo path derived from the basename, so worktree runs work.
Records the S5 vmbox result; host S5 check dropped by kuhy.

vmbox harness: 16/16 PASS, ring-alsa-busy-fallback 10/10.

Todo: 364208fc
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:07:41 +01:00

135 lines
4.4 KiB
Python

"""The RTC alarm and our record of it, with sudo faked."""
from __future__ import annotations
import json
from pathlib import Path
import subprocess
from unittest.mock import MagicMock, patch
from wake_alarm import _rtc_hw
from wake_alarm._rtc_hw import (
ArmedRecord,
arm,
current_alarm,
disarm_ours,
read_record,
)
# Bound at import, before conftest swaps the seam for a raiser.
REAL_RUN = _rtc_hw._run
EPOCH = 1791265560
def _done(code: int = 0, err: str = "") -> subprocess.CompletedProcess[str]:
return subprocess.CompletedProcess([], code, stdout="", stderr=err)
def test_reads_the_pending_alarm() -> None:
assert current_alarm() is None
_rtc_hw.WAKEALARM_FILE.write_text("\n")
assert current_alarm() is None
_rtc_hw.WAKEALARM_FILE.write_text(f"{EPOCH}\n")
assert current_alarm() == EPOCH
def test_already_set_needs_no_sudo_but_is_recorded() -> None:
_rtc_hw.WAKEALARM_FILE.write_text(str(EPOCH))
assert arm(EPOCH, "2026-10-06") == (True, "already set")
assert read_record() == ArmedRecord(EPOCH, "2026-10-06")
def test_arms_through_sudo_rtcwake() -> None:
run = MagicMock(return_value=_done())
with patch.object(_rtc_hw, "_run", run):
assert arm(EPOCH, "2026-10-06") == (True, "armed")
assert run.call_args.args[0] == [
"sudo",
"-n",
"/usr/sbin/rtcwake",
"-m",
"no",
"-t",
str(EPOCH),
]
assert read_record() == ArmedRecord(EPOCH, "2026-10-06")
def test_arm_failures_record_nothing() -> None:
with patch.object(_rtc_hw, "_run", return_value=_done(1, "denied")):
assert arm(EPOCH, "d") == (False, "denied")
with patch.object(_rtc_hw, "_run", side_effect=OSError("no sudo")):
assert arm(EPOCH, "d") == (False, "no sudo")
assert read_record() is None
def test_disarm_only_ours() -> None:
assert disarm_ours() == (None, False, "nothing of ours armed")
with patch.object(_rtc_hw, "_run", return_value=_done()):
arm(EPOCH, "2026-10-06")
_rtc_hw.WAKEALARM_FILE.write_text(str(EPOCH))
run = MagicMock(return_value=_done())
with patch.object(_rtc_hw, "_run", run):
record, disarmed, detail = disarm_ours()
assert (record, disarmed, detail) == (
ArmedRecord(EPOCH, "2026-10-06"),
True,
"disarmed",
)
assert run.call_args.args[0][-2:] == ["-m", "disable"]
assert read_record() is None
def test_a_fired_or_replaced_alarm_is_just_forgotten() -> None:
_rtc_hw.WAKEALARM_FILE.write_text(str(EPOCH))
arm(EPOCH, "2026-10-06")
_rtc_hw.WAKEALARM_FILE.write_text("")
_, disarmed, detail = disarm_ours()
assert (disarmed, detail) == (False, "no longer pending")
assert read_record() is None
def test_record_tolerates_garbage() -> None:
for body in ("{bad", "[1]", '{"epoch": true}', '{"epoch": "5"}'):
_rtc_hw.RTC_ARMED_FILE.write_text(body)
assert read_record() is None
_rtc_hw.RTC_ARMED_FILE.write_text(json.dumps({"epoch": 5, "target_date": None}))
assert read_record() == ArmedRecord(5, "")
def test_record_write_failure_only_logs() -> None:
_rtc_hw.WAKEALARM_FILE.write_text(str(EPOCH))
with patch.object(Path, "write_text", side_effect=OSError("ro")):
assert arm(EPOCH, "d") == (True, "already set")
assert read_record() is None
def test_real_seam() -> None:
with patch.object(subprocess, "run", return_value=_done()) as run:
REAL_RUN(["true"])
assert run.call_args.args[0] == ["true"]
def test_rtc_offset_still_matches_our_alarm() -> None:
"""rtcwake shifts -t by the RTC/system clock offset (seen: +1 s)."""
with patch.object(_rtc_hw, "_run", return_value=_done()):
arm(EPOCH, "2026-10-06")
_rtc_hw.WAKEALARM_FILE.write_text(str(EPOCH + 1))
run = MagicMock(return_value=_done())
with patch.object(_rtc_hw, "_run", run):
assert arm(EPOCH, "2026-10-06") == (True, "already set")
record, disarmed, _ = disarm_ours()
assert (record, disarmed) == (ArmedRecord(EPOCH, "2026-10-06"), True)
assert run.call_args.args[0][-2:] == ["-m", "disable"]
def test_alarm_an_hour_away_is_not_ours() -> None:
with patch.object(_rtc_hw, "_run", return_value=_done()):
arm(EPOCH, "2026-10-06")
_rtc_hw.WAKEALARM_FILE.write_text(str(EPOCH + 3600))
assert disarm_ours() == (
ArmedRecord(EPOCH, "2026-10-06"),
False,
"no longer pending",
)