test_enterprise_context.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """Checks for the four enterprise-context pages in docs/wiki/.
  2. These pages mix verified vendor claims with general industry knowledge, so the
  3. tests keep the mix honest: each page carries the orientation banner, links its
  4. neighbors, has a contrast table and a predict-then-check exercise, and every
  5. row of its claims register is either dated and sourced, general knowledge, or
  6. marked UNVERIFIED. The landscape page's Doppler section is held to the same rule.
  7. """
  8. import ipaddress
  9. import itertools
  10. import re
  11. from pathlib import Path
  12. import pytest
  13. ROOT = Path(__file__).resolve().parent.parent
  14. WIKI = ROOT / "docs" / "wiki"
  15. PAGES = [
  16. "Enterprise-Networks-Campus-Branch-Datacenter-Cloud",
  17. "Enterprise-Infrastructure-Differences",
  18. "Enterprise-Identity-and-Secrets-at-Scale",
  19. "Careers-Roles-and-What-to-Learn-Next",
  20. ]
  21. # Reading order of the whole Enterprise context section, including the page
  22. # written separately (change management), which has its own conventions.
  23. ORDER = [*PAGES[:2], "Enterprise-Change-Management-for-Beginners", *PAGES[2:]]
  24. BANNER = "Orientation, not a design guide: verified claims dated"
  25. LINK = re.compile(r"\]\(([A-Za-z0-9_-]+)\.md(?:#([^)]*))?\)")
  26. VERIFIED = re.compile(r"^Verified \d{4}-\d{2}-\d{2}$")
  27. OK_STATUS = (VERIFIED, re.compile(r"^General knowledge$"), re.compile(r"^UNVERIFIED$"))
  28. def text(name: str) -> str:
  29. return (WIKI / f"{name}.md").read_text()
  30. def slug(heading: str) -> str:
  31. heading = re.sub(r"[^\w\- ]", "", heading.strip().lower())
  32. return heading.replace(" ", "-")
  33. def anchors(name: str) -> set[str]:
  34. found = set()
  35. for line in text(name).splitlines():
  36. m = re.match(r"#{1,6}\s+(.*)", line)
  37. if m:
  38. found.add(slug(m.group(1)))
  39. return found
  40. def register_rows(body: str) -> list[list[str]]:
  41. """Rows of the table under '## Claims register'."""
  42. section = body.split("## Claims register", 1)[1]
  43. rows = []
  44. for line in section.splitlines():
  45. if line.startswith("|") and not re.match(r"^\|\s*-", line):
  46. cells = [c.strip() for c in line.strip().strip("|").split("|")]
  47. if cells[0] != "Claim":
  48. rows.append(cells)
  49. return rows
  50. @pytest.mark.parametrize("name", PAGES)
  51. def test_banner_prerequisites_and_next(name):
  52. body = text(name)
  53. assert BANNER in "\n".join(body.splitlines()[:4]), f"{name}: banner missing"
  54. assert re.search(r"^\*\*Prerequisites:\*\* .*\]\(", body, re.MULTILINE)
  55. assert re.search(r"^Next: .*\]\(", body, re.MULTILINE)
  56. @pytest.mark.parametrize("name", PAGES)
  57. def test_contrast_table_and_exercise(name):
  58. body = text(name)
  59. assert re.search(r"^\|.*In this project.*In (an enterprise|a company).*\|", body, re.MULTILINE), (
  60. f"{name}: no 'In this project / In an enterprise' contrast table"
  61. )
  62. for needle in ("Predict, then check", "Reference answers", "Try it yourself"):
  63. assert needle in body, f"{name}: missing {needle}"
  64. @pytest.mark.parametrize("name", PAGES)
  65. def test_every_claims_register_row_is_dated_general_or_unverified(name):
  66. rows = register_rows(text(name))
  67. assert len(rows) >= 5, f"{name}: claims register is too thin"
  68. for row in rows:
  69. assert len(row) == 3, f"{name}: bad register row {row}"
  70. claim, status, source = row
  71. assert any(p.match(status) for p in OK_STATUS), f"{name}: status {status!r} for {claim!r}"
  72. if VERIFIED.match(status):
  73. assert re.search(r"https://\S+", source), f"{name}: verified claim without a URL: {claim!r}"
  74. @pytest.mark.parametrize("name", PAGES)
  75. def test_every_wiki_link_and_anchor_resolves(name):
  76. problems = []
  77. for target, anchor in LINK.findall(text(name)):
  78. if not (WIKI / f"{target}.md").exists():
  79. problems.append(f"{target} (missing page)")
  80. elif anchor and anchor not in anchors(target):
  81. problems.append(f"{target}#{anchor} (missing anchor)")
  82. assert not problems, f"{name}: {problems}"
  83. def test_next_lines_follow_the_chain():
  84. for earlier, later in itertools.pairwise(ORDER):
  85. line = re.search(r"^Next: .*$", text(earlier), re.MULTILINE).group(0)
  86. assert f"({later}.md)" in line, f"{earlier}: Next should name {later}"
  87. def test_pages_are_in_contents_and_reading_order_under_enterprise_context():
  88. for page, heading in (("Contents", "Track 5: Other clouds and enterprise practice"), ("Reading-Order", "## Enterprise context")):
  89. body = text(page)
  90. assert heading in body, f"{page}: no enterprise section ({heading})"
  91. links = [t for t, _ in LINK.findall(body)]
  92. positions = [links.index(n) for n in ORDER]
  93. assert positions == sorted(positions), f"{page}: pages out of order"
  94. assert "Enterprise-Networks-Campus-Branch-Datacenter-Cloud.md" in text("Home")
  95. def test_landscape_page_says_doppler_is_used_in_enterprises_with_a_date():
  96. body = text("Small-Example-Enterprise-Landscape")
  97. section = body.split("### Doppler is used in enterprises too", 1)[1].split("\n## ", 1)[0]
  98. assert re.search(r"retrieved \d{4}-\d{2}-\d{2}", section)
  99. assert "docs.doppler.com" in section
  100. assert "not an endorsement" in section
  101. for page in PAGES:
  102. assert f"({page}.md)" in body, f"landscape page does not point to {page}"
  103. @pytest.mark.parametrize("name", PAGES)
  104. def test_only_documentation_or_private_example_addresses_and_no_secret_shapes(name):
  105. body = text(name)
  106. ok_nets = [ipaddress.ip_network(n) for n in ("192.0.2.0/24", "198.51.100.0/24", "203.0.113.0/24", "10.0.0.0/8")]
  107. for ip in re.findall(r"\b\d{1,3}(?:\.\d{1,3}){3}\b", body):
  108. try:
  109. addr = ipaddress.ip_address(ip)
  110. except ValueError:
  111. continue
  112. assert any(addr in net for net in ok_nets), f"{name}: address outside example ranges"
  113. assert not re.search(r"dop_(v1|pt|st)_|AKIA[0-9A-Z]{12,}|-----BEGIN", body), f"{name}: secret-shaped literal"