Files
FabledSteward/tests/test_plugin_dirs.py
bvandeusen a7a281cb11 feat(plugins): fold first-party plugins in-tree; bundled + external roots
First-party plugins (host_agent, http, snmp, traefik, unifi, docker) are now
tracked under plugins/ and baked into the image, so they version atomically
with core — ending the cross-repo import drift the roundtable->steward rename
exposed. History for these files is preserved in the archived Roundtable-plugins
repo.

Plugin discovery becomes multi-root: PLUGIN_DIR (single) -> PLUGIN_DIRS
(bundled first, then external) + PLUGIN_INSTALL_DIR. Bundled ships in the image;
third-party plugins still mount at runtime into the external root
(STEWARD_PLUGIN_DIR, default /data/plugins) and downloads/installs land there.
Bundled shadows external on a name collision.

- config.py: load_bootstrap returns plugin_dirs + plugin_install_dir
- app.py: iterate PLUGIN_DIRS at the migration + load sites
- migration_runner.py: discover_all_in() unions every plugin root
- plugin_manager.py: resolve_plugin_path() (pure, first-root-wins); load /
  install / hot-reload span all roots; installs target the external root
- settings/routes.py: _discover_plugins scans all roots, dedup bundled-first
- Dockerfile: COPY plugins/ ; docker-compose: drop host bind, document external
- tests/test_plugin_dirs.py: resolution, multi-root discovery, bootstrap split

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-01 08:37:24 -04:00

85 lines
3.2 KiB
Python

"""Unit tests for the bundled + external multi-root plugin model.
Pure-function coverage (no DB, no app): path resolution, multi-root migration
discovery, and the config split into bundled/external roots + install dir.
"""
from __future__ import annotations
from pathlib import Path
from steward.core.migration_runner import discover_all_in
from steward.core.plugin_manager import resolve_plugin_path
def _make_plugin(root: Path, name: str, with_migrations: bool = False) -> None:
pdir = root / name
pdir.mkdir(parents=True)
(pdir / "plugin.yaml").write_text(f"name: {name}\n")
if with_migrations:
(pdir / "migrations" / "versions").mkdir(parents=True)
def test_resolve_plugin_path_prefers_earlier_root(tmp_path):
bundled = tmp_path / "bundled"
external = tmp_path / "external"
_make_plugin(bundled, "dupe")
_make_plugin(external, "dupe")
_make_plugin(external, "only_external")
# Bundled is listed first → it shadows the external copy of the same name.
assert resolve_plugin_path([bundled, external], "dupe") == bundled / "dupe"
# A name only present externally still resolves.
assert resolve_plugin_path([bundled, external], "only_external") == external / "only_external"
def test_resolve_plugin_path_missing_returns_none(tmp_path):
assert resolve_plugin_path([tmp_path], "nope") is None
def test_discover_all_in_unions_roots(tmp_path):
bundled = tmp_path / "bundled"
external = tmp_path / "external"
_make_plugin(bundled, "a", with_migrations=True)
_make_plugin(bundled, "no_mig") # no migrations dir → not discovered
_make_plugin(external, "b", with_migrations=True)
found = discover_all_in([bundled, external])
assert (bundled / "a" / "migrations") in found
assert (external / "b" / "migrations") in found
assert (bundled / "no_mig" / "migrations") not in found
def test_discover_all_in_skips_missing_root(tmp_path):
bundled = tmp_path / "bundled"
_make_plugin(bundled, "a", with_migrations=True)
# A non-existent external root must not raise.
found = discover_all_in([bundled, tmp_path / "does_not_exist"])
assert found == [bundled / "a" / "migrations"]
def test_bootstrap_splits_bundled_and_external(tmp_path, monkeypatch):
from steward.config import load_bootstrap
monkeypatch.setenv("STEWARD_DATABASE_URL", "postgresql+asyncpg://x/y")
monkeypatch.setenv("STEWARD_SECRET_KEY", "k" * 32)
monkeypatch.setenv("STEWARD_PLUGIN_DIR", "/srv/external-plugins")
boot = load_bootstrap(config_path=tmp_path / "absent.yaml")
assert boot["plugin_dirs"] == ["plugins", "/srv/external-plugins"]
# Installs target the external (writable) root, never the bundled one.
assert boot["plugin_install_dir"] == "/srv/external-plugins"
def test_bootstrap_external_defaults_to_data_plugins(tmp_path, monkeypatch):
from steward.config import load_bootstrap
monkeypatch.setenv("STEWARD_DATABASE_URL", "postgresql+asyncpg://x/y")
monkeypatch.setenv("STEWARD_SECRET_KEY", "k" * 32)
monkeypatch.delenv("STEWARD_PLUGIN_DIR", raising=False)
boot = load_bootstrap(config_path=tmp_path / "absent.yaml")
assert boot["plugin_dirs"] == ["plugins", "/data/plugins"]
assert boot["plugin_install_dir"] == "/data/plugins"