"""Every Azure and AWS IaC pattern passes `terraform validate` against its pinned provider, and every bundle loads with all its roles. Patterns need Terraform and TF_MIRROR.""" import os import re import shutil from pathlib import Path import pytest from app.core.providers import load_provider from app.engine.agent import AGENTS_DIR from app.engine.local import TerraformRunner from app.engine.roles import load_roles SKILLS = Path(__file__).resolve().parents[1] / "skills" MIRROR = os.getenv("TF_MIRROR") VARIABLES = { "azure-iac-patterns": ("subscription_id", "location", "resource_group_name"), "aws-iac-patterns": ("region",), } PATTERNS = [(skill, path.stem) for skill in VARIABLES for path in sorted((SKILLS / skill / "references").glob("*.md"))] @pytest.mark.parametrize("name", ["gcp", "azure", "aws"]) def test_a_bundle_loads_with_a_skill_for_every_pillar_and_every_role(name): bundle = load_provider(name) roles = load_roles(AGENTS_DIR, bundle) assert set(bundle.pillar_skills.values()) <= set(bundle.skills) assert len(bundle.contract.pillar_sections) == len(bundle.pillar_skills) assert roles["iac-author"].skills @pytest.mark.parametrize( "name,pillars", [("azure", 5), ("aws", 6)] ) def test_each_cloud_has_its_frameworks_pillars(name, pillars): assert len(load_provider(name).pillar_skills) == pillars @pytest.mark.parametrize("name", ["azure", "aws"]) def test_the_guide_template_has_a_section_for_each_pillar(name): bundle = load_provider(name) template = bundle.output_template.read_text() for section in bundle.contract.pillar_sections: assert re.search(rf"^### 5\.\d\. {re.escape(section)}$", template, re.MULTILINE), section @pytest.mark.skipif(not MIRROR or not shutil.which("terraform"), reason="needs Terraform and TF_MIRROR") @pytest.mark.parametrize("skill,name", PATTERNS) async def test_a_pattern_validates(skill, name, tmp_path, monkeypatch): config = tmp_path / "terraformrc" config.write_text(f'provider_installation {{\n filesystem_mirror {{\n path = "{MIRROR}"\n }}\n}}\n') monkeypatch.setenv("TF_CLI_CONFIG_FILE", str(config)) text = (SKILLS / skill / "references" / f"{name}.md").read_text() code = re.search(r"```hcl\n(.*?)```", text, re.DOTALL).group(1) variables = "".join(f'variable "{v}" {{\n type = string\n}}\n' for v in VARIABLES[skill]) result = await TerraformRunner().validate( { "versions.tf": (SKILLS / skill / "assets" / "versions.tf").read_text(), "variables.tf": variables, "main.tf": code, } ) assert "failed" not in result.message.lower(), result.message