b758a7c242
---ci--- project: acdl phase: 21 milestone: v1.6 status: execute ---/ci--- Rename the acdl_platform/ package to core/ across the directory, all imports in tests/scripts/pipelines/workflows, and doc references. The package is imported as core.confidence_signal / core.contract_resolver / core.outbox_writer. The deploy workflow's platform-repo checkout dir is renamed acdl-platform/ -> platform/ (workspace path, not the python package). Both .gitea + .github workflows stay byte-identical. Note: the original target name 'platform/' shadows Python's stdlib platform module (pytest's import uuid -> platform.system() fails when the repo root is on sys.path, which every test does). 'core/' avoids the clash while honoring the intent (drop the verbose acdl_platform). Tests: 154 pass. run_ci.sh green.
361 lines
14 KiB
Python
361 lines
14 KiB
Python
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import jsonschema
|
|
import pytest
|
|
import yaml
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
def _load_yaml(path):
|
|
with open(ROOT / path) as f:
|
|
return yaml.safe_load(f)
|
|
|
|
|
|
def _load_workflow(path):
|
|
wf = _load_yaml(path)
|
|
if True in wf:
|
|
wf["on"] = wf[True]
|
|
return wf
|
|
|
|
|
|
class TestPipelineSchema:
|
|
def test_schema_is_valid_json_schema(self):
|
|
schema = json.load(open(ROOT / "schemas/pipeline.schema.json"))
|
|
jsonschema.Draft202012Validator.check_schema(schema)
|
|
|
|
def test_schema_has_required_fields(self):
|
|
schema = json.load(open(ROOT / "schemas/pipeline.schema.json"))
|
|
assert "name" in schema["required"]
|
|
assert "triggers" in schema["required"]
|
|
assert "runner" in schema["required"]
|
|
assert "stages" in schema["required"]
|
|
|
|
def test_schema_stage_def_has_command_and_required(self):
|
|
schema = json.load(open(ROOT / "schemas/pipeline.schema.json"))
|
|
stage_def = schema["$defs"]["stage"]
|
|
assert "command" in stage_def["required"]
|
|
assert "required" in stage_def["required"]
|
|
|
|
|
|
class TestPipelineContract:
|
|
def test_contract_validates_against_schema(self):
|
|
schema = json.load(open(ROOT / "schemas/pipeline.schema.json"))
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
jsonschema.validate(contract, schema)
|
|
|
|
def test_contract_has_three_stages(self):
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
stage_names = [s["name"] for s in contract["stages"]]
|
|
assert stage_names == ["lint", "test", "check-only"]
|
|
|
|
def test_contract_runner_is_ubuntu_latest(self):
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
assert contract["runner"] == "ubuntu-latest"
|
|
|
|
def test_contract_python_version(self):
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
assert contract["python_version"] == "3.12"
|
|
|
|
def test_contract_triggers_push_main(self):
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
assert "main" in contract["triggers"]["push"]
|
|
|
|
def test_contract_triggers_pr_main(self):
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
assert "main" in contract["triggers"]["pull_request"]
|
|
|
|
def test_contract_all_stages_required(self):
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
for stage in contract["stages"]:
|
|
assert stage["required"] is True
|
|
|
|
def test_contract_lint_command_compiles_python(self):
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
lint = next(s for s in contract["stages"] if s["name"] == "lint")
|
|
assert "py_compile" in lint["command"]
|
|
assert "core/confidence_signal.py" in lint["command"]
|
|
assert "adapters/terraform/adapter.py" in lint["command"]
|
|
|
|
def test_contract_test_command_runs_pytest(self):
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
test_stage = next(s for s in contract["stages"] if s["name"] == "test")
|
|
assert "pytest" in test_stage["command"]
|
|
|
|
def test_contract_check_only_runs_platform(self):
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
check = next(s for s in contract["stages"] if s["name"] == "check-only")
|
|
assert "run_platform.sh" in check["command"]
|
|
assert "--check-only" in check["command"]
|
|
|
|
|
|
class TestWorkflowConformance:
|
|
def test_gitea_workflow_exists(self):
|
|
assert (ROOT / ".gitea/workflows/ci.yml").is_file()
|
|
|
|
def test_github_workflow_exists(self):
|
|
assert (ROOT / ".github/workflows/ci.yml").is_file()
|
|
|
|
def test_workflows_are_byte_identical(self):
|
|
gitea = open(ROOT / ".gitea/workflows/ci.yml", "rb").read()
|
|
github = open(ROOT / ".github/workflows/ci.yml", "rb").read()
|
|
assert gitea == github, "Gitea and GitHub workflows must be byte-identical"
|
|
|
|
def test_gitea_workflow_name_matches_contract(self):
|
|
wf = _load_workflow(".gitea/workflows/ci.yml")
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
assert wf["name"] == contract["name"]
|
|
|
|
def test_gitea_workflow_has_three_jobs(self):
|
|
wf = _load_workflow(".gitea/workflows/ci.yml")
|
|
assert set(wf["jobs"].keys()) == {"lint", "test", "check-only"}
|
|
|
|
def test_gitea_workflow_triggers_match_contract(self):
|
|
wf = _load_workflow(".gitea/workflows/ci.yml")
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
assert wf["on"]["push"]["branches"] == contract["triggers"]["push"]
|
|
assert wf["on"]["pull_request"]["branches"] == contract["triggers"]["pull_request"]
|
|
|
|
def test_gitea_workflow_runner_matches_contract(self):
|
|
wf = _load_workflow(".gitea/workflows/ci.yml")
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
for job in wf["jobs"].values():
|
|
assert job["runs-on"] == contract["runner"]
|
|
|
|
def test_gitea_workflow_python_version_matches_contract(self):
|
|
wf = _load_workflow(".gitea/workflows/ci.yml")
|
|
contract = _load_yaml("pipelines/ci.yaml")
|
|
for job in wf["jobs"].values():
|
|
setup_step = next(
|
|
s for s in job["steps"] if "setup-python" in s.get("uses", "")
|
|
)
|
|
assert setup_step["with"]["python-version"] == contract["python_version"]
|
|
|
|
def test_gitea_lint_command_matches_contract(self):
|
|
wf = _load_workflow(".gitea/workflows/ci.yml")
|
|
lint_job = wf["jobs"]["lint"]
|
|
run_step = next(s for s in lint_job["steps"] if "run" in s)
|
|
assert "py_compile" in run_step["run"]
|
|
for py_file in [
|
|
"core/confidence_signal.py",
|
|
"core/outbox_writer.py",
|
|
"core/contract_resolver.py",
|
|
"adapters/terraform/adapter.py",
|
|
"adapters/terraform/policy/checkov_adapter.py",
|
|
"scripts/push_consumer_image.py",
|
|
]:
|
|
assert py_file in run_step["run"], f"{py_file} missing from lint command"
|
|
|
|
def test_gitea_test_command_matches_contract(self):
|
|
wf = _load_workflow(".gitea/workflows/ci.yml")
|
|
test_job = wf["jobs"]["test"]
|
|
run_step = next(s for s in test_job["steps"] if "run" in s and "pytest" in s["run"])
|
|
assert "pytest" in run_step["run"]
|
|
|
|
def test_gitea_check_only_command_matches_contract(self):
|
|
wf = _load_workflow(".gitea/workflows/ci.yml")
|
|
check_job = wf["jobs"]["check-only"]
|
|
run_step = next(
|
|
s for s in check_job["steps"] if "run" in s and "run_platform" in s["run"]
|
|
)
|
|
assert "run_platform.sh" in run_step["run"]
|
|
assert "--check-only" in run_step["run"]
|
|
|
|
|
|
class TestRunCiScript:
|
|
def test_run_ci_script_exists_and_executable(self):
|
|
path = ROOT / "scripts/run_ci.sh"
|
|
assert path.is_file()
|
|
assert os.access(path, os.X_OK)
|
|
|
|
def test_run_ci_script_contains_lint_stage(self):
|
|
content = open(ROOT / "scripts/run_ci.sh").read()
|
|
assert "py_compile" in content
|
|
assert "core/confidence_signal.py" in content
|
|
assert "core/contract_resolver.py" in content
|
|
assert "adapters/terraform/adapter.py" in content
|
|
|
|
def test_run_ci_script_contains_test_stage(self):
|
|
content = open(ROOT / "scripts/run_ci.sh").read()
|
|
assert "pytest" in content
|
|
assert "tests/" in content
|
|
|
|
def test_run_ci_script_contains_check_only_stage(self):
|
|
content = open(ROOT / "scripts/run_ci.sh").read()
|
|
assert "run_platform.sh" in content
|
|
assert "--check-only" in content
|
|
|
|
def test_run_ci_script_has_success_message(self):
|
|
content = open(ROOT / "scripts/run_ci.sh").read()
|
|
assert "CI PIPELINE OK" in content
|
|
|
|
def test_run_ci_lint_and_check_only_pass(self):
|
|
result = subprocess.run(
|
|
["bash", "-c",
|
|
f"cd {ROOT} && "
|
|
"python3 -m py_compile "
|
|
"core/confidence_signal.py "
|
|
"core/outbox_writer.py "
|
|
"core/contract_resolver.py "
|
|
"adapters/terraform/adapter.py "
|
|
"adapters/terraform/policy/checkov_adapter.py "
|
|
"scripts/push_consumer_image.py && "
|
|
"echo 'lint: OK' && "
|
|
"bash scripts/run_platform.sh --check-only && "
|
|
"echo 'check-only: OK'"],
|
|
capture_output=True, text=True, cwd=str(ROOT),
|
|
timeout=30,
|
|
)
|
|
assert result.returncode == 0, f"stdout: {result.stdout}\nstderr: {result.stderr}"
|
|
assert "lint: OK" in result.stdout
|
|
assert "check-only: OK" in result.stdout
|
|
assert "PLATFORM CHECK OK" in result.stdout
|
|
|
|
|
|
class TestRunPlatformStreaming:
|
|
def test_check_only_streams_emitted_terraform(self):
|
|
result = subprocess.run(
|
|
["bash", str(ROOT / "scripts/run_platform.sh"), "--check-only"],
|
|
capture_output=True, text=True, cwd=str(ROOT),
|
|
timeout=30,
|
|
)
|
|
assert result.returncode == 0
|
|
assert "PLATFORM CHECK OK" in result.stdout
|
|
assert "--- emitted terraform/spike/main.tf ---" in result.stdout
|
|
assert "aws_s3_bucket" in result.stdout
|
|
|
|
def test_check_only_quiet_suppresses_terraform(self):
|
|
result = subprocess.run(
|
|
["bash", str(ROOT / "scripts/run_platform.sh"), "--check-only", "--quiet"],
|
|
capture_output=True, text=True, cwd=str(ROOT),
|
|
timeout=30,
|
|
)
|
|
assert result.returncode == 0
|
|
assert "PLATFORM CHECK OK" in result.stdout
|
|
assert "--- emitted terraform/spike/main.tf ---" not in result.stdout
|
|
|
|
|
|
class TestDeployPipelineSchema:
|
|
def test_deploy_schema_is_valid_json_schema(self):
|
|
schema = json.load(open(ROOT / "schemas/deploy-pipeline.schema.json"))
|
|
jsonschema.Draft202012Validator.check_schema(schema)
|
|
|
|
def test_deploy_schema_has_required_fields(self):
|
|
schema = json.load(open(ROOT / "schemas/deploy-pipeline.schema.json"))
|
|
assert "name" in schema["required"]
|
|
assert "triggers" in schema["required"]
|
|
assert "runner" in schema["required"]
|
|
assert "stages" in schema["required"]
|
|
|
|
def test_deploy_schema_stage_def_has_command_and_required(self):
|
|
schema = json.load(open(ROOT / "schemas/deploy-pipeline.schema.json"))
|
|
stage_def = schema["$defs"]["stage"]
|
|
assert "command" in stage_def["required"]
|
|
assert "required" in stage_def["required"]
|
|
|
|
|
|
class TestDeployPipelineContract:
|
|
def test_deploy_contract_validates_against_schema(self):
|
|
schema = json.load(open(ROOT / "schemas/deploy-pipeline.schema.json"))
|
|
contract = _load_yaml("pipelines/deploy.yaml")
|
|
jsonschema.validate(contract, schema)
|
|
|
|
def test_deploy_contract_has_six_stages(self):
|
|
contract = _load_yaml("pipelines/deploy.yaml")
|
|
stage_names = [s["name"] for s in contract["stages"]]
|
|
assert stage_names == [
|
|
"validate-contract",
|
|
"resolve-stack",
|
|
"terraform-plan",
|
|
"checkov",
|
|
"confidence",
|
|
"apply",
|
|
]
|
|
|
|
def test_deploy_contract_runner_is_ubuntu_latest(self):
|
|
contract = _load_yaml("pipelines/deploy.yaml")
|
|
assert contract["runner"] == "ubuntu-latest"
|
|
|
|
|
|
class TestDeployWorkflowConformance:
|
|
def test_gitea_deploy_workflow_exists(self):
|
|
assert (ROOT / ".gitea/workflows/deploy.yml").is_file()
|
|
|
|
def test_github_deploy_workflow_exists(self):
|
|
assert (ROOT / ".github/workflows/deploy.yml").is_file()
|
|
|
|
def test_deploy_workflows_are_byte_identical(self):
|
|
gitea = open(ROOT / ".gitea/workflows/deploy.yml", "rb").read()
|
|
github = open(ROOT / ".github/workflows/deploy.yml", "rb").read()
|
|
assert gitea == github, "Gitea and GitHub deploy workflows must be byte-identical"
|
|
|
|
def test_deploy_workflow_name_matches_contract(self):
|
|
wf = _load_workflow(".gitea/workflows/deploy.yml")
|
|
contract = _load_yaml("pipelines/deploy.yaml")
|
|
assert wf["name"] == contract["name"]
|
|
|
|
def test_deploy_workflow_is_reusable(self):
|
|
wf = _load_workflow(".gitea/workflows/deploy.yml")
|
|
assert "workflow_call" in wf["on"]
|
|
|
|
def test_deploy_workflow_has_contract_input(self):
|
|
wf = _load_workflow(".gitea/workflows/deploy.yml")
|
|
inputs = wf["on"]["workflow_call"]["inputs"]
|
|
assert "contract" in inputs
|
|
assert inputs["contract"]["default"] == ".acdl/contract.yaml"
|
|
|
|
def test_deploy_workflow_has_mode_input(self):
|
|
wf = _load_workflow(".gitea/workflows/deploy.yml")
|
|
inputs = wf["on"]["workflow_call"]["inputs"]
|
|
assert "mode" in inputs
|
|
assert inputs["mode"]["default"] == "full"
|
|
|
|
def test_deploy_workflow_runner_matches_contract(self):
|
|
wf = _load_workflow(".gitea/workflows/deploy.yml")
|
|
contract = _load_yaml("pipelines/deploy.yaml")
|
|
for job in wf["jobs"].values():
|
|
assert job["runs-on"] == contract["runner"]
|
|
|
|
def test_deploy_workflow_python_version_matches_contract(self):
|
|
wf = _load_workflow(".gitea/workflows/deploy.yml")
|
|
contract = _load_yaml("pipelines/deploy.yaml")
|
|
for job in wf["jobs"].values():
|
|
setup_step = next(
|
|
s for s in job["steps"] if "setup-python" in s.get("uses", "")
|
|
)
|
|
assert setup_step["with"]["python-version"] == contract["python_version"]
|
|
|
|
def test_deploy_workflow_invokes_run_platform(self):
|
|
wf = _load_workflow(".gitea/workflows/deploy.yml")
|
|
deploy_job = wf["jobs"]["deploy"]
|
|
run_step = next(
|
|
s for s in deploy_job["steps"] if "run" in s and "run_platform" in s["run"]
|
|
)
|
|
assert "run_platform.sh" in run_step["run"]
|
|
|
|
def test_deploy_workflow_checks_out_platform_repo(self):
|
|
wf = _load_workflow(".gitea/workflows/deploy.yml")
|
|
deploy_job = wf["jobs"]["deploy"]
|
|
platform_checkout = next(
|
|
s for s in deploy_job["steps"]
|
|
if "checkout" in s.get("uses", "") and s.get("with", {}).get("path") == "platform"
|
|
)
|
|
assert platform_checkout["with"]["repository"] == "acdl/acdl"
|
|
|
|
def test_deploy_workflow_permissions_id_token_write(self):
|
|
wf = _load_workflow(".gitea/workflows/deploy.yml")
|
|
assert wf["permissions"]["id-token"] == "write"
|
|
assert wf["permissions"]["contents"] == "read"
|
|
|
|
|
|
class TestSampleContractVersioning:
|
|
def test_sample_contract_uses_versioned_tag(self):
|
|
contract = _load_yaml("contracts/static-asset.yaml")
|
|
uses = contract["uses"]
|
|
assert "@v" in uses, "sample contract must use a versioned @vX.Y tag"
|
|
assert "@main" not in uses, "sample contract must not use @main"
|
|
assert uses == "acdl/pipelines/deploy.yaml@v1.4" |