validation Expand the V2 metadata engine to support workload source tracking and enforce environment validity prior to execution. - Add SourceConfig dataclass to track upstream git repositories and refs - Update SitesApp to support both Source-Backed and Image-Only workloads - Enforce valid git transport schemes (https, ssh, git@) during normalization - Implement validate_environment() to fail fast on missing domain or cor paths - Add unit tests for source parsing, default injection, and error capture - Stub get_docker_status and cmd_sync_homepage with xfail TDD markers - Update Nix flake and python environments with required dependencies
258 lines
8.0 KiB
Python
258 lines
8.0 KiB
Python
import sys
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
# Import appctl engine v2
|
|
CORE_ROOT = Path(__file__).resolve().parent.parent.parent
|
|
sys.path.insert(0, str(CORE_ROOT / "scripts"))
|
|
|
|
import appctl_engine_v2 as engine
|
|
|
|
|
|
@dataclass
|
|
class AppFactory:
|
|
"""Helper class to generate mock app directories and configurations"""
|
|
sites_dir: Path
|
|
|
|
def create(
|
|
self,
|
|
dir_name: str,
|
|
manifest_yaml: str = "",
|
|
has_compose: bool = True,
|
|
) -> Path:
|
|
app_dir: Path = self.sites_dir / dir_name
|
|
app_dir.mkdir(parents=True, exist_ok=True)
|
|
if manifest_yaml:
|
|
(app_dir / "app.yaml").write_text(manifest_yaml, encoding="utf-8")
|
|
if has_compose:
|
|
(app_dir / "docker-compose.yml").write_text("services: {}\n", encoding="utf-8")
|
|
return app_dir
|
|
|
|
@pytest.fixture
|
|
def mock_sites_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
"""Fixture that creates an isolated Sites directory and points engine.SITES_DIR to it"""
|
|
sites = tmp_path / "Sites"
|
|
monkeypatch.setattr(engine, "SITES_DIR", str(sites))
|
|
monkeypatch.setattr(engine, "HOMELAB_DOMAIN", "mydomain.test")
|
|
return sites
|
|
|
|
@pytest.fixture
|
|
def app_factory(mock_sites_dir: Path) -> AppFactory:
|
|
"""Fixture that provides the AppFactory instance to test"""
|
|
return AppFactory(mock_sites_dir)
|
|
|
|
@pytest.fixture
|
|
def mock_core_dir(
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch
|
|
) -> Path:
|
|
"""Fixture that creates an isolated Core directory and points engine.CORE_DIR to it"""
|
|
core = tmp_path / "CORE"
|
|
core.mkdir(parents=True, exist_ok=True)
|
|
return core
|
|
|
|
## GREEN TEST: Existing implementation should not break
|
|
|
|
def test_resolve_app_hierarchy(app_factory: AppFactory):
|
|
"""Ensure resolution follows: Canonical Name > Alias > Dir Name > Prefix"""
|
|
app_factory.create(
|
|
dir_name="homelab-jellyfin",
|
|
manifest_yaml="""
|
|
name: media-server
|
|
aliases:
|
|
- movies
|
|
""",
|
|
)
|
|
apps = engine.get_sites_apps()
|
|
|
|
assert engine.resolve_app("media-server", apps) is not None
|
|
assert engine.resolve_app("movies", apps) is not None
|
|
assert engine.resolve_app("homelab-jellyfin", apps) is not None
|
|
assert engine.resolve_app("unknown", apps) is None
|
|
|
|
def test_manifest_data_overrides_default_values(app_factory: AppFactory):
|
|
"""Ensure app.yaml values successfully override the system defaults"""
|
|
app_factory.create(
|
|
dir_name="homelab-custom",
|
|
manifest_yaml="""
|
|
name: custom-app
|
|
domain: custom.roadtotech.local
|
|
auth: false
|
|
visible: false
|
|
networks:
|
|
- custom-net
|
|
"""
|
|
)
|
|
|
|
apps = engine.get_sites_apps()
|
|
app = engine.resolve_app("custom-app", apps)
|
|
|
|
assert app is not None
|
|
assert app.domain == "custom.roadtotech.local"
|
|
assert app.auth is False
|
|
assert app.networks == ["custom-net"]
|
|
|
|
def test_sites_app_normalization(app_factory: AppFactory):
|
|
"""Ensure missing or badly typed manifest fields are safely normalized"""
|
|
app_factory.create(
|
|
dir_name="homelab-custom",
|
|
manifest_yaml="""
|
|
name: testapp
|
|
# Missing domain - should fallback to <name>.roadtotech.me
|
|
aliases: single-string-alias # should become a list[str]
|
|
auth: "False" # should evaluate based on boolean casting
|
|
homepage:
|
|
weight: "99" # this should be casted as an int
|
|
"""
|
|
)
|
|
apps = engine.get_sites_apps()
|
|
app = engine.resolve_app("testapp", apps)
|
|
|
|
assert app is not None
|
|
assert app.name == "testapp"
|
|
assert app.domain == "testapp.mydomain.test"
|
|
assert app.aliases == ["single-string-alias"]
|
|
assert app.auth is True
|
|
assert app.homepage is not None
|
|
assert app.homepage.weight == 99
|
|
|
|
|
|
def test_sites_app_source_provenance_normalization(app_factory: AppFactory):
|
|
"""Ensure source provenance block is correctly parsed into SourceConfig"""
|
|
# 1. Source-backed app with explicit ref
|
|
app_factory.create(
|
|
dir_name="homelab-bitetrack",
|
|
manifest_yaml="""
|
|
name: bitetrack
|
|
source:
|
|
type: git
|
|
url: https://git.roadtotech.me/kiskaadee/bitetrack.git
|
|
ref: v1.2.0
|
|
"""
|
|
)
|
|
# 2. Source-backed app with default ref (main)
|
|
app_factory.create(
|
|
dir_name="homelab-doc2site",
|
|
manifest_yaml="""
|
|
name: doc2site
|
|
source:
|
|
url: https://git.roadtotech.me/kiskaadee/doc2site.git
|
|
"""
|
|
)
|
|
# 3. Image-only app (no source block)
|
|
app_factory.create(
|
|
dir_name="homelab-jellyfin",
|
|
manifest_yaml="""
|
|
name: jellyfin
|
|
"""
|
|
)
|
|
|
|
apps = engine.get_sites_apps()
|
|
|
|
bitetrack = engine.resolve_app("bitetrack", apps)
|
|
assert bitetrack is not None
|
|
assert bitetrack.source is not None
|
|
assert bitetrack.source.type == "git"
|
|
assert bitetrack.source.url == "https://git.roadtotech.me/kiskaadee/bitetrack.git"
|
|
assert bitetrack.source.ref == "v1.2.0"
|
|
assert bitetrack.to_dict()["source"]["ref"] == "v1.2.0"
|
|
|
|
doc2site = engine.resolve_app("doc2site", apps)
|
|
assert doc2site is not None
|
|
assert doc2site.source is not None
|
|
assert doc2site.source.ref == "main"
|
|
|
|
jellyfin = engine.resolve_app("jellyfin", apps)
|
|
assert jellyfin is not None
|
|
assert jellyfin.source is None
|
|
assert jellyfin.to_dict()["source"] is None
|
|
|
|
# 4. App with illegal source transport (e.g. file://)
|
|
app_factory.create(
|
|
dir_name="homelab-malformed",
|
|
manifest_yaml="""
|
|
name: malformed
|
|
source:
|
|
url: file:///etc/passwd
|
|
"""
|
|
)
|
|
apps_updated = engine.get_sites_apps()
|
|
malformed = engine.resolve_app("malformed", apps_updated)
|
|
assert malformed is not None
|
|
assert malformed.source is None
|
|
assert malformed.manifest_error is not None
|
|
assert "Invalid source.url scheme" in malformed.manifest_error
|
|
|
|
|
|
## RED TESTS: TDD Goals for next implementation
|
|
|
|
@pytest.mark.xfail(reason="TDD in progress: get_docker_status not yet implemented")
|
|
def test_docker_status_parser_running(monkeypatch):
|
|
"""
|
|
TDD Goal: Port `get_docker_status` to V2
|
|
It must shell out to `docker-compse ps` and `docker inspect`
|
|
then return the correct UI badge
|
|
"""
|
|
def mock_run(args, **kwargs):
|
|
"""Mocking a running"""
|
|
mock = MagicMock()
|
|
if "ps" in args:
|
|
mock.stdout = "container123\n"
|
|
if "inspect" in args:
|
|
mock.stdout = "true\n"
|
|
return mock
|
|
|
|
monkeypatch.setattr("subprocess.run", mock_run)
|
|
|
|
status = engine.get_docker_status("/fake/dir")
|
|
assert status == "🟢 Running (1)"
|
|
|
|
@pytest.mark.xfail(reason="TDD in progress: cmd_sync_homepage not yet implemented")
|
|
def test_homepage_sync_groups_and_sorts_correctly(
|
|
mock_core_dir: Path,
|
|
app_factory: AppFactory,
|
|
monkeypatch):
|
|
"""
|
|
TDD Goal: Port `cmd_sync_homepage` to V2 using the new Dataclass structure.
|
|
It must group by `homepage.group` and sort by `homepage.weight`.
|
|
"""
|
|
hp_config_dir = mock_core_dir / "config" / "homepage"
|
|
hp_config_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
app_factory.create(
|
|
dir_name="homelab-app1",
|
|
manifest_yaml="""
|
|
name: App1
|
|
domain: app1.local.test
|
|
homepage:
|
|
title: App1
|
|
group: Group B
|
|
weight: 20
|
|
"""
|
|
)
|
|
|
|
app_factory.create(
|
|
dir_name="homelab-app2",
|
|
manifest_yaml="""
|
|
name: App2
|
|
domain: app2.local.test
|
|
homepage:
|
|
title: App2
|
|
group: Group A
|
|
weight: 10
|
|
"""
|
|
)
|
|
|
|
monkeypatch.setattr(engine, "get_core_services", list)
|
|
engine.cmd_sync_homepage(["--homepage-dir=" + str(hp_config_dir)])
|
|
|
|
output_file = hp_config_dir / "services.yaml"
|
|
assert output_file.exists(), "The services.yaml file must be generated"
|
|
|
|
content = output_file.read_text()
|
|
assert content.find("- Group A") < content.find("- Group B") # Group A should appear before Group B
|
|
assert "App2" in content # App 2 is in the file
|