test_first_hour_lesson.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """The baseline script on the first-hour lesson runs, and detects the drift the page says it detects.
  2. docs/wiki/First-Hour-on-a-New-System-Updates-and-Baseline.md teaches an order (update, baseline,
  3. harden, then applications) and a Bash script that records a baseline. A script that does not run,
  4. or that misses a change, teaches the wrong thing. The test pulls the script out of the page, takes
  5. a baseline, changes a throwaway copy of the inputs it can be pointed at (the output file only; the
  6. script is read-only and never changes the system), and checks the comparison reports no drift on
  7. two identical runs and a difference when a line is edited.
  8. """
  9. import re
  10. import subprocess
  11. from pathlib import Path
  12. import pytest
  13. PAGE = Path(__file__).resolve().parent.parent / "docs" / "wiki" / "First-Hour-on-a-New-System-Updates-and-Baseline.md"
  14. FENCE = re.compile(r"^```(\w*)\n(.*?)^```$", re.MULTILINE | re.DOTALL)
  15. SECTIONS = ["os", "packages", "listening-ports", "users", "groups-with-admin-rights", "enabled-services", "firewall", "config-hashes"]
  16. def script() -> str:
  17. found = [b for tag, b in FENCE.findall(PAGE.read_text(encoding="utf-8")) if tag == "bash" and "baseline.sh - write" in b]
  18. assert len(found) == 1, f"expected one baseline.sh block, found {len(found)}"
  19. return found[0]
  20. def run(path: Path, *args: str) -> subprocess.CompletedProcess:
  21. return subprocess.run(["bash", str(path), *args], capture_output=True, text=True, timeout=120, check=False)
  22. @pytest.fixture()
  23. def baseline(tmp_path):
  24. p = tmp_path / "baseline.sh"
  25. p.write_text(script())
  26. return p
  27. def test_the_order_is_in_the_page_and_in_that_order():
  28. text = PAGE.read_text(encoding="utf-8")
  29. steps = ["1. Update, then turn on automatic updates", "2. Take a baseline", "3. Harden", "4. Applications and workloads"]
  30. positions = [text.index(s) for s in steps]
  31. assert positions == sorted(positions)
  32. def test_snapshot_has_every_section(baseline, tmp_path):
  33. out = tmp_path / "a.txt"
  34. done = run(baseline, "snapshot", str(out))
  35. assert done.returncode == 0, done.stderr
  36. body = out.read_text()
  37. for name in SECTIONS:
  38. assert f"## {name}" in body
  39. def test_compare_is_quiet_without_drift_and_loud_with_it(baseline, tmp_path):
  40. a, b = tmp_path / "a.txt", tmp_path / "b.txt"
  41. assert run(baseline, "snapshot", str(a)).returncode == 0
  42. b.write_text(a.read_text())
  43. quiet = run(baseline, "compare", str(a), str(b))
  44. assert quiet.returncode == 0 and "no drift" in quiet.stdout
  45. b.write_text(a.read_text().replace("## users\n", "## users\nintruder uid=0 shell=/bin/sh\n"))
  46. loud = run(baseline, "compare", str(a), str(b))
  47. assert loud.returncode != 0 or "intruder" in loud.stdout
  48. assert "+intruder" in loud.stdout
  49. def test_bad_usage_exits_nonzero(baseline):
  50. assert run(baseline).returncode == 2
  51. assert run(baseline, "snapshot").returncode == 2