Files
MeltMind/tools/test_s2_e1_supervisor.py

58 lines
2.6 KiB
Python

"""Bounded synthetic safety checks; no project data, labels or training."""
import json
from pathlib import Path
import sys
import tempfile
import unittest
from s2_e1_supervisor import supervise
class SupervisorChecks(unittest.TestCase):
def setUp(self):
self.tmp=tempfile.TemporaryDirectory(prefix='meltmind-e1-supervisor-')
self.root=Path(self.tmp.name)
self.limits=dict(total_cpu_seconds=10.,job_cpu_seconds=5.,job_wall_seconds=3.,
tree_rss_bytes=160*1024**2,disk_bytes=16*1024**2,max_processes=8)
def tearDown(self):
self.tmp.cleanup()
def run_code(self,code,**changes):
return supervise([sys.executable,'-c',code],self.root,'test',dict(self.limits,**changes),poll=.01)
def test_success_and_duplicate_attempt_rejected(self):
r=self.run_code('import os; assert os.environ["CUDA_VISIBLE_DEVICES"] == ""; print("synthetic")')
self.assertEqual(r['status'],'PASS')
with self.assertRaises(FileExistsError): self.run_code('pass')
def test_waiting_child_is_killed_on_wall_budget(self):
r=self.run_code('import subprocess,sys; subprocess.run([sys.executable,"-c","import time; time.sleep(10)"])',job_wall_seconds=.5)
self.assertEqual(r['stop_reason'],'wall_limit')
self.assertGreaterEqual(r['peak_process_count'],2)
def test_child_rss_counts_toward_tree(self):
code='import subprocess,sys; subprocess.run([sys.executable,"-c","import time; x=bytearray(90*1024**2); time.sleep(2)"])'
r=self.run_code(code,tree_rss_bytes=60*1024**2)
self.assertEqual(r['status'],'STOPPED_OR_FAILED')
self.assertIn('rss',r['stop_reason'])
def test_cpu_limit_charges_failed_attempt(self):
r=self.run_code('while True: pass',job_cpu_seconds=.12)
self.assertEqual(r['stop_reason'],'job_cpu_limit')
ledger=json.loads((self.root/'ledger.json').read_text())
self.assertGreaterEqual(ledger['cpu_seconds_charged'],.12)
def test_total_budget_prevents_start(self):
(self.root/'ledger.json').write_text(json.dumps({'cpu_seconds_charged':10.,'attempts':[]}))
with self.assertRaisesRegex(RuntimeError,'Total CPU'): self.run_code('pass')
self.assertFalse((self.root/'test.started.json').exists())
def test_disk_growth_is_stopped(self):
path=str(self.root/'payload')
r=self.run_code('from pathlib import Path; import time; Path('+repr(path)+').write_bytes(b"x"*(3*1024**2)); time.sleep(1)',disk_bytes=2*1024**2)
self.assertEqual(r['stop_reason'],'disk_limit')
if __name__=='__main__': unittest.main(verbosity=2)