195 lines
9.5 KiB
Python
195 lines
9.5 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_scoped_secret(return_value, importable=True):
|
|
"""Installiere ein Fake-``gateway.platforms._shared``-Modul in sys.modules.
|
|
|
|
Das Plugin importiert ``from gateway.platforms._shared import get_scoped_secret``
|
|
innerhalb von ``_skip_adapter_flag``. Im Test-venv existiert ``gateway`` nicht.
|
|
Wir injizieren ein Fake-Modul, damit der ECHTE Import-Pfad des Plugins läuft.
|
|
``importable=False`` lässt das Modul weg → das Plugin fällt auf os.environ zurück
|
|
(der single-profile/unit-test-Fallback).
|
|
"""
|
|
if not importable:
|
|
return {}
|
|
import types
|
|
|
|
fake = types.ModuleType("gateway.platforms._shared")
|
|
fake.get_scoped_secret = lambda name, default=None: return_value
|
|
plats = types.ModuleType("gateway.platforms")
|
|
plats._shared = fake
|
|
gw = types.ModuleType("gateway")
|
|
gw.platforms = plats
|
|
return {"gateway": gw, "gateway.platforms": plats, "gateway.platforms._shared": 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_scoped_secret(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_scoped_secret("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_scoped_secret("false")):
|
|
assert _skip_adapter_flag(cfg) is False
|
|
|
|
def test_scoped_miss_fails_closed_does_not_borrow_env(self):
|
|
"""Scoped miss (get_scoped_secret → None) leiht NICHT os.environ aus (fail-closed).
|
|
|
|
Das ist der kanonische Multiplex-Vertrag: ein Satellit ohne eigenes Flag
|
|
darf NICHT das Flag eines anderen Profils aus os.environ übernehmen.
|
|
Das Plugin liest ausschließlich get_scoped_secret; ein None-Ergebnis
|
|
bedeutet „kein Flag“ → kein Skip (Adapter startet).
|
|
"""
|
|
cfg = _FakeConfig()
|
|
with patch.dict(sys.modules, _fake_scoped_secret(None)):
|
|
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "true"}):
|
|
assert _skip_adapter_flag(cfg) is False
|
|
|
|
def test_scoped_true_skips_default_profile(self):
|
|
"""get_scoped_secret liefert '1' (Default-Profil/scoped) → Skip."""
|
|
cfg = _FakeConfig()
|
|
with patch.dict(sys.modules, _fake_scoped_secret("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_scoped_secret(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_scoped_secret("false")):
|
|
with patch.dict(os.environ, {"ROCKETCHAT_SKIP_ADAPTER": "true"}):
|
|
assert _skip_adapter_flag(cfg) is False
|
|
|
|
def test_scoped_secret_import_error_degrades_to_env(self):
|
|
"""gateway.platforms._shared nicht importierbar → Fallback auf os.environ (single-profile/Unit-Test)."""
|
|
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 (single-profile-Pfad).
|
|
with patch.dict(sys.modules, {}, clear=False):
|
|
sys.modules.pop("gateway", None)
|
|
sys.modules.pop("gateway.platforms", None)
|
|
sys.modules.pop("gateway.platforms._shared", None)
|
|
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_scoped_secret("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_scoped_secret(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_scoped_secret(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
|