84 lines
5.0 KiB
Python
84 lines
5.0 KiB
Python
import datetime as dt
|
|
import hashlib
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
from types import SimpleNamespace
|
|
import unittest
|
|
from unittest.mock import patch
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
import query_cutover_guard as guard
|
|
|
|
class CutoverGuardTests(unittest.TestCase):
|
|
|
|
def plan(self):
|
|
return {'shadowPolicy': {'runID': 'same-run', 'maxRecords': 15000000, 'recordsPerSecond': 100, 'bytesPerSecond': 1048576, 'commandsPerSecond': 16, 'batchSize': 100, 'maintenanceUntil': '2099-01-01T00:00:00Z'}, 'expiresAt': '2099-01-01T00:00:00Z', 'sourceCommit': 'candidate', 'namespaceUIDs': {'droboticflow-query-shadow': 'shadow-uid'}}
|
|
|
|
def evidence(self):
|
|
snap = {'state': {'factEpoch': 3}, 'global': {'heads': {'p': {'generation': 'p-gen', 'chunks': 1, 'asOf': '2026-11-16T12:00:00Z', 'sourceRevision': 4, 'globalRevision': 2}, 'w': {'generation': 'workforce-facts/v1', 'factEpoch': 1, 'asOf': '2026-11-16T12:00:00Z', 'sourceRevision': 4, 'globalRevision': 2}}}}
|
|
cases = []
|
|
for i, scenario in enumerate(sorted(guard.SCENARIOS)):
|
|
cases.append({'role': sorted(guard.ROLES)[i % len(guard.ROLES)], 'scenario': scenario, 'kind': ['counts', 'dashboard', 'work-records'][i % 3], 'authorityDigest': 'a' * 64, 'queryDigest': 'a' * 64, 'authorityAllowed': scenario not in ['cross-supplier', 'cross-organization', 'permission-revoked'], 'queryAllowed': scenario not in ['cross-supplier', 'cross-organization', 'permission-revoked'], 'productionHeadKey': 'p', 'workforceHeadKey': 'w', 'authorityScopeDigest': 'scope', 'queryScopeDigest': 'scope'})
|
|
e = {'replicaFactEpoch': 3, 'environment': 'shadow-live', 'queryDatabase': 'drobodata_fanyu_query_shadow_20261114', 'sourceCommit': 'candidate', 'namespaceUID': 'shadow-uid', 'observedAt': '2026-11-16T12:00:00Z', 'cohortDigest': hashlib.sha256(json.dumps(snap['global']['heads'], sort_keys=True, separators=(',', ':')).encode()).hexdigest(), 'cases': cases}
|
|
return (e, snap)
|
|
|
|
def test_shadow_keeps_budget_safety_and_nonce(self):
|
|
args = guard.bounded_shadow_args(self.plan(), 'nonce')
|
|
self.assertIn('--safety-run=same-run', args)
|
|
self.assertIn('--max-source-records=15000000', args)
|
|
self.assertIn('--resume-pause=nonce', args)
|
|
p = self.plan()
|
|
p['shadowPolicy']['maxRecords'] = 0
|
|
with self.assertRaises(RuntimeError):
|
|
guard.bounded_shadow_args(p)
|
|
|
|
def test_approved_throughput_stages_are_not_capped_at_100(self):
|
|
for batch in (100, 150, 200):
|
|
p = self.plan()
|
|
p['shadowPolicy']['batchSize'] = batch
|
|
p['shadowPolicy']['recordsPerSecond'] = batch
|
|
self.assertIn('--batch-size=' + str(batch), guard.bounded_shadow_args(p))
|
|
p['shadowPolicy']['batchSize'] = -1
|
|
with self.assertRaises(RuntimeError):
|
|
guard.bounded_shadow_args(p)
|
|
|
|
def test_production_writes_need_explicit_scope(self):
|
|
with self.assertRaises(RuntimeError):
|
|
guard.approved_plan(SimpleNamespace(command='shadow-sync'), None)
|
|
self.assertIsNone(guard.approved_plan(SimpleNamespace(command='preflight'), None))
|
|
|
|
def test_real_comparison_requires_results_permissions_scope_and_coverage(self):
|
|
e, s = self.evidence()
|
|
now = dt.datetime(2026, 11, 16, 12, 1, tzinfo=dt.timezone.utc)
|
|
guard.require_result_comparison(e, s, self.plan(), now)
|
|
for key, value in [('queryDigest', 'b' * 64), ('queryAllowed', True), ('queryScopeDigest', 'other')]:
|
|
changed = json.loads(json.dumps(e))
|
|
changed['cases'][0][key] = value
|
|
with self.assertRaises(RuntimeError):
|
|
guard.require_result_comparison(changed, s, self.plan(), now)
|
|
for key, value in [('cases', e['cases'][:2]), ('environment', 'synthetic'), ('namespaceUID', 'other'), ('cohortDigest', 'other')]:
|
|
changed = dict(e)
|
|
changed[key] = value
|
|
with self.assertRaises(RuntimeError):
|
|
guard.require_result_comparison(changed, s, self.plan(), now)
|
|
|
|
def test_stale_evidence_not_repackaged_as_fresh(self):
|
|
e, s = self.evidence()
|
|
with self.assertRaises(RuntimeError):
|
|
guard.require_result_comparison(e, s, self.plan(), dt.datetime(2026, 11, 16, 13, tzinfo=dt.timezone.utc))
|
|
|
|
def test_cutover_cannot_be_implied_by_shadow_approval(self):
|
|
p = self.plan()
|
|
p.update(operations=['cutover-api-flags'], context='ctx', serverSHA256='a' * 64, allowProductionCutover=False)
|
|
with tempfile.TemporaryDirectory() as d:
|
|
f = Path(d) / 'plan.json'
|
|
raw = json.dumps(p).encode()
|
|
f.write_bytes(raw)
|
|
a = SimpleNamespace(command='cutover-api-flags', approved_plan=str(f), confirm_plan=hashlib.sha256(raw).hexdigest(), approval_ref='explicit-shadow-only')
|
|
with self.assertRaisesRegex(RuntimeError, 'production entry'):
|
|
guard.approved_plan(a, None)
|
|
if __name__ == '__main__':
|
|
unittest.main()
|