forked from mtusi2026/mtusi2026lab3
119 lines
5.4 KiB
Python
119 lines
5.4 KiB
Python
"""Offline regression tests for the test harness, not evidence about the API."""
|
|
import copy
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
import run as harness
|
|
|
|
|
|
class HarnessTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.config = json.loads(Path(__file__).with_name("config.example.json").read_text())
|
|
|
|
def test_default_all_is_blocked_and_does_not_pretend_success(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
output = Path(directory) / "result.json"
|
|
env = {key: value for key, value in os.environ.items()
|
|
if key not in ("LAB2_BASE_URL", "LAB2_EXPIRY_BASE_URL", "LAB2_TEST_DATABASE_URL", "LAB2_RESTORED_DATABASE_URL")}
|
|
result = subprocess.run([sys.executable, str(Path(harness.__file__)), "--suite", "all", "--output", str(output)],
|
|
env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
|
self.assertEqual(result.returncode, 2)
|
|
items = json.loads(output.read_text())["results"]
|
|
self.assertGreater(len(items), 10)
|
|
self.assertTrue(all(item["status"] == "BLOCKED" for item in items))
|
|
|
|
def test_hooks_need_both_explicit_opt_in_and_real_configuration(self):
|
|
h = harness.Harness(self.config, Path(__file__))
|
|
with self.assertRaises(harness.Blocked):
|
|
h.require_hooks("stop_postgres")
|
|
h.allow_hooks = True
|
|
with self.assertRaises(harness.Blocked):
|
|
h.require_hooks("stop_postgres")
|
|
|
|
def test_recovery_runs_even_when_failure_hook_partially_fails(self):
|
|
config = copy.deepcopy(self.config)
|
|
config["hooks"].update(stop_postgres=["not-executed"], start_postgres=["not-executed"])
|
|
h = harness.Harness(config, Path(__file__), True)
|
|
calls = []
|
|
|
|
def hook(name):
|
|
calls.append(name)
|
|
if name == "stop_postgres":
|
|
raise harness.Failure("synthetic partial failure")
|
|
|
|
h.hook = hook
|
|
with self.assertRaises(harness.Failure):
|
|
with h.disruption("stop_postgres", "start_postgres"):
|
|
self.fail("failed setup must not execute test body")
|
|
self.assertEqual(calls, ["stop_postgres", "start_postgres"])
|
|
|
|
def test_adapter_output_is_not_exposed_on_failure(self):
|
|
with self.assertRaises(harness.Failure) as error:
|
|
harness.bounded_command([sys.executable, "-c", "print('synthetic-private-marker'); raise SystemExit(7)"],
|
|
Path(__file__).parent, 3)
|
|
self.assertNotIn("synthetic-private-marker", str(error.exception))
|
|
|
|
def test_unimplemented_adapter_is_blocked(self):
|
|
with self.assertRaises(harness.Blocked):
|
|
harness.bounded_command([sys.executable, "-c", "raise SystemExit(2)"], Path(__file__).parent, 3)
|
|
|
|
def test_counter_reset_missing_and_boolean_do_not_pass(self):
|
|
for before, after in (({}, {}), ({"sql_queries": 5}, {"sql_queries": 4}),
|
|
({"sql_queries": False}, {"sql_queries": True})):
|
|
with self.subTest(before=before):
|
|
with self.assertRaises(harness.Failure):
|
|
harness.counter_delta(before, after, "sql_queries")
|
|
self.assertEqual(harness.counter_delta({"sql_queries": 5}, {"sql_queries": 8}, "sql_queries"), 3)
|
|
|
|
def test_snapshot_comparison_rejects_missing_changed_or_same_database(self):
|
|
tables = {name: {"rows": 3, "sha256": "a" * 64}
|
|
for name in ("users", "chats", "chat_members", "messages")}
|
|
source = {"identity_sha256": "source", "empty": False, "tables": tables}
|
|
restored = dict(source, identity_sha256="target")
|
|
harness.compare_snapshots(source, restored)
|
|
for broken in (source, dict(restored, tables={}), dict(restored, empty=True)):
|
|
with self.assertRaises(harness.Failure):
|
|
harness.compare_snapshots(source, broken)
|
|
broken = copy.deepcopy(restored)
|
|
broken["tables"]["messages"]["sha256"] = "b" * 64
|
|
with self.assertRaises(harness.Failure):
|
|
harness.compare_snapshots(source, broken)
|
|
|
|
def test_client_timeout_is_failure_not_a_successful_server_timeout(self):
|
|
config = copy.deepcopy(self.config)
|
|
config["hooks"].update(block_query=["not-executed"], unblock_query=["not-executed"])
|
|
h = harness.Harness(config, Path(__file__), True)
|
|
calls = []
|
|
h.hook = lambda name: calls.append(name)
|
|
|
|
class FakeClient:
|
|
def user(self, _):
|
|
return {"id": "fixture", "token": "never-printed"}
|
|
|
|
def call(self, method, path, expected=200, **kwargs):
|
|
if expected == 503:
|
|
raise harness.Failure("connection timeout")
|
|
return {}
|
|
|
|
h.client = lambda **_: FakeClient()
|
|
with self.assertRaises(harness.Failure):
|
|
h.timeout("query")
|
|
self.assertEqual(calls, ["block_query", "unblock_query"])
|
|
|
|
def test_fixture_size_thresholds_are_not_silently_lowered(self):
|
|
for key, value in (("plan_rows", 9999), ("research_rows", 99999), ("repetitions", 1)):
|
|
config = copy.deepcopy(self.config)
|
|
config["sql"][key] = value
|
|
with self.assertRaises(harness.Failure):
|
|
harness.validate_config(config)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|