181 lines
8.7 KiB
Python
181 lines
8.7 KiB
Python
"""
|
|
F556 — Multi-Profil-Adapter-Reduktion (Variante C): ROCKETCHAT_SKIP_ADAPTER.
|
|
|
|
@implements: [F-556]
|
|
|
|
Problem: Der Hermes-Gateway startet im Multiplex-Mode für jedes Profil
|
|
(don, finn, miguel, rainer, viola) einen eigenen Rocket.Chat-Adapter. 5 Adapter
|
|
subscriben dieselben Räume → Mehrfach-Antworten in Rocket.Chat.
|
|
|
|
Variante A (Credentials aus Profil-.env entfernen) und Variante B
|
|
(platforms.rocketchat.enabled: false) scheiterten, weil der Multiplex-Mode den
|
|
Adapter trotzdem startet (Credentials werden vom Default-Profil vererbt bzw.
|
|
der enabled-Key wird für Profil-Adapter ignoriert).
|
|
|
|
Variante C (Plugin-Layer): Das Plugin prüft in validate_config(), ob
|
|
``ROCKETCHAT_SKIP_ADAPTER`` gesetzt ist. Gesetzt → validate_config() liefert
|
|
False → ``platform_registry.create_adapter()`` liefert None → der Adapter wird
|
|
NICHT instanziiert. Nur das Default-Profil (kein Flag) startet einen Adapter.
|
|
|
|
Kritisch (F-556 Root-Cause): Ein Multiplex-Satellit wird innerhalb von
|
|
``_profile_runtime_scope()`` gestartet. Dessen eigenes .env ist nur über
|
|
``agent.secret_scope.get_secret()`` lesbar — ``os.getenv()`` fällt auf das
|
|
geerbte Prozess-Env des Default-Profils zurück und sähe das Flag NICHT.
|
|
Die Auflösungs-Reihenfolge lautet: secret_scope → os.environ → config.extra.
|
|
|
|
Dieser Test verifiziert:
|
|
1. Flag via secret_scope (Satellit) → skip
|
|
2. Flag via os.environ (Default/Prozess) → skip
|
|
3. Flag via config.extra → skip
|
|
4. Kein Flag → kein skip (Adapter startet)
|
|
5. Truthy-Normalisierung (true/1/yes/on, case-insensitive, false/0/no/off)
|
|
6. CLI-Delegation unbeeinträchtigt: validate_config ohne Flag bleibt rein
|
|
credential-basiert (kein Core-Eingriff nötig).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
_REPO = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(_REPO))
|
|
sys.path.insert(0, str(_REPO / "plugin"))
|
|
|
|
from plugin import _skip_adapter_flag, _truthy, validate_config # noqa: E402
|
|
|
|
|
|
class _FakeConfig:
|
|
"""Minimal PlatformConfig-Ersatz (nur die Felder, die das Plugin liest)."""
|
|
|
|
def __init__(self, extra=None):
|
|
self.extra = extra or {}
|
|
|
|
|
|
def _fake_secret_scope(return_value):
|
|
"""Installiere ein Fake-``agent.secret_scope``-Modul in sys.modules.
|
|
|
|
Das Plugin importiert ``from agent.secret_scope import get_secret`` innerhalb
|
|
von ``_skip_adapter_flag``. ``agent`` existiert im Test-venv nicht — wir
|
|
injizieren ein Fake-Modul, damit der ECHTE Import-Pfad des Plugins läuft
|
|
(und nicht ein gemockter Aufruf, der am fehlenden Modul scheitert).
|
|
"""
|
|
import types
|
|
|
|
fake = types.ModuleType("agent.secret_scope")
|
|
fake.get_secret = lambda name, default=None: return_value
|
|
parent = types.ModuleType("agent")
|
|
parent.secret_scope = fake
|
|
return {"agent": parent, "agent.secret_scope": fake}
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
# _truthy — Normalisierung
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
|
|
class TestTruthy:
|
|
@pytest.mark.parametrize("value", ["true", "True", "TRUE", "1", "yes", "on", " ON ", 1, True])
|
|
def test_truthy_values(self, value):
|
|
assert _truthy(value) is True
|
|
|
|
@pytest.mark.parametrize("value", ["false", "False", "0", "no", "off", "", None, 0, False])
|
|
def test_falsy_values(self, value):
|
|
assert _truthy(value) is False
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
# _skip_adapter_flag — Auflösungs-Reihenfolge
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
|
|
class TestSkipAdapterFlag:
|
|
def test_no_flag_returns_false(self):
|
|
"""Kein Flag in irgendeiner Quelle → kein Skip."""
|
|
cfg = _FakeConfig()
|
|
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
|
|
with patch.dict(sys.modules, _fake_secret_scope(None)):
|
|
assert _skip_adapter_flag(cfg) is False
|
|
|
|
def test_secret_scope_true_skips(self):
|
|
"""Satellit: Flag im secret_scope (.env) → Skip."""
|
|
cfg = _FakeConfig()
|
|
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
|
|
with patch.dict(sys.modules, _fake_secret_scope("true")):
|
|
assert _skip_adapter_flag(cfg) is True
|
|
|
|
def test_secret_scope_false_does_not_skip(self):
|
|
"""Satellit: Flag=false im secret_scope → kein Skip (explizit deaktiviert)."""
|
|
cfg = _FakeConfig()
|
|
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
|
|
with patch.dict(sys.modules, _fake_secret_scope("false")):
|
|
assert _skip_adapter_flag(cfg) is False
|
|
|
|
def test_secret_scope_missing_falls_through_to_env(self):
|
|
"""secret_scope liefert None → Fallthrough auf os.environ."""
|
|
cfg = _FakeConfig()
|
|
with patch.dict(sys.modules, _fake_secret_scope(None)):
|
|
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "true"}):
|
|
assert _skip_adapter_flag(cfg) is True
|
|
|
|
def test_env_true_skips(self):
|
|
"""Default/Prozess: Flag im os.environ → Skip."""
|
|
cfg = _FakeConfig()
|
|
with patch.dict(sys.modules, _fake_secret_scope(None)):
|
|
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "1"}):
|
|
assert _skip_adapter_flag(cfg) is True
|
|
|
|
def test_config_extra_true_skips(self):
|
|
"""config.yaml: extra['skip_adapter'] → Skip."""
|
|
cfg = _FakeConfig(extra={"skip_adapter": "yes"})
|
|
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
|
|
with patch.dict(sys.modules, _fake_secret_scope(None)):
|
|
assert _skip_adapter_flag(cfg) is True
|
|
|
|
def test_secret_scope_takes_priority_over_env(self):
|
|
"""secret_scope=false gewinnt über os.environ=true."""
|
|
cfg = _FakeConfig()
|
|
with patch.dict(sys.modules, _fake_secret_scope("false")):
|
|
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "true"}):
|
|
assert _skip_adapter_flag(cfg) is False
|
|
|
|
def test_secret_scope_import_error_degrades_to_env(self):
|
|
"""agent.secret_scope nicht importierbar → graceful Fallthrough auf env."""
|
|
cfg = _FakeConfig()
|
|
# Kein Fake-Modul installiert → der try/except-Import im Plugin schlägt fehl
|
|
# und der Code fällt auf os.environ zurück.
|
|
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "true"}):
|
|
assert _skip_adapter_flag(cfg) is True
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
# validate_config — Integration
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
|
|
class TestValidateConfig:
|
|
def test_skip_flag_forces_false_even_with_credentials(self):
|
|
"""Selbst mit gültigen Credentials liefert validate_config False bei Flag."""
|
|
cfg = _FakeConfig(extra={"base_url": "https://rc", "user": "b", "password": "p"})
|
|
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
|
|
with patch.dict(sys.modules, _fake_secret_scope("true")):
|
|
assert validate_config(cfg) is False
|
|
|
|
def test_no_flag_with_credentials_is_true(self):
|
|
"""Kein Flag + gültige Credentials → True (Adapter darf starten)."""
|
|
cfg = _FakeConfig(extra={"base_url": "https://rc", "user": "b", "password": "p"})
|
|
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
|
|
with patch.dict(sys.modules, _fake_secret_scope(None)):
|
|
assert validate_config(cfg) is True
|
|
|
|
def test_no_flag_without_credentials_is_false(self):
|
|
"""Kein Flag + fehlende Credentials → False (unverändertes Verhalten)."""
|
|
cfg = _FakeConfig()
|
|
os.environ.pop("ROCKETCHAT_SKIP_ADAPTER", None)
|
|
with patch.dict(sys.modules, _fake_secret_scope(None)):
|
|
os.environ.pop("ROCKETCHAT_BASE_URL", None)
|
|
os.environ.pop("ROCKETCHAT_USER", None)
|
|
os.environ.pop("ROCKETCHAT_PASSWORD", None)
|
|
assert validate_config(cfg) is False
|