faas-gitops 0.5.0__tar.gz → 0.6.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. {faas_gitops-0.5.0/src/faas_gitops.egg-info → faas_gitops-0.6.1}/PKG-INFO +1 -1
  2. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/pyproject.toml +1 -1
  3. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/adapters/bizy_function_provider.py +6 -0
  4. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/cli/app.py +36 -2
  5. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/cli/parser.py +10 -0
  6. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/protocols/faas_function.py +12 -0
  7. faas_gitops-0.6.1/src/faas_gitops/usecases/admit_check.py +225 -0
  8. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/deploy_functions.py +10 -4
  9. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/plan.py +37 -5
  10. {faas_gitops-0.5.0 → faas_gitops-0.6.1/src/faas_gitops.egg-info}/PKG-INFO +1 -1
  11. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops.egg-info/SOURCES.txt +2 -0
  12. faas_gitops-0.6.1/tests/test_admit_check.py +383 -0
  13. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_deploy_functions.py +18 -0
  14. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_plan.py +41 -2
  15. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/MANIFEST.in +0 -0
  16. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/setup.cfg +0 -0
  17. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/__init__.py +0 -0
  18. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/adapters/__init__.py +0 -0
  19. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/adapters/bizy_template_provider.py +0 -0
  20. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/adapters/git_cli_gateway.py +0 -0
  21. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/cli/__init__.py +0 -0
  22. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/config.py +0 -0
  23. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/filtering.py +0 -0
  24. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/infra/__init__.py +0 -0
  25. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/infra/yaml_utils.py +0 -0
  26. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/layout.py +0 -0
  27. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/models.py +0 -0
  28. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/protocols/__init__.py +0 -0
  29. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/protocols/faas_template.py +0 -0
  30. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/protocols/git_gateway.py +0 -0
  31. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/result.py +0 -0
  32. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/__init__.py +0 -0
  33. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/apply_function_changes.py +0 -0
  34. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/apply_plan.py +0 -0
  35. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/apply_template_changes.py +0 -0
  36. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/detect_drift.py +0 -0
  37. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/detect_function_drift.py +0 -0
  38. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/detect_template_drift.py +0 -0
  39. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/format.py +0 -0
  40. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/render_definitions.py +0 -0
  41. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/snapshot.py +0 -0
  42. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/sync_functions.py +0 -0
  43. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops/usecases/sync_templates.py +0 -0
  44. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops.egg-info/dependency_links.txt +0 -0
  45. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops.egg-info/entry_points.txt +0 -0
  46. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops.egg-info/requires.txt +0 -0
  47. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/src/faas_gitops.egg-info/top_level.txt +0 -0
  48. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_apply_function_changes.py +0 -0
  49. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_apply_plan.py +0 -0
  50. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_apply_template_changes.py +0 -0
  51. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_detect_drift.py +0 -0
  52. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_detect_function_drift.py +0 -0
  53. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_format.py +0 -0
  54. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_git_cli_gateway.py +0 -0
  55. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_render_definitions.py +0 -0
  56. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_result_contract.py +0 -0
  57. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_scope_args.py +0 -0
  58. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_snapshot.py +0 -0
  59. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_sync_functions.py +0 -0
  60. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_sync_templates.py +0 -0
  61. {faas_gitops-0.5.0 → faas_gitops-0.6.1}/tests/test_version.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: faas-gitops
3
- Version: 0.5.0
3
+ Version: 0.6.1
4
4
  Summary: Declarative GitOps for FaaS
5
5
  Requires-Python: >=3.12
6
6
  Requires-Dist: aiofiles>=23.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "faas-gitops"
7
- version = "0.5.0"
7
+ version = "0.6.1"
8
8
  description = "Declarative GitOps for FaaS"
9
9
  requires-python = ">=3.12"
10
10
  dependencies = [
@@ -1,4 +1,6 @@
1
1
  from __future__ import annotations
2
+ from pathlib import Path
3
+
2
4
  from bizy_deploy import AsyncFaaSActionProvider
3
5
 
4
6
 
@@ -20,6 +22,10 @@ class BizyFunctionProvider:
20
22
  result = await self._p.get_latest_function_definition(function_id=func_id)
21
23
  return result.to_dict() if result else None
22
24
 
25
+ async def render_definition(self, func_id: str, version: str) -> str | None:
26
+ path = await self._p.get_definition_render(function_id=func_id, version=version)
27
+ return Path(path).read_text() if path else None
28
+
23
29
  async def push_definition(self, func_id: str, data: dict, comment: str = "") -> str:
24
30
  return await self._p.create_function_definition(merged_def=data, function_id=func_id)
25
31
 
@@ -18,6 +18,7 @@ from faas_gitops.config import load_settings, warn_deprecated_env
18
18
  from faas_gitops.models import ApplyResult
19
19
  from faas_gitops.usecases.plan import Plan, PlanBuild
20
20
  from faas_gitops.usecases.apply_function_changes import ApplyFunctionChanges
21
+ from faas_gitops.usecases.admit_check import AdmitCheck, admit_report
21
22
  from faas_gitops.usecases.apply_plan import ApplyPlan, StalePlanError
22
23
  from faas_gitops.usecases.apply_template_changes import ApplyTemplateChanges
23
24
  from faas_gitops.usecases.deploy_functions import DeployFunctions
@@ -88,9 +89,10 @@ async def async_main() -> int:
88
89
  return 0
89
90
 
90
91
  if args.command == "plan":
92
+ faas = await _make_faas(stack)
91
93
  git = _make_git()
92
94
  repo_path = await git.clone_repo(repo_slug)
93
- plan = await PlanBuild(git, settings).run(
95
+ plan = await PlanBuild(git, settings, BizyFunctionProvider(faas)).run(
94
96
  repo_path, base=args.to, ref=args.from_ref, repo_slug=repo_slug,
95
97
  )
96
98
  plan.log()
@@ -101,6 +103,26 @@ async def async_main() -> int:
101
103
  logger.info(f"plan written to {out.resolve()}")
102
104
  return 0 if plan.kind == "none" else 2
103
105
 
106
+ if args.command == "admit":
107
+ faas = await _make_faas(stack)
108
+ git = _make_git()
109
+ repo_path = await git.clone_repo(repo_slug)
110
+ plan = Plan.from_dict(json.loads(Path(args.plan).read_text()))
111
+ try:
112
+ admitted = await AdmitCheck(BizyFunctionProvider(faas), BizyTemplateProvider(faas), git).run(repo_path, plan)
113
+ except StalePlanError as e:
114
+ logger.error(str(e))
115
+ return 1
116
+ out = Path(args.output)
117
+ out.parent.mkdir(parents=True, exist_ok=True)
118
+ out.write_text(json.dumps(admitted.payload(), ensure_ascii=False, indent=2, sort_keys=True) + "\n")
119
+ logger.info(f"admitted plan written to {out.resolve()}")
120
+ rc = admit_report(admitted.payload()).emit()
121
+ if args.strict and admitted.skipped:
122
+ logger.error(f"--strict: {len(admitted.skipped)} entr(y/ies) refused → exit 1")
123
+ return 1
124
+ return rc
125
+
104
126
  if args.command == "apply-plan":
105
127
  faas = await _make_faas(stack)
106
128
  git = _make_git()
@@ -122,9 +144,21 @@ async def async_main() -> int:
122
144
  print_apply_report(tpl_result, dry_run, _out("templates"), domain="templates"),
123
145
  print_apply_report(fn_result, dry_run, _out("functions"), domain="functions"),
124
146
  )
147
+ for f in plan.functions: # per-deployment visibility (fid/depid, admit marks)
148
+ for d in f.deployments:
149
+ if isinstance(d, dict):
150
+ refused = d.get("refused")
151
+ logger.info(
152
+ f" ◆ {f.id}/{d['id']} {d.get('status', '?')} v{d.get('definition_version', '?')}"
153
+ + (f" ⏸ {refused}" if refused else "")
154
+ )
125
155
  if not dry_run and args.auto_deploy and fn_result.applied_versions:
156
+ refused_deployments = {
157
+ (f.id, d["id"]) for f in plan.functions for d in f.deployments
158
+ if isinstance(d, dict) and "refused" in d
159
+ }
126
160
  deploy_result = await DeployFunctions(BizyFunctionProvider(faas), settings).run(
127
- fn_result.applied_versions, repo_path
161
+ fn_result.applied_versions, repo_path, refused_deployments=refused_deployments
128
162
  )
129
163
  rc = max(rc, print_deploy_report(deploy_result, _out("deploy")))
130
164
  return rc
@@ -90,6 +90,16 @@ def build_parser(settings: Settings) -> argparse.ArgumentParser:
90
90
  add_scope_args(p)
91
91
  _add_repo(p)
92
92
 
93
+ p = sub.add_parser("admit")
94
+ p.add_argument("--plan", required=True, help="path to plan.json produced by `faas-gitops plan`")
95
+ p.add_argument("-o", "--output", required=True,
96
+ help="write the narrowed plan here (drop-in --plan input for apply-plan; "
97
+ "refused deployments marked in place, all-refused functions under `skipped: [{id, error}]`)")
98
+ p.add_argument("--strict", action="store_true",
99
+ help="exit 1 when anything was refused (default exit 0 — refusal is a skip)")
100
+ add_scope_args(p)
101
+ p.add_argument("--repo", default=settings.default_repo)
102
+
93
103
  p = sub.add_parser("render-definitions")
94
104
  p.add_argument("--branch", default=None)
95
105
  add_scope_args(p)
@@ -31,6 +31,18 @@ class FaaSFunctionProvider(Protocol):
31
31
  """
32
32
  ...
33
33
 
34
+ async def render_definition(self, func_id: str, version: str) -> str | None:
35
+ """Render one definition version into deployable YAML (current template on FaaS).
36
+
37
+ Args:
38
+ func_id: Function identifier.
39
+ version: Definition version number (e.g. a deployment's definition_version).
40
+
41
+ Returns:
42
+ Rendered YAML string (same shape as rendered.yaml files), or None if unavailable.
43
+ """
44
+ ...
45
+
34
46
  async def push_definition(self, func_id: str, data: dict, comment: str = "") -> str:
35
47
  """Create a new function definition version.
36
48
 
@@ -0,0 +1,225 @@
1
+ """F24 admit: read-only plan filter — verify the plan's baseline (``from_ref``
2
+ snapshot) still holds before anything is updated:
3
+
4
+ * templates: FaaS template content vs baseline template (diff surfaced on refusal);
5
+ a diverged template is dropped from the plan;
6
+ * functions: per active deployment, the platform's rendering of the deployed
7
+ ``definition_version`` vs baseline ``rendered.yaml`` — a diverged (or
8
+ un-checkable) deployment is MARKED ``"refused": <reason>`` and rides along
9
+ in the plan; the function's definition update is skipped only when EVERY
10
+ deployment is refused (baseline ``rendered.yaml`` required — missing → the
11
+ function is refused).
12
+
13
+ The narrowed result is an ``AdmitPlan`` — a ``Plan`` subclass carrying the
14
+ refusals as ``skipped: [Refusal]`` dataclasses. ``AdmitPlan.payload()`` reuses
15
+ ``Plan.payload()`` and appends the skipped bucket, so it stays a drop-in
16
+ ``--plan`` input for apply-plan (``Plan.from_dict`` ignores the extra key).
17
+ One refused entry never refuses the run (refusal → exit 0); refused entries
18
+ resurface on the next converge run until the drift is resolved by hand.
19
+
20
+ Self-contained by design (#105): plan.py / apply_plan.py / result.py are NOT
21
+ touched — StalePlanError is imported from its current home (apply_plan) and the
22
+ freshness guard mirrors ApplyPlan._assert_fresh locally.
23
+ """
24
+ from __future__ import annotations
25
+
26
+ import difflib
27
+ import json
28
+ from dataclasses import asdict, dataclass, field, replace
29
+ from pathlib import Path
30
+
31
+ from loguru import logger
32
+
33
+ from faas_gitops.infra.yaml_utils import load_yaml
34
+ from faas_gitops.layout import FUNCTION_DIR, FUNCTION_RENDERED, TEMPLATE_DIR
35
+ from faas_gitops.protocols.faas_function import FaaSFunctionProvider
36
+ from faas_gitops.protocols.faas_template import FaaSTemplateProvider
37
+ from faas_gitops.protocols.git_gateway import GitWriter
38
+ from faas_gitops.result import Bucket, Buckets, Report
39
+ from faas_gitops.usecases.apply_plan import StalePlanError
40
+ from faas_gitops.usecases.plan import FunctionChange, Plan, PlanContext
41
+ from faas_gitops.usecases.sync_functions import read_function_from_dir
42
+ from faas_gitops.usecases.sync_templates import read_template_from_dir
43
+
44
+
45
+ def _canon(doc) -> str:
46
+ """Content identity of a YAML document — comment/key-order/str-vs-dict agnostic."""
47
+ return json.dumps(load_yaml(doc) if isinstance(doc, str) else doc,
48
+ sort_keys=True, ensure_ascii=False, default=str)
49
+
50
+
51
+ def _diff(ref: str, cur: str, limit: int = 8) -> str:
52
+ """First differing lines of a baseline→remote unified diff (capped, one line)."""
53
+ lines = [l for l in difflib.unified_diff(ref.splitlines(), cur.splitlines(), lineterm="")
54
+ if not l.startswith(("---", "+++", "@@"))]
55
+ return " | ".join(lines[:limit]) + (" …" if len(lines) > limit else "")
56
+
57
+
58
+ def _kind(n_templates: int, n_functions: int) -> str:
59
+ if n_templates and n_functions:
60
+ return "mixed"
61
+ if n_templates:
62
+ return "templates"
63
+ if n_functions:
64
+ return "functions"
65
+ return "none"
66
+
67
+
68
+ @dataclass(frozen=True)
69
+ class Refusal:
70
+ id: str
71
+ error: str
72
+
73
+
74
+ @dataclass(frozen=True)
75
+ class AdmitPlan(Plan):
76
+ """A plan narrowed by admit — refused templates/functions are dropped from
77
+ their lists and ride along as ``skipped`` refusals; refused deployments
78
+ stay in their function's ``deployments`` records, marked ``refused``."""
79
+
80
+ skipped: list[Refusal] = field(default_factory=list)
81
+
82
+ @classmethod
83
+ def narrowed(cls, plan: Plan, templates: list[str],
84
+ functions: list[FunctionChange], skipped: list[Refusal]) -> "AdmitPlan":
85
+ return cls(
86
+ kind=_kind(len(templates), len(functions)),
87
+ templates=templates,
88
+ functions=functions,
89
+ context=plan.context,
90
+ skipped=sorted(skipped, key=lambda s: s.id),
91
+ )
92
+
93
+ def payload(self) -> dict:
94
+ """Plan payload + skipped bucket — drop-in for apply-plan (extra key ignored)."""
95
+ return super().payload() | {"skipped": [asdict(s) for s in self.skipped]}
96
+
97
+
98
+ def admit_report(payload: dict) -> Report:
99
+ """F01 contract built from existing buckets — refusal is a skip (exit 0), not a failure."""
100
+ r = Report(headline="admit report")
101
+ r.add(Bucket("admitted", "✓"), [f["id"] for f in payload["functions"]])
102
+ r.add(Buckets.SKIPPED, payload["skipped"])
103
+ r.add(Buckets.FAILED, [])
104
+ return r
105
+
106
+
107
+ class AdmitCheck:
108
+ """Per-entry isolation — a diverged (or un-checkable) entry never refuses
109
+ the run: templates and all-deployments-refused functions are dropped;
110
+ single refused deployments are marked in place (fail-safe, fail-local)."""
111
+
112
+ def __init__(
113
+ self,
114
+ func_provider: FaaSFunctionProvider,
115
+ tpl_provider: FaaSTemplateProvider,
116
+ git: GitWriter,
117
+ ) -> None:
118
+ self._provider = func_provider
119
+ self._tpl = tpl_provider
120
+ self._git = git
121
+
122
+ async def run(self, repo_path: Path, plan: Plan) -> AdmitPlan:
123
+ """Return the narrowed AdmitPlan (context preserved, kind recomputed,
124
+ function refusals under skipped: [Refusal], per-deployment refusals
125
+ marked in ``deployments`` records)."""
126
+ await self._assert_fresh(repo_path, plan.context)
127
+ from_ref = plan.context.from_ref
128
+ await self._git.checkout_origin(repo_path, from_ref)
129
+
130
+ templates: list[str] = []
131
+ functions: list[FunctionChange] = []
132
+ skipped: list[Refusal] = []
133
+ for t in plan.templates:
134
+ error = await self._guard(self._tpl_check, repo_path, from_ref, t)
135
+ if error is None:
136
+ templates.append(t)
137
+ else:
138
+ self._refuse(skipped, t, error)
139
+ for f in plan.functions:
140
+ try:
141
+ refusal, deployments = await self._fn_check(repo_path, from_ref, f)
142
+ except Exception as e: # un-enumerable → refuse that function only (fail-safe)
143
+ refusal, deployments = f"admit-check error: {e}", f.deployments
144
+ if refusal is None:
145
+ functions.append(replace(f, deployments=deployments))
146
+ else:
147
+ self._refuse(skipped, f.id, refusal)
148
+ return AdmitPlan.narrowed(plan, templates, functions, skipped)
149
+
150
+ async def _guard(self, check, repo_path: Path, from_ref: str, entry: str) -> str | None:
151
+ try:
152
+ return await check(repo_path, from_ref, entry)
153
+ except Exception as e: # un-checkable → refuse that entry only (fail-safe)
154
+ return f"admit-check error: {e}"
155
+
156
+ @staticmethod
157
+ def _refuse(skipped: list[Refusal], entry: str, error: str) -> None:
158
+ logger.warning(f" ⏸ {entry}: {error} — 本轮跳过,欠账下轮 plan 自动现形")
159
+ skipped.append(Refusal(entry, error))
160
+
161
+ async def _assert_fresh(self, repo_path: Path, c: PlanContext) -> None:
162
+ """Mirrors ApplyPlan._assert_fresh (kept local: apply_plan.py untouched)."""
163
+ for ref, sha in ((c.from_ref, c.from_sha), (c.to_ref, c.to_sha)):
164
+ try:
165
+ current = await self._git.short_sha(repo_path, ref)
166
+ except RuntimeError:
167
+ raise StalePlanError(f"plan is stale: {ref} no longer exists (expected {sha})") from None
168
+ if current != sha:
169
+ raise StalePlanError(f"plan is stale: {ref} moved {sha} → {current}; re-run `faas-gitops plan`")
170
+
171
+ async def _tpl_check(self, repo_path: Path, from_ref: str, tpl_id: str) -> str | None:
172
+ """Rule 1 — FaaS template vs baseline template. None = admit; error = refuse."""
173
+ org, _, name = tpl_id.partition("/")
174
+ tpl_dir = repo_path / TEMPLATE_DIR / org / name
175
+ if not tpl_dir.exists():
176
+ return f"template-baseline-missing: 基线 {from_ref} 无 templates/{tpl_id}"
177
+ ref = (await read_template_from_dir(tpl_dir)).to_faas_yaml()
178
+ remote = await self._tpl.get_template_content(org, name)
179
+ if _canon(remote) != _canon(ref):
180
+ return f"template-diverged: FaaS 模板 ≠ 基线 {from_ref}: {_diff(ref, remote)}"
181
+ return None
182
+
183
+ async def _fn_check(self, repo_path: Path, from_ref: str,
184
+ f: FunctionChange) -> tuple[str | None, list[dict]]:
185
+ """Rule 2 — per deployment: the platform must still render the deployed
186
+ version to the baseline rendered.yaml. A diverged (or un-checkable)
187
+ deployment is marked ``refused`` in its record; the function's
188
+ definition update is skipped only when EVERY deployment is refused.
189
+ Returns (function refusal | None, annotated deployments)."""
190
+ func_dir = repo_path / FUNCTION_DIR / f.id
191
+ parts = await read_function_from_dir(func_dir)
192
+ if parts.definition is None:
193
+ return None, f.deployments # no baseline definition to protect → admit
194
+ rendered_path = func_dir / FUNCTION_RENDERED
195
+ if not rendered_path.exists():
196
+ logger.warning(f" ! {f.id}: 基线 {from_ref} 无 {FUNCTION_RENDERED},无法核验部署渲染(先 render-definitions)")
197
+ return f"rendered-missing: 基线 {from_ref} 无 {FUNCTION_RENDERED}", f.deployments
198
+ deployments = f.deployments or await self._load_deployments(f.id) # old/pure-git plan → fresh listing
199
+ if not deployments:
200
+ return None, deployments # nothing deployed → admit
201
+ want = _canon(rendered_path.read_text())
202
+ annotated: list[dict] = []
203
+ for d in deployments:
204
+ entry = dict(d)
205
+ dep, version = d["id"], str(d.get("definition_version", ""))
206
+ try:
207
+ rendered = await self._provider.render_definition(f.id, version)
208
+ if _canon(rendered or "") == want:
209
+ annotated.append(entry)
210
+ continue
211
+ reason = f"render-diverged: v{version} 渲染结果 ≠ 基线 rendered.yaml @{from_ref}"
212
+ except Exception as e: # per-deployment failure blocks that deployment only
213
+ reason = f"admit-check error: {e}"
214
+ logger.warning(f" ⏸ {f.id}/{dep}: {reason} — 标记该部署不可更新")
215
+ annotated.append(entry | {"refused": reason})
216
+ if all("refused" in e for e in annotated):
217
+ ids = ", ".join(e["id"] for e in annotated)
218
+ return f"all-deployments-refused: {ids} 全部部署不可更新(基线 {from_ref})", annotated
219
+ return None, annotated
220
+
221
+ async def _load_deployments(self, fid: str) -> list[dict]:
222
+ """Non-terminated deployment records — mirrors plan-time recording."""
223
+ ds = await self._provider.list_deployments(fid)
224
+ return [{k: d.get(k) for k in ("id", "status", "definition_version")}
225
+ for d in ds if d.get("status") != "terminated"]
@@ -10,19 +10,21 @@ from faas_gitops.protocols.faas_function import FaaSFunctionProvider
10
10
 
11
11
 
12
12
  class DeployFunctions:
13
- """Deploy applied functions to all their active deployments.
13
+ """Deploy applied functions to their active deployments.
14
14
 
15
15
  For each applied function, deploys the pushed version to every
16
16
  non-terminated deployment. Skips deployments already at the target
17
- version (idempotency guard). Fire-and-forget: triggers deploy without
18
- waiting for readiness.
17
+ version (idempotency guard) and (fid, deployment_id) pairs rejected
18
+ by admit. Fire-and-forget: triggers deploy without waiting for
19
+ readiness.
19
20
  """
20
21
 
21
22
  def __init__(self, provider: FaaSFunctionProvider, settings: Settings) -> None:
22
23
  self._provider = provider
23
24
  self._settings = settings
24
25
 
25
- async def run(self, applied_versions: dict[str, str], repo_path: Path) -> DeployResult:
26
+ async def run(self, applied_versions: dict[str, str], repo_path: Path,
27
+ refused_deployments: set[tuple[str, str]] | None = None) -> DeployResult:
26
28
  result = DeployResult()
27
29
  for fid, version in applied_versions.items():
28
30
  try:
@@ -34,6 +36,10 @@ class DeployFunctions:
34
36
  continue
35
37
  for dep in active:
36
38
  deploy_id = dep["id"]
39
+ if (fid, deploy_id) in (refused_deployments or set()):
40
+ logger.warning(f" ⏸ {fid}/{deploy_id}: refused by admit — skipped")
41
+ result.skipped.append(f"{fid}/{deploy_id}")
42
+ continue
37
43
  current = str(dep.get("definition_version", ""))
38
44
  if current == str(version):
39
45
  logger.info(f" = {fid}/{deploy_id}: already at version {version}")
@@ -1,13 +1,15 @@
1
1
  """F03 plan: pure git, read-only diff between two refs → plan.json."""
2
2
  from __future__ import annotations
3
3
 
4
- from dataclasses import asdict, dataclass
4
+ import asyncio
5
+ from dataclasses import asdict, dataclass, field
5
6
  from datetime import datetime, timezone
6
7
  from pathlib import Path
7
8
 
8
9
  from loguru import logger
9
10
 
10
11
  from faas_gitops.config import Settings
12
+ from faas_gitops.protocols.faas_function import FaaSFunctionProvider
11
13
  from faas_gitops.protocols.git_gateway import GitWriter
12
14
  from faas_gitops.usecases.detect_function_drift import DetectFunctionDrift
13
15
  from faas_gitops.usecases.detect_template_drift import DetectTemplateDrift
@@ -18,6 +20,14 @@ class FunctionChange:
18
20
  id: str
19
21
  channel: str
20
22
  reason: str
23
+ deployments: list[dict] = field(default_factory=list) # non-terminated: {id, status, definition_version}
24
+
25
+ @classmethod
26
+ def from_dict(cls, data: dict) -> "FunctionChange":
27
+ return cls(
28
+ id=data["id"], channel=data["channel"], reason=data["reason"],
29
+ deployments=list(data.get("deployments", [])),
30
+ )
21
31
 
22
32
 
23
33
  @dataclass(frozen=True)
@@ -45,7 +55,7 @@ class Plan:
45
55
  return cls(
46
56
  kind=data["kind"],
47
57
  templates=list(data.get("templates", [])),
48
- functions=[FunctionChange(**f) for f in data.get("functions", [])],
58
+ functions=[FunctionChange.from_dict(f) for f in data.get("functions", [])],
49
59
  context=PlanContext(**data["context"]),
50
60
  )
51
61
 
@@ -72,9 +82,13 @@ class Plan:
72
82
  class PlanBuild:
73
83
  """Read-only plan of merge-able changes between two refs."""
74
84
 
75
- def __init__(self, git: GitWriter, settings: Settings) -> None:
85
+ def __init__(self, git: GitWriter, settings: Settings,
86
+ func_provider: FaaSFunctionProvider | None = None) -> None:
87
+ """``func_provider`` (optional) records each function's active deployment
88
+ ids in the plan; without it the plan stays pure-git."""
76
89
  self._git = git
77
90
  self._settings = settings
91
+ self._func = func_provider
78
92
 
79
93
  async def run(
80
94
  self, repo_path: Path, base: str = "main", ref: str = "sync/faas-functions", repo_slug: str = "",
@@ -96,7 +110,9 @@ class PlanBuild:
96
110
 
97
111
  # rendered-only drift ⇒ meta-only edits are not drift; every planned
98
112
  # function is definition-channel by construction
99
- functions = [FunctionChange(fid, "definition", "modified") for fid in fn.drifted]
113
+ active = await self._active_deployments(fn.drifted)
114
+ functions = [FunctionChange(fid, "definition", "modified", active.get(fid, []))
115
+ for fid in fn.drifted]
100
116
  templates = tpl.drifted
101
117
  if templates and functions:
102
118
  kind = "mixed"
@@ -112,4 +128,20 @@ class PlanBuild:
112
128
  to_sha=await self._git.short_sha(repo_path, base),
113
129
  captured_at=f"{datetime.now(timezone.utc):%Y-%m-%dT%H:%M:%SZ}",
114
130
  )
115
- return Plan(kind, templates, functions, context)
131
+ return Plan(kind, templates, functions, context)
132
+
133
+ async def _active_deployments(self, fids: list[str]) -> dict[str, list[dict]]:
134
+ """Non-terminated deployment records ({id, status, definition_version}) per
135
+ drifted function (recorded in the plan so downstream steps know which
136
+ deployments a change touches and what they run)."""
137
+ if self._func is None or not fids:
138
+ return {}
139
+
140
+ async def _one(fid: str) -> tuple[str, list[dict]]:
141
+ ds = await self._func.list_deployments(fid)
142
+ return fid, [
143
+ {k: d.get(k) for k in ("id", "status", "definition_version")}
144
+ for d in ds if d.get("status") != "terminated"
145
+ ]
146
+
147
+ return dict(await asyncio.gather(*[_one(f) for f in fids]))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: faas-gitops
3
- Version: 0.5.0
3
+ Version: 0.6.1
4
4
  Summary: Declarative GitOps for FaaS
5
5
  Requires-Python: >=3.12
6
6
  Requires-Dist: aiofiles>=23.0
@@ -26,6 +26,7 @@ src/faas_gitops/protocols/faas_function.py
26
26
  src/faas_gitops/protocols/faas_template.py
27
27
  src/faas_gitops/protocols/git_gateway.py
28
28
  src/faas_gitops/usecases/__init__.py
29
+ src/faas_gitops/usecases/admit_check.py
29
30
  src/faas_gitops/usecases/apply_function_changes.py
30
31
  src/faas_gitops/usecases/apply_plan.py
31
32
  src/faas_gitops/usecases/apply_template_changes.py
@@ -39,6 +40,7 @@ src/faas_gitops/usecases/render_definitions.py
39
40
  src/faas_gitops/usecases/snapshot.py
40
41
  src/faas_gitops/usecases/sync_functions.py
41
42
  src/faas_gitops/usecases/sync_templates.py
43
+ tests/test_admit_check.py
42
44
  tests/test_apply_function_changes.py
43
45
  tests/test_apply_plan.py
44
46
  tests/test_apply_template_changes.py
@@ -0,0 +1,383 @@
1
+ """F24 tests: admit — mark refused deployments; skip a function's definition
2
+ update only when EVERY deployment is refused.
3
+
4
+ Rule 1 (templates): FaaS template content vs baseline template — diverged
5
+ template dropped from the plan (diff surfaced).
6
+ Rule 2 (functions): per active deployment (records carried by the plan, fresh
7
+ listing as fallback), the platform's rendering of its definition_version vs
8
+ baseline rendered.yaml — diverged deployment MARKED "refused": <reason> in the
9
+ plan; all-refused → function dropped under skipped: [Refusal].
10
+
11
+ Result model: AdmitCheck returns an AdmitPlan (Plan subclass carrying
12
+ skipped: [Refusal]); its payload() reuses Plan.payload() + skipped bucket and
13
+ round-trips through Plan.from_dict (extra keys ignored).
14
+ """
15
+ from __future__ import annotations
16
+
17
+ import subprocess
18
+ from pathlib import Path
19
+ from unittest.mock import AsyncMock, MagicMock
20
+
21
+ import pytest
22
+
23
+ from faas_gitops.adapters.git_cli_gateway import GitCliGateway
24
+ from faas_gitops.infra.yaml_utils import load_yaml
25
+ from faas_gitops.models import TemplateParts
26
+ from faas_gitops.result import EXIT_OK
27
+ from faas_gitops.usecases.admit_check import AdmitCheck, AdmitPlan, Refusal, admit_report, _canon
28
+ from faas_gitops.usecases.apply_plan import StalePlanError
29
+ from faas_gitops.usecases.plan import FunctionChange, Plan, PlanContext
30
+
31
+
32
+ def _git(*args: str, cwd: Path) -> None:
33
+ subprocess.run(["git", *args], cwd=cwd, capture_output=True, check=True)
34
+
35
+
36
+ def _short_sha(cwd: Path, ref: str) -> str:
37
+ return subprocess.run(
38
+ ["git", "rev-parse", "--short", f"origin/{ref}"], cwd=cwd,
39
+ capture_output=True, text=True, check=True,
40
+ ).stdout.strip()
41
+
42
+
43
+ _RENDER_OK = "image: v1\n" # matches rendered.yaml on the snapshot branch
44
+
45
+
46
+ @pytest.fixture
47
+ def repo(tmp_path: Path) -> Path:
48
+ """main vs snapshot branch: fn1/fn2 baseline rendered.yaml, fn3 meta-only,
49
+ fn4 no rendered.yaml (required-baseline rule), plus one baseline template."""
50
+ r = tmp_path / "repo"
51
+ r.mkdir()
52
+ _git("init", "-b", "main", cwd=r)
53
+ _git("config", "user.email", "t@t", cwd=r)
54
+ _git("config", "user.name", "t", cwd=r)
55
+ for fid in ("fn1", "fn2"): # rendered baseline → per-deployment rule
56
+ d = r / "functions" / fid
57
+ d.mkdir(parents=True)
58
+ (d / "meta.yaml").write_text(f"functionName: {fid}\ntags: []\n")
59
+ (d / "base.yaml").write_text("templateName: org/tpl1\n")
60
+ (d / "definition.yaml").write_text("repo: siliconflow/base\n")
61
+ (d / "rendered.yaml").write_text(_RENDER_OK)
62
+ d = r / "functions" / "fn4" # definition but no rendered.yaml → refused
63
+ d.mkdir(parents=True)
64
+ (d / "meta.yaml").write_text("functionName: fn4\ntags: []\n")
65
+ (d / "base.yaml").write_text("templateName: org/tpl1\n")
66
+ (d / "definition.yaml").write_text("repo: siliconflow/base\n")
67
+ d = r / "functions" / "fn3" # meta-only (no baseline def)
68
+ d.mkdir(parents=True)
69
+ (d / "meta.yaml").write_text("functionName: fn3\ntags: []\n")
70
+ (d / "rendered.yaml").write_text(_RENDER_OK)
71
+ t = r / "templates" / "org" / "tpl1"
72
+ t.mkdir(parents=True)
73
+ (t / "info.yaml").write_text("subject: org\nid: tpl1\ndisplayName: T1\n")
74
+ (t / "template.jinja").write_text("image: {{ .image }}\n")
75
+ (r / "README").write_text("hi")
76
+ _git("add", ".", cwd=r)
77
+ _git("commit", "-m", "snapshot baseline", cwd=r)
78
+ _git("branch", "remote/state-x", cwd=r)
79
+ for fid in ("fn1", "fn2", "fn3"):
80
+ (r / "functions" / fid / "rendered.yaml").write_text("image: v2\n")
81
+ (r / "functions" / "fn1" / "definition.yaml").write_text("repo: siliconflow/main-new\n")
82
+ _git("add", ".", cwd=r)
83
+ _git("commit", "-m", "change main", cwd=r)
84
+ _git("remote", "add", "origin", str(r), cwd=r)
85
+ _git("fetch", "origin", cwd=r)
86
+ return r
87
+
88
+
89
+ def _dep(dep_id: str, version: int = 203, status: str = "ready") -> dict:
90
+ return {"id": dep_id, "status": status, "definition_version": version}
91
+
92
+
93
+ def _plan(
94
+ repo: Path, *fids: str, from_sha: str | None = None, templates: list[str] | None = None,
95
+ deployments: dict[str, list[dict]] | None = None,
96
+ ) -> Plan:
97
+ deployments = deployments or {}
98
+
99
+ def _fns(fids: tuple[str, ...]) -> list[FunctionChange]:
100
+ return [FunctionChange(f, "definition", "modified", deployments.get(f, [])) for f in fids]
101
+
102
+ return Plan(
103
+ kind="mixed" if (templates or []) and fids else ("templates" if templates else "functions"),
104
+ templates=templates or [],
105
+ functions=_fns(fids),
106
+ context=PlanContext(
107
+ repo="org/repo", from_ref="remote/state-x", to_ref="main",
108
+ from_sha=from_sha or _short_sha(repo, "remote/state-x"),
109
+ to_sha=_short_sha(repo, "main"),
110
+ captured_at="2026-11-05T00:00:00Z",
111
+ ),
112
+ )
113
+
114
+
115
+ async def _admit(repo: Path, plan: Plan, provider: MagicMock, tpl: MagicMock | None = None) -> AdmitPlan:
116
+ tpl = tpl if tpl is not None else _tpl_ok(repo)
117
+ return await AdmitCheck(provider, tpl, GitCliGateway(repo.parent)).run(repo, plan)
118
+
119
+
120
+ def _tpl_ok(repo: Path) -> MagicMock:
121
+ """Template provider echoing the baseline template back verbatim."""
122
+ tpl = MagicMock()
123
+ tpl.get_template_content = AsyncMock(return_value=_tpl_ref(repo))
124
+ return tpl
125
+
126
+
127
+ def _tpl_ref(repo: Path) -> str:
128
+ d = repo / "templates" / "org" / "tpl1"
129
+ return TemplateParts(load_yaml((d / "info.yaml").read_text()), (d / "template.jinja").read_text()).to_faas_yaml()
130
+
131
+
132
+ def _provider(
133
+ deployments: dict[str, list[dict]] | None = None,
134
+ renders: dict[tuple[str, str], str | None] | None = None,
135
+ render_errors: dict[tuple[str, str], Exception] | None = None,
136
+ error_on: str | None = None,
137
+ ) -> MagicMock:
138
+ """Default answers match the baseline: renders = rendered.yaml content —
139
+ so an empty override map admits every deployment."""
140
+ deployments, renders, render_errors = deployments or {}, renders or {}, render_errors or {}
141
+
142
+ async def _list(fid: str) -> list[dict]:
143
+ if fid == error_on:
144
+ raise RuntimeError("api down")
145
+ return deployments.get(fid, [])
146
+
147
+ async def _render(fid: str, version: str) -> str | None:
148
+ key = (fid, str(version))
149
+ if key in render_errors:
150
+ raise render_errors[key]
151
+ return renders.get(key, _RENDER_OK)
152
+
153
+ p = MagicMock()
154
+ p.list_deployments = AsyncMock(side_effect=_list)
155
+ p.render_definition = AsyncMock(side_effect=_render)
156
+ return p
157
+
158
+
159
+ _RENDER_TAMPERED = "image: v9\n"
160
+
161
+
162
+ class TestDeploymentMarking:
163
+ """逐部署标记:漂移的部署打 refused 标记,函数保留;全部 refused 才跳过函数。"""
164
+
165
+ @pytest.mark.asyncio
166
+ async def test_admits_when_all_deployments_render_baseline(self, repo: Path) -> None:
167
+ deps = [_dep("dep1"), _dep("dep2", version=204)]
168
+ p = _provider() # renders default to baseline for any version
169
+ plan = _plan(repo, "fn1", deployments={"fn1": deps})
170
+ ap = await _admit(repo, plan, p)
171
+ assert [f.id for f in ap.functions] == ["fn1"]
172
+ assert ap.functions[0].deployments == deps # annotations untouched
173
+ assert ap.skipped == []
174
+ p.list_deployments.assert_not_awaited() # plan records used, no fresh listing
175
+ p.render_definition.assert_any_await("fn1", "204") # each recorded version checked
176
+
177
+ @pytest.mark.asyncio
178
+ async def test_marks_only_the_diverged_deployment(self, repo: Path) -> None:
179
+ deps = [_dep("dep1", version=1), _dep("dep2", version=2)]
180
+ p = _provider(renders={("fn1", "2"): _RENDER_TAMPERED})
181
+ ap = await _admit(repo, _plan(repo, "fn1", deployments={"fn1": deps}), p)
182
+ assert [f.id for f in ap.functions] == ["fn1"] # definition update proceeds
183
+ assert ap.skipped == []
184
+ assert ap.payload()["functions"][0]["deployments"] == [
185
+ _dep("dep1", version=1), # updatable, untouched record
186
+ _dep("dep2", version=2) | {"refused": ap.payload()["functions"][0]["deployments"][1]["refused"]},
187
+ ]
188
+ assert ap.payload()["functions"][0]["deployments"][1]["refused"].startswith("render-diverged")
189
+
190
+ @pytest.mark.asyncio
191
+ async def test_skip_function_only_when_every_deployment_refused(self, repo: Path) -> None:
192
+ deps = [_dep("dep1", version=1), _dep("dep2", version=2)]
193
+ p = _provider(renders={("fn1", "1"): _RENDER_TAMPERED, ("fn1", "2"): _RENDER_TAMPERED})
194
+ ap = await _admit(repo, _plan(repo, "fn1", deployments={"fn1": deps}), p)
195
+ assert ap.functions == [] # nothing updatable → skip the definition update
196
+ assert [s.id for s in ap.skipped] == ["fn1"]
197
+ assert ap.skipped[0].error.startswith("all-deployments-refused")
198
+ assert "dep1" in ap.skipped[0].error and "dep2" in ap.skipped[0].error
199
+
200
+ @pytest.mark.asyncio
201
+ async def test_all_refused_yields_kind_none(self, repo: Path) -> None:
202
+ p = _provider(renders={("fn1", "203"): _RENDER_TAMPERED})
203
+ ap = await _admit(repo, _plan(repo, "fn1", deployments={"fn1": [_dep("dep1")]}), p)
204
+ assert ap.kind == "none" # apply-plan then no-ops cleanly (exit 0)
205
+
206
+ @pytest.mark.asyncio
207
+ async def test_unfetchable_render_blocks_that_deployment(self, repo: Path) -> None:
208
+ deps = [_dep("dep1", version=1), _dep("dep2", version=2)]
209
+ p = _provider(renders={("fn1", "2"): None}) # render unavailable → not the baseline
210
+ ap = await _admit(repo, _plan(repo, "fn1", deployments={"fn1": deps}), p)
211
+ assert [f.id for f in ap.functions] == ["fn1"]
212
+ assert ap.functions[0].deployments[1]["refused"].startswith("render-diverged")
213
+
214
+ @pytest.mark.asyncio
215
+ async def test_render_error_blocks_that_deployment_only(self, repo: Path) -> None:
216
+ deps = [_dep("dep1", version=1), _dep("dep2", version=2)]
217
+ p = _provider(render_errors={("fn1", "2"): RuntimeError("render api down")})
218
+ ap = await _admit(repo, _plan(repo, "fn1", deployments={"fn1": deps}), p)
219
+ assert [f.id for f in ap.functions] == ["fn1"]
220
+ assert ap.functions[0].deployments[1]["refused"].startswith("admit-check error")
221
+
222
+ @pytest.mark.asyncio
223
+ async def test_marks_are_isolated_per_function(self, repo: Path) -> None:
224
+ # fn1: one of two deployments diverged → stays, marked; fn2: sole deployment diverged → skipped
225
+ p = _provider(renders={("fn1", "2"): _RENDER_TAMPERED, ("fn2", "203"): _RENDER_TAMPERED})
226
+ ap = await _admit(repo, _plan(
227
+ repo, "fn1", "fn2",
228
+ deployments={"fn1": [_dep("dep1", version=1), _dep("dep2", version=2)],
229
+ "fn2": [_dep("dep3")]},
230
+ ), p)
231
+ assert [f.id for f in ap.functions] == ["fn1"]
232
+ assert "refused" in ap.functions[0].deployments[1]
233
+ assert [s.id for s in ap.skipped] == ["fn2"]
234
+
235
+ @pytest.mark.asyncio
236
+ async def test_list_error_refuses_whole_function(self, repo: Path) -> None:
237
+ """cannot enumerate deployments at all → fail-safe: refuse the function."""
238
+ p = _provider(error_on="fn1") # plan carries no records → fresh listing raised
239
+ ap = await _admit(repo, _plan(repo, "fn1"), p)
240
+ assert ap.functions == []
241
+ assert ap.skipped[0].error.startswith("admit-check error")
242
+
243
+
244
+ class TestFallbackListing:
245
+ """Plan 未携带部署记录(旧 plan.json / 纯 git)→ 现场列举。"""
246
+
247
+ @pytest.mark.asyncio
248
+ async def test_fresh_listing_when_plan_has_no_records(self, repo: Path) -> None:
249
+ dep, term = _dep("dep1"), _dep("dep0", status="terminated")
250
+ p = _provider({"fn1": [term, dep]})
251
+ ap = await _admit(repo, _plan(repo, "fn1"), p) # no deployments in plan
252
+ p.list_deployments.assert_awaited_once_with("fn1")
253
+ assert ap.functions[0].deployments == [dep] # terminated excluded, stable keys
254
+
255
+ @pytest.mark.asyncio
256
+ async def test_no_active_deployments_admits(self, repo: Path) -> None:
257
+ p = _provider({"fn1": [_dep("dep0", status="terminated")] * 2})
258
+ ap = await _admit(repo, _plan(repo, "fn1"), p)
259
+ assert [f.id for f in ap.functions] == ["fn1"]
260
+ p.render_definition.assert_not_awaited()
261
+
262
+
263
+ class TestPayloadContract:
264
+ @pytest.mark.asyncio
265
+ async def test_payload_marks_and_skips_drop_in(self, repo: Path) -> None:
266
+ p = _provider(renders={("fn1", "2"): _RENDER_TAMPERED, ("fn2", "203"): _RENDER_TAMPERED})
267
+ plan = _plan(
268
+ repo, "fn1", "fn2",
269
+ deployments={"fn1": [_dep("dep1", version=1), _dep("dep2", version=2)],
270
+ "fn2": [_dep("dep3")]},
271
+ )
272
+ ap = await _admit(repo, plan, p)
273
+ assert ap.payload() == {
274
+ "kind": "functions",
275
+ "templates": [],
276
+ "functions": [{
277
+ "id": "fn1", "channel": "definition", "reason": "modified",
278
+ "deployments": [
279
+ _dep("dep1", version=1),
280
+ _dep("dep2", version=2) | {"refused": ap.payload()["functions"][0]["deployments"][1]["refused"]},
281
+ ],
282
+ }],
283
+ "context": plan.payload()["context"], # provenance untouched
284
+ "skipped": [{"id": "fn2", "error": ap.skipped[0].error}],
285
+ }
286
+ reread = Plan.from_dict(ap.payload()) # from_dict ignores extra keys, keeps marks
287
+ assert [f.id for f in reread.functions] == ["fn1"]
288
+ assert "refused" in reread.functions[0].deployments[1]
289
+ assert reread.context == plan.context
290
+
291
+ @pytest.mark.asyncio
292
+ async def test_skipped_sorted_by_id(self, repo: Path) -> None:
293
+ p = _provider(renders={("fn1", "203"): _RENDER_TAMPERED, ("fn2", "203"): _RENDER_TAMPERED})
294
+ ap = await _admit(repo, _plan(
295
+ repo, "fn2", "fn1",
296
+ deployments={"fn1": [_dep("dep1")], "fn2": [_dep("dep2")]},
297
+ ), p)
298
+ assert [s.id for s in ap.skipped] == ["fn1", "fn2"]
299
+
300
+
301
+ class TestEarlyAdmitsAndStale:
302
+ @pytest.mark.asyncio
303
+ async def test_no_baseline_definition_admits(self, repo: Path) -> None:
304
+ p = _provider({"fn3": [_dep("dep1")]})
305
+ ap = await _admit(repo, _plan(repo, "fn3"), p)
306
+ assert [f.id for f in ap.functions] == ["fn3"]
307
+ p.list_deployments.assert_not_awaited()
308
+
309
+ @pytest.mark.asyncio
310
+ async def test_rendered_missing_refuses_function(self, repo: Path) -> None:
311
+ p = _provider()
312
+ ap = await _admit(repo, _plan(repo, "fn4", deployments={"fn4": [_dep("dep1")]}), p)
313
+ assert ap.functions == []
314
+ assert ap.skipped == [Refusal("fn4", ap.skipped[0].error)]
315
+ assert ap.skipped[0].error.startswith("rendered-missing")
316
+ p.render_definition.assert_not_awaited()
317
+
318
+ @pytest.mark.asyncio
319
+ async def test_stale_plan_raises(self, repo: Path) -> None:
320
+ with pytest.raises(StalePlanError):
321
+ await _admit(repo, _plan(repo, "fn1", from_sha="deadbee"), _provider())
322
+
323
+
324
+ class TestTemplateRule:
325
+ """Rule 1 与 Rule 2 独立:模板漂移 → 模板整体剔除(无子实体可标记)。"""
326
+
327
+ def _tpl_bad(self, repo: Path) -> MagicMock:
328
+ tpl = MagicMock()
329
+ tpl.get_template_content = AsyncMock(return_value=_tpl_ref(repo).replace("{{ .image }}", "{{ .imageTAMPERED }}"))
330
+ return tpl
331
+
332
+ @pytest.mark.asyncio
333
+ async def test_admits_matching_template(self, repo: Path) -> None:
334
+ ap = await _admit(repo, _plan(repo, templates=["org/tpl1"]), _provider())
335
+ assert ap.templates == ["org/tpl1"]
336
+ assert ap.skipped == []
337
+
338
+ @pytest.mark.asyncio
339
+ async def test_refuses_diverged_template_with_diff(self, repo: Path) -> None:
340
+ ap = await _admit(repo, _plan(repo, templates=["org/tpl1"]), _provider(), self._tpl_bad(repo))
341
+ assert ap.templates == []
342
+ error = ap.skipped[0].error
343
+ assert error.startswith("template-diverged") and "imageTAMPERED" in error # diff surfaced
344
+
345
+ @pytest.mark.asyncio
346
+ async def test_missing_baseline_dir_refused(self, repo: Path) -> None:
347
+ ap = await _admit(repo, _plan(repo, templates=["org/other"]), _provider(), _tpl_ok(repo))
348
+ assert ap.skipped[0].error.startswith("template-baseline-missing")
349
+
350
+ @pytest.mark.asyncio
351
+ async def test_template_keeps_plan_alive_when_function_skipped(self, repo: Path) -> None:
352
+ p = _provider(renders={("fn1", "203"): _RENDER_TAMPERED})
353
+ ap = await _admit(
354
+ repo, _plan(repo, "fn1", templates=["org/tpl1"],
355
+ deployments={"fn1": [_dep("dep1")]}),
356
+ p,
357
+ )
358
+ assert ap.templates == ["org/tpl1"] # template keeps the plan non-empty
359
+ assert ap.functions == []
360
+ assert ap.kind == "templates"
361
+
362
+
363
+ class TestCanon:
364
+ def test_comment_key_order_and_str_vs_dict_agnostic(self):
365
+ assert _canon("# rendered by faas-gitops, do not modify\nimage: v1\n") == _canon('{"image": "v1"}') # header comment
366
+ assert _canon("a: 1\nb: 2\n") == _canon({"b": 2, "a": 1}) # key order
367
+ assert _canon("image: v1\n") != _canon("image: v2\n")
368
+
369
+
370
+ class TestAdmitContract:
371
+ def test_refusal_is_exit0_not_failure(self):
372
+ payload = {"functions": [{"id": "fn2"}],
373
+ "skipped": [{"id": "fn1", "error": "all-deployments-refused: dep1, dep2 全部部署不可更新"}]}
374
+ r = admit_report(payload)
375
+ assert r.payload == {
376
+ "admitted": ["fn2"],
377
+ "skipped": payload["skipped"],
378
+ "failed": [],
379
+ }
380
+ assert r.emit() == EXIT_OK # logs the skipped rows without raising
381
+
382
+ def test_empty_admit_is_exit0(self):
383
+ assert admit_report({"functions": [], "skipped": []}).emit() == EXIT_OK
@@ -113,3 +113,21 @@ class TestDeployFunctions:
113
113
  assert "fn1/dep1" in result.deployed
114
114
  assert "fn1/dep2" in result.deployed
115
115
  assert provider.deploy_version.call_count == 2
116
+
117
+ @pytest.mark.asyncio
118
+ async def test_admit_refused_pair_skips_only_that_deployment(self) -> None:
119
+ """(fid, dep_id) 组合精确拒绝:同 id 部署在不同函数下互不影响。"""
120
+ repo = _make_repo(("fn1", _META_WITH_TAG), ("fn2", _META_WITH_TAG))
121
+ provider = MagicMock()
122
+ provider.list_deployments = AsyncMock(return_value=[
123
+ {"id": "dep1", "status": "ready", "definition_version": 3},
124
+ {"id": "dep2", "status": "ready", "definition_version": 4},
125
+ ])
126
+ provider.deploy_version = AsyncMock()
127
+ result = await DeployFunctions(provider, Settings()).run(
128
+ {"fn1": "5", "fn2": "5"}, repo, refused_deployments={("fn1", "dep1")}
129
+ )
130
+ assert result.deployed == ["fn1/dep2", "fn2/dep1", "fn2/dep2"]
131
+ assert "fn1/dep1" in result.skipped
132
+ provider.deploy_version.assert_any_call("fn1", "dep2", "5")
133
+ provider.deploy_version.assert_any_call("fn2", "dep1", "5")
@@ -9,7 +9,8 @@ import pytest
9
9
 
10
10
  from faas_gitops.adapters.git_cli_gateway import GitCliGateway
11
11
  from faas_gitops.config import Settings
12
- from faas_gitops.usecases.plan import PlanBuild
12
+ from faas_gitops.usecases.plan import Plan, PlanBuild
13
+ from unittest.mock import AsyncMock, MagicMock
13
14
 
14
15
 
15
16
  def _git(*args: str, cwd: Path) -> None:
@@ -71,7 +72,7 @@ class TestBuildPlan:
71
72
  assert plan.payload() == {
72
73
  "kind": "mixed",
73
74
  "templates": ["org/tpl1"],
74
- "functions": [{"id": "fn1", "channel": "definition", "reason": "modified"}],
75
+ "functions": [{"id": "fn1", "channel": "definition", "reason": "modified", "deployments": []}],
75
76
  "context": {
76
77
  "repo": "org/repo",
77
78
  "from_ref": "feat/x",
@@ -144,3 +145,41 @@ class TestBuildPlan:
144
145
  plan = await PlanBuild(git, Settings()).run(repo, base="main", ref="feat/tpl-only")
145
146
  assert plan.kind == "templates"
146
147
  assert plan.functions == []
148
+
149
+
150
+ class TestDeploymentRecording:
151
+ """drifted function 的非 terminated 部署记录({id, status, definition_version})记入 plan.json。"""
152
+
153
+ @pytest.mark.asyncio
154
+ async def test_records_active_deployment_ids_terminated_excluded(self, repo: Path) -> None:
155
+ p = MagicMock()
156
+ p.list_deployments = AsyncMock(return_value=[
157
+ {"id": "dep1", "status": "ready", "definition_version": 1},
158
+ {"id": "dep2", "status": "terminated", "definition_version": 1},
159
+ {"id": "dep3", "status": "running", "definition_version": 2},
160
+ ])
161
+ plan = await PlanBuild(GitCliGateway(repo.parent), Settings(), p).run(
162
+ repo, base="main", ref="feat/x", repo_slug="org/repo",
163
+ )
164
+ p.list_deployments.assert_awaited_once_with("fn1") # only the drifted function
165
+ dep1 = {"id": "dep1", "status": "ready", "definition_version": 1}
166
+ dep3 = {"id": "dep3", "status": "running", "definition_version": 2}
167
+ assert plan.functions[0].deployments == [dep1, dep3] # terminated excluded
168
+ assert plan.payload()["functions"][0]["deployments"] == [dep1, dep3] # into plan.json
169
+
170
+ @pytest.mark.asyncio
171
+ async def test_pure_git_without_provider_records_empty(self, repo: Path) -> None:
172
+ plan = await PlanBuild(GitCliGateway(repo.parent), Settings()).run(
173
+ repo, base="main", ref="feat/x", repo_slug="org/repo",
174
+ )
175
+ assert [f.deployments for f in plan.functions] == [[]]
176
+
177
+ def test_from_dict_reads_old_plan_without_deployments(self) -> None:
178
+ old = {
179
+ "kind": "functions", "templates": [],
180
+ "functions": [{"id": "fn1", "channel": "definition", "reason": "modified"}],
181
+ "context": {"repo": "org/repo", "from_ref": "feat/x", "to_ref": "main",
182
+ "from_sha": "aaa", "to_sha": "bbb", "captured_at": "t"},
183
+ }
184
+ plan = Plan.from_dict(old)
185
+ assert plan.functions[0].deployments == []
File without changes
File without changes