forked from longtao.wu/droboticflow
64 lines
4.1 KiB
Python
64 lines
4.1 KiB
Python
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
import unittest
|
|
import tempfile
|
|
import datetime as dt
|
|
|
|
spec = importlib.util.spec_from_file_location('shadow_observer', Path(__file__).with_name('query-shadow-observer.py'))
|
|
m = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)
|
|
|
|
class ObserverTests(unittest.TestCase):
|
|
def bindings(self):
|
|
return dict(sourceNamespace='authority', shadowNamespace='shadow', sourceDatabase='source', targetDatabase='query',
|
|
baselineCPU=85, writers=[{} for _ in range(5)], controllers=[dict(name='query-replica', uid='p'), dict(name='query-read-builder', uid='b')])
|
|
def test_cpu_and_identity(self):
|
|
self.assertAlmostEqual(m.cpu_percent(dict(sourceProcess='s', uptimeMillis=1000, cpuMicroseconds=0),
|
|
dict(sourceProcess='s', uptimeMillis=2000, cpuMicroseconds=2000000)), 50)
|
|
with self.assertRaises(ValueError):
|
|
m.cpu_percent(dict(sourceProcess='s', uptimeMillis=2, cpuMicroseconds=2), dict(sourceProcess='x', uptimeMillis=3, cpuMicroseconds=3))
|
|
def test_stop_scope_and_uid_cas(self):
|
|
b = self.bindings(); m.check_bindings(b)
|
|
observer = m.Observer(SimpleNamespace(kubeconfig='unused', stop_on_failure=True), b); calls = []
|
|
observer.get = lambda ns, kind, name: dict(metadata=dict(uid='p' if name=='query-replica' else 'b', resourceVersion='7'), spec=dict(replicas=1))
|
|
observer.call = lambda *args, **kwargs: calls.append(args) or ''
|
|
observer.stop_shadow()
|
|
self.assertEqual(len(calls), 2)
|
|
for args in calls:
|
|
self.assertEqual(args[:4], ('-n', 'shadow', 'patch', 'deployment'))
|
|
payload = json.loads(args[-1]); self.assertEqual(payload[0]['op'], 'test'); self.assertEqual(payload[-1]['value'], 0)
|
|
observer.get = lambda *args: dict(metadata=dict(uid='replaced', resourceVersion='8'), spec=dict(replicas=1))
|
|
with self.assertRaises(ValueError): observer.stop_shadow()
|
|
self.assertEqual(len(calls), 2)
|
|
b['controllers'][0]['name'] = 'droboticflow-api'
|
|
with self.assertRaises(ValueError): m.check_bindings(b)
|
|
def test_distinct_samples_and_thresholds(self):
|
|
o = m.Observer(SimpleNamespace(kubeconfig='unused'), self.bindings())
|
|
good = dict(cpu=90, ready=True, writersCovered=True, workErrors=False, protectedIOFull=0, targetUsedFraction=.1, hostFreeBytes=120<<30)
|
|
self.assertFalse(o.unsafe(good)); self.assertTrue(o.unsafe(good))
|
|
good['cpu']=85; self.assertFalse(o.unsafe(good));good['targetUsedFraction']=.7;self.assertTrue(o.unsafe(good))
|
|
def test_business_health_missing_stale_and_real_errors(self):
|
|
now = dt.datetime.now(dt.timezone.utc)
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = Path(folder) / 'health.json'
|
|
with self.assertRaises(FileNotFoundError): m.business_health(path, now)
|
|
feed = dict(at=now.isoformat(), coverage='save-slice-submit-review-acceptance', unexpectedErrors=False, latencyRegressed=False)
|
|
path.write_text(json.dumps(feed)); self.assertFalse(m.business_health(path, now))
|
|
feed['latencyRegressed'] = True
|
|
path.write_text(json.dumps(feed)); self.assertTrue(m.business_health(path, now))
|
|
feed['latencyRegressed'] = 'false'
|
|
path.write_text(json.dumps(feed))
|
|
with self.assertRaises(ValueError): m.business_health(path, now)
|
|
feed['latencyRegressed'] = False; feed['at'] = (now - dt.timedelta(seconds=46)).isoformat()
|
|
path.write_text(json.dumps(feed))
|
|
with self.assertRaises(ValueError): m.business_health(path, now)
|
|
feed['at'] = now.isoformat(); feed['coverage'] = 'readiness-only'
|
|
path.write_text(json.dumps(feed))
|
|
with self.assertRaises(ValueError): m.business_health(path, now)
|
|
def test_unknown_io_is_not_zero(self):
|
|
with self.assertRaises(ValueError): m.full_io_pressure('no evidence')
|
|
self.assertEqual(m.full_io_pressure('some avg10=2.0\nfull avg10=1.25 avg60=0'), 1.25)
|
|
|
|
if __name__ == '__main__': unittest.main()
|