a3758954f8
B16: effective_multiroom() — EINE Wahrheitsquelle für Multiroom.
Root-Cause: RC_MULTIROOM=${RC_MULTIROOM:-true} wurde VOR dem .env-Read
gesetzt → --repair (frische Shell) überschrieb die Wizard-Wahl N mit true.
Jetzt: Reihenfolge env MULTIROOM_AGENT → .env MULTIROOM_AGENT → RC_MULTIROOM
→ Default true. Default-Init in read_env + compute_cascade entfernt,
compute_cascade ruft effective_multiroom auf.
B15: ARCHIVAR_BASE_URL in compute_cascade abgeleitet.
Lokales Ollama → localhost:11434, Remote/Cloud → PROVIDER_BASE_URL.
write_env_file + repair_env_file nutzen ${ARCHIVAR_BASE_URL:-...}.
Tests: test_f98_i2.py (B16 effective_multiroom + B20 systemd-Machine + B18
Stufe 8), test_f98_cascade.py um B15-Zellen + effective_multiroom-Extraktion
erweitert. 82 passed.
Branch main @ 209daf8. KEIN Push, KEIN Deploy.
211 lines
7.9 KiB
Python
211 lines
7.9 KiB
Python
"""
|
|
F98-I2: B16 State-Konsistenz (effective_multiroom) + B20 systemd-Machine-Fix Tests.
|
|
|
|
Testet die Iteration-2-Fixes aus Rainers Architektur-Review:
|
|
1. B16: effective_multiroom() liest Multiroom-Wahrheit aus EINER Quelle
|
|
(env MULTIROOM_AGENT → .env → RC_MULTIROOM → Default true).
|
|
2. B20: gateway_systemctl_v2() nutzt im sudo-Kontext --machine=<user>@.host.
|
|
3. B18: check_components() setzt REPAIR_UNIT_MISSING bei fehlender Unit.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
|
|
INSTALL_SH = Path(__file__).resolve().parents[1] / "install.sh"
|
|
|
|
|
|
def _read_install_sh() -> str:
|
|
return INSTALL_SH.read_text()
|
|
|
|
|
|
def _extract_functions(*names: str) -> str:
|
|
"""Extrahiert Funktions-Definitionen (plus log-Helfer) aus install.sh."""
|
|
content = INSTALL_SH.read_text()
|
|
lines = content.splitlines()
|
|
|
|
def _find_func(name: str) -> tuple[int, int] | None:
|
|
start = None
|
|
for i, line in enumerate(lines):
|
|
if line.startswith(f"{name}()"):
|
|
start = i
|
|
break
|
|
if start is None:
|
|
return None
|
|
end = start
|
|
for j in range(start + 1, len(lines)):
|
|
if lines[j].strip() == "}":
|
|
end = j
|
|
break
|
|
return start, end
|
|
|
|
header_parts = []
|
|
for helper in ("mask_secrets", "log", "warn", "err", "ok", "banner", "die"):
|
|
r = _find_func(helper)
|
|
if r:
|
|
header_parts.append("\n".join(lines[r[0]:r[1] + 1]))
|
|
header = "C_RESET=''; C_BLUE=''; C_GREEN=''; C_YELLOW=''; C_RED=''; C_BOLD=''\n\n" + "\n\n".join(header_parts)
|
|
|
|
funcs = []
|
|
for name in names:
|
|
r = _find_func(name)
|
|
if r:
|
|
funcs.append("\n".join(lines[r[0]:r[1] + 1]))
|
|
return header + "\n\n" + "\n\n".join(funcs) + "\n"
|
|
|
|
|
|
def _run_bash(snippet: str, extra_env: dict | None = None) -> tuple[str, str, int]:
|
|
env = {
|
|
"PATH": "/usr/bin:/bin:/usr/local/bin",
|
|
"HOME": "/tmp/f98-i2-home",
|
|
}
|
|
if extra_env:
|
|
env.update(extra_env)
|
|
result = subprocess.run(
|
|
["bash", "-c", snippet],
|
|
capture_output=True, text=True, env=env, timeout=30,
|
|
)
|
|
return result.stdout, result.stderr, result.returncode
|
|
|
|
|
|
class TestEffectiveMultiroom:
|
|
"""B16: effective_multiroom() — eine Wahrheitsquelle für Multiroom."""
|
|
|
|
def test_env_multiroom_agent_wins(self):
|
|
snippet = (
|
|
_extract_functions("effective_multiroom")
|
|
+ "MULTIROOM_AGENT='false'\n"
|
|
+ "RC_MULTIROOM='true'\n"
|
|
+ "echo \"RESULT=$(effective_multiroom)\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0, f"effective_multiroom crashte:\n{out}\n{err}"
|
|
assert "RESULT=false" in out, f"env MULTIROOM_AGENT=false sollte gewinnen:\n{out}"
|
|
|
|
def test_dotenv_multiroom_agent_wins_over_rc(self):
|
|
"""MULTIROOM_AGENT=false in .env gewinnt über RC_MULTIROOM=true."""
|
|
import tempfile, os
|
|
with tempfile.TemporaryDirectory() as d:
|
|
hermes_dir = os.path.join(d, ".hermes")
|
|
os.makedirs(hermes_dir)
|
|
envfile = os.path.join(hermes_dir, ".env")
|
|
with open(envfile, "w") as f:
|
|
f.write("MULTIROOM_AGENT=false\n")
|
|
snippet = (
|
|
_extract_functions("effective_multiroom")
|
|
+ f"HOME='{d}'\n"
|
|
+ "RC_MULTIROOM='true'\n"
|
|
+ "echo \"RESULT=$(effective_multiroom)\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0
|
|
assert "RESULT=false" in out, f".env MULTIROOM_AGENT=false sollte gewinnen:\n{out}\n{err}"
|
|
|
|
def test_rc_multiroom_used_when_no_env(self):
|
|
snippet = (
|
|
_extract_functions("effective_multiroom")
|
|
+ "RC_MULTIROOM='false'\n"
|
|
+ "echo \"RESULT=$(effective_multiroom)\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0
|
|
assert "RESULT=false" in out, f"RC_MULTIROOM=false sollte gewinnen:\n{out}"
|
|
|
|
def test_default_true_when_nothing_set(self):
|
|
snippet = (
|
|
_extract_functions("effective_multiroom")
|
|
+ "echo \"RESULT=$(effective_multiroom)\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0
|
|
assert "RESULT=true" in out, f"Default sollte true sein:\n{out}"
|
|
|
|
|
|
class TestGatewaySystemctlV2:
|
|
"""B20: gateway_systemctl_v2() — Cross-User-fähiger systemctl-Wrapper."""
|
|
|
|
def test_function_exists(self):
|
|
content = _read_install_sh()
|
|
assert "gateway_systemctl_v2()" in content
|
|
|
|
def test_uses_machine_transport(self):
|
|
"""Der Machine-Transport --machine=<user>@.host muss im Code stehen."""
|
|
content = _read_install_sh()
|
|
assert "--machine=" in content
|
|
assert "@.host" in content
|
|
|
|
def test_derives_sudo_user(self):
|
|
"""SUDO_USER-Ableitung muss vorhanden sein."""
|
|
content = _read_install_sh()
|
|
assert "SUDO_USER" in content
|
|
|
|
def test_non_root_uses_plain_user(self):
|
|
"""Nicht-root: systemctl --user (kein Machine-Transport)."""
|
|
# Simuliere nicht-root: id -u liefert 1000, USER=bernd.
|
|
import tempfile, os
|
|
fake_id = "#!/bin/bash\nif [ \"$1\" = \"-u\" ]; then echo 1000; exit 0; fi\nexit 0\n"
|
|
fake_systemctl = "#!/bin/bash\necho \"ARGS=$*\"\n"
|
|
with tempfile.TemporaryDirectory() as d:
|
|
id_path = os.path.join(d, "id")
|
|
sc_path = os.path.join(d, "systemctl")
|
|
with open(id_path, "w") as f:
|
|
f.write(fake_id)
|
|
with open(sc_path, "w") as f:
|
|
f.write(fake_systemctl)
|
|
os.chmod(id_path, 0o755)
|
|
os.chmod(sc_path, 0o755)
|
|
snippet = (
|
|
_extract_functions("gateway_systemctl_v2")
|
|
+ "USER='bernd'\n"
|
|
+ "gateway_systemctl_v2 is-active hermes-gateway\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet, extra_env={"PATH": f"{d}:/usr/bin:/bin"})
|
|
# Nicht-root → systemctl --user is-active hermes-gateway
|
|
assert "--user" in out, f"Nicht-root sollte --user nutzen:\n{out}\n{err}"
|
|
assert "--machine=" not in out, f"Nicht-root darf keinen Machine-Transport nutzen:\n{out}"
|
|
|
|
def test_root_uses_machine_transport(self):
|
|
"""Root + SUDO_USER → --machine=<user>@.host --user."""
|
|
import tempfile, os
|
|
fake_id = "#!/bin/bash\nif [ \"$1\" = \"-u\" ]; then echo 0; exit 0; fi\nexit 0\n"
|
|
fake_systemctl = "#!/bin/bash\necho \"ARGS=$*\"\n"
|
|
with tempfile.TemporaryDirectory() as d:
|
|
id_path = os.path.join(d, "id")
|
|
sc_path = os.path.join(d, "systemctl")
|
|
with open(id_path, "w") as f:
|
|
f.write(fake_id)
|
|
with open(sc_path, "w") as f:
|
|
f.write(fake_systemctl)
|
|
os.chmod(id_path, 0o755)
|
|
os.chmod(sc_path, 0o755)
|
|
snippet = (
|
|
_extract_functions("gateway_systemctl_v2")
|
|
+ "USER='root'\n"
|
|
+ "SUDO_USER='bernd'\n"
|
|
+ "gateway_systemctl_v2 is-active hermes-gateway\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet, extra_env={"PATH": f"{d}:/usr/bin:/bin"})
|
|
assert "--machine=bernd@.host" in out, f"Root sollte Machine-Transport nutzen:\n{out}\n{err}"
|
|
assert "--user" in out, f"Root sollte --user behalten:\n{out}"
|
|
|
|
|
|
class TestRepairUnitMissing:
|
|
"""B18: check_components() setzt REPAIR_UNIT_MISSING bei fehlender Unit."""
|
|
|
|
def test_repair_unit_missing_flag_declared(self):
|
|
content = _read_install_sh()
|
|
assert "REPAIR_UNIT_MISSING" in content
|
|
|
|
def test_check_components_checks_unit_file(self):
|
|
content = _read_install_sh()
|
|
assert "hermes-gateway.service" in content
|
|
assert "Stufe 8" in content
|
|
|
|
def test_repair_components_has_stage_8(self):
|
|
content = _read_install_sh()
|
|
assert "Reparatur Stufe 8" in content
|