test_module_policy.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """Repository-wide rules the per-module tofu tests cannot enforce on their own.
  2. A provider connector module must reuse the shared bootstrap, and CI must run
  3. the tests of every module, so a new provider cannot quietly skip either.
  4. """
  5. import re
  6. from pathlib import Path
  7. ROOT = Path(__file__).resolve().parent.parent
  8. BOOTSTRAP = "twingate-bootstrap"
  9. def tofu_dirs():
  10. found = []
  11. for parent in ("modules", "stacks"):
  12. for tf in sorted((ROOT / parent).rglob("*.tf")):
  13. if ".terraform" in tf.parts:
  14. continue
  15. rel = tf.parent.relative_to(ROOT).as_posix()
  16. if rel not in found:
  17. found.append(rel)
  18. return found
  19. def ci_directories():
  20. text = (ROOT / ".github/workflows/ci.yml").read_text()
  21. match = re.search(r'directories:\s*"([^"]+)"', text)
  22. assert match, "ci.yml has no directories: list"
  23. return match.group(1).split()
  24. def test_ci_runs_every_module_and_stack():
  25. missing = sorted(set(tofu_dirs()) - set(ci_directories()))
  26. assert not missing, f"not run by CI (add to directories in ci.yml): {missing}"
  27. def test_ci_lists_no_directory_that_does_not_exist():
  28. gone = sorted(d for d in ci_directories() if d not in tofu_dirs())
  29. assert not gone, f"ci.yml lists directories with no .tf files: {gone}"
  30. def bootstrap_callers():
  31. callers = []
  32. for rel in tofu_dirs():
  33. if not rel.startswith("modules/") or rel == f"modules/{BOOTSTRAP}":
  34. continue
  35. main = (ROOT / rel / "main.tf").read_text()
  36. if re.search(r'source\s*=\s*"(\.\./)+' + BOOTSTRAP + '"', main):
  37. callers.append((rel, main))
  38. return callers
  39. def test_at_least_one_provider_uses_the_bootstrap():
  40. assert bootstrap_callers(), "no provider module calls the shared bootstrap"
  41. def test_a_bootstrap_caller_passes_the_shared_boot_data_straight_to_the_machine():
  42. for rel, main in bootstrap_callers():
  43. name = re.search(
  44. r'module\s+"([^"]+)"\s*\{[^}]*?source\s*=\s*"(?:\.\./)+' + BOOTSTRAP,
  45. main,
  46. re.DOTALL,
  47. ).group(1)
  48. assert re.search(
  49. r"(?m)^\s*(user_data|custom_data|metadata_startup_script)\s*=\s*module\."
  50. + name
  51. + r"\.user_data\s*$",
  52. main,
  53. ), f"{rel}: the machine's boot data must be module.{name}.user_data, unmodified"
  54. assert "templatefile" not in main, (
  55. f"{rel}: a provider module must not render its own cloud-init"
  56. )
  57. assert not (ROOT / rel / "cloud-init.yaml.tftpl").exists(), (
  58. f"{rel}: private copy of the cloud-init"
  59. )
  60. assert not re.search(r'(?m)^resource\s+"twingate_', main), (
  61. f"{rel}: Twingate resources belong in the bootstrap"
  62. )
  63. def test_the_bootstrap_template_is_the_only_cloud_init():
  64. found = sorted(
  65. p.relative_to(ROOT).as_posix()
  66. for p in ROOT.glob("modules/**/cloud-init*")
  67. if ".terraform" not in p.parts
  68. )
  69. # The edge host is a different machine role (no Twingate connector, no admin
  70. # path), so it has its own, much smaller template and does not use the
  71. # connector bootstrap. The workload host (docker-host) is a third machine
  72. # role: a Docker host with no Twingate connector, key-only SSH and Portainer.
  73. # Both are named here on purpose: a fourth copy fails.
  74. assert found == [
  75. "modules/docker-host/cloud-init.yaml.tftpl",
  76. "modules/edge-host/cloud-init.yaml.tftpl",
  77. f"modules/{BOOTSTRAP}/cloud-init.yaml.tftpl",
  78. ], found