| author | |
| committer | |
| log | 711e7f082e04608ad1e120898526ee9c9870483f |
| tree | e48bc6ae7472d4371e479e8642394b1ba24575cb |
| parent | 8f88e549f87fd6e7308b6d04d29c78942b355d41 |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
Assisted-by: gpt-6.1-sol3 files changed, 71 insertions(+), 13 deletions(-)
tools/computer-test.py+32-3| ... | ... | @@ -146,7 +146,7 @@ class GuestCancellation(unittest.TestCase): |
| 146 | 146 | desktop.assert_awaited_once_with('fixture', actions, 'windows', False, False) |
| 147 | 147 | work.assert_not_called() |
| 148 | 148 | |
| 149 | def test_failed_cleanup_blocks_later_control_until_reconnection(self): | |
| 149 | def test_failed_cleanup_keeps_status_available_and_blocks_later_control(self): | |
| 150 | 150 | for platform, identity in [('linux', 'snowglobe-computer-request-' + 'a' * 32), ('windows', 'a' * 32)]: |
| 151 | 151 | operation = {'platform': platform, 'id': identity} |
| 152 | 152 | computer.record_operation('fixture', operation) |
| ... | ... | @@ -157,14 +157,43 @@ class GuestCancellation(unittest.TestCase): |
| 157 | 157 | def virsh(action, *args, **kwargs): |
| 158 | 158 | return 'running' if action == 'domstate' else '<domain/>' |
| 159 | 159 | with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work: |
| 160 | with patch.object(computer, 'finish_operation', side_effect=AssertionError('Status must not wait for cleanup')): | |
| 161 | status = computer.handle('fixture', {'action': 'status'}) | |
| 162 | self.assertEqual(status['readiness'], 'recovery_required') | |
| 163 | self.assertEqual(status['active_operation'], operation) | |
| 160 | 164 | with patch.object(computer, 'guest_exec', side_effect=TimeoutError()), patch.object(computer, 'powershell', side_effect=TimeoutError()), self.assertRaises(BlockingIOError): |
| 161 | computer.handle('fixture', {'action': 'status'}) | |
| 165 | computer.handle('fixture', {'action': 'exec', 'runtime': 'default', 'script': 'true', 'timeout_ms': 1000}) | |
| 162 | 166 | work.assert_not_called() |
| 163 | 167 | with patch.object(computer, 'guest_exec', return_value=b'{}'), patch.object(computer, 'powershell', return_value=b'{}'): |
| 164 | computer.handle('fixture', {'action': 'status'}) | |
| 168 | computer.handle('fixture', {'action': 'exec', 'runtime': 'default', 'script': 'true', 'timeout_ms': 1000}) | |
| 165 | 169 | work.assert_called_once() |
| 166 | 170 | self.assertFalse(pending.exists()) |
| 167 | 171 | |
| 172 | def test_busy_execution_keeps_status_and_screenshots_available(self): | |
| 173 | def virsh(action, *args, **kwargs): | |
| 174 | return 'running' if action == 'domstate' else '<domain/>' | |
| 175 | with computer.vms.computer_lock('fixture', 'exec'), patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work: | |
| 176 | status = computer.handle('fixture', {'action': 'status'}) | |
| 177 | self.assertEqual(status['readiness'], 'busy') | |
| 178 | self.assertEqual(status['active_operation']['action'], 'exec') | |
| 179 | self.assertGreaterEqual(status['active_operation']['age_seconds'], 0) | |
| 180 | with patch.object(computer, 'desktop', new_callable=AsyncMock, return_value={'png': 'fixture'}) as desktop: | |
| 181 | self.assertEqual(computer.handle('fixture', {'action': 'shot'}), {'png': 'fixture'}) | |
| 182 | desktop.assert_awaited_once_with('fixture') | |
| 183 | with self.assertRaisesRegex(BlockingIOError, 'computer_status'): | |
| 184 | computer.handle('fixture', {'action': 'exec', 'script': 'true', 'runtime': 'default', 'timeout_ms': 1000}) | |
| 185 | work.assert_not_called() | |
| 186 | ||
| 187 | def test_stopped_vm_reports_status_without_starting_a_guest_worker(self): | |
| 188 | def virsh(action, *args, **kwargs): | |
| 189 | return 'shut off' if action == 'domstate' else '<domain/>' | |
| 190 | with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work: | |
| 191 | status = computer.handle('fixture', {'action': 'status'}) | |
| 192 | self.assertEqual(status['power_state'], 'shut off') | |
| 193 | self.assertEqual(status['readiness'], 'stopped') | |
| 194 | self.assertEqual(status['capabilities'], []) | |
| 195 | work.assert_not_called() | |
| 196 | ||
| 168 | 197 | def test_spawn_returns_its_handle_when_cleanup_must_wait_for_reconnection(self): |
| 169 | 198 | result = {'id': '123:456', 'running': True} |
| 170 | 199 | def execute(name, script, **kwargs): |
tools/vm-computer.py+34-9| ... | ... | @@ -499,21 +499,46 @@ def handle(name, request): |
| 499 | 499 | return vms.mac_vms.handle('computer', {'name':name,'request':request}) |
| 500 | 500 | token = DEADLINE.set(time.monotonic() + 210) |
| 501 | 501 | try: |
| 502 | with vms.computer_lock(name): | |
| 502 | with contextlib.ExitStack() as scope: | |
| 503 | busy = False | |
| 504 | try: | |
| 505 | scope.enter_context(vms.computer_lock(name, request['action'])) | |
| 506 | except BlockingIOError: | |
| 507 | if request['action'] not in ('status', 'shot'): | |
| 508 | raise BlockingIOError('Another computer operation is running. Read computer_status before retrying.') from None | |
| 509 | busy = True | |
| 503 | 510 | root = ET.fromstring(vms.virsh('dumpxml',name,timeout=remaining(15))) |
| 504 | if vms.virsh('domstate',name,timeout=remaining(15)).strip() != 'running': | |
| 505 | raise ValueError('Start the VM before using its computer tools.') | |
| 511 | power = vms.virsh('domstate',name,timeout=remaining(15)).strip() | |
| 506 | 512 | platform = root.findtext(f'metadata/{vms.NS}vm/{vms.NS}platform','linux') |
| 513 | status = {'platform': platform, 'power_state': power, 'readiness': 'starting', | |
| 514 | 'capabilities': ['shot', 'input'] if power == 'running' else [], | |
| 515 | 'runtimes': [], 'coordinate_units': 'screenshot_pixels'} | |
| 516 | if power != 'running': | |
| 517 | if request['action'] == 'status': | |
| 518 | return {**status, 'readiness': 'stopped' if power == 'shut off' else power} | |
| 519 | raise ValueError('Start the VM before using its computer tools.') | |
| 507 | 520 | ai = import_module('vm-ai') |
| 508 | ai.lease(name, ai.LIMITS['idle_seconds']) | |
| 521 | if not busy: | |
| 522 | ai.lease(name, ai.LIMITS['idle_seconds']) | |
| 509 | 523 | if request['action'] == 'shot': |
| 510 | 524 | return asyncio.run(desktop(name)) |
| 525 | if busy: | |
| 526 | with os.fdopen(vms.open_regular(vms.DISKS / name / 'computer.lock')) as incoming: | |
| 527 | try: | |
| 528 | operation = json.load(incoming) | |
| 529 | except ValueError: | |
| 530 | operation = {'action': 'computer'} | |
| 531 | operation['age_seconds'] = round(max(0, time.time() - os.fstat(incoming.fileno()).st_mtime), 1) | |
| 532 | return {**status, 'readiness': 'busy', 'capabilities': ['shot'], 'active_operation': operation} | |
| 511 | 533 | try: |
| 512 | 534 | with os.fdopen(vms.open_regular(vms.DISKS / name / 'computer-operation.json')) as incoming: |
| 513 | 535 | unfinished = json.load(incoming) |
| 514 | 536 | except FileNotFoundError: |
| 515 | 537 | pass |
| 516 | 538 | else: |
| 539 | if request['action'] == 'status': | |
| 540 | return {**status, 'readiness': 'recovery_required', 'capabilities': ['shot'], 'active_operation': unfinished, | |
| 541 | 'worker_error': 'The previous operation needs cleanup. Inspect its screenshot or restart the VM before retrying.'} | |
| 517 | 542 | finish_operation(name, unfinished) |
| 518 | 543 | native_text = True |
| 519 | 544 | if platform == 'windows' and (request['action'] == 'status' or request['action'] == 'input' and any(a['type'] == 'text' for a in request['actions'])): |
| ... | ... | @@ -523,14 +548,14 @@ def handle(name, request): |
| 523 | 548 | except (ValueError, subprocess.TimeoutExpired): |
| 524 | 549 | native_text = False |
| 525 | 550 | if request['action'] == 'status': |
| 526 | result = {'platform':platform,'capabilities':['shot','input'],'coordinate_units':'screenshot_pixels'} | |
| 527 | 551 | if not native_text: |
| 528 | return {**result, 'runtimes': [], 'text_encoding': 'ascii', 'worker_error': 'Desktop input is available. Commands, Unicode text and UI actions need a working guest agent with command support.'} | |
| 552 | return {**status, 'text_encoding': 'ascii', 'worker_error': 'Desktop input is available. Commands, Unicode text and UI actions need a working guest agent with command support.'} | |
| 529 | 553 | try: |
| 530 | result.update(worker(name,platform,request)) | |
| 554 | status.update(worker(name,platform,request)) | |
| 555 | status['readiness'] = 'ready' | |
| 531 | 556 | except ValueError as error: |
| 532 | result['worker_error'] = str(error) | |
| 533 | return result | |
| 557 | status['worker_error'] = str(error) | |
| 558 | return status | |
| 534 | 559 | if request['action'] == 'input': |
| 535 | 560 | return asyncio.run(desktop(name,request['actions'],platform,request.get('screenshot',True),native_text)) |
| 536 | 561 | if platform=='windows' and request['action']=='file' and request['operation'] in ('read','write'): |
tools/vms.py+5-1| ... | ... | @@ -208,7 +208,7 @@ def validate(action, payload): |
| 208 | 208 | |
| 209 | 209 | |
| 210 | 210 | @contextlib.contextmanager |
| 211 | def computer_lock(name): | |
| 211 | def computer_lock(name, action="computer"): | |
| 212 | 212 | ensure_name(name) |
| 213 | 213 | directory = DISKS / name |
| 214 | 214 | if directory.is_symlink(): |
| ... | ... | @@ -216,6 +216,10 @@ def computer_lock(name): |
| 216 | 216 | descriptor = os.open(directory / "computer.lock", os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600) |
| 217 | 217 | with os.fdopen(descriptor, "w") as lock: |
| 218 | 218 | fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 219 | lock.seek(0) | |
| 220 | lock.truncate() | |
| 221 | json.dump({"action": action}, lock) | |
| 222 | lock.flush() | |
| 219 | 223 | yield |
| 220 | 224 | |
| 221 | 225 |