feat: implement repoctl L1 core (models, schemas, validate, discover, context, init, adopt)
Adds the repository-stewardship Python package (src/stewardship) with: - JSON Schemas (Draft 2020-12) for RepositoryContract, KnowledgeBase, CatalogIndex, ContextBundle, BootstrapPlan, AdoptionAssessment/Plan/Report, user config and the repoctl-result envelope. - Safe YAML I/O, path confinement, deterministic clock and canonical-JSON hashing. - validate: schema, ID uniqueness, path safety, KDB references, freshness, secret-shaped fields, shell-string detection, and Markdown frontmatter/fence/link checks. - discover/CatalogIndex with deterministic sorting and duplicate-name detection. - describe (contract-only) and context (intent-scoped ContextBundle with budgeted materialization and --strict truncation). - init: plan-then-apply bootstrap with SHA-256 plan-hash approval, no fake domain artifacts, bundled Claude skills. - adopt: assess/plan/apply/verify overlay workflow per docs/adoption-runbook.md, preserving legacy content roots and KDB, with baseline-hash re-check and rollback. - argparse-based `repoctl` CLI with human/yaml/json output and spec exit codes. - pytest suite (39 tests) covering positive/negative fixtures, discovery, context, bootstrap and adoption (including LXD- and Antivirus-shaped legacy layouts). Managed with uv (.venv), ruff clean, mypy --strict clean. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
9a39b8a941
commit
96d4d08230
@@ -0,0 +1,48 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
FIXTURES = Path(__file__).parent / "fixtures"
|
||||
|
||||
|
||||
def _git_init(repo: Path) -> None:
|
||||
subprocess.run(["git", "init", "-q"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "config", "user.email", "test@example.com"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "config", "user.name", "Test"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "add", "-A"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "commit", "-q", "-m", "fixture baseline"], cwd=repo, check=True)
|
||||
|
||||
|
||||
def materialize_fixture(tmp_path: Path, relative: str, *, git: bool = True) -> Path:
|
||||
src = FIXTURES / relative
|
||||
dest = tmp_path / Path(relative).name
|
||||
shutil.copytree(src, dest)
|
||||
if git:
|
||||
_git_init(dest)
|
||||
return dest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_repo(tmp_path):
|
||||
def _make(relative: str, *, git: bool = True) -> Path:
|
||||
return materialize_fixture(tmp_path, relative, git=git)
|
||||
|
||||
return _make
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def negative_repo(tmp_path):
|
||||
"""Materialize a negative/<name>/contract.yaml fixture as ops/contract.yaml under a fresh repo."""
|
||||
|
||||
def _make(name: str) -> Path:
|
||||
dest = tmp_path / name
|
||||
(dest / "ops").mkdir(parents=True)
|
||||
shutil.copy(FIXTURES / "negative" / name / "contract.yaml", dest / "ops" / "contract.yaml")
|
||||
_git_init(dest)
|
||||
return dest
|
||||
|
||||
return _make
|
||||
@@ -0,0 +1,2 @@
|
||||
custom_format: true
|
||||
notes: Pre-existing custom knowledge base that already occupies kdb/kdb.yaml
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
windows_agent_rollout:
|
||||
summary: Legacy root-level knowledge base, predates Repository Stewardship
|
||||
@@ -0,0 +1,2 @@
|
||||
name: default
|
||||
config: {}
|
||||
@@ -0,0 +1,2 @@
|
||||
name: default
|
||||
config: {}
|
||||
@@ -0,0 +1,15 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: RepositoryContract
|
||||
metadata:
|
||||
name: bad-absolute-path
|
||||
spec:
|
||||
summary: Content root uses an absolute/UNC-shaped path
|
||||
repository:
|
||||
type: project
|
||||
lifecycle: stub
|
||||
contentRoots:
|
||||
- id: absolute
|
||||
role: documentation
|
||||
path: "/etc/passwd"
|
||||
kind: file
|
||||
authority: source
|
||||
@@ -0,0 +1,20 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: RepositoryContract
|
||||
metadata:
|
||||
name: bad-duplicate-ids
|
||||
spec:
|
||||
summary: Has duplicate content root ids
|
||||
repository:
|
||||
type: project
|
||||
lifecycle: stub
|
||||
contentRoots:
|
||||
- id: docs
|
||||
role: documentation
|
||||
path: docs
|
||||
kind: directory
|
||||
authority: source
|
||||
- id: docs
|
||||
role: documentation
|
||||
path: docs2
|
||||
kind: directory
|
||||
authority: source
|
||||
@@ -0,0 +1,15 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: RepositoryContract
|
||||
metadata:
|
||||
name: bad-path-traversal
|
||||
spec:
|
||||
summary: Content root escapes the repository root
|
||||
repository:
|
||||
type: project
|
||||
lifecycle: stub
|
||||
contentRoots:
|
||||
- id: escape
|
||||
role: documentation
|
||||
path: "../outside"
|
||||
kind: directory
|
||||
authority: source
|
||||
@@ -0,0 +1,11 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: RepositoryContract
|
||||
metadata:
|
||||
name: bad-secret-field
|
||||
annotations:
|
||||
token: "super-secret-value-should-be-blocked"
|
||||
spec:
|
||||
summary: Contains a secret-shaped field
|
||||
repository:
|
||||
type: project
|
||||
lifecycle: stub
|
||||
@@ -0,0 +1,18 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: RepositoryContract
|
||||
metadata:
|
||||
name: bad-shell-string
|
||||
spec:
|
||||
summary: Operation argv contains a shell metacharacter
|
||||
repository:
|
||||
type: infrastructure-config
|
||||
lifecycle: stub
|
||||
operations:
|
||||
- id: container.inspect
|
||||
phase: inspect
|
||||
effect: read-only
|
||||
risk: low
|
||||
execution:
|
||||
handler: ops/handlers/lxd.py
|
||||
argv:
|
||||
- "inspect && rm -rf /"
|
||||
@@ -0,0 +1,10 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: RepositoryContract
|
||||
metadata:
|
||||
name: bad-unknown-field
|
||||
spec:
|
||||
summary: Has an unknown normative field
|
||||
repository:
|
||||
type: project
|
||||
lifecycle: active
|
||||
unexpectedField: surprise
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
# infra-lxd agent instructions
|
||||
|
||||
Read `ops/contract.yaml`, then use `repoctl context` for the task at hand.
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
# CLAUDE.md
|
||||
|
||||
@AGENTS.md
|
||||
@@ -0,0 +1,3 @@
|
||||
# Container deployment
|
||||
|
||||
Create containers from `profiles-source/`.
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: KnowledgeBase
|
||||
metadata:
|
||||
name: infra-lxd-kdb
|
||||
spec:
|
||||
repository: repo:infra-lxd
|
||||
defaultLanguage: en
|
||||
topics:
|
||||
- id: container-deployment
|
||||
summary: Canonical local process for creating an LXD container
|
||||
intents:
|
||||
- container.deploy
|
||||
documents:
|
||||
- container-deployment-howto
|
||||
documents:
|
||||
- id: container-deployment-howto
|
||||
title: Container deployment how-to
|
||||
role: how-to
|
||||
path: kdb/container-deployment.md
|
||||
mediaType: text/markdown
|
||||
authority: local-canonical
|
||||
status: verified
|
||||
@@ -0,0 +1,46 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: RepositoryContract
|
||||
metadata:
|
||||
name: infra-lxd
|
||||
title: LXD infrastructure
|
||||
owners:
|
||||
- infrastructure
|
||||
spec:
|
||||
summary: Desired state and operations for managed LXD containers
|
||||
repository:
|
||||
type: infrastructure-config
|
||||
lifecycle: production
|
||||
namespace: Infra
|
||||
agent:
|
||||
primary: CLAUDE.md
|
||||
portable: AGENTS.md
|
||||
knowledge:
|
||||
entrypoint: kdb/kdb.yaml
|
||||
contentRoots:
|
||||
- id: profiles-source
|
||||
role: profiles
|
||||
path: profiles-source
|
||||
kind: directory
|
||||
authority: source
|
||||
writePolicy: managed
|
||||
selector: "**/*.yaml"
|
||||
- id: profiles-observed
|
||||
role: observed
|
||||
path: profiles
|
||||
kind: directory
|
||||
authority: mirror
|
||||
writePolicy: generated
|
||||
selector: "**/*.yaml"
|
||||
provides:
|
||||
- id: container
|
||||
capability: compute.lxd-container
|
||||
summary: Managed LXD system container
|
||||
operations:
|
||||
- id: container.inspect
|
||||
title: Inspect containers
|
||||
intents:
|
||||
- container.inspect
|
||||
- container.deploy
|
||||
phase: inspect
|
||||
effect: read-only
|
||||
risk: low
|
||||
@@ -0,0 +1,2 @@
|
||||
name: default
|
||||
config: {}
|
||||
@@ -0,0 +1,2 @@
|
||||
name: default
|
||||
config: {}
|
||||
@@ -0,0 +1,3 @@
|
||||
# example-project agent instructions
|
||||
|
||||
Read `ops/contract.yaml`, then use `repoctl context`.
|
||||
@@ -0,0 +1,3 @@
|
||||
# CLAUDE.md
|
||||
|
||||
@AGENTS.md
|
||||
@@ -0,0 +1,3 @@
|
||||
# ADR 0001
|
||||
|
||||
Placeholder decision record.
|
||||
@@ -0,0 +1,3 @@
|
||||
# Deliverable one
|
||||
|
||||
Placeholder deliverable content for a fixture repository.
|
||||
@@ -0,0 +1,9 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: KnowledgeBase
|
||||
metadata:
|
||||
name: example-project-kdb
|
||||
spec:
|
||||
repository: repo:example-project
|
||||
defaultLanguage: en
|
||||
topics: []
|
||||
documents: []
|
||||
@@ -0,0 +1,30 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: RepositoryContract
|
||||
metadata:
|
||||
name: example-project
|
||||
title: Example project
|
||||
owners:
|
||||
- example-team
|
||||
spec:
|
||||
summary: Example non-code project
|
||||
repository:
|
||||
type: project
|
||||
lifecycle: active
|
||||
agent:
|
||||
primary: CLAUDE.md
|
||||
portable: AGENTS.md
|
||||
knowledge:
|
||||
entrypoint: kdb/kdb.yaml
|
||||
contentRoots:
|
||||
- id: deliverables
|
||||
role: deliverables
|
||||
path: deliverables
|
||||
kind: directory
|
||||
authority: source
|
||||
writePolicy: managed
|
||||
- id: decisions
|
||||
role: decisions
|
||||
path: decisions
|
||||
kind: directory
|
||||
authority: source
|
||||
writePolicy: managed
|
||||
@@ -0,0 +1,3 @@
|
||||
# services-antivirus agent instructions
|
||||
|
||||
Read `ops/contract.yaml`, then use `repoctl context`.
|
||||
@@ -0,0 +1,3 @@
|
||||
# CLAUDE.md
|
||||
|
||||
@AGENTS.md
|
||||
@@ -0,0 +1,9 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: KnowledgeBase
|
||||
metadata:
|
||||
name: services-antivirus-kdb
|
||||
spec:
|
||||
repository: repo:services-antivirus
|
||||
defaultLanguage: en
|
||||
topics: []
|
||||
documents: []
|
||||
@@ -0,0 +1,46 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: RepositoryContract
|
||||
metadata:
|
||||
name: services-antivirus
|
||||
title: Antivirus service
|
||||
owners:
|
||||
- security
|
||||
spec:
|
||||
summary: Product configuration, knowledge and acceptance for antivirus services
|
||||
repository:
|
||||
type: service-config
|
||||
lifecycle: production
|
||||
namespace: Services
|
||||
agent:
|
||||
primary: CLAUDE.md
|
||||
portable: AGENTS.md
|
||||
knowledge:
|
||||
entrypoint: kdb/kdb.yaml
|
||||
contentRoots:
|
||||
- id: product-policies
|
||||
role: policies
|
||||
path: policies
|
||||
kind: directory
|
||||
authority: source
|
||||
writePolicy: managed
|
||||
- id: service-acceptance
|
||||
role: acceptance
|
||||
path: acceptance
|
||||
kind: directory
|
||||
authority: source
|
||||
writePolicy: managed
|
||||
requires:
|
||||
- id: control-plane-container
|
||||
capability: compute.lxd-container
|
||||
summary: Container for antivirus control plane
|
||||
acceptance:
|
||||
- operation:services-antivirus/control-plane.verify
|
||||
operations:
|
||||
- id: control-plane.verify
|
||||
title: Verify antivirus control plane
|
||||
intents:
|
||||
- container.deploy
|
||||
- service.verify
|
||||
phase: verify
|
||||
effect: read-only
|
||||
risk: low
|
||||
@@ -0,0 +1,3 @@
|
||||
# example-software agent instructions
|
||||
|
||||
Read `ops/contract.yaml`, then use `repoctl context`.
|
||||
@@ -0,0 +1,3 @@
|
||||
# CLAUDE.md
|
||||
|
||||
@AGENTS.md
|
||||
@@ -0,0 +1,9 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: KnowledgeBase
|
||||
metadata:
|
||||
name: example-software-kdb
|
||||
spec:
|
||||
repository: repo:example-software
|
||||
defaultLanguage: en
|
||||
topics: []
|
||||
documents: []
|
||||
@@ -0,0 +1,30 @@
|
||||
apiVersion: stewardship.corsus.ru/v1alpha1
|
||||
kind: RepositoryContract
|
||||
metadata:
|
||||
name: example-software
|
||||
title: Example software
|
||||
owners:
|
||||
- example-team
|
||||
spec:
|
||||
summary: Example software repository
|
||||
repository:
|
||||
type: software
|
||||
lifecycle: active
|
||||
agent:
|
||||
primary: CLAUDE.md
|
||||
portable: AGENTS.md
|
||||
knowledge:
|
||||
entrypoint: kdb/kdb.yaml
|
||||
contentRoots:
|
||||
- id: source
|
||||
role: source
|
||||
path: src
|
||||
kind: directory
|
||||
authority: source
|
||||
writePolicy: managed
|
||||
- id: tests
|
||||
role: tests
|
||||
path: tests
|
||||
kind: directory
|
||||
authority: source
|
||||
writePolicy: managed
|
||||
@@ -0,0 +1 @@
|
||||
print("example")
|
||||
@@ -0,0 +1,2 @@
|
||||
def test_placeholder():
|
||||
assert True
|
||||
@@ -0,0 +1,91 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from stewardship import adoption
|
||||
|
||||
|
||||
def test_assess_lxd_legacy_preserves_layout(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
result = adoption.assess(repo)
|
||||
assert result["repository"]["dirty"] is False
|
||||
assert result["classification"]["proposedType"] == "infrastructure-config"
|
||||
kdb_actions = {(entry["path"], entry["action"]) for entry in result["knowledge"]}
|
||||
assert ("kdb.yaml", "wrap") in kdb_actions
|
||||
root_paths = {cr["path"] for cr in result["proposedContentRoots"]}
|
||||
assert "profiles-source" in root_paths
|
||||
|
||||
|
||||
def test_plan_lxd_legacy_creates_contract_and_kdb_adapter_without_moving_profiles(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(
|
||||
repo, assessment, confirmed_name="infra-lxd", confirmed_owners=["infrastructure"], confirmed_lifecycle="production"
|
||||
)
|
||||
changes_by_path = {c["path"]: c for c in plan["changes"]}
|
||||
assert changes_by_path["ops/contract.yaml"]["action"] == "create"
|
||||
assert changes_by_path["kdb/kdb.yaml"]["action"] == "create"
|
||||
assert changes_by_path["kdb.yaml"]["action"] == "preserve"
|
||||
assert "profiles-source/**" in plan["forbiddenChanges"]
|
||||
assert "profiles/**" in plan["forbiddenChanges"]
|
||||
assert plan["planHash"].startswith("sha256:")
|
||||
|
||||
|
||||
def test_apply_does_not_touch_working_tree_before_approval(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
before = sorted(p.relative_to(repo).as_posix() for p in repo.rglob("*") if p.is_file())
|
||||
assessment = adoption.assess(repo)
|
||||
adoption.build_plan(repo, assessment, confirmed_name="infra-lxd")
|
||||
after = sorted(p.relative_to(repo).as_posix() for p in repo.rglob("*") if p.is_file())
|
||||
assert before == after
|
||||
|
||||
|
||||
def test_apply_rejects_stale_baseline(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="infra-lxd")
|
||||
plan["baselineRevision"] = "0" * 40
|
||||
with pytest.raises(ValueError):
|
||||
adoption.apply_plan(repo, plan, plan["planHash"])
|
||||
assert not (repo / "ops" / "contract.yaml").exists()
|
||||
|
||||
|
||||
def test_apply_and_verify_lxd_legacy(make_repo):
|
||||
repo = make_repo("adoption/lxd-legacy")
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(
|
||||
repo, assessment, confirmed_name="infra-lxd", confirmed_owners=["infrastructure"], confirmed_lifecycle="production"
|
||||
)
|
||||
written = adoption.apply_plan(repo, plan, plan["planHash"])
|
||||
assert "ops/contract.yaml" in written
|
||||
assert (repo / "profiles-source" / "default.yaml").exists()
|
||||
assert (repo / "profiles" / "default.yaml").exists()
|
||||
assert (repo / "kdb.yaml").exists()
|
||||
|
||||
report = adoption.verify(repo, plan)
|
||||
assert "profiles-source" in report["preservedPaths"] or "kdb.yaml" in report["preservedPaths"]
|
||||
assert report["outcome"] in {"pass", "pass-with-warnings"}
|
||||
|
||||
|
||||
def test_assess_antivirus_legacy_detects_occupied_kdb_path(make_repo):
|
||||
repo = make_repo("adoption/antivirus-legacy")
|
||||
result = adoption.assess(repo)
|
||||
kdb_paths = {entry["path"] for entry in result["knowledge"]}
|
||||
assert "kdb/kdb.yaml" in kdb_paths
|
||||
|
||||
|
||||
def test_plan_antivirus_legacy_does_not_overwrite_existing_kdb(make_repo):
|
||||
repo = make_repo("adoption/antivirus-legacy")
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="services-antivirus")
|
||||
changes_by_path = {c["path"]: c for c in plan["changes"]}
|
||||
assert changes_by_path["kdb/kdb.yaml"]["action"] == "preserve"
|
||||
|
||||
|
||||
def test_apply_preserves_existing_antivirus_kdb_bytes(make_repo):
|
||||
repo = make_repo("adoption/antivirus-legacy")
|
||||
original = (repo / "kdb" / "kdb.yaml").read_bytes()
|
||||
assessment = adoption.assess(repo)
|
||||
plan = adoption.build_plan(repo, assessment, confirmed_name="services-antivirus")
|
||||
adoption.apply_plan(repo, plan, plan["planHash"])
|
||||
assert (repo / "kdb" / "kdb.yaml").read_bytes() == original
|
||||
@@ -0,0 +1,51 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from stewardship.bootstrap import BootstrapAnswers, apply_bootstrap_plan, build_bootstrap_plan
|
||||
from stewardship.validate import validate_repository
|
||||
|
||||
|
||||
def test_bootstrap_plan_writes_nothing(tmp_path):
|
||||
answers = BootstrapAnswers(name="new-proj", title="New project", owners=["team"], repository_type="project")
|
||||
plan = build_bootstrap_plan(answers)
|
||||
assert list(tmp_path.iterdir()) == []
|
||||
assert plan["planHash"].startswith("sha256:")
|
||||
|
||||
|
||||
def test_bootstrap_apply_requires_matching_approval_hash(tmp_path):
|
||||
answers = BootstrapAnswers(name="new-proj", title="New project", owners=["team"], repository_type="project")
|
||||
plan = build_bootstrap_plan(answers)
|
||||
with pytest.raises(ValueError):
|
||||
apply_bootstrap_plan(tmp_path, plan, "sha256:not-the-real-hash")
|
||||
assert list(tmp_path.iterdir()) == []
|
||||
|
||||
|
||||
def test_bootstrap_apply_writes_and_validates(tmp_path):
|
||||
import subprocess
|
||||
|
||||
subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True)
|
||||
answers = BootstrapAnswers(
|
||||
name="new-proj", title="New project", owners=["team"], repository_type="project", lifecycle="active"
|
||||
)
|
||||
plan = build_bootstrap_plan(answers)
|
||||
written = apply_bootstrap_plan(tmp_path, plan, plan["planHash"])
|
||||
assert "ops/contract.yaml" in written
|
||||
assert (tmp_path / "AGENTS.md").exists()
|
||||
assert (tmp_path / ".claude/skills/repository-bootstrap/SKILL.md").exists()
|
||||
|
||||
result = validate_repository(tmp_path)
|
||||
errors = [f for f in result.findings if f.severity == "error"]
|
||||
assert errors == []
|
||||
|
||||
|
||||
def test_bootstrap_apply_refuses_to_overwrite(tmp_path):
|
||||
import subprocess
|
||||
|
||||
subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True)
|
||||
(tmp_path / "AGENTS.md").write_text("existing", encoding="utf-8")
|
||||
answers = BootstrapAnswers(name="new-proj", title="New project", owners=["team"], repository_type="project")
|
||||
plan = build_bootstrap_plan(answers)
|
||||
with pytest.raises(FileExistsError):
|
||||
apply_bootstrap_plan(tmp_path, plan, plan["planHash"])
|
||||
assert (tmp_path / "AGENTS.md").read_text(encoding="utf-8") == "existing"
|
||||
@@ -0,0 +1,41 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from stewardship.cli import main
|
||||
|
||||
|
||||
def test_validate_exit_codes(make_repo, negative_repo):
|
||||
good = make_repo("positive/infra-lxd")
|
||||
assert main(["validate", "--repo", str(good), "--output", "json"]) == 0
|
||||
|
||||
bad = negative_repo("secret-field")
|
||||
assert main(["validate", "--repo", str(bad), "--output", "json"]) == 1
|
||||
|
||||
|
||||
def test_missing_repository_returns_exit_3(tmp_path):
|
||||
missing = tmp_path / "does-not-exist"
|
||||
assert main(["validate", "--repo", str(missing), "--output", "json"]) == 3
|
||||
|
||||
|
||||
def test_context_strict_truncation_returns_exit_4(make_repo):
|
||||
repo = make_repo("positive/infra-lxd")
|
||||
code = main(
|
||||
[
|
||||
"context",
|
||||
"--repo",
|
||||
str(repo),
|
||||
"--intent",
|
||||
"container.deploy",
|
||||
"--materialize",
|
||||
"--budget-bytes",
|
||||
"1",
|
||||
"--strict",
|
||||
"--output",
|
||||
"json",
|
||||
]
|
||||
)
|
||||
assert code == 4
|
||||
|
||||
|
||||
def test_init_apply_without_hash_returns_exit_2(tmp_path):
|
||||
code = main(["init", "--repo", str(tmp_path), "--apply", "--output", "json"])
|
||||
assert code == 2
|
||||
@@ -0,0 +1,35 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from stewardship.context import build_context
|
||||
|
||||
|
||||
def test_context_selects_only_matching_topic(make_repo):
|
||||
repo = make_repo("positive/infra-lxd")
|
||||
result = build_context(repo, "container.deploy")
|
||||
bundle = result.bundle
|
||||
assert bundle["spec"]["intent"] == "container.deploy"
|
||||
assert bundle["spec"]["topics"] == ["topic:infra-lxd/container-deployment"]
|
||||
assert bundle["spec"]["documents"] == ["container-deployment-howto"]
|
||||
assert "operation:infra-lxd/container.inspect" in bundle["spec"]["operations"]
|
||||
|
||||
|
||||
def test_context_unrelated_intent_returns_empty_topics(make_repo):
|
||||
repo = make_repo("positive/infra-lxd")
|
||||
result = build_context(repo, "backup.restore")
|
||||
bundle = result.bundle
|
||||
assert bundle["spec"]["topics"] == []
|
||||
assert bundle["spec"]["documents"] == []
|
||||
|
||||
|
||||
def test_context_materialize_respects_budget_and_warns(make_repo):
|
||||
repo = make_repo("positive/infra-lxd")
|
||||
result = build_context(repo, "container.deploy", materialize=True, budget_bytes=5)
|
||||
assert result.truncated is True
|
||||
assert any("CONTEXT_BUDGET_TRUNCATED" in w for w in result.bundle["spec"]["warnings"])
|
||||
assert result.materialized[0].bytes_used <= 5
|
||||
|
||||
|
||||
def test_context_strict_mode_signaled_via_truncation_flag(make_repo):
|
||||
repo = make_repo("positive/infra-lxd")
|
||||
result = build_context(repo, "container.deploy", materialize=True, budget_bytes=0, strict=True)
|
||||
assert result.truncated is True
|
||||
@@ -0,0 +1,64 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from stewardship.discovery import build_catalog_index
|
||||
|
||||
|
||||
def test_catalog_finds_all_positive_fixtures_and_is_deterministic(tmp_path, make_repo):
|
||||
make_repo("positive/infra-lxd")
|
||||
make_repo("positive/service-antivirus")
|
||||
make_repo("positive/software-example")
|
||||
make_repo("positive/project-example")
|
||||
|
||||
catalog1, findings1 = build_catalog_index([tmp_path])
|
||||
catalog2, _findings2 = build_catalog_index([tmp_path])
|
||||
|
||||
names = {r["ref"] for r in catalog1["spec"]["repositories"]}
|
||||
assert names == {
|
||||
"repo:infra-lxd",
|
||||
"repo:services-antivirus",
|
||||
"repo:example-software",
|
||||
"repo:example-project",
|
||||
}
|
||||
assert not any(f.severity == "error" for f in findings1)
|
||||
|
||||
# Deterministic rebuild ignoring the volatile generatedAt timestamp.
|
||||
repos1 = catalog1["spec"]["repositories"]
|
||||
repos2 = catalog2["spec"]["repositories"]
|
||||
assert repos1 == repos2
|
||||
assert catalog1["spec"]["relations"] == catalog2["spec"]["relations"]
|
||||
|
||||
|
||||
def test_catalog_matches_requirement_to_capability(tmp_path, make_repo):
|
||||
make_repo("positive/infra-lxd")
|
||||
make_repo("positive/service-antivirus")
|
||||
|
||||
catalog, _ = build_catalog_index([tmp_path])
|
||||
relations = catalog["spec"]["relations"]
|
||||
matches = [r for r in relations if r["type"] == "matches"]
|
||||
assert any(
|
||||
r["from"] == "requirement:services-antivirus/control-plane-container"
|
||||
and r["to"] == "capability:infra-lxd/container"
|
||||
for r in matches
|
||||
)
|
||||
|
||||
|
||||
def test_catalog_detects_duplicate_repository_name(tmp_path, make_repo):
|
||||
first = make_repo("positive/infra-lxd", git=False)
|
||||
import shutil
|
||||
|
||||
second_parent = tmp_path / "duplicate-holder"
|
||||
second_parent.mkdir()
|
||||
second = second_parent / "infra-lxd-copy"
|
||||
shutil.copytree(first, second)
|
||||
|
||||
import subprocess
|
||||
|
||||
for repo in (first, second):
|
||||
subprocess.run(["git", "init", "-q"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "config", "user.email", "test@example.com"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "config", "user.name", "Test"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "add", "-A"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "commit", "-q", "-m", "baseline"], cwd=repo, check=True)
|
||||
|
||||
_, findings = build_catalog_index([tmp_path])
|
||||
assert any(f.code == "CATALOG_DUPLICATE_REPOSITORY_NAME" for f in findings)
|
||||
@@ -0,0 +1,32 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from io import StringIO
|
||||
|
||||
import pytest
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from stewardship.cli import main
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"fixture",
|
||||
[
|
||||
"positive/infra-lxd",
|
||||
"positive/service-antivirus",
|
||||
"positive/software-example",
|
||||
"positive/project-example",
|
||||
],
|
||||
)
|
||||
def test_validate_yaml_and_json_output_have_same_findings(make_repo, fixture, capsys):
|
||||
repo = make_repo(fixture)
|
||||
|
||||
assert main(["validate", "--repo", str(repo), "--output", "json"]) == 0
|
||||
json_out = json.loads(capsys.readouterr().out)
|
||||
|
||||
assert main(["validate", "--repo", str(repo), "--output", "yaml"]) == 0
|
||||
yaml_text = capsys.readouterr().out
|
||||
parsed_yaml = YAML(typ="safe").load(StringIO(yaml_text))
|
||||
|
||||
assert json_out["findings"] == parsed_yaml["findings"]
|
||||
assert json_out["outcome"] == parsed_yaml["outcome"]
|
||||
@@ -0,0 +1,23 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from stewardship.validate import validate_repository
|
||||
|
||||
CASES = {
|
||||
"unknown-field": "CONTRACT_SCHEMA_INVALID",
|
||||
"duplicate-ids": "ID_DUPLICATE",
|
||||
"path-traversal": "CONTRACT_PATH_OUTSIDE_REPOSITORY",
|
||||
"absolute-path": "CONTRACT_PATH_OUTSIDE_REPOSITORY",
|
||||
"secret-field": "SECRET_SHAPED_FIELD",
|
||||
"shell-string": "OPERATION_ARGV_SHELL_STRING",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name,expected_code", CASES.items())
|
||||
def test_negative_fixture_is_rejected(negative_repo, name, expected_code):
|
||||
repo = negative_repo(name)
|
||||
result = validate_repository(repo)
|
||||
codes = {f.code for f in result.findings}
|
||||
assert expected_code in codes, (name, codes)
|
||||
assert not result.ok
|
||||
@@ -0,0 +1,27 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from stewardship.validate import validate_repository
|
||||
|
||||
POSITIVE_FIXTURES = [
|
||||
"positive/infra-lxd",
|
||||
"positive/service-antivirus",
|
||||
"positive/software-example",
|
||||
"positive/project-example",
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("fixture", POSITIVE_FIXTURES)
|
||||
def test_positive_fixture_validates_clean(make_repo, fixture):
|
||||
repo = make_repo(fixture)
|
||||
result = validate_repository(repo)
|
||||
errors = [f for f in result.findings if f.severity == "error"]
|
||||
assert errors == [], errors
|
||||
assert result.ok
|
||||
|
||||
|
||||
def test_infra_lxd_content_roots_not_moved(make_repo):
|
||||
repo = make_repo("positive/infra-lxd")
|
||||
assert (repo / "profiles-source" / "default.yaml").exists()
|
||||
assert (repo / "profiles" / "default.yaml").exists()
|
||||
Reference in New Issue
Block a user