test(gitops,appctl): extract isolated env subprocess fixture and split daemon test
- Add tests/conftest.py with run_isolated_script_expr fixture stripping HOMELAB_DEPLOYMENTS_FILE, WORKLOADS_DIR, and SITES_DIR and setting HOME/PYTHONPATH - Refactor gitops_security, appctl_auth, and appctl_daemon tests to use run_isolated_script_expr instead of duplicated subprocess env-isolation boilerplate - Split test_daemon_deployments_file_defaults_to_workloads_and_fails_closed into a pure default/override test and a daemon_env 403 fail-closed test
This commit is contained in:
1 parent
c337914fb3
commit
9306dae56b
4 files changed
+82
-76
No files matched your search
@@ -0,0 +1,53 @@
|
||||
"""Shared pytest fixtures for Canopy NixOS Core test suites."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Protocol
|
||||
|
||||
import pytest
|
||||
|
||||
_STRIPPED_WORKLOAD_ENV_VARS = frozenset(
|
||||
{"HOMELAB_DEPLOYMENTS_FILE", "WORKLOADS_DIR", "SITES_DIR"}
|
||||
)
|
||||
_SCRIPTS_DIR = Path(__file__).resolve().parent.parent / "scripts"
|
||||
|
||||
|
||||
class IsolatedScriptRunner(Protocol):
|
||||
"""Callable protocol for executing a Python expression in an isolated env."""
|
||||
|
||||
def __call__(
|
||||
self, expr: str, extra_env: dict[str, str] | None = None
|
||||
) -> str: ...
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def run_isolated_script_expr(tmp_path: Path) -> IsolatedScriptRunner:
|
||||
"""
|
||||
Execute a Python expression in a clean subprocess with workload env vars stripped.
|
||||
|
||||
Strips ``HOMELAB_DEPLOYMENTS_FILE``, ``WORKLOADS_DIR``, and ``SITES_DIR``,
|
||||
sets ``HOME`` to ``tmp_path`` and ``PYTHONPATH`` to ``scripts/``, and
|
||||
merges any optional ``extra_env`` overrides.
|
||||
"""
|
||||
|
||||
def _run(expr: str, extra_env: dict[str, str] | None = None) -> str:
|
||||
env = {
|
||||
k: v
|
||||
for k, v in os.environ.items()
|
||||
if k not in _STRIPPED_WORKLOAD_ENV_VARS
|
||||
}
|
||||
env["HOME"] = str(tmp_path)
|
||||
env["PYTHONPATH"] = str(_SCRIPTS_DIR)
|
||||
if extra_env:
|
||||
env.update(extra_env)
|
||||
return subprocess.check_output(
|
||||
[sys.executable, "-c", expr],
|
||||
env=env,
|
||||
text=True,
|
||||
).strip()
|
||||
|
||||
return _run
|
||||
@@ -270,27 +270,12 @@ def test_registry_resolves_workloads_and_sites_tilde_prefixes(
|
||||
|
||||
|
||||
def test_deployments_file_defaults_to_workloads_and_does_not_fallback(
|
||||
tmp_path: Path, monkeypatch
|
||||
tmp_path: Path, monkeypatch, run_isolated_script_expr
|
||||
):
|
||||
"""Ensure DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml and fails closed without CONFIG_DIR/CORE_DIR fallback."""
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
scripts_dir = str(Path(__file__).resolve().parents[2] / "scripts")
|
||||
base_env = {
|
||||
k: v
|
||||
for k, v in os.environ.items()
|
||||
if k not in {"HOMELAB_DEPLOYMENTS_FILE", "WORKLOADS_DIR", "SITES_DIR"}
|
||||
}
|
||||
base_env["HOME"] = str(tmp_path)
|
||||
base_env["PYTHONPATH"] = scripts_dir
|
||||
|
||||
resolved_default = subprocess.check_output(
|
||||
[sys.executable, "-c", "import gitops_dispatcher; print(gitops_dispatcher.DEPLOYMENTS_FILE)"],
|
||||
env=base_env,
|
||||
text=True,
|
||||
).strip()
|
||||
resolved_default = run_isolated_script_expr(
|
||||
"import gitops_dispatcher; print(gitops_dispatcher.DEPLOYMENTS_FILE)"
|
||||
)
|
||||
expected_default = tmp_path / "Workloads" / "deployments.yaml"
|
||||
assert Path(resolved_default) == expected_default
|
||||
|
||||
@@ -310,12 +295,10 @@ def test_deployments_file_defaults_to_workloads_and_does_not_fallback(
|
||||
|
||||
# WORKLOADS_DIR override when HOMELAB_DEPLOYMENTS_FILE is unset
|
||||
custom_workloads = tmp_path / "CustomWorkloads"
|
||||
env_with_workloads = dict(base_env, WORKLOADS_DIR=str(custom_workloads))
|
||||
resolved_override = subprocess.check_output(
|
||||
[sys.executable, "-c", "import gitops_dispatcher; print(gitops_dispatcher.DEPLOYMENTS_FILE)"],
|
||||
env=env_with_workloads,
|
||||
text=True,
|
||||
).strip()
|
||||
resolved_override = run_isolated_script_expr(
|
||||
"import gitops_dispatcher; print(gitops_dispatcher.DEPLOYMENTS_FILE)",
|
||||
extra_env={"WORKLOADS_DIR": str(custom_workloads)},
|
||||
)
|
||||
assert Path(resolved_override) == custom_workloads / "deployments.yaml"
|
||||
|
||||
|
||||
|
||||
@@ -495,29 +495,14 @@ def test_rbac_viewer_denied_on_sensitive_reads():
|
||||
|
||||
|
||||
def test_default_deployments_file_resolves_to_workloads_dir(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, run_isolated_script_expr
|
||||
):
|
||||
"""DEFAULT_DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml and fails closed when absent."""
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import appctl_auth
|
||||
|
||||
scripts_dir = str(Path(__file__).resolve().parents[2] / "scripts")
|
||||
env = {
|
||||
k: v
|
||||
for k, v in os.environ.items()
|
||||
if k not in {"HOMELAB_DEPLOYMENTS_FILE", "WORKLOADS_DIR", "SITES_DIR"}
|
||||
}
|
||||
env["HOME"] = str(tmp_path)
|
||||
env["PYTHONPATH"] = scripts_dir
|
||||
|
||||
resolved = subprocess.check_output(
|
||||
[sys.executable, "-c", "import appctl_auth; print(appctl_auth.DEFAULT_DEPLOYMENTS_FILE)"],
|
||||
env=env,
|
||||
text=True,
|
||||
).strip()
|
||||
resolved = run_isolated_script_expr(
|
||||
"import appctl_auth; print(appctl_auth.DEFAULT_DEPLOYMENTS_FILE)"
|
||||
)
|
||||
expected = tmp_path / "Workloads" / "deployments.yaml"
|
||||
assert Path(resolved) == expected
|
||||
|
||||
|
||||
@@ -593,45 +593,30 @@ def test_gitops_dispatcher_delegates_to_daemon(daemon_env, monkeypatch):
|
||||
assert mock_sub.call_count >= 1
|
||||
|
||||
|
||||
def test_daemon_deployments_file_defaults_to_workloads_and_fails_closed(
|
||||
daemon_env, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
def test_daemon_deployments_file_defaults_to_workloads_and_honors_override(
|
||||
tmp_path: Path, run_isolated_script_expr
|
||||
):
|
||||
"""
|
||||
DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml (or WORKLOADS_DIR/deployments.yaml)
|
||||
and homelab-operator fails closed (403) when ~/Workloads/deployments.yaml is absent.
|
||||
"""
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import appctl_auth
|
||||
|
||||
scripts_dir = str(Path(__file__).resolve().parents[2] / "scripts")
|
||||
base_env = {
|
||||
k: v
|
||||
for k, v in os.environ.items()
|
||||
if k not in {"HOMELAB_DEPLOYMENTS_FILE", "WORKLOADS_DIR", "SITES_DIR"}
|
||||
}
|
||||
base_env["HOME"] = str(tmp_path)
|
||||
base_env["PYTHONPATH"] = scripts_dir
|
||||
|
||||
resolved_default = subprocess.check_output(
|
||||
[sys.executable, "-c", "import appctl_daemon; print(appctl_daemon.DEPLOYMENTS_FILE)"],
|
||||
env=base_env,
|
||||
text=True,
|
||||
).strip()
|
||||
"""DEPLOYMENTS_FILE defaults to ~/Workloads/deployments.yaml and honors WORKLOADS_DIR override."""
|
||||
resolved_default = run_isolated_script_expr(
|
||||
"import appctl_daemon; print(appctl_daemon.DEPLOYMENTS_FILE)"
|
||||
)
|
||||
assert Path(resolved_default) == tmp_path / "Workloads" / "deployments.yaml"
|
||||
|
||||
# WORKLOADS_DIR override when HOMELAB_DEPLOYMENTS_FILE is unset
|
||||
custom_workloads = tmp_path / "CustomWorkloads"
|
||||
env_with_workloads = dict(base_env, WORKLOADS_DIR=str(custom_workloads))
|
||||
resolved_override = subprocess.check_output(
|
||||
[sys.executable, "-c", "import appctl_daemon; print(appctl_daemon.DEPLOYMENTS_FILE)"],
|
||||
env=env_with_workloads,
|
||||
text=True,
|
||||
).strip()
|
||||
resolved_override = run_isolated_script_expr(
|
||||
"import appctl_daemon; print(appctl_daemon.DEPLOYMENTS_FILE)",
|
||||
extra_env={"WORKLOADS_DIR": str(custom_workloads)},
|
||||
)
|
||||
assert Path(resolved_override) == custom_workloads / "deployments.yaml"
|
||||
|
||||
|
||||
def test_daemon_operator_fails_closed_when_deployments_file_absent(
|
||||
daemon_env, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
):
|
||||
"""homelab-operator fails closed (403 ERR_PERMISSION_DENIED) when ~/Workloads/deployments.yaml is absent."""
|
||||
import appctl_auth
|
||||
|
||||
# When HOMELAB_DEPLOYMENTS_FILE is unset and ~/Workloads/deployments.yaml is absent,
|
||||
# homelab-operator must be denied (403) without falling back to CORE_DIR/config/deployments.yaml.
|
||||
monkeypatch.delenv("HOMELAB_DEPLOYMENTS_FILE", raising=False)
|
||||
|
||||
Reference in new issue
Block a user