Repo-side rename only (Phases 1-3 of the migration plan); the live box (~/.castle, systemd units, /data/castle, domains) is a separate cutover. - Slug `castle` -> `wildpc`: CLI command, module names (wildpc_core/cli/api), dist names, entry point `wildpc = wildpc_cli.main:main`. - Identifiers: CastleConfig/NATSClient/DirError/MDNS -> Wildpc*. - Env/constants: CASTLE_* -> WILDPC_*; ~/.castle -> ~/.wildpc, castle.yaml -> wildpc.yaml, /data/castle -> /data/wildpc. - Systemd UNIT_PREFIX castle- -> wildpc-; own programs castle-api/gateway/etc. - Display prose "Castle" -> "Wild PC" in docs, agent-guide files, README, frontend. - Package dirs and bootstrap yaml renamed via git mv; lockfiles regenerated; redundant nested uv.lock files dropped (workspace root lock is authoritative). Tests: core 273, cli 47, wildpc-api 120 all pass. Frontend type-checks + builds. Fixed a stale test fixture (secret_env_path kind arg) broken pre-rename.
88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
"""Tests for the generated secret env file and its registry visibility."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import stat
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import wildpc_core.deploy as deploy
|
|
from wildpc_core.registry import (
|
|
Deployment,
|
|
NodeConfig,
|
|
NodeRegistry,
|
|
load_registry,
|
|
save_registry,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def secret_env_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
"""Redirect the secret-env file location into a temp dir."""
|
|
d = tmp_path / "secrets" / "env"
|
|
monkeypatch.setattr(deploy, "SECRET_ENV_DIR", d)
|
|
monkeypatch.setattr(
|
|
deploy,
|
|
"secret_env_path",
|
|
lambda name, kind="service": d
|
|
/ f"wildpc-{name}{'-job' if kind == 'job' else ''}.service.env",
|
|
)
|
|
return d
|
|
|
|
|
|
class TestWriteSecretEnvFile:
|
|
def test_writes_mode_0600_in_0700_dir(self, secret_env_dir: Path) -> None:
|
|
path = deploy._write_secret_env_file("svc", {"API_KEY": "sk-123"})
|
|
assert path is not None and path.exists()
|
|
assert stat.S_IMODE(path.stat().st_mode) == 0o600
|
|
assert stat.S_IMODE(secret_env_dir.stat().st_mode) == 0o700
|
|
|
|
def test_content_format(self, secret_env_dir: Path) -> None:
|
|
path = deploy._write_secret_env_file(
|
|
"svc", {"A": "1", "NEO4J_AUTH": "neo4j/pw"}
|
|
)
|
|
assert path is not None
|
|
assert path.read_text() == "A=1\nNEO4J_AUTH=neo4j/pw\n"
|
|
|
|
def test_empty_unlinks_and_returns_none(self, secret_env_dir: Path) -> None:
|
|
path = deploy.secret_env_path("svc")
|
|
secret_env_dir.mkdir(parents=True)
|
|
path.write_text("STALE=1\n")
|
|
result = deploy._write_secret_env_file("svc", {})
|
|
assert result is None
|
|
assert not path.exists()
|
|
|
|
def test_rewrite_truncates_and_fixes_mode(self, secret_env_dir: Path) -> None:
|
|
deploy._write_secret_env_file("svc", {"A": "old", "B": "x"})
|
|
path = deploy._write_secret_env_file("svc", {"A": "new"})
|
|
assert path is not None
|
|
assert path.read_text() == "A=new\n"
|
|
assert stat.S_IMODE(path.stat().st_mode) == 0o600
|
|
|
|
|
|
class TestRegistrySecretKeys:
|
|
def test_round_trip_persists_keys_only(self, tmp_path: Path) -> None:
|
|
reg_path = tmp_path / "registry.yaml"
|
|
registry = NodeRegistry(node=NodeConfig(hostname="h"))
|
|
registry.put(
|
|
Deployment(
|
|
manager="systemd", launcher="container",
|
|
run_cmd=["docker", "run"],
|
|
env={"PORT": "9001"},
|
|
secret_env_keys=["ANTHROPIC_API_KEY", "OPENAI_API_KEY"],
|
|
managed=True,
|
|
name="svc",
|
|
)
|
|
)
|
|
save_registry(registry, reg_path)
|
|
|
|
text = reg_path.read_text()
|
|
assert "ANTHROPIC_API_KEY" in text # names are fine
|
|
assert "sk-ant" not in text # but no values ever
|
|
|
|
loaded = load_registry(reg_path)
|
|
svc = loaded.get("service", "svc")
|
|
assert svc.secret_env_keys == ["ANTHROPIC_API_KEY", "OPENAI_API_KEY"]
|
|
assert svc.env == {"PORT": "9001"}
|