data-gen-bpu/scripts/test_target_resources.py

106 lines
6.0 KiB
Python

"""Frozen target identity and read-only board resource admission."""
from contextlib import ExitStack, redirect_stdout
from io import StringIO
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
from harness import cli, db, paths, registry, scheduler
class TargetResources(unittest.TestCase):
def setUp(self):
self.stack = ExitStack()
self.addCleanup(self.stack.close)
self.config = {'march_by_board': {'s100': 'nash-e', 's100p': 'nash-m', 's600': 'nash-p'}, 'boards': {}}
self.stack.enter_context(patch.object(registry, 'boards', side_effect=lambda: self.config))
self.secrets = {'A_HOST': 'private-host-fixture', 'A_KEY': 'private-key-fixture'}
self.stack.enter_context(patch.object(paths, 'secret', side_effect=lambda name: self.secrets.get(name)))
self.stack.enter_context(patch.object(paths, 'config', return_value={
'paths': {'venv': {'bench': '/fixture/bench', 'ptq': '/fixture/ptq'}}}))
def build(self, march='nash-p', target=None):
spec = registry.JobSpec('job', 'cls', 'model', march, 'ptq', 'int8', 'A', 'oracle')
with patch.object(registry, 'model_row', return_value={'input_spec': '1x3x2x2'}), \
patch.object(registry, 'board_for_march', return_value=None), \
patch.object(registry, 'data_snapshot', return_value={}), \
patch.object(registry, 'gate_snapshot', return_value={}), \
patch.object(registry, 'registry_fingerprint', return_value='fixture'):
return registry.build_job_json(spec, 'run', 'fixture', target=target)
def test_current_targets_and_no_target_defaults_are_preserved(self):
for name, march in self.config['march_by_board'].items():
with self.subTest(name=name):
target = {'board_type': name, 'march': march}
self.assertEqual(registry.target_spec(name), target)
self.assertEqual(self.build(march)['target'], target)
self.assertEqual(self.build(march, target)['target'], target)
def test_complete_target_is_retained_and_march_mismatch_rejected(self):
target = {'board_type': 's600', 'march': 'nash-p', 'revision': 'frozen-fixture'}
job = self.build(target=target)
self.assertEqual(job['target'], target)
self.assertIsNot(job['target'], target)
for invalid in ({**target, 'march': 'nash-m'}, {**target, 'board_type': 's100p'}):
with self.subTest(invalid=invalid), self.assertRaisesRegex(ValueError, 'target.*match'):
self.build(target=invalid)
def test_ambiguous_march_is_rejected_before_plan_or_slot_acquisition(self):
self.config['march_by_board']['s600.variant'] = 'nash-p'
for operation in (lambda: registry.target_spec('s600'), lambda: self.build(),
lambda: registry.board_slots('nash-p')):
with self.subTest(operation=operation), self.assertRaisesRegex(ValueError, '同 march 多板型'):
operation()
with patch.object(scheduler, '_try_slots') as claim:
with self.assertRaisesRegex(ValueError, '同 march 多板型'):
with scheduler.acquire('board', march='nash-p'):
self.fail('ambiguous resource must not be allocated')
claim.assert_not_called()
def test_mappingless_legacy_resources_cannot_mix_explicit_types(self):
self.config = {'boards': {'a': {'slot': 0, 'march': 'nash-p', 'board_type': 'type-a'},
'b': {'slot': 1, 'march': 'nash-p', 'board_type': 'type-b'}}}
with self.assertRaisesRegex(ValueError, '同 march 多板型'):
registry.board_slots('nash-p')
def test_new_same_type_board_only_needs_registration_and_slot(self):
self.config['boards'] = {'a': {'slot': 0, 'board_type': 's600', 'host_env': 'A_HOST', 'ssh_key_env': 'A_KEY'},
'b': {'slot': 7, 'board_type': 's600', 'host_env': 'B_HOST', 'ssh_key_env': 'B_KEY'},
'disabled': {'slot': 9, 'board_type': 's600', 'enabled': False}}
self.assertEqual(list(registry.board_slots('nash-p')), [0, 7])
summary = registry.board_resource_summary('nash-p', 's600')
self.assertEqual((summary['registered'], summary['enabled_matching'], summary['credentials_ready']), (3, 2, 1))
second = summary['resources'][1]
self.assertEqual(second['missing'], ['host', 'ssh_key'])
self.assertTrue(second['credential_fields']['port']['has_default'])
self.assertFalse(summary['authentication_checked'])
self.assertFalse(summary['inference_checked'])
self.assertNotIn('private-host-fixture', json.dumps(summary))
self.assertNotIn('private-key-fixture', json.dumps(summary))
def test_capabilities_empty_target_is_readonly_and_not_live_acceptance(self):
with tempfile.TemporaryDirectory() as td, patch.object(paths, 'STATE', Path(td) / 'absent-state'), \
patch.object(db, 'connect', side_effect=AssertionError('preview must not open DB')):
output = StringIO()
with redirect_stdout(output):
self.assertEqual(cli.main(['capabilities', '--target', 's600']), 0)
result = json.loads(output.getvalue())
resources = result['selection']['board_resources']
self.assertEqual(resources['registered'], 0)
self.assertEqual(resources['enabled_matching'], 0)
self.assertEqual(resources['scope'], 'configuration_only')
self.assertFalse((Path(td) / 'absent-state').exists())
def test_registered_legacy_resource_missing_type_is_visible_as_unmatched(self):
self.config['boards'] = {'legacy': {'march': 'nash-p'}}
summary = registry.board_resource_summary('nash-p', 's600')
self.assertEqual(summary['registered'], 1)
self.assertEqual(summary['enabled_matching'], 0)
self.assertIn('matching_board_type', summary['resources'][0]['missing'])
if __name__ == '__main__':
unittest.main()