Files
acdl/tests/test_contract_resolver.py
T
Jon Chery dca35c78ec feat(P22): rename static-asset→static-assets + cloudfront/waf primitives + production stack + @v1.6 bump
---ci---
phase: 22
title: rename-and-production-static-assets-stack
status: complete
verification:
  - scripts/run_ci.sh: PASS (CI PIPELINE OK)
  - python3 -m pytest tests/ -v: 175 passed
  - scripts/run_platform.sh --check-only: PASS (PLATFORM CHECK OK)
  - grep -R "static-asset[^s]" . (excl .git/): 0 hits
  - grep -R "static-asset$" . (excl .git/): 0 hits
  - floating git tags v1.6 + v1 point at v1.6.0 (a90a756)
changed_files:
  - Task 1 (rename): contracts/static-asset.yaml→static-assets.yaml (git mv); modules/l2/static-asset→static-assets (git mv); sed replaceAll static-asset→static-assets in 22 files (README, docs, scripts/run_platform.sh, pipelines/deploy.yaml, modules/registry.json, tests/*, .ciagent/* historical narrative)
  - Task 2 (cloudfront primitive): modules/l1/cloudfront/interface.json + README.md
  - Task 3 (waf primitive): modules/l1/waf/interface.json + README.md
  - Task 4 (registry): modules/registry.json (+cloudfront, +waf, static-assets renamed)
  - Task 5 (augment static-assets): modules/l2/static-assets/composition.json (s3+cloudfront+waf, depth 1); modules/l1/s3/interface.json +instance.json (+bucket_regional_domain_name output); modules/l2/static-assets/README.md (production stack docs)
  - Task 6 (adapter): adapters/terraform/adapter.py (+TYPE_MAP/INPUT_MAP/OUTPUT_MAP for cloudfront distribution+OAC+wafv2 webacl; special handling in _emit_resource for OAC defaults, distribution origin/cache_behavior/restrictions/viewer_certificate/web_acl_id, waf scope/default_action/visibility_config/managed rules)
  - Task 7 (contract schema): no change needed (generic inputs object; new module names match ^[a-z][a-z0-9-]*$)
  - Task 8 (@v1.6 bump): contracts/static-assets.yaml, .gitea/.github/workflows/deploy.yml (ref: v1.6 + header comments), docs/consumer-guide.md, docs/contracts/index.md, docs/pipeline/versioning.md, docs/pipeline/index.md, docs/architecture.md, README.md, modules/l2/microservice/README.md, tests/test_environment_check.py, tests/test_pipeline_contract.py
  - Task 9 (floating tags): git tag -f v1.6 v1.6.0; git tag -f v1 v1.6.0
  - Task 10 (tests): tests/test_adapter.py (registry 11 entries/9 L1/2 L2; cloudfront+waf type map tests; TestS3Output bucket_regional_domain_name; TestStaticAssetsStack 4 tests); tests/test_contract_resolver.py (+s3/cloudfront/waf resource assertions)
generated:
  - terraform/spike/main.tf + terraform.tf (regenerated by run_platform.sh --check-only; reflect static-assets production stack + backend key spike/static-assets/)
notes:
  - D-048 full rewrite of .ciagent/ historical narrative (verbatim phase descriptions, REQ-25/27/50, D-036) — produces intentional tautologies (e.g. "Rename static-assets → static-assets") per the decision to override the v1.6 preservation precedent.
  - cloudfront interface.json resources array ordered distribution-first so the resolver (first-match wire resolution) routes bucket_regional_domain_name/waf_web_acl_arn/region to the distribution; the OAC gets adapter-provided defaults (name=acdl-oac, origin_type=s3, signing_behavior=always).
  - .ciagent/ @v1.4 references left as historical record (D-048 scope was static-asset rename only; @v1.4 is historical narrative of Phase 20).
  - s3 OUTPUT_MAP bucket_regional_domain_name not added (identity fallback in adapt() already handles it; OUTPUT_MAP documents non-identity mappings only).
---ci---
2026-07-22 19:56:52 +00:00

163 lines
6.4 KiB
Python

import json
import os
import sys
from pathlib import Path
import jsonschema
import pytest
import yaml
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
ROOT = Path(__file__).resolve().parent.parent
class TestContractSchema:
def test_schema_is_valid_json_schema(self):
schema = json.load(open(ROOT / "schemas/contract.schema.json"))
jsonschema.Draft202012Validator.check_schema(schema)
def test_schema_requires_uses_module_environment_inputs(self):
schema = json.load(open(ROOT / "schemas/contract.schema.json"))
for field in ["uses", "module", "environment", "inputs"]:
assert field in schema["required"]
class TestResolveStaticAsset:
def test_resolve_static_asset_contract(self, tmp_path):
from core.contract_resolver import resolve
stack = resolve(str(ROOT / "contracts/static-assets.yaml"), str(ROOT))
assert stack["stack"]["name"] == "static-assets"
assert stack["stack"]["kind"] == "l2"
assert stack["stack"]["depth"] == 1
assert len(stack["resources"]) >= 1
def test_resolve_static_asset_has_s3_cloudfront_waf_resources(self):
from core.contract_resolver import resolve
stack = resolve(str(ROOT / "contracts/static-assets.yaml"), str(ROOT))
types = [r["type"] for r in stack["resources"]]
assert "aws:s3:bucket" in types
assert "aws:cloudfront:distribution" in types
assert "aws:cloudfront:originaccesscontrol" in types
assert "aws:wafv2:webacl" in types
def test_resolve_static_asset_has_s3_resource(self):
from core.contract_resolver import resolve
stack = resolve(str(ROOT / "contracts/static-assets.yaml"), str(ROOT))
s3_res = [r for r in stack["resources"] if r["type"] == "aws:s3:bucket"]
assert len(s3_res) == 1
assert s3_res[0]["inputs"]["bucket_name"] == "acdl-spike-bucket"
assert s3_res[0]["inputs"]["region"] == "us-east-1"
def test_resolve_static_asset_validates_against_stack_schema(self):
from core.contract_resolver import resolve
stack = resolve(str(ROOT / "contracts/static-assets.yaml"), str(ROOT))
schema = json.load(open(ROOT / "schemas/stack.schema.json"))
jsonschema.validate(stack, schema)
class TestResolveMicroservice:
def test_resolve_microservice_contract(self):
contract = {
"uses": "acdl/pipelines/deploy.yaml@v1",
"module": "microservice",
"environment": "dev",
"inputs": {
"image": "581513795199.dkr.ecr.us-east-1.amazonaws.com/acdl-microservice:latest",
"port": 8080,
"region": "us-east-1",
},
}
contract_path = ROOT / "contracts" / "test-microservice.yaml"
with open(contract_path, "w") as fh:
yaml.dump(contract, fh)
try:
from core.contract_resolver import resolve
stack = resolve(str(contract_path), str(ROOT))
assert stack["stack"]["name"] == "microservice"
assert stack["stack"]["kind"] == "l2"
assert len(stack["resources"]) >= 6
finally:
os.remove(contract_path)
class TestResolveL1Direct:
def test_resolve_s3_direct(self, tmp_path):
contract = {
"uses": "acdl/pipelines/deploy.yaml@v1",
"module": "s3",
"environment": "dev",
"inputs": {
"bucket_name": "my-test-bucket",
"region": "us-east-1",
},
}
contract_path = tmp_path / "test-s3.yaml"
with open(contract_path, "w") as fh:
yaml.dump(contract, fh)
from core.contract_resolver import resolve
stack = resolve(str(contract_path), str(ROOT))
assert stack["stack"]["name"] == "s3"
assert stack["stack"]["kind"] == "l1"
assert len(stack["resources"]) == 1
assert stack["resources"][0]["type"] == "aws:s3:bucket"
assert stack["resources"][0]["inputs"]["bucket_name"] == "my-test-bucket"
def test_resolve_s3_validates_against_stack_schema(self, tmp_path):
contract = {
"uses": "acdl/pipelines/deploy.yaml@v1",
"module": "s3",
"environment": "dev",
"inputs": {"bucket_name": "test", "region": "us-east-1"},
}
contract_path = tmp_path / "test-s3-schema.yaml"
with open(contract_path, "w") as fh:
yaml.dump(contract, fh)
from core.contract_resolver import resolve
stack = resolve(str(contract_path), str(ROOT))
schema = json.load(open(ROOT / "schemas/stack.schema.json"))
jsonschema.validate(stack, schema)
class TestResolveErrors:
def test_unknown_module_raises(self, tmp_path):
contract = {
"uses": "acdl/pipelines/deploy.yaml@v1",
"module": "nonexistent",
"environment": "dev",
"inputs": {},
}
contract_path = tmp_path / "bad.yaml"
with open(contract_path, "w") as fh:
yaml.dump(contract, fh)
from core.contract_resolver import resolve
with pytest.raises(ValueError, match="not found in registry"):
resolve(str(contract_path), str(ROOT))
def test_missing_required_field_fails_validation(self, tmp_path):
contract = {"uses": "acdl/pipelines/deploy.yaml@v1", "module": "s3"}
contract_path = tmp_path / "incomplete.yaml"
with open(contract_path, "w") as fh:
yaml.dump(contract, fh)
from core.contract_resolver import resolve
with pytest.raises(jsonschema.ValidationError):
resolve(str(contract_path), str(ROOT))
class TestDeployPipelineContract:
def test_deploy_pipeline_validates_against_schema(self):
schema = json.load(open(ROOT / "schemas/deploy-pipeline.schema.json"))
with open(ROOT / "pipelines/deploy.yaml") as fh:
contract = yaml.safe_load(fh)
jsonschema.validate(contract, schema)
def test_deploy_pipeline_has_six_stages(self):
with open(ROOT / "pipelines/deploy.yaml") as fh:
contract = yaml.safe_load(fh)
stage_names = [s["name"] for s in contract["stages"]]
assert "validate-contract" in stage_names
assert "resolve-stack" in stage_names
assert "terraform-plan" in stage_names
assert "checkov" in stage_names
assert "confidence" in stage_names
assert "apply" in stage_names