113 lines
6.5 KiB
Python
113 lines
6.5 KiB
Python
"""Synthetic input/dispatch checks; no scientific data or fits."""
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
from zipfile import ZipFile
|
|
import make_gre24_v4_contract as builder
|
|
import continue_gre24_v4_after_receive as chain
|
|
|
|
LOG=b'Process Settings:\n Filling Control:\n Fill time = 0.40 s\nFilling Phase: Status\nUNREAD OUTCOME\n'
|
|
|
|
|
|
class Checks(unittest.TestCase):
|
|
def test_setting_stops_before_outcomes(self):
|
|
stream=io.BytesIO(LOG)
|
|
setting=builder.read_setting(stream)
|
|
self.assertEqual(setting['setting']['seconds'],.4)
|
|
self.assertEqual(stream.read(),b'UNREAD OUTCOME\n')
|
|
|
|
def test_invalid_settings_stop(self):
|
|
for log in [LOG.replace(b'0.40',b'0'),LOG.replace(b'0.40',b'-1'),
|
|
LOG.replace(b'Filling Phase:',b'No phase:'),
|
|
LOG.replace(b'Filling Phase:',b'Fill time = 1 s\nFilling Phase:')]:
|
|
with self.assertRaises(ValueError):builder.read_setting(io.BytesIO(log))
|
|
|
|
def test_split_counts_disjoint_and_deterministic(self):
|
|
cases=[f'{i:08d}' for i in range(629)]
|
|
seeds=list(range(2026111601,2026111606))
|
|
first=builder.make_splits(cases,seeds)
|
|
self.assertEqual(first,builder.make_splits(cases,seeds))
|
|
self.assertEqual(len(first),5)
|
|
for s in first:
|
|
self.assertEqual([len(s[k]) for k in ['test','train','fnn_validation','fnn_learning']],[315,314,32,282])
|
|
self.assertFalse(set(s['test'])&set(s['train']))
|
|
self.assertEqual(set(s['fnn_learning'])|set(s['fnn_validation']),set(s['train']))
|
|
|
|
def test_wrong_population_rejected(self):
|
|
with self.assertRaises(ValueError):builder.make_splits(['x']*629,[1])
|
|
with self.assertRaises(ValueError):builder.make_splits([str(i) for i in range(628)],[1])
|
|
|
|
def test_synthetic_contract_preserves_fixed_science_and_model(self):
|
|
with tempfile.TemporaryDirectory(prefix='gre24-v4-contract-test-') as td:
|
|
root=Path(td);(root/'data').mkdir();archive=root/'data/data.zip'
|
|
with ZipFile(archive,'w') as z:
|
|
for i in range(629):
|
|
case=f'{i:08d}'
|
|
z.writestr(case+'.yml','synthetic: true\n')
|
|
z.writestr(case+'.log',LOG)
|
|
z.writestr(case+'_sim.vtu','<VTKFile><UnstructuredGrid><Piece NumberOfPoints="4"/></UnstructuredGrid></VTKFile>')
|
|
st=archive.stat()
|
|
prep_path=builder.BASE/'v5-protocol/contract.json'
|
|
plan_path=builder.BASE/'v5-models-continuation-2026-11-16/model-plan.json'
|
|
pre=dict(source_pins={},original_prep_contract_sha256=builder.sha(prep_path),
|
|
original_model_plan_sha256=builder.sha(plan_path),expected_md5='synthetic',expected_bytes=st.st_size,
|
|
split_seeds=list(range(2026111601,2026111606)),authorizing_rulings=['synthetic'])
|
|
builder.write(root/'preflight-and-seed-freeze.json',pre)
|
|
builder.write(root/'data/archive-verification.json',dict(status='VERIFIED',archive=str(archive),
|
|
md5='synthetic',bytes=st.st_size,mtime_ns=st.st_mtime_ns,inode=st.st_ino))
|
|
with patch.object(builder,'PREFLIGHT_SHA',builder.sha(root/'preflight-and-seed-freeze.json')):
|
|
dest=builder.build(root)
|
|
c=json.loads((dest/'contract.json').read_text());old=json.loads(prep_path.read_text())
|
|
self.assertEqual(len(c['parts']),629)
|
|
self.assertEqual(c['code_sha256'],old['code_sha256'])
|
|
self.assertFalse(any('reuse_feature_root' in p for p in c['parts']))
|
|
self.assertEqual(c['limits']['total_worker_cpu_seconds'],144000)
|
|
self.assertEqual(c['limits']['per_part_wall_seconds'],1800)
|
|
newplan=json.loads((dest/'model-plan.json').read_text());oldplan=json.loads(plan_path.read_text())
|
|
changes={k for k in newplan if newplan[k]!=oldplan[k]}
|
|
self.assertLessEqual(changes,{'version','dataset_version','protocol','authorizing_rulings','initial_gpu_seconds','limits_notes','timestamp'})
|
|
self.assertEqual(newplan['total_worker_cpu_seconds'],216000)
|
|
self.assertEqual(newplan['total_gpu_seconds'],7200)
|
|
self.assertEqual(newplan['initial_gpu_seconds'],0.)
|
|
|
|
def prepare_root(self,root,status):
|
|
(root/'data').mkdir()
|
|
builder.write(root/'continuation-source-freeze.json',dict(code_sha256={}))
|
|
builder.write(root/'data/receive-started.json',dict(pid=1,unix=0))
|
|
builder.write(root/'data/receive-complete.json',dict(status=status))
|
|
|
|
def test_unverified_receiver_stops_without_launch(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
root=Path(td);self.prepare_root(root,'STOPPED')
|
|
with patch.object(chain,'build') as build,patch.object(chain,'launch') as launch:
|
|
self.assertEqual(chain.run(root),1)
|
|
build.assert_not_called();launch.assert_not_called()
|
|
|
|
def test_changed_source_stops(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
root=Path(td);self.prepare_root(root,'VERIFIED')
|
|
source=root/'source.py';source.write_text('new')
|
|
(root/'continuation-source-freeze.json').write_text(json.dumps(dict(code_sha256={str(source):'wrong'})))
|
|
with self.assertRaises(ValueError):chain.run(root)
|
|
|
|
def test_verified_launches_one_prep_and_one_waiter(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
root=Path(td);self.prepare_root(root,'VERIFIED');dest=root/'protocol';dest.mkdir()
|
|
builder.write(dest/'input-freeze.json',dict(synthetic=True))
|
|
builder.write(root/'data/archive-verification.json',dict(synthetic=True))
|
|
calls=[]
|
|
def launch(unit,protocol,script,extra=(),preparation=False):
|
|
calls.append((unit,script,preparation))
|
|
builder.write(dest/('queue-started.json' if preparation else 'model-chain-started.json'),dict(pid=123,synthetic=True))
|
|
with patch.object(chain,'build',return_value=dest),patch.object(chain,'launch',side_effect=launch),patch.object(chain,'service_state',return_value=dict(MainPID='123')):
|
|
self.assertEqual(chain.run(root),0)
|
|
self.assertEqual(calls,[(chain.PREP_UNIT,'tools/run_gre24_protocol_preparation.py',True),
|
|
(chain.MODEL_UNIT,'tools/run_gre24_models.py',False)])
|
|
with self.assertRaises(FileExistsError):chain.run(root)
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|