1#!/usr/bin/env python3
2"""Computer protocol and guest file boundary tests using disposable files."""
3from importlib import import_module
4import base64
5import asyncio
6import time
7import json
8import tempfile
9import unittest
10from pathlib import Path
11from unittest.mock import patch, AsyncMock, Mock
12
13computer=import_module('vm-computer')
14worker=import_module('mac-computer')
15leases=import_module('vm-ai')
16
17
18class Protocol(unittest.TestCase):
19 def test_rejects_malformed_actions_before_input(self):
20 for action in [None,[],{'type':[]},{'type':'click','x':True,'y':0},
21 {'type':'drag','x':0,'y':0,'to_x':0,'to_y':-1},
22 {'type':'key','keys':['Invalid']},{'type':'key','keys':'Enter'},
23 {'type':'scroll','x':0,'y':0,'direction':'forward'},
24 {'type':'wait','duration_ms':5001},{'type':'text','text':'x'*16385},
25 {'type':'click','x':0,'y':0,'command':'id'}]:
26 with self.subTest(action=action),self.assertRaises(ValueError):
27 computer.validate({'action':'input','actions':[action],'screenshot':False})
28
29 def test_rejects_unbounded_files_and_ui_targets(self):
30 for request in [{'action':'file','operation':'read','path':'/tmp/x','length':65537},
31 {'action':'file','operation':'write','path':'/tmp/x','data':'a'*90001},
32 {'action':'file','operation':'read','path':'/tmp/x','offset':True},
33 {'action':'ui','pid':True},
34 {'action':'ui_action','target':{'name':'x'*5000},'operation':'press'}]:
35 with self.subTest(request=request),self.assertRaises(ValueError):computer.validate(request)
36
37 def test_file_upload_is_exclusive_and_commit_does_not_replace_by_default(self):
38 with tempfile.TemporaryDirectory() as directory:
39 temporary=str(Path(directory)/'upload');destination=str(Path(directory)/'Unicode 🦊')
40 data=b'\0binary\xff\n'*8192
41 def call(operation,**values):return worker.execute({'action':'file','operation':operation,'path':temporary,**values})
42 call('write',data=base64.b64encode(data[:65536]).decode(),offset=0)
43 with self.assertRaises(FileExistsError):call('write',data='',offset=0)
44 with self.assertRaises(ValueError):call('write',data='',offset=42)
45 call('write',data=base64.b64encode(data[65536:]).decode(),offset=65536)
46 self.assertEqual(Path(temporary).read_bytes(),data)
47 Path(destination).write_bytes(b'existing')
48 with self.assertRaises(FileExistsError):call('commit',destination=destination,overwrite=False)
49 self.assertEqual(Path(destination).read_bytes(),b'existing')
50 call('commit',destination=destination,overwrite=True)
51 self.assertEqual(Path(destination).read_bytes(),data)
52 self.assertFalse(Path(temporary).exists())
53
54 def test_upload_refuses_symlink_and_directory(self):
55 with tempfile.TemporaryDirectory() as directory:
56 target=Path(directory)/'target';target.write_bytes(b'original')
57 link=Path(directory)/'link';link.symlink_to(target)
58 for path in [link,Path(directory)]:
59 with self.assertRaises(OSError):worker.execute({'action':'file','operation':'write','path':str(path),'data':'','offset':0})
60 self.assertEqual(target.read_bytes(),b'original')
61
62 def test_stale_process_handle_cannot_stop_reused_pid(self):
63 with patch.object(worker,'MAC',True),patch.object(worker,'process_identity',return_value='new-start'),patch.object(worker.os,'killpg') as kill:
64 self.assertEqual(worker.execute({'action':'process','id':'100:old-start','operation':'stop'}),{'running':False})
65 kill.assert_not_called()
66
67
68class GuestCancellation(unittest.TestCase):
69 def setUp(self):
70 directory = tempfile.TemporaryDirectory()
71 self.addCleanup(directory.cleanup)
72 root = Path(directory.name)
73 (root / 'fixture').mkdir()
74 patcher = patch.object(computer.vms, 'DISKS', root)
75 patcher.start()
76 self.addCleanup(patcher.stop)
77
78 def test_windows_powershell_reads_redirected_output_without_console_capture(self):
79 with patch.object(computer, 'write_file'), patch.object(computer, 'guest_exec', return_value=b''), patch.object(computer, 'read_file', return_value=b'file output'):
80 self.assertEqual(computer.powershell('fixture', "Write-Output 'file output'"), b'file output')
81
82 def test_windows_powershell_rejects_failed_execution_before_reading_output(self):
83 with patch.object(computer, 'write_file'), patch.object(computer, 'guest_exec', side_effect=ValueError('Failed')), patch.object(computer, 'read_file') as read:
84 with self.assertRaises(ValueError):
85 computer.powershell('fixture', 'exit 7')
86 read.assert_not_called()
87
88 def test_reused_guest_pid_waits_for_current_process(self):
89 replies = [{'pid': 2596}, {'exited': True, 'exitcode': 1}, {'exited': False},
90 {'exited': True, 'exitcode': 0, 'out-data': base64.b64encode(b'current').decode()},
91 ProcessLookupError()]
92 with patch.object(computer, 'qga', side_effect=replies), patch.object(computer.time, 'sleep'):
93 self.assertEqual(computer.guest_exec('fixture', 'cmd.exe', []), b'current')
94
95 def test_fast_reused_guest_pid_returns_last_completed_record(self):
96 replies = [{'pid': 2596}, {'exited': True, 'exitcode': 0, 'out-data': 'b2xk'},
97 {'exited': True, 'exitcode': 0, 'out-data': 'bmV3'}, ProcessLookupError()]
98 with patch.object(computer, 'qga', side_effect=replies):
99 self.assertEqual(computer.guest_exec('fixture', 'cmd.exe', []), b'new')
100
101 def test_guest_completion_requires_explicit_end_of_records(self):
102 for error in [ValueError('Disconnected'), TimeoutError()]:
103 replies = [{'pid': 2596}, {'exited': True, 'exitcode': 0}, error]
104 with self.subTest(error=type(error)), patch.object(computer, 'qga', side_effect=replies):
105 with self.assertRaises(type(error)):
106 computer.guest_exec('fixture', 'cmd.exe', [])
107
108 def test_guest_pid_absence_is_distinct_from_transport_failure(self):
109 for message in ['PID 2596 does not exist', 'PID ld does not exist', 'PID lld does not exist', "Invalid parameter 'pid'", 'Guest agent is not responding']:
110 response = Mock(stdout='', stderr='error: ' + message + '\nerror: Failed to create virsh cache: Read-only file system\n')
111 with self.subTest(message=message), patch.object(computer.subprocess, 'run', return_value=response):
112 with self.assertRaises(ValueError if message == 'Guest agent is not responding' else ProcessLookupError):
113 computer.qga('fixture', 'guest-exec-status', pid=2596)
114
115 def test_windows_stops_guest_task_after_its_operation_budget_expires(self):
116 running = []
117 def execute(name, script, **kwargs):
118 if 'RegisterTaskDefinition' in script:
119 running.append(True)
120 computer.DEADLINE.set(time.monotonic() - 1)
121 return b''
122 if '$t.Stop(0)' in script:
123 self.assertGreater(computer.remaining(20), 0)
124 running.clear()
125 return b''
126 return b'[true,true]'
127 token = computer.DEADLINE.set(time.monotonic() + 30)
128 try:
129 with patch.object(computer, 'powershell', execute), patch.object(computer, 'write_file'):
130 with self.assertRaises(TimeoutError):
131 computer.worker('fixture', 'windows', {'action': 'input', 'actions': [{'type': 'text', 'text': 'hello'}]})
132 self.assertEqual(running, [])
133 finally:
134 computer.DEADLINE.reset(token)
135
136 def test_windows_console_remains_available_without_guest_agent(self):
137 def virsh(action, *args, **kwargs):
138 return 'running' if action == 'domstate' else '<domain><metadata><vm xmlns="https://paperclover.net/snowglobe"><platform>windows</platform></vm></metadata></domain>'
139 with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'qga', side_effect=ValueError('Disconnected')), patch.object(computer, 'worker') as work:
140 status = computer.handle('fixture', {'action': 'status'})
141 self.assertEqual(status['capabilities'], ['shot', 'input'])
142 self.assertEqual(status['text_encoding'], 'ascii')
143 actions = [{'type': 'text', 'text': 'recovery console'}]
144 with patch.object(computer, 'desktop', new_callable=AsyncMock, return_value={'completed': 1}) as desktop:
145 self.assertEqual(computer.handle('fixture', {'action': 'input', 'actions': actions, 'screenshot': False}), {'completed': 1})
146 desktop.assert_awaited_once_with('fixture', actions, 'windows', False, False)
147 work.assert_not_called()
148
149 def test_failed_cleanup_blocks_later_control_until_reconnection(self):
150 for platform, identity in [('linux', 'snowglobe-computer-request-' + 'a' * 32), ('windows', 'a' * 32)]:
151 operation = {'platform': platform, 'id': identity}
152 computer.record_operation('fixture', operation)
153 with patch.object(computer, 'guest_exec', side_effect=TimeoutError()), patch.object(computer, 'powershell', side_effect=TimeoutError()), self.assertRaises(BlockingIOError):
154 computer.finish_operation('fixture', operation)
155 pending = computer.vms.DISKS / 'fixture' / 'computer-operation.json'
156 self.assertEqual(json.loads(pending.read_text()), operation)
157 def virsh(action, *args, **kwargs):
158 return 'running' if action == 'domstate' else '<domain/>'
159 with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work:
160 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'})
162 work.assert_not_called()
163 with patch.object(computer, 'guest_exec', return_value=b'{}'), patch.object(computer, 'powershell', return_value=b'{}'):
164 computer.handle('fixture', {'action': 'status'})
165 work.assert_called_once()
166 self.assertFalse(pending.exists())
167
168 def test_spawn_returns_its_handle_when_cleanup_must_wait_for_reconnection(self):
169 result = {'id': '123:456', 'running': True}
170 def execute(name, script, **kwargs):
171 if 'DeleteTask' in script:
172 raise TimeoutError()
173 return b'[true,true]'
174 with patch.object(computer, 'powershell', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=json.dumps(result).encode()):
175 self.assertEqual(computer.worker('fixture', 'windows', {'action': 'spawn'}), result)
176 pending = computer.vms.DISKS / 'fixture' / 'computer-operation.json'
177 operation = json.loads(pending.read_text())
178 self.assertTrue(operation['keep_spawn'])
179 with patch.object(computer, 'powershell', return_value=b'') as execute:
180 computer.finish_operation('fixture', operation)
181 self.assertNotIn('$t.Stop(0)', execute.call_args.args[1])
182 self.assertFalse(pending.exists())
183
184 def test_spawn_is_stopped_if_its_success_cannot_be_persisted(self):
185 record = computer.record_operation
186 def persist(name, operation):
187 if operation.get('keep_spawn'):
188 raise OSError('Disk full')
189 record(name, operation)
190 with patch.object(computer, 'record_operation', persist), patch.object(computer, 'powershell', return_value=b'[true,true]') as execute, patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=b'{"id":"123:456"}'):
191 with self.assertRaises(OSError):
192 computer.worker('fixture', 'windows', {'action': 'spawn'})
193 self.assertIn('$t.Stop(0)', execute.call_args.args[1])
194 self.assertFalse((computer.vms.DISKS / 'fixture' / 'computer-operation.json').exists())
195
196 def test_linux_stops_the_exact_request_unit_with_a_fresh_cleanup_budget(self):
197 units = []
198 def execute(name, executable, arguments, data=None, **kwargs):
199 request = json.loads(data)
200 if '_prepare' in request:
201 return b'{"prepared":true}'
202 if '_unit' in request:
203 units.append(request['_unit'])
204 computer.DEADLINE.set(time.monotonic() - 1)
205 raise TimeoutError()
206 self.assertEqual(request, {'_stop': units.pop()})
207 self.assertGreater(computer.remaining(20), 0)
208 return b'{"stopped":true}'
209 token = computer.DEADLINE.set(time.monotonic() + 30)
210 try:
211 with patch.object(computer, 'guest_exec', execute), self.assertRaises(TimeoutError):
212 computer.worker('fixture', 'linux', {'action': 'status'})
213 self.assertEqual(units, [])
214 finally:
215 computer.DEADLINE.reset(token)
216
217
218class DesktopBounds(unittest.IsolatedAsyncioTestCase):
219 async def asyncSetUp(self):
220 self.writer = Mock(drain=AsyncMock(), wait_closed=AsyncMock())
221 self.screen = Mock(width=3840, height=2160, start=AsyncMock())
222 async def read(ready, clear):
223 ready()
224 await asyncio.Future()
225 self.screen.read = read
226 self.patches = [patch.object(computer.vms, 'console_target', return_value={'port': 5900}),
227 patch.object(computer.asyncio, 'open_connection', AsyncMock(return_value=(None, self.writer))),
228 patch.object(computer.rfb, 'Desktop', return_value=self.screen)]
229 for item in self.patches:
230 item.start()
231 self.addCleanup(item.stop)
232
233 async def test_input_without_screenshot_never_encodes_or_requests_another_frame(self):
234 with patch.object(computer.rfb, 'png', side_effect=AssertionError('No capture requested')):
235 result = await computer.desktop('fixture', [{'type': 'click', 'x': 1, 'y': 1}], 'linux', False)
236 self.assertEqual(result, {'completed': 1})
237 self.screen.request.assert_not_called()
238
239 async def test_cancelled_batch_keeps_capture_slot_until_text_worker_returns(self):
240 finished = []
241 def type_text(*args):
242 self.assertLess(computer.remaining(999), 181)
243 time.sleep(.06)
244 finished.append(True)
245 wait_for = asyncio.wait_for
246 with patch.object(computer, 'worker', type_text), patch.object(computer.asyncio, 'wait_for', lambda task, seconds: wait_for(task, .02 if seconds > 170 else seconds)):
247 with self.assertRaises(asyncio.TimeoutError):
248 await computer.desktop('fixture', [{'type': 'text', 'text': 'one'}] * 100, 'linux', False)
249 self.assertEqual(finished, [True])
250 self.assertTrue(computer.SCREEN_SLOT.acquire(blocking=False))
251 computer.SCREEN_SLOT.release()
252
253 async def test_expired_batch_never_starts_another_guest_command(self):
254 token = computer.DEADLINE.set(time.monotonic() - 1)
255 try:
256 with patch.object(computer.subprocess, 'run') as run, self.assertRaises(TimeoutError):
257 computer.qga('fixture', 'guest-ping')
258 run.assert_not_called()
259 finally:
260 computer.DEADLINE.reset(token)
261
262
263class TestVmExpiry(unittest.TestCase):
264 def test_creation_catalog_excludes_unprepared_windows_and_oversized_disks(self):
265 prepared = {'id': 'ready', 'platform': 'windows', 'setup': 'windows-v4', 'capacity': 40 * 2**30, 'os': 'Windows', 'arch': 'x86_64'}
266 linux = {**prepared, 'id': 'linux', 'platform': 'linux'}
267 broken = {**prepared, 'id': 'unprepared', 'setup': None}
268 oversized = {**linux, 'id': 'oversized', 'capacity': 81 * 2**30}
269 with patch.object(leases.vms, 'library', return_value={'presets': [prepared, linux, broken, oversized]}), patch.object(leases.vms, 'create') as create:
270 catalog = leases.manage({'action': 'presets', 'owner': 'fixture'})
271 self.assertEqual([p['id'] for p in catalog['presets']], ['ready', 'linux'])
272 for preset in ['unprepared', 'oversized']:
273 with self.assertRaisesRegex(ValueError, 'prepared preset'):
274 leases.create({'action': 'create', 'owner': 'fixture', 'preset': preset, 'name': 'test', 'vcpus': 2, 'memory': 4})
275 create.assert_not_called()
276
277 def test_expiry_preserves_personal_vms_and_skips_active_calls(self):
278 with tempfile.TemporaryDirectory() as directory, patch.object(leases.vms, 'DISKS', Path(directory)), patch.object(leases.time, 'time', return_value=10000):
279 for name, expiry in [('personal', 1), ('ai-prefix-only', None), ('ai-expired', 9999), ('ai-live', 10001), ('ai-busy', 9999)]:
280 root=Path(directory)/name;root.mkdir()
281 if expiry is not None:(root/'ai.json').write_text(json.dumps({'expiresAt':expiry}))
282 with leases.vms.computer_lock('ai-busy'), patch.object(leases.vms, 'remove') as remove:
283 leases.reap()
284 remove.assert_called_once_with({'name':'ai-expired','disks':True},missing_ok=True)
285
286 def test_activity_renews_only_test_vms_and_cannot_resurrect_expired_vms(self):
287 with tempfile.TemporaryDirectory() as directory, patch.object(leases.vms, 'DISKS', Path(directory)), patch.object(leases.time, 'time', return_value=10000):
288 root=Path(directory)/'ai-fixture';root.mkdir()
289 path=root/'ai.json';path.write_text(json.dumps({'expiresAt':10001}))
290 self.assertEqual(leases.lease('ai-fixture'),{'expiresAt':10001})
291 self.assertEqual(leases.lease('ai-fixture',3600),{'expiresAt':13600})
292 self.assertEqual(leases.lease('ai-fixture',100),{'expiresAt':13600})
293 self.assertIsNone(leases.lease('personal',3600))
294 path.write_text(json.dumps({'expiresAt':9999}))
295 with self.assertRaisesRegex(ValueError,'expired'):leases.lease('ai-fixture',86400)
296
297 def test_foreign_owner_and_personal_vm_cannot_be_deleted_or_extended(self):
298 with tempfile.TemporaryDirectory() as directory, patch.object(leases.vms, 'DISKS', Path(directory)), patch.object(leases.vms, 'remove') as remove:
299 for name in ['ai-fixture','personal']:(Path(directory)/name).mkdir()
300 (Path(directory)/'ai-fixture/ai.json').write_text(json.dumps({'expiresAt':20000}))
301 with patch.object(leases.vms,'virsh',return_value='<domain><metadata xmlns:s="https://paperclover.net/snowglobe"><s:vm><s:owner>owner</s:owner></s:vm></metadata></domain>'):
302 for name, owner in [('ai-fixture','other'),('personal','owner')]:
303 for action in ['delete','extend']:
304 with self.assertRaises(ValueError):leases.manage({'action':action,'name':name,'owner':owner,'seconds':3600})
305 remove.assert_not_called()
306
307
308if __name__=='__main__':unittest.main()