Files
Hermes_rocket.chat_Plugin/tests/test_install_update_only.py
T
Bernd (Hermes) 7457dc81a9 fix(install.sh): --update-only kopiert an korrekten pipx-venv-Pfad + Cleanup
Root Cause: install.sh --update-only kopierte Plugin-Dateien nach
~/.hermes/plugins/rocketchat/, aber der Gateway lädt Plugins aus dem
pipx-venv site-packages/plugins/platforms/rocketchat/.

Änderungen:
- systemd drop-in PYTHONPATH: hardcoded python3.13 → dynamisch via
  find_site_packages() mit Fallback
- deploy_plugin() + --update-only: alte tools.py löschen
  (wurde zu rc_tools.py umbenannt, verhindert Shadowing)
- --update-only: stale ~/.hermes/plugins/rocketchat/ bereinigen
  (alter falscher Pfad aus früheren Versionen)
- Tests: restart_gateway-Funktion statt literal systemctl-String
- Neue Tests: tools.py-Cleanup, stale-Pfad-Cleanup, kein hardcoded
  python3.13

143 tests passed, bash -n clean.
2026-07-25 17:43:39 +02:00

475 lines
21 KiB
Python

"""
V8.6: install.sh --update-only Tests (TDD: RED first).
Testet die neuen --update-only Features:
1. __pycache__-Löschung VOR dem Kopieren
2. __pycache__-Löschung NACH dem Kopieren
3. systemctl --user restart hermes-gateway
4. Post-Deployment-Verifikation (journalctl)
5. Idempotenz
"""
from __future__ import annotations
import os
import re
import subprocess
from pathlib import Path
import pytest
# ═══════════════════════════════════════════════════════════════════════════
# Hilfsfunktionen
# ═══════════════════════════════════════════════════════════════════════════
INSTALL_SH = Path(__file__).resolve().parents[1] / "install.sh"
def _read_install_sh() -> str:
"""Liest install.sh komplett ein."""
return INSTALL_SH.read_text()
def _extract_update_only_section(content: str) -> str:
"""Extrahiert den --update-only Block aus install.sh."""
# Der Block beginnt bei 'if $UPDATE_ONLY; then' und endet bei 'fi' vor dem Hauptfluss
match = re.search(
r'if \$UPDATE_ONLY; then.*?(?=\nfi\n\n# 1\. System-Dependencies|\nfi\n\nexit 0)',
content,
re.DOTALL,
)
if match:
return match.group(0)
return ""
# ═══════════════════════════════════════════════════════════════════════════
# Test 1: Syntax-Check
# ═══════════════════════════════════════════════════════════════════════════
class TestInstallShSyntax:
"""install.sh muss bash -n bestehen."""
def test_bash_syntax_ok(self):
"""bash -n findet keine Syntax-Fehler."""
if not INSTALL_SH.exists():
pytest.skip("install.sh nicht gefunden")
result = subprocess.run(
["bash", "-n", str(INSTALL_SH)],
capture_output=True, text=True,
)
assert result.returncode == 0, (
f"bash -n failed:\n{result.stderr}"
)
# ═══════════════════════════════════════════════════════════════════════════
# Test 2: __pycache__-Löschung VOR dem Kopieren
# ═══════════════════════════════════════════════════════════════════════════
class TestUpdateOnlyCacheCleanupBefore:
"""__pycache__ muss VOR dem Kopieren gelöscht werden."""
def test_find_pycache_delete_before_copy(self):
"""Der find-Befehl für __pycache__ erscheint VOR dem Kopier-Befehl."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
# Suche nach __pycache__-Löschung
assert "find" in update_section, (
f"Kein find-Befehl im --update-only Block:\n{update_section}"
)
assert "__pycache__" in update_section, (
f"Kein __pycache__-Bezug im --update-only Block:\n{update_section}"
)
# Die erste __pycache__-Löschung muss VOR deploy_plugin kommen
first_pycache_pos = update_section.find("__pycache__")
deploy_pos = update_section.find("deploy_plugin")
assert first_pycache_pos < deploy_pos, (
f"__pycache__-Löschung ({first_pycache_pos}) muss VOR deploy_plugin ({deploy_pos}) kommen"
)
def test_pycache_delete_uses_rm_rf(self):
"""__pycache__-Löschung nutzt rm -rf."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
# Mindestens ein find ... -exec rm -rf ... für __pycache__
assert re.search(
r"find.*__pycache__.*rm\s+-rf",
update_section,
), (
f"Kein 'find ... __pycache__ ... rm -rf' gefunden:\n{update_section}"
)
# ═══════════════════════════════════════════════════════════════════════════
# Test 3: __pycache__-Löschung NACH dem Kopieren
# ═══════════════════════════════════════════════════════════════════════════
class TestUpdateOnlyCacheCleanupAfter:
"""__pycache__ muss NACH dem Kopieren NOCHMAL gelöscht werden."""
def test_second_pycache_delete_after_copy(self):
"""Zweite __pycache__-Löschung erscheint NACH deploy_plugin."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
# deploy_plugin-Position
deploy_pos = update_section.find("deploy_plugin")
# Zweites __pycache__-Vorkommen NACH deploy_plugin
after_deploy = update_section[deploy_pos:]
assert "__pycache__" in after_deploy, (
f"Keine __pycache__-Löschung NACH deploy_plugin:\n{after_deploy}"
)
# ═══════════════════════════════════════════════════════════════════════════
# Test 4: systemctl --user restart hermes-gateway
# ═══════════════════════════════════════════════════════════════════════════
class TestUpdateOnlyServiceRestart:
"""Gateway-Dienst muss nach dem Update neu gestartet werden."""
def test_systemctl_restart_present(self):
"""restart_gateway (oder systemctl restart) ist im --update-only Block."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
# restart_gateway ist die Funktion, die systemctl --user restart kapselt
assert "restart_gateway" in update_section, (
f"restart_gateway fehlt im --update-only Block:\n{update_section}"
)
def test_no_restart_flag_respected(self):
"""--no-restart Flag überspringt den Neustart."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
# --no-restart muss im Block behandelt werden
assert "NO_RESTART" in update_section, (
f"NO_RESTART-Flag wird nicht im --update-only Block behandelt:\n{update_section}"
)
def test_restart_after_deploy(self):
"""restart_gateway kommt NACH deploy_plugin."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
deploy_pos = update_section.find("deploy_plugin")
restart_pos = update_section.find("restart_gateway")
assert restart_pos > deploy_pos, (
f"restart_gateway ({restart_pos}) muss NACH deploy_plugin ({deploy_pos}) kommen"
)
# ═══════════════════════════════════════════════════════════════════════════
# Test 5: Post-Deployment-Verifikation
# ═══════════════════════════════════════════════════════════════════════════
class TestUpdateOnlyPostDeployVerification:
"""Nach dem Deployment muss das Log verifiziert werden."""
def test_journalctl_presence_check(self):
"""journalctl prüft auf 'PresenceEngine' oder 'presence'."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
# Muss journalctl mit PresenceEngine/presence-Suche enthalten
assert "journalctl" in update_section, (
f"journalctl fehlt im --update-only Block:\n{update_section}"
)
assert re.search(
r"[Pp]resence(?:Engine)?",
update_section,
), (
f"Keine PresenceEngine/presence-Prüfung im --update-only Block:\n{update_section}"
)
def test_journalctl_archivar_check(self):
"""journalctl prüft auf 'Archivar initialisiert'."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
assert "Archivar initialisiert" in update_section, (
f"'Archivar initialisiert'-Check fehlt im --update-only Block:\n{update_section}"
)
def test_journalctl_connected_check(self):
"""journalctl prüft auf 'Connected to'."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
assert "Connected to" in update_section, (
f"'Connected to'-Check fehlt im --update-only Block:\n{update_section}"
)
def test_verification_errors_are_fatal(self):
"""Fehlende Log-Einträge führen zu ERROR (nicht stillschweigend)."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
# Muss err() oder die() oder exit 1 bei Fehlern enthalten
assert re.search(
r'(err|die|exit\s+1)',
update_section,
), (
f"Keine Fehlerbehandlung (err/die/exit 1) bei Verifikation:\n{update_section}"
)
def test_retry_loop_present(self):
"""Verifikation nutzt Retry-Schleife mit bis zu 3 Versuchen."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
assert "_check_attempt" in update_section, (
f"Keine _check_attempt Variable (Retry-Schleife) im --update-only Block:\n{update_section}"
)
assert "for _check_attempt in 1 2 3" in update_section, (
f"Keine 'for _check_attempt in 1 2 3' Schleife:\n{update_section}"
)
def test_precise_timestamp(self):
"""journalctl nutzt präzisen Zeitstempel statt '2 minutes ago'."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
assert "_verify_since" in update_section, (
f"Keine _verify_since Variable (präziser Zeitstempel):\n{update_section}"
)
assert "date '+%Y-%m-%d %H:%M:%S'" in update_section, (
f"Kein date-Aufruf für präzisen Zeitstempel:\n{update_section}"
)
# Darf KEIN '2 minutes ago' mehr enthalten
assert "2 minutes ago" not in update_section, (
f"'2 minutes ago' noch im --update-only Block (sollte durch _verify_since ersetzt sein):\n{update_section}"
)
def test_no_pager_flag(self):
"""journalctl nutzt --no-pager für zuverlässige Ausgabe."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
assert "--no-pager" in update_section, (
f"Kein --no-pager Flag bei journalctl:\n{update_section}"
)
def test_sleep_12_seconds(self):
"""Initialer Sleep ist 12s (nicht 8s)."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
assert "sleep 12" in update_section, (
f"Kein 'sleep 12' im --update-only Block:\n{update_section}"
)
def test_hit_count_output(self):
"""Jede Prüfung gibt Treffer-Anzahl aus (nicht nur 'NICHT gefunden')."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
# Mindestens 3 Treffer-Ausgaben (eine pro Check)
assert update_section.count("Treffer") >= 3, (
f"Weniger als 3 'Treffer'-Ausgaben (sollte pro Check eine sein):\n{update_section}"
)
def test_retry_wait_message(self):
"""Zwischen Retry-Versuchen wird 'Warte 5s' ausgegeben."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
assert "Warte 5s" in update_section, (
f"Keine 'Warte 5s'-Meldung zwischen Retry-Versuchen:\n{update_section}"
)
# ═══════════════════════════════════════════════════════════════════════════
# Test 6: Idempotenz
# ═══════════════════════════════════════════════════════════════════════════
class TestUpdateOnlyIdempotency:
"""--update-only muss mehrfach ohne Side-Effects ausführbar sein."""
def test_no_state_mutation_outside_plugin_dir(self):
"""--update-only ändert NUR das Plugin-Verzeichnis + Gateway-Status."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
# Darf KEINE config.yaml, .env, room-map.json, SOUL.md schreiben
forbidden = ["config.yaml", ".env", "room-map.json", "SOUL.md"]
for pattern in forbidden:
assert pattern not in update_section, (
f"--update-only darf '{pattern}' nicht anfassen:\n{update_section}"
)
def test_git_pull_before_deploy(self):
"""git pull kommt VOR deploy_plugin."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
git_pull_pos = update_section.find("git pull")
deploy_pos = update_section.find("deploy_plugin")
assert git_pull_pos < deploy_pos, (
f"git pull ({git_pull_pos}) muss VOR deploy_plugin ({deploy_pos}) kommen"
)
def test_dry_run_supported(self):
"""--update-only respektiert --dry-run."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
# DRY_RUN muss im Block referenziert werden
assert "DRY_RUN" in update_section or "$DRY_RUN" in update_section, (
f"DRY_RUN wird nicht im --update-only Block behandelt:\n{update_section}"
)
# ═══════════════════════════════════════════════════════════════════════════
# Test 7: deploy_plugin hat bereits __pycache__-Cleanup (bestehend)
# ═══════════════════════════════════════════════════════════════════════════
class TestDeployPluginCacheCleanup:
"""deploy_plugin() muss ebenfalls __pycache__ bereinigen."""
def test_deploy_plugin_has_pycache_cleanup(self):
"""deploy_plugin() enthält __pycache__-Löschung."""
content = _read_install_sh()
# Extrahiere deploy_plugin()-Funktion
match = re.search(
r'deploy_plugin\(\)\s*\{.*?(?=\n\}\n\n#)',
content,
re.DOTALL,
)
assert match, "deploy_plugin()-Funktion nicht gefunden"
func_body = match.group(0)
assert "__pycache__" in func_body, (
f"deploy_plugin() enthält keine __pycache__-Bereinigung:\n{func_body[:500]}"
)
assert "rm -rf" in func_body, (
f"deploy_plugin() nutzt kein rm -rf für __pycache__:\n{func_body[:500]}"
)
# ═══════════════════════════════════════════════════════════════════════════
# Test 8: tools.py Cleanup (V8.6: rename tools.py → rc_tools.py)
# ═══════════════════════════════════════════════════════════════════════════
class TestToolsPyCleanup:
"""Alte tools.py muss in deploy_plugin() und --update-only gelöscht werden."""
def test_deploy_plugin_removes_old_tools_py(self):
"""deploy_plugin() löscht alte tools.py."""
content = _read_install_sh()
match = re.search(
r'deploy_plugin\(\)\s*\{.*?(?=\n\}\n\n#)',
content,
re.DOTALL,
)
assert match, "deploy_plugin()-Funktion nicht gefunden"
func_body = match.group(0)
assert "tools.py" in func_body, (
f"deploy_plugin() enthält keine tools.py-Bereinigung:\n{func_body[:500]}"
)
assert "rc_tools.py" in func_body, (
f"deploy_plugin() erwähnt rc_tools.py nicht:\n{func_body[:500]}"
)
def test_update_only_removes_old_tools_py(self):
"""--update-only Block löscht alte tools.py."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
assert "tools.py" in update_section, (
f"--update-only Block enthält keine tools.py-Bereinigung:\n{update_section}"
)
assert "rc_tools.py" in update_section, (
f"--update-only Block erwähnt rc_tools.py nicht:\n{update_section}"
)
# ═══════════════════════════════════════════════════════════════════════════
# Test 9: Stale ~/.hermes/plugins/rocketchat/ Cleanup (V8.6)
# ═══════════════════════════════════════════════════════════════════════════
class TestStalePluginDirCleanup:
"""--update-only muss den alten falschen Pfad ~/.hermes/plugins/rocketchat/ bereinigen."""
def test_update_only_removes_stale_hermes_plugins_dir(self):
"""--update-only Block löscht ~/.hermes/plugins/rocketchat/."""
content = _read_install_sh()
update_section = _extract_update_only_section(content)
assert ".hermes/plugins/rocketchat" in update_section, (
f"--update-only Block bereinigt nicht ~/.hermes/plugins/rocketchat/:\n{update_section}"
)
assert "rm -rf" in update_section, (
f"--update-only Block nutzt kein rm -rf für stale Pfad:\n{update_section}"
)
# ═══════════════════════════════════════════════════════════════════════════
# Test 10: Kein hardcoded python3.13 (V8.6: dynamischer site-packages-Pfad)
# ═══════════════════════════════════════════════════════════════════════════
class TestNoHardcodedPythonVersion:
"""install.sh darf keinen hardcoded python3.13-Pfad enthalten."""
def test_no_hardcoded_python3_13_in_site_packages_path(self):
"""Kein 'python3.13/site-packages' als hartcodierter Pfad (außer Fallback/Dry-Run)."""
content = _read_install_sh()
# python3.13 darf nur in Kommentaren, Fallback-Strings, Dry-Run-Platzhaltern
# oder apt-Install vorkommen. Aber NICHT als hartcodierter site-packages-Pfad.
lines_with_python3_13 = [
line for line in content.split("\n")
if "python3.13" in line
]
for line in lines_with_python3_13:
if "site-packages" in line:
# Erlaubt: Fallback nach find_site_packages, Dry-Run-Platzhalter, Kommentare
ok = (
"find_site_packages" in line
or "||" in line
or "#" in line.strip()[:1]
or "<PIPX_VENV>" in line # Dry-Run-Platzhalter
)
assert ok, (
f"Hardcoded python3.13/site-packages ohne Fallback:\n {line.strip()}"
)
def test_find_site_packages_used_for_dropin(self):
"""systemd drop-in nutzt find_site_packages (nicht hardcoded python3.13)."""
content = _read_install_sh()
# Der drop-in-Block muss find_site_packages aufrufen
match = re.search(
r'install_gateway_service\(\).*?(?=\n\}\n\n#)',
content,
re.DOTALL,
)
assert match, "install_gateway_service()-Funktion nicht gefunden"
func_body = match.group(0)
assert "find_site_packages" in func_body, (
f"install_gateway_service() nutzt nicht find_site_packages:\n{func_body[:500]}"
)