from __future__ import annotations import re import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[2] WORKFLOW = ROOT / ".gitea" / "workflows" / "ci.yml" def workflow_text() -> str: return WORKFLOW.read_text(encoding="utf-8") def job_block(name: str) -> str: lines = workflow_text().splitlines() start = None for index, line in enumerate(lines): if line == f" {name}:": start = index break if start is None: raise AssertionError(f"job {name!r} not found") end = len(lines) for index in range(start + 1, len(lines)): if re.match(r"^ [A-Za-z0-9_-]+:\s*$", lines[index]): end = index break return "\n".join(lines[start:end]) def deploy_guard() -> str: match = re.search(r"(?m)^ if:\s*(.+)$", job_block("deploy")) if match is None: raise AssertionError("deploy job has no guard") return match.group(1).strip() def guard_conditions() -> dict[str, str]: expression = deploy_guard() if expression.startswith("${{") and expression.endswith("}}"): expression = expression[3:-2].strip() conditions: dict[str, str] = {} for part in expression.split("&&"): match = re.fullmatch( r"([A-Za-z0-9_.]+)\s*==\s*['\"]([^'\"]+)['\"]", part.strip() ) if match is None: raise AssertionError(f"unsupported deploy guard condition: {part!r}") conditions[match.group(1)] = match.group(2) return conditions def deploys(context: dict[str, str | None]) -> bool: return all( context.get(condition) == expected for condition, expected in guard_conditions().items() ) def dispatch_input(name: str) -> str: match = re.search( rf"(?m)^ {re.escape(name)}:\n((?: .*\n)+)", workflow_text() ) if match is None: raise AssertionError(f"workflow_dispatch input {name!r} not found") return match.group(0) class DeployPolicyTests(unittest.TestCase): def test_guard_allows_only_manual_production_feature_on_main(self): self.assertEqual( guard_conditions(), { "gitea.event_name": "workflow_dispatch", "gitea.ref": "refs/heads/main", "inputs.validation_level": "feature", "inputs.deploy_target": "production", }, ) def test_manual_production_feature_on_main_deploys(self): self.assertTrue( deploys( { "gitea.event_name": "workflow_dispatch", "gitea.ref": "refs/heads/main", "inputs.validation_level": "feature", "inputs.deploy_target": "production", } ) ) def test_push_to_main_does_not_deploy(self): self.assertFalse( deploys( { "gitea.event_name": "push", "gitea.ref": "refs/heads/main", "inputs.validation_level": "feature", "inputs.deploy_target": "production", } ) ) def test_pull_request_with_feature_label_does_not_deploy(self): self.assertFalse( deploys( { "gitea.event_name": "pull_request", "gitea.ref": "refs/pull/42/head", "inputs.validation_level": "feature", "inputs.deploy_target": "production", "gitea.event.pull_request.labels.*.name": "feature-ready", } ) ) def test_missing_or_none_deploy_target_does_not_deploy(self): base = { "gitea.event_name": "workflow_dispatch", "gitea.ref": "refs/heads/main", "inputs.validation_level": "feature", } self.assertFalse(deploys(base)) self.assertFalse(deploys({**base, "inputs.deploy_target": "none"})) def test_story_dispatch_does_not_deploy(self): self.assertFalse( deploys( { "gitea.event_name": "workflow_dispatch", "gitea.ref": "refs/heads/main", "inputs.validation_level": "story", "inputs.deploy_target": "production", } ) ) def test_dispatch_on_other_ref_does_not_deploy(self): self.assertFalse( deploys( { "gitea.event_name": "workflow_dispatch", "gitea.ref": "refs/heads/release", "inputs.validation_level": "feature", "inputs.deploy_target": "production", } ) ) def test_deploy_target_is_explicit_choice_defaulting_to_none(self): block = dispatch_input("deploy_target") self.assertIn("type: choice", block) self.assertIn("options: [none, production]", block) self.assertIn("default: none", block) self.assertIn("required: true", block) def test_push_main_source_ci_stays_enabled(self): self.assertRegex( workflow_text(), r"(?m)^ push:\n branches:\n - main$", ) def test_deploy_requires_successful_required_checks(self): deploy = job_block("deploy") self.assertRegex(deploy, r"(?ms)^ needs:\n - required-checks$") def test_required_checks_gate_feature_jobs(self): block = job_block("required-checks") for job in ("rust-feature", "frontend-e2e", "deployment"): self.assertIn(f" - {job}", block) for variable in ("RUST_FEATURE_RESULT", "E2E_RESULT", "IMAGE_RESULT"): self.assertIn(variable, block) def test_only_deploy_loads_production_secrets(self): text = workflow_text() self.assertEqual(text.count("scripts/load-openbao-env.sh"), 1) self.assertIn("scripts/load-openbao-env.sh", job_block("deploy")) self.assertNotIn("openbao", job_block("deployment").lower()) def test_deploy_keeps_build_scan_backup_health_and_smoke_steps(self): deploy = job_block("deploy") for step in ( "Load deployment secrets from OpenBao", "Build and push images", "Save deploy image scan evidence", "Sync deployment files to server", "Write environment file", "Validate required environment variables", "Deploy with Docker Compose", "Verify health endpoints", "Run authenticated product smoke", ): self.assertIn(f"name: {step}", deploy) self.assertIn("scripts/scan-images.sh", deploy) self.assertIn("deploy-community.sh", deploy) if __name__ == "__main__": unittest.main()