test_update_order_pinned.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """The order of a new machine's first hour is stated the same way everywhere it is described.
  2. Issue #232: update, baseline, harden, then applications. The stage guides and the edge design must
  3. say it, the template's real structure must follow it (the
  4. update before the connector install, the hardening switched on before the connector install), and
  5. the edge design must record the install rule, the version pins and the reboot-slot window.
  6. """
  7. import re
  8. from pathlib import Path
  9. ROOT = Path(__file__).resolve().parent.parent
  10. TEMPLATE = ROOT / "modules" / "twingate-bootstrap" / "cloud-init.yaml.tftpl"
  11. EDGE = ROOT / "docs" / "superpowers" / "specs" / "2026-10-19-edge-dmz-design.md"
  12. LESSON = ROOT / "docs" / "wiki" / "First-Hour-on-a-New-System-Updates-and-Baseline.md"
  13. GUIDES = [ROOT / "docs" / "stages" / n for n in ("01-access.md", "aws-access.md", "02-telemetry.md")]
  14. ORDER = "update, baseline, harden, then applications"
  15. def read(p: Path) -> str:
  16. return p.read_text(encoding="utf-8")
  17. SHORT = ORDER
  18. LONG = "update (and switch on unattended security updates), take a baseline, harden, then applications"
  19. REQUIRED = {**{g: SHORT for g in GUIDES}, EDGE: LONG}
  20. def test_every_guide_and_the_edge_design_state_the_order():
  21. assert len(REQUIRED) == len(GUIDES) + 1
  22. for p, phrase in REQUIRED.items():
  23. flat = re.sub(r"\s+", " ", read(p)).lower()
  24. assert phrase in flat, f"{p.name} must carry: {phrase}"
  25. def test_the_template_follows_the_order():
  26. t = read(TEMPLATE)
  27. assert t.index("package_update: true") < t.index("package_upgrade: true") < t.index("\nruncmd:")
  28. assert "unattended-upgrades" in t.split("write_files:")[0]
  29. run = t.split("\nruncmd:")[1]
  30. harden = run.index("sshd -t && systemctl restart ssh")
  31. connector = run.index("apt-get install -yq twingate-connector")
  32. assert harden < connector, "hardening must be switched on before the connector is installed"
  33. def test_edge_design_records_the_edge_rules():
  34. flat = re.sub(r"\s+", " ", read(EDGE))
  35. assert "No service is installed until its own stack is built and approved" in flat
  36. assert "Exact version pins from each project's releases" in flat
  37. assert "between 01:00 and 03:00 Eastern" in flat
  38. assert "04:00" in flat and "05:00" in flat
  39. assert "No two droplets share a minute" in flat
  40. assert "rings of devices" in flat
  41. def test_slot_window_is_before_the_connector_windows():
  42. # The connector windows the design and the lesson cite come from the real droplet hardening page.
  43. hardening = read(ROOT / "docs" / "wiki" / "Droplet-Hardening.md")
  44. assert "04:00" in hardening and "05:00" in hardening
  45. assert "between 01:00 and 03:00 Eastern" in read(LESSON)
  46. def header_comment(text: str) -> str:
  47. head = text.split("\ndisable_root:")[0]
  48. return re.sub(r"\s+", " ", re.sub(r"(?m)^#\s?", "", head))
  49. def test_the_template_states_the_order_in_a_comment():
  50. """#305: the order is written into the template, as a comment, in the order the file follows."""
  51. t = read(TEMPLATE)
  52. assert ORDER in header_comment(t), f"the template header must carry: {ORDER}"
  53. # A comment is not rendered config: no template syntax inside it.
  54. assert "${" not in t.split("\ntimezone:")[0] and "%{" not in t.split("\ntimezone:")[0]
  55. assert t.index("\n# First-hour order") < t.index("package_update: true")
  56. def test_the_order_check_bites_when_the_comment_is_missing():
  57. stripped = read(TEMPLATE).replace("update, baseline,", "something, else,")
  58. assert ORDER not in header_comment(stripped)