test_module_contract.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """The provider module contract, checked structurally.
  2. Every provider supplies the same module roles with the same output names, and
  3. documents what differs in a README. A new provider's modules are found by their
  4. directory name, so adding one without the contract fails here.
  5. """
  6. import re
  7. from pathlib import Path
  8. ROOT = Path(__file__).resolve().parent.parent
  9. CONTRACT = {
  10. "vpc": {"id", "ip_range", "name"},
  11. "firewall": {"id", "name", "rule_targets", "source_members", "destination_members"},
  12. "twingate-connector": {"instance_id", "instance_name", "private_ip", "size"},
  13. }
  14. def provider_modules():
  15. for role in CONTRACT:
  16. for path in sorted((ROOT / "modules").glob(f"**/{role}")):
  17. if ".terraform" in path.parts or not path.is_dir():
  18. continue
  19. yield role, path
  20. def outputs(path):
  21. text = (path / "outputs.tf").read_text()
  22. return set(re.findall(r'(?m)^output\s+"([A-Za-z0-9_-]+)"', text))
  23. def providers():
  24. """Map provider (modules/<provider>/<role>; DigitalOcean is modules/<role>) to its roles."""
  25. found = {}
  26. for role, path in provider_modules():
  27. # DigitalOcean sits directly under modules/; others under modules/<provider>/.
  28. provider = "digitalocean" if path.parent.name == "modules" else path.parent.name
  29. found.setdefault(provider, {})[role] = path
  30. return found
  31. def test_every_provider_supplies_every_role():
  32. assert providers(), "no provider modules found"
  33. for provider, roles in providers().items():
  34. missing = set(CONTRACT) - set(roles)
  35. assert not missing, f"provider {provider} lacks modules: {sorted(missing)}"
  36. def test_every_provider_module_has_the_contract_outputs():
  37. for role, path in provider_modules():
  38. missing = CONTRACT[role] - outputs(path)
  39. assert not missing, (
  40. f"{path.relative_to(ROOT)} lacks contract outputs: {sorted(missing)}"
  41. )
  42. def test_no_output_exposes_boot_data_or_tokens():
  43. # The connector's user_data holds the Twingate tokens. A contract output
  44. # name proves nothing about its value, so look at every output expression.
  45. for _, path in provider_modules():
  46. text = (path / "outputs.tf").read_text()
  47. for needle in (
  48. "user_data",
  49. "custom_data",
  50. "metadata_startup_script",
  51. "token",
  52. "bootstrap",
  53. ):
  54. assert needle not in text, (
  55. f"{path.relative_to(ROOT)}/outputs.tf mentions {needle!r}"
  56. )
  57. def test_every_provider_module_documents_roles_and_extensions():
  58. for role, path in provider_modules():
  59. readme = path / "README.md"
  60. assert readme.exists(), f"{path.relative_to(ROOT)} has no README.md"
  61. text = readme.read_text()
  62. rows = re.findall(r"(?m)^\| Role \|.*\n\|[-| ]+\|\n((?:\|.*\n)+)", text)
  63. assert rows and len(rows[0].strip().splitlines()) >= 1, (
  64. f"{readme.relative_to(ROOT)}: role table has no rows"
  65. )
  66. ext = re.search(r"## Provider extensions\n+(.+)", text, re.DOTALL)
  67. assert ext and len(ext.group(1).strip()) >= 40, (
  68. f"{readme.relative_to(ROOT)}: empty extensions section"
  69. )
  70. for name in CONTRACT[role]:
  71. assert f"`{name}`" in text, (
  72. f"{readme.relative_to(ROOT)} does not describe contract output `{name}`"
  73. )