Files
gcp_solution_architecture_a…/tests/test_iac_patterns.py
Jonathan Boniface 5bb22d984e
Some checks failed
validation / verify (push) Failing after 8s
fixed multile issues
2026-10-07 17:33:36 +01:00

63 lines
2.4 KiB
Python

"""The IaC patterns Skill: every pattern validates against the pinned provider.
A pattern that does not validate teaches the mistake it was written to prevent, so
each reference's Terraform is run through `terraform validate` with the Skill's own
versions file. The provider comes from the plugin cache; with no cache and no
network the test is skipped, not failed.
"""
import re
import shutil
from pathlib import Path
import pytest
from app.config import Settings
from app.providers import load_provider
from app.tools.local import TerraformRunner
SKILL = "google-cloud-iac-patterns"
ROOT = Path(__file__).resolve().parent.parent / "skills" / SKILL
REFERENCES = sorted((ROOT / "references").glob("*.md"))
_HCL = re.compile(r"^```hcl\n(.*?)^```", re.MULTILINE | re.DOTALL)
needs_terraform = pytest.mark.skipif(
shutil.which("terraform") is None, reason="terraform CLI not installed"
)
def _runner() -> TerraformRunner:
return TerraformRunner(plugin_cache_dir=Settings().terraform_plugin_cache())
def test_every_reference_is_listed_in_the_skill_and_holds_a_pattern():
skill = (ROOT / "SKILL.md").read_text(encoding="utf-8")
assert REFERENCES
for reference in REFERENCES:
assert f"references/{reference.name}" in skill
assert _HCL.findall(reference.read_text(encoding="utf-8")), reference.name
listed = set(re.findall(r"references/([\w-]+\.md)", skill))
assert listed == {reference.name for reference in REFERENCES}
def test_the_skill_pins_the_version_the_provider_bundle_pins():
pin = load_provider("gcp").terraform_providers["google"]
versions = (ROOT / "assets" / "versions.tf").read_text(encoding="utf-8")
skill = (ROOT / "SKILL.md").read_text(encoding="utf-8")
assert f'source = "{pin["source"]}"' in versions
assert f'version = "{pin["version"]}"' in versions
assert f"{pin['source']} {pin['version']}" in skill
@needs_terraform
@pytest.mark.parametrize("reference", REFERENCES, ids=lambda path: path.stem)
async def test_pattern_validates(reference: Path):
files = {
"terraform/versions.tf": (ROOT / "assets" / "versions.tf").read_text(),
"terraform/main.tf": "\n".join(_HCL.findall(reference.read_text())),
}
result = await _runner().validate(files)
if result.message.startswith("terraform init failed"):
pytest.skip("the pinned provider could not be installed")
assert result.message.startswith("terraform validate passed"), result.message