Files
computer-playbook/tests/unit/python/roles/sys-ctl-cln-bkps/files/test_script.py
T
kevinveenbirkenbachandClaude Opus 5 7162b7bf4c Merge fork/feature/svc-net-tor into feature/ai-agent-employees
834 commits from the Tor onion-service branch against 299 of ours, from merge
base 7b646bdece: 127 conflicted files, 3506 files changed.

The two sides are largely orthogonal. Theirs adds svc-net-tor and a `tor` service
across the role tree, plus two sweeping refactors: shipped scripts sorted by
language (files/ruby/, files/python/, files/shell/, scripts/lint/*.sh) and the
unit suite moved under tests/unit/python/. Ours is requirement 036, the native AI
gateway, plus the MCP role work. Most conflicts were both sides appending to the
same service map, resolved as unions.

Resolutions that were not unions:

Credentials moved namespace. Theirs renamed meta/schema.yml to meta/secrets.yml
and re-addressed credentials as secrets.credentials.<name>. Git carried our
litellm_api_key blocks through the rename, but every consumer we added still read
'credentials.<name>', which no longer resolves. All 45 call sites are migrated.

plugins/filter/container_volumes.py is deleted by theirs and relocated to
plugins/lookup/container_volumes.py, where it carries the same tmpfs defect our
f85996b43 had just fixed: a hard-coded {"size": ...} that silently drops a per
mount `mode`. The fix is re-applied in the relocated file, as that commit's merge
note anticipated.

compose.yml keeps only INFINITO_PLAYWRIGHT_REPORTS_BASE_DIR. Theirs also forwards
INFINITO_PLAYWRIGHT_STAGE_BASE_DIR; that line is deliberately dropped, per the
analysis recorded in 45804e9ad -- STAGE has a drift-proof role-vars fallback,
while forwarding it promotes a setdefault-pinnable .env value above that SPOT.

tasks/stages/01_constructor.yml takes their include, ./tasks/utils/setup/group.yml,
because their refactor removed tasks/groups/*-roles.yml, leaving our include
target nonexistent. Our task name is kept, since svc-virt is in the loop.

Makefile swarm-zombie is a union: their validate plus teardown, then our
lab_subnet cleanup, which their side dropped while leaving lab_subnet.sh as the
only orphaned reference in the file.

01_bootstrap.sh keeps our deterministic --ip pinning, which distinguishes an
already-attached node from a real failure, over their `|| true`.

Their language-sorted layout orphaned references our side still held: zammad's
two scripts/apply/*.rb, jenkins' java-with-ca-trust.sh, and the notify topics
their handlers renamed to hyphenated form. All corrected.

roles/sys-svc-compose/files/shell/swarm/stack_ready.sh gains our latched-update
block: paused, rollback_paused, rollback_started and rollback_completed exit 2
rather than polling a state that cannot resolve without another deploy.

config_from_run is unioned to (title, logged, jobs); the production caller in
trigger/__main__.py now passes jobs, without which our suite suppression would
have been inert.

READMEs are regenerated rather than hand-merged: 15 LiteLLM rows and 91 Tor rows
now derive from the merged metadata, which is itself evidence the meta merge is
coherent.

Verified: 5161 unit tests pass. The single failure, test_query.py TestSortSpec,
also fails on fork/feature/svc-net-tor at b51d2f9919, measured in a baseline
worktree -- so this merge introduces no new unit-test failure. Two modules are
excluded from that run because `baudolo`, a dependency their branch introduces,
is not installed on this host.
Not verified: no deploy has run against the merged tree, and make quality has not
been run end to end here.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-24 16:32:30 +02:00

190 lines
6.3 KiB
Python

from __future__ import annotations
import io
import itertools
import os
import runpy
import sys
import unittest
from contextlib import redirect_stdout
from pathlib import Path
from types import ModuleType, SimpleNamespace
from unittest.mock import patch
from . import PROJECT_ROOT
def _resolved_repo_root() -> Path:
"""Repository root resolution for the script-under-test path.
The ``INFINITO_REPO_ROOT`` env var still wins when explicitly set
(used by some CI fixtures); otherwise the canonical ``PROJECT_ROOT``
constant from the package ``__init__.py`` is returned.
"""
env = os.environ.get("INFINITO_REPO_ROOT") # nocheck: test-fixture
if env:
return Path(env).expanduser().resolve()
return PROJECT_ROOT
SCRIPT_PATH = _resolved_repo_root() / "roles/sys-ctl-cln-bkps/files/python/script.py"
def _fake_psutil(percent: int) -> ModuleType:
"""Create a fake psutil module with disk_usage()."""
fake = ModuleType("psutil")
fake.disk_usage = lambda _: SimpleNamespace(percent=percent)
return fake
class TestSysCtlClnBkpsScript(unittest.TestCase):
def _run_script(
self,
argv: list[str],
disk_percent: int,
listdir_map: dict[str, list[str]],
time_source: object = None,
) -> tuple[str, object]:
"""
Execute the script as __main__ with provided argv, returning stdout and rmtree mock.
Fully mocks psutil and os.listdir so the test does not depend on system packages or FS.
Args:
time_source: iterable of monotonically rising values for time.time(); when
omitted the clock is frozen at 0, which lets the script's own hour-long
time limit never trigger.
"""
if not SCRIPT_PATH.is_file():
raise FileNotFoundError(
f"script.py not found at expected path: {SCRIPT_PATH} (repo_root={_resolved_repo_root()})"
)
fake_psutil = _fake_psutil(disk_percent)
def listdir_side_effect(path: str) -> list[str]:
return listdir_map.get(path, [])
time_patch = (
patch("time.time", return_value=0)
if time_source is None
else patch("time.time", side_effect=time_source)
)
buf = io.StringIO()
with (
patch.dict(sys.modules, {"psutil": fake_psutil}),
patch.object(sys, "argv", argv),
patch("os.listdir", side_effect=listdir_side_effect),
patch("shutil.rmtree") as rmtree_mock,
time_patch,
redirect_stdout(buf),
):
runpy.run_path(str(SCRIPT_PATH), run_name="__main__")
return buf.getvalue(), rmtree_mock
def test_exits_immediately_when_disk_usage_below_threshold(self) -> None:
"""
If disk usage is already <= threshold, the while-loop must not run.
Script should print final usage and finish message.
"""
backup_dir = os.environ["INFINITO_DIR_BACKUPS"]
argv = [
"script.py",
"--maximum-backup-size-percent",
"75",
"--backups-folder-path",
backup_dir,
]
out, rmtree_mock = self._run_script(
argv=argv,
disk_percent=10,
listdir_map={backup_dir: []},
)
self.assertIn("Cleaning up finished.", out)
self.assertIn(f"% of disk {backup_dir} are used", out)
rmtree_mock.assert_not_called()
def test_breaks_when_no_versions_found_even_if_disk_usage_high(self) -> None:
"""
Regression test for the busy-loop:
If disk usage is above threshold but no backup structure exists,
average_version_directories_per_application() returns 0 and the script
must break out.
"""
backup_dir = os.environ["INFINITO_DIR_BACKUPS"]
argv = [
"script.py",
"--maximum-backup-size-percent",
"75",
"--backups-folder-path",
backup_dir,
]
out, rmtree_mock = self._run_script(
argv=argv,
disk_percent=90,
listdir_map={
backup_dir: [],
},
)
self.assertIn("Delete Iteration: 1", out)
self.assertIn(
"No backup versions found to delete (average_version_directories=0). Exiting.",
out,
)
self.assertIn("Cleaning up finished.", out)
rmtree_mock.assert_not_called()
def test_stops_when_a_pass_cannot_delete_anything(self) -> None:
"""
Regression test for the busy-loop in its surviving shape:
every application holds exactly one version, which is_directory_deletable()
always skips as the last one, so average_version_directories is 1 and the
`average <= 0` break never fires. The disk stays above the threshold no
matter how often the pass repeats, so a pass that deletes nothing must end it.
"""
backup_dir = os.environ["INFINITO_DIR_BACKUPS"]
argv = [
"script.py",
"--maximum-backup-size-percent",
"75",
"--backups-folder-path",
backup_dir,
]
host_path = str(Path(backup_dir) / "host1")
versions_path = str(Path(host_path) / "app1")
original_is_dir = Path.is_dir
def fake_is_dir(self: Path) -> bool:
if str(self).startswith(backup_dir):
return True
return original_is_dir(self)
with patch("pathlib.Path.is_dir", fake_is_dir):
out, rmtree_mock = self._run_script(
argv=argv,
disk_percent=90,
listdir_map={
backup_dir: ["host1"],
host_path: ["app1"],
versions_path: ["20260810012433"],
versions_path + "/": ["20260810012433"],
},
time_source=itertools.count(0, 100),
)
self.assertIn("contains the last version of the backup. Skipped.", out)
self.assertIn("No deletable backup version", out)
self.assertIn("Cleaning up finished.", out)
self.assertIn("Delete Iteration: 1", out)
self.assertNotIn("Delete Iteration: 2", out)
rmtree_mock.assert_not_called()
if __name__ == "__main__":
unittest.main()