CmvrControlStation/tests/test_handeye_runtime.py

74 lines
3.5 KiB
Python
Raw Normal View History

"""Relocate the actual bundle; test with no Python/Conda entries on PATH."""
import hashlib
import json
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
from test_handeye_joint_worker import request_for, rendered_board
ROOT = Path(__file__).resolve().parents[1]
class BundledRuntimeTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temporary = tempfile.TemporaryDirectory(prefix='CMVR 标定 portable ')
cls.app = Path(cls.temporary.name)/'上位机 runtime'
cls.runtime = cls.app/'runtime/handeye'
shutil.copytree(ROOT/'third_party/handeye-runtime', cls.runtime)
(cls.app/'scripts').mkdir()
for name in ('handeye_worker.py', 'handeye_math.py', 'handeye_refinement.py'):
shutil.copy2(ROOT/'scripts'/name, cls.app/'scripts'/name)
cls.env = dict(os.environ, PATH=os.path.join(os.environ['SystemRoot'], 'System32'),
PYTHONHOME='Z:/unavailable-python', PYTHONPATH='Z:/unavailable-packages',
PYTHONUSERBASE='Z:/unavailable-user-packages')
@classmethod
def tearDownClass(cls):
cls.temporary.cleanup()
def run_python(self, *arguments, input=None):
return subprocess.run([str(self.runtime/'python.exe'), '-I', '-B', *arguments],
cwd=os.environ['SystemRoot'], env=self.env, input=input,
capture_output=True, text=True, encoding='utf-8', timeout=60,
check=True)
def test_all_packages_and_search_paths_are_inside_relocated_bundle(self):
code = ('import sys,json,numpy,cv2; '
'print(json.dumps(dict(prefix=sys.prefix,paths=sys.path,'
'numpy=numpy.__file__,opencv=cv2.__file__,isolated=sys.flags.isolated)))')
data = json.loads(self.run_python('-c', code).stdout)
self.assertEqual(data['isolated'], 1)
for path in [data['prefix'], data['numpy'], data['opencv'], *data['paths']]:
self.assertTrue(Path(path).resolve().is_relative_to(self.runtime.resolve()), path)
def test_manifest_and_redistribution_licenses(self):
manifest = json.loads((self.runtime/'manifest.json').read_text())
for name, digest in manifest['files'].items():
self.assertEqual(hashlib.sha256((self.runtime/name).read_bytes()).hexdigest(), digest, name)
self.assertTrue((self.runtime/'LICENSE.txt').exists())
self.assertTrue(list(self.runtime.glob('Lib/site-packages/numpy*.dist-info/**/LICENSE*')))
self.assertTrue(list(self.runtime.glob('Lib/site-packages/opencv*.dist-info/LICENSE*')))
def test_real_worker_detects_and_solves_both_modes_after_relocation(self):
requests = [dict(op='check', id='portable-check'), rendered_board('chessboard')]
requests += [request_for(mode, noisy=True)[2] for mode in ('eye_in_hand', 'eye_to_hand')]
output = self.run_python('-u', str(self.app/'scripts/handeye_worker.py'),
input=''.join(json.dumps(r)+'\n' for r in requests))
replies = [json.loads(line) for line in output.stdout.splitlines()]
self.assertEqual(len(replies), len(requests))
for reply in replies:
self.assertTrue(reply['ok'], reply)
self.assertTrue(reply['valid'], reply)
for reply in replies[2:]:
self.assertTrue(reply['refinement']['accepted'])
self.assertEqual(reply['refinement']['metrics']['validation_sample_count'], 12)
if __name__ == '__main__':
unittest.main()