Files
hand-motion-pipeline/tests/test_external_resources.py

66 lines
3.0 KiB
Python

"""External resources fail clearly; release guards catch model shards."""
import hashlib
import json
from pathlib import Path
import sys
import tempfile
import unittest
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
from external_resources import check_resources, load_resources, select_resources
from check_release_policy import forbidden_paths
from check_pipeline_resources import check_assets
class ExternalResourcesTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
(self.root / 'configs').mkdir()
self.groups = [dict(id='test', name='test', scope='rgbd', source_url='https://example.org/provider',
files=[dict(path='weights/test.pt', bytes=4, sha256=hashlib.sha256(b'good').hexdigest())]),
dict(id='optional', name='optional', scope='optional', source_url='https://example.org/optional',
files=[dict(path='weights/optional.pt')])]
self.write_manifest()
def write_manifest(self):
(self.root / 'configs/external_resources.json').write_text(json.dumps({'resources': self.groups}))
def test_missing_resource_has_upstream_url_and_affects_pipeline_check(self):
row = check_resources(self.root)[0]
self.assertEqual(row['status'], 'missing')
self.assertEqual(row['source_url'], 'https://example.org/provider')
self.assertTrue(any('weights/test.pt' in x and row['source_url'] in x for x in check_assets(self.root)))
def test_pointer_and_same_size_corruption_are_rejected(self):
path = self.root / 'weights/test.pt'
path.parent.mkdir()
path.write_bytes(b'version https://git-lfs.github.com/spec/v1\n')
self.assertEqual(check_resources(self.root)[0]['status'], 'lfs_pointer')
path.write_bytes(b'evil')
self.assertEqual(check_resources(self.root, verify_sha256=True)[0]['status'], 'sha256_mismatch')
path.write_bytes(b'good')
self.assertEqual(check_resources(self.root, verify_sha256=True)[0]['status'], 'present')
def test_optional_groups_are_explicit(self):
self.assertEqual(len(check_resources(self.root)), 1)
self.assertEqual(len(check_resources(self.root, scope='all')), 2)
self.assertEqual(select_resources(self.root, group_ids=['optional'])[0]['id'], 'optional')
with self.assertRaises(ValueError):
select_resources(self.root, group_ids=['unknown'])
def test_manifest_cannot_escape_root(self):
self.groups[0]['files'][0]['path'] = '../outside.pt'
self.write_manifest()
with self.assertRaises(ValueError):
load_resources(self.root)
def test_index_guard_detects_full_models_shards_and_mano(self):
tracked = ['weights/test.pt', 'weights/test.pt.part-000', 'other/MANO_LEFT.pkl', 'docs/sample.mp4']
self.assertEqual(forbidden_paths(tracked, self.groups), tracked[:3])
if __name__ == '__main__':
unittest.main()