qvm_start_gui.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import functools
  21. import os
  22. import signal
  23. import tempfile
  24. import unittest.mock
  25. import asyncio
  26. import qubesadmin.tests
  27. import qubesadmin.tools.qvm_start_gui
  28. import qubesadmin.vm
  29. class TC_00_qvm_start_gui(qubesadmin.tests.QubesTestCase):
  30. def setUp(self):
  31. super(TC_00_qvm_start_gui, self).setUp()
  32. self.launcher = qubesadmin.tools.qvm_start_gui.GUILauncher(self.app)
  33. @unittest.mock.patch('subprocess.check_output')
  34. def test_000_kde_args(self, proc_mock):
  35. self.app.expected_calls[
  36. ('dom0', 'admin.vm.List', None, None)] = \
  37. b'0\x00test-vm class=AppVM state=Running\n'
  38. self.app.expected_calls[
  39. ('test-vm', 'admin.vm.property.Get', 'label', None)] = \
  40. b'0\x00default=False type=label red'
  41. proc_mock.side_effect = [
  42. b'KWIN_RUNNING = 0x1\n',
  43. b'access control enabled, only authorized clients can connect\n'
  44. b'SI:localuser:root\n'
  45. b'SI:localuser:' + os.environ['USER'].encode() + b'\n',
  46. ]
  47. args = self.launcher.kde_guid_args(self.app.domains['test-vm'])
  48. self.assertEqual(args, ['-T', '-p',
  49. '_KDE_NET_WM_COLOR_SCHEME=s:' +
  50. os.path.expanduser('~/.local/share/qubes-kde/red.colors')])
  51. self.assertAllCalled()
  52. @unittest.mock.patch('subprocess.check_output')
  53. def test_001_kde_args_none(self, proc_mock):
  54. self.app.expected_calls[
  55. ('dom0', 'admin.vm.List', None, None)] = \
  56. b'0\x00test-vm class=AppVM state=Running\n'
  57. proc_mock.side_effect = [b'']
  58. args = self.launcher.kde_guid_args(self.app.domains['test-vm'])
  59. self.assertEqual(args, [])
  60. self.assertAllCalled()
  61. def test_010_common_args(self):
  62. self.app.expected_calls[
  63. ('dom0', 'admin.vm.List', None, None)] = \
  64. b'0\x00test-vm class=AppVM state=Running\n'
  65. self.app.expected_calls[
  66. ('test-vm', 'admin.vm.property.Get', 'label', None)] = \
  67. b'0\x00default=False type=label red'
  68. self.app.expected_calls[
  69. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  70. b'0\x00default=False type=bool False'
  71. self.app.expected_calls[
  72. ('dom0', 'admin.label.Get', 'red', None)] = \
  73. b'0\x000xff0000'
  74. self.app.expected_calls[
  75. ('dom0', 'admin.label.Index', 'red', None)] = \
  76. b'0\x001'
  77. with unittest.mock.patch.object(self.launcher, 'kde_guid_args') as \
  78. kde_mock:
  79. kde_mock.return_value = []
  80. args = self.launcher.common_guid_args(self.app.domains['test-vm'])
  81. self.assertEqual(args, [
  82. '/usr/bin/qubes-guid', '-N', 'test-vm',
  83. '-c', '0xff0000',
  84. '-i', '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  85. '-l', '1', '-q'])
  86. self.assertAllCalled()
  87. def test_011_common_args_debug(self):
  88. self.app.expected_calls[
  89. ('dom0', 'admin.vm.List', None, None)] = \
  90. b'0\x00test-vm class=AppVM state=Running\n'
  91. self.app.expected_calls[
  92. ('test-vm', 'admin.vm.property.Get', 'label', None)] = \
  93. b'0\x00default=False type=label red'
  94. self.app.expected_calls[
  95. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  96. b'0\x00default=False type=bool True'
  97. self.app.expected_calls[
  98. ('dom0', 'admin.label.Get', 'red', None)] = \
  99. b'0\x000xff0000'
  100. self.app.expected_calls[
  101. ('dom0', 'admin.label.Index', 'red', None)] = \
  102. b'0\x001'
  103. with unittest.mock.patch.object(self.launcher, 'kde_guid_args') as \
  104. kde_mock:
  105. kde_mock.return_value = []
  106. args = self.launcher.common_guid_args(self.app.domains['test-vm'])
  107. self.assertEqual(args, [
  108. '/usr/bin/qubes-guid', '-N', 'test-vm',
  109. '-c', '0xff0000',
  110. '-i', '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  111. '-l', '1', '-v', '-v'])
  112. self.assertAllCalled()
  113. @unittest.mock.patch('asyncio.create_subprocess_exec')
  114. def test_020_start_gui_for_vm(self, proc_mock):
  115. loop = asyncio.new_event_loop()
  116. asyncio.set_event_loop(loop)
  117. self.addCleanup(loop.close)
  118. self.app.expected_calls[
  119. ('dom0', 'admin.vm.List', None, None)] = \
  120. b'0\x00test-vm class=AppVM state=Running\n'
  121. self.app.expected_calls[
  122. ('test-vm', 'admin.vm.List', None, None)] = \
  123. b'0\x00test-vm class=AppVM state=Running\n'
  124. self.app.expected_calls[
  125. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  126. b'0\x00default=False type=int 3000'
  127. self.app.expected_calls[
  128. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  129. b'0\x00default=False type=str pv'
  130. self.app.expected_calls[
  131. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  132. 'no-monitor-layout', None)] = \
  133. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  134. with unittest.mock.patch.object(self.launcher,
  135. 'common_guid_args', lambda vm: []):
  136. loop.run_until_complete(self.launcher.start_gui_for_vm(
  137. self.app.domains['test-vm']))
  138. # common arguments dropped for simplicity
  139. proc_mock.assert_called_once_with('-d', '3000')
  140. self.assertAllCalled()
  141. @unittest.mock.patch('asyncio.create_subprocess_exec')
  142. def test_021_start_gui_for_vm_hvm(self, proc_mock):
  143. loop = asyncio.new_event_loop()
  144. asyncio.set_event_loop(loop)
  145. self.addCleanup(loop.close)
  146. self.app.expected_calls[
  147. ('dom0', 'admin.vm.List', None, None)] = \
  148. b'0\x00test-vm class=AppVM state=Running\n'
  149. self.app.expected_calls[
  150. ('test-vm', 'admin.vm.List', None, None)] = \
  151. b'0\x00test-vm class=AppVM state=Running\n'
  152. self.app.expected_calls[
  153. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  154. b'0\x00default=False type=int 3000'
  155. self.app.expected_calls[
  156. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  157. b'0\x00default=False type=int 3001'
  158. self.app.expected_calls[
  159. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  160. b'0\x00default=False type=str hvm'
  161. self.app.expected_calls[
  162. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  163. b'0\x00default=False type=bool False'
  164. self.app.expected_calls[
  165. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'rpc-clipboard',
  166. None)] = \
  167. b'0\x00True'
  168. self.app.expected_calls[
  169. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  170. 'no-monitor-layout', None)] = \
  171. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  172. with unittest.mock.patch.object(self.launcher,
  173. 'common_guid_args', lambda vm: []):
  174. loop.run_until_complete(self.launcher.start_gui_for_vm(
  175. self.app.domains['test-vm']))
  176. # common arguments dropped for simplicity
  177. proc_mock.assert_called_once_with('-d', '3000', '-n', '-Q')
  178. self.assertAllCalled()
  179. def test_022_start_gui_for_vm_hvm_stubdom(self):
  180. loop = asyncio.new_event_loop()
  181. asyncio.set_event_loop(loop)
  182. self.addCleanup(loop.close)
  183. self.app.expected_calls[
  184. ('dom0', 'admin.vm.List', None, None)] = \
  185. b'0\x00test-vm class=AppVM state=Running\n'
  186. self.app.expected_calls[
  187. ('test-vm', 'admin.vm.List', None, None)] = \
  188. b'0\x00test-vm class=AppVM state=Running\n'
  189. self.app.expected_calls[
  190. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  191. b'0\x00default=False type=int 3000'
  192. self.app.expected_calls[
  193. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  194. b'0\x00default=False type=int 3001'
  195. self.app.expected_calls[
  196. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  197. b'0\x00default=False type=str hvm'
  198. self.app.expected_calls[
  199. ('test-vm', 'admin.vm.property.Get', 'debug', None)] = \
  200. b'0\x00default=False type=bool False'
  201. self.app.expected_calls[
  202. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'rpc-clipboard',
  203. None)] = \
  204. b'0\x00True'
  205. self.app.expected_calls[
  206. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  207. 'no-monitor-layout', None)] = \
  208. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  209. pidfile = tempfile.NamedTemporaryFile()
  210. pidfile.write(b'1234\n')
  211. pidfile.flush()
  212. self.addCleanup(pidfile.close)
  213. patch_proc = unittest.mock.patch('asyncio.create_subprocess_exec')
  214. patch_args = unittest.mock.patch.object(self.launcher,
  215. 'common_guid_args', lambda vm: [])
  216. patch_pidfile = unittest.mock.patch.object(self.launcher,
  217. 'guid_pidfile', lambda vm: pidfile.name)
  218. try:
  219. mock_proc = patch_proc.start()
  220. patch_args.start()
  221. patch_pidfile.start()
  222. loop.run_until_complete(self.launcher.start_gui_for_vm(
  223. self.app.domains['test-vm']))
  224. # common arguments dropped for simplicity
  225. mock_proc.assert_called_once_with(
  226. '-d', '3000', '-n', '-Q', '-K', '1234')
  227. finally:
  228. unittest.mock.patch.stopall()
  229. self.assertAllCalled()
  230. def test_030_start_gui_for_stubdomain(self):
  231. loop = asyncio.new_event_loop()
  232. asyncio.set_event_loop(loop)
  233. self.addCleanup(loop.close)
  234. self.app.expected_calls[
  235. ('dom0', 'admin.vm.List', None, None)] = \
  236. b'0\x00test-vm class=AppVM state=Running\n'
  237. self.app.expected_calls[
  238. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  239. b'0\x00default=False type=int 3000'
  240. self.app.expected_calls[
  241. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  242. b'0\x00default=False type=int 3001'
  243. self.app.expected_calls[
  244. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui', None)] = \
  245. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  246. proc_mock = unittest.mock.Mock()
  247. with unittest.mock.patch('asyncio.create_subprocess_exec',
  248. lambda *args: self.mock_coroutine(proc_mock, *args)):
  249. with unittest.mock.patch.object(self.launcher,
  250. 'common_guid_args', lambda vm: []):
  251. loop.run_until_complete(self.launcher.start_gui_for_stubdomain(
  252. self.app.domains['test-vm']))
  253. # common arguments dropped for simplicity
  254. proc_mock.assert_called_once_with('-d', '3001', '-t', '3000')
  255. self.assertAllCalled()
  256. @asyncio.coroutine
  257. def mock_coroutine(self, mock, *args, **kwargs):
  258. mock(*args, **kwargs)
  259. def test_040_start_gui(self):
  260. loop = asyncio.new_event_loop()
  261. asyncio.set_event_loop(loop)
  262. self.addCleanup(loop.close)
  263. self.app.expected_calls[
  264. ('dom0', 'admin.vm.List', None, None)] = \
  265. b'0\x00test-vm class=AppVM state=Running\n'
  266. self.app.expected_calls[
  267. ('test-vm', 'admin.vm.List', None, None)] = \
  268. b'0\x00test-vm class=AppVM state=Running\n'
  269. self.app.expected_calls[
  270. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui', None)] = \
  271. b'0\x00True'
  272. self.app.expected_calls[
  273. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  274. 'no-monitor-layout', None)] = \
  275. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  276. self.app.expected_calls[
  277. ('test-vm', 'admin.vm.property.Get', 'virt_mode', None)] = \
  278. b'0\x00default=False type=str hvm'
  279. self.app.expected_calls[
  280. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  281. b'0\x00default=False type=int 3000'
  282. self.app.expected_calls[
  283. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  284. b'0\x00default=False type=int 3001'
  285. vm = self.app.domains['test-vm']
  286. mock_start_vm = unittest.mock.Mock()
  287. mock_start_stubdomain = unittest.mock.Mock()
  288. patch_start_vm = unittest.mock.patch.object(
  289. self.launcher, 'start_gui_for_vm', functools.partial(
  290. self.mock_coroutine, mock_start_vm))
  291. patch_start_stubdomain = unittest.mock.patch.object(
  292. self.launcher, 'start_gui_for_stubdomain', lambda vm_, force:
  293. self.mock_coroutine(mock_start_stubdomain, vm_))
  294. try:
  295. patch_start_vm.start()
  296. patch_start_stubdomain.start()
  297. loop.run_until_complete(self.launcher.start_gui(vm))
  298. mock_start_vm.assert_called_once_with(vm, monitor_layout=None)
  299. mock_start_stubdomain.assert_called_once_with(vm)
  300. finally:
  301. unittest.mock.patch.stopall()
  302. def test_041_start_gui_running(self):
  303. # simulate existing pidfiles, should not start processes
  304. self.skipTest('todo')
  305. def test_042_start_gui_pvh(self):
  306. # PVH - no stubdomain
  307. self.skipTest('todo')
  308. @unittest.mock.patch('subprocess.Popen')
  309. def test_050_get_monitor_layout1(self, proc_mock):
  310. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  311. HDMI1 connected 1920x1200+0+0 (normal left inverted right x axis y axis) 518mm x 324mm
  312. 1920x1200 59.95*+
  313. 1920x1080 60.00 50.00 59.94
  314. 1920x1080i 60.00 50.00 59.94
  315. 1600x1200 60.00
  316. 1680x1050 59.88
  317. 1280x1024 60.02
  318. 1440x900 59.90
  319. 1280x960 60.00
  320. 1280x720 60.00 50.00 59.94
  321. 1024x768 60.00
  322. 800x600 60.32
  323. 720x576 50.00
  324. 720x480 60.00 59.94
  325. 720x480i 60.00 59.94
  326. 640x480 60.00 59.94
  327. HDMI2 disconnected (normal left inverted right x axis y axis)
  328. VGA1 disconnected (normal left inverted right x axis y axis)
  329. VIRTUAL1 disconnected (normal left inverted right x axis y axis)
  330. '''.splitlines()
  331. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  332. ['1920 1200 0 0\n'])
  333. @unittest.mock.patch('subprocess.Popen')
  334. def test_051_get_monitor_layout_multiple(self, proc_mock):
  335. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 2880 x 1024, maximum 32767 x 32767
  336. LVDS1 connected 1600x900+0+0 (normal left inverted right x axis y axis)
  337. VGA1 connected 1280x1024+1600+0 (normal left inverted right x axis y axis)
  338. '''.splitlines()
  339. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  340. ['1600 900 0 0\n', '1280 1024 1600 0\n'])
  341. @unittest.mock.patch('subprocess.Popen')
  342. def test_052_get_monitor_layout_hidpi1(self, proc_mock):
  343. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  344. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 372mm x 208mm
  345. 1920x1200 60.00*+
  346. '''.splitlines()
  347. dpi = 150
  348. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  349. ['2560 1920 0 0 {} {}\n'.format(
  350. int(2560/dpi*254/10), int(1920/dpi*254/10))])
  351. @unittest.mock.patch('subprocess.Popen')
  352. def test_052_get_monitor_layout_hidpi2(self, proc_mock):
  353. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  354. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 310mm x 174mm
  355. 1920x1200 60.00*+
  356. '''.splitlines()
  357. dpi = 200
  358. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  359. ['2560 1920 0 0 {} {}\n'.format(
  360. int(2560/dpi*254/10), int(1920/dpi*254/10))])
  361. @unittest.mock.patch('subprocess.Popen')
  362. def test_052_get_monitor_layout_hidpi3(self, proc_mock):
  363. proc_mock().stdout = b'''Screen 0: minimum 8 x 8, current 1920 x 1200, maximum 32767 x 32767
  364. HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 206mm x 116mm
  365. 1920x1200 60.00*+
  366. '''.splitlines()
  367. dpi = 300
  368. self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
  369. ['2560 1920 0 0 {} {}\n'.format(
  370. int(2560/dpi*254/10), int(1920/dpi*254/10))])
  371. def test_060_send_monitor_layout(self):
  372. loop = asyncio.new_event_loop()
  373. asyncio.set_event_loop(loop)
  374. self.addCleanup(loop.close)
  375. self.app.expected_calls[
  376. ('dom0', 'admin.vm.List', None, None)] = \
  377. b'0\x00test-vm class=AppVM state=Running\n'
  378. self.app.expected_calls[
  379. ('test-vm', 'admin.vm.List', None, None)] = \
  380. b'0\x00test-vm class=AppVM state=Running\n'
  381. self.app.expected_calls[
  382. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  383. 'no-monitor-layout', None)] = \
  384. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  385. vm = self.app.domains['test-vm']
  386. mock_run_service = unittest.mock.Mock(spec={})
  387. patch_run_service = unittest.mock.patch.object(
  388. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  389. mock_run_service)
  390. patch_run_service.start()
  391. self.addCleanup(patch_run_service.stop)
  392. monitor_layout = ['1920 1080 0 0\n']
  393. loop.run_until_complete(self.launcher.send_monitor_layout(
  394. vm, layout=monitor_layout, startup=True))
  395. mock_run_service.assert_called_once_with(
  396. 'qubes.SetMonitorLayout', b'1920 1080 0 0\n')
  397. self.assertAllCalled()
  398. def test_061_send_monitor_layout_exclude(self):
  399. loop = asyncio.new_event_loop()
  400. asyncio.set_event_loop(loop)
  401. self.addCleanup(loop.close)
  402. self.app.expected_calls[
  403. ('dom0', 'admin.vm.List', None, None)] = \
  404. b'0\x00test-vm class=AppVM state=Running\n'
  405. self.app.expected_calls[
  406. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  407. 'no-monitor-layout', None)] = \
  408. b'0\x00True'
  409. vm = self.app.domains['test-vm']
  410. mock_run_service = unittest.mock.Mock()
  411. patch_run_service = unittest.mock.patch.object(
  412. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  413. mock_run_service)
  414. patch_run_service.start()
  415. self.addCleanup(patch_run_service.stop)
  416. monitor_layout = ['1920 1080 0 0\n']
  417. loop.run_until_complete(self.launcher.send_monitor_layout(
  418. vm, layout=monitor_layout, startup=True))
  419. self.assertFalse(mock_run_service.called)
  420. self.assertAllCalled()
  421. def test_062_send_monitor_layout_not_running(self):
  422. loop = asyncio.new_event_loop()
  423. asyncio.set_event_loop(loop)
  424. self.addCleanup(loop.close)
  425. self.app.expected_calls[
  426. ('dom0', 'admin.vm.List', None, None)] = \
  427. b'0\x00test-vm class=AppVM state=Halted\n'
  428. self.app.expected_calls[
  429. ('test-vm', 'admin.vm.List', None, None)] = \
  430. b'0\x00test-vm class=AppVM state=Halted\n'
  431. self.app.expected_calls[
  432. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  433. 'no-monitor-layout', None)] = \
  434. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  435. vm = self.app.domains['test-vm']
  436. mock_run_service = unittest.mock.Mock()
  437. patch_run_service = unittest.mock.patch.object(
  438. qubesadmin.vm.QubesVM, 'run_service_for_stdio',
  439. mock_run_service)
  440. patch_run_service.start()
  441. self.addCleanup(patch_run_service.stop)
  442. monitor_layout = ['1920 1080 0 0\n']
  443. loop.run_until_complete(self.launcher.send_monitor_layout(
  444. vm, layout=monitor_layout, startup=True))
  445. self.assertFalse(mock_run_service.called)
  446. self.assertAllCalled()
  447. def test_063_send_monitor_layout_signal_existing(self):
  448. loop = asyncio.new_event_loop()
  449. asyncio.set_event_loop(loop)
  450. self.addCleanup(loop.close)
  451. self.app.expected_calls[
  452. ('dom0', 'admin.vm.List', None, None)] = \
  453. b'0\x00test-vm class=AppVM state=Running\n'
  454. self.app.expected_calls[
  455. ('test-vm', 'admin.vm.List', None, None)] = \
  456. b'0\x00test-vm class=AppVM state=Running\n'
  457. self.app.expected_calls[
  458. ('test-vm', 'admin.vm.property.Get', 'xid', None)] = \
  459. b'0\x00default=False type=int 123'
  460. self.app.expected_calls[
  461. ('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
  462. b'0\x00default=False type=int 124'
  463. self.app.expected_calls[
  464. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  465. 'no-monitor-layout', None)] = \
  466. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  467. vm = self.app.domains['test-vm']
  468. self.addCleanup(unittest.mock.patch.stopall)
  469. with tempfile.NamedTemporaryFile() as pidfile:
  470. pidfile.write(b'1234\n')
  471. pidfile.flush()
  472. patch_guid_pidfile = unittest.mock.patch.object(
  473. self.launcher, 'guid_pidfile')
  474. mock_guid_pidfile = patch_guid_pidfile.start()
  475. mock_guid_pidfile.return_value = pidfile.name
  476. mock_kill = unittest.mock.patch('os.kill').start()
  477. monitor_layout = ['1920 1080 0 0\n']
  478. loop.run_until_complete(self.launcher.send_monitor_layout(
  479. vm, layout=monitor_layout, startup=False))
  480. self.assertEqual(mock_guid_pidfile.mock_calls,
  481. [unittest.mock.call(123),
  482. unittest.mock.call(124)])
  483. self.assertEqual(mock_kill.mock_calls,
  484. [unittest.mock.call(1234, signal.SIGHUP),
  485. unittest.mock.call(1234, signal.SIGHUP)])
  486. self.assertAllCalled()
  487. def test_070_send_monitor_layout_all(self):
  488. loop = asyncio.new_event_loop()
  489. asyncio.set_event_loop(loop)
  490. self.addCleanup(loop.close)
  491. self.app.expected_calls[
  492. ('dom0', 'admin.vm.List', None, None)] = \
  493. b'0\x00test-vm class=AppVM state=Running\n' \
  494. b'test-vm2 class=AppVM state=Running\n' \
  495. b'test-vm3 class=AppVM state=Runnig\n' \
  496. b'test-vm4 class=AppVM state=Halted\n'
  497. self.app.expected_calls[
  498. ('test-vm', 'admin.vm.List', None, None)] = \
  499. b'0\x00test-vm class=AppVM state=Running\n'
  500. self.app.expected_calls[
  501. ('test-vm2', 'admin.vm.List', None, None)] = \
  502. b'0\x00test-vm2 class=AppVM state=Running\n'
  503. self.app.expected_calls[
  504. ('test-vm3', 'admin.vm.List', None, None)] = \
  505. b'0\x00test-vm3 class=AppVM state=Running\n'
  506. self.app.expected_calls[
  507. ('test-vm4', 'admin.vm.List', None, None)] = \
  508. b'0\x00test-vm4 class=AppVM state=Halted\n'
  509. self.app.expected_calls[
  510. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  511. 'gui', None)] = \
  512. b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
  513. self.app.expected_calls[
  514. ('test-vm2', 'admin.vm.feature.CheckWithTemplate',
  515. 'gui', None)] = \
  516. b'0\x00True'
  517. self.app.expected_calls[
  518. ('test-vm3', 'admin.vm.feature.CheckWithTemplate',
  519. 'gui', None)] = \
  520. b'0\x00'
  521. vm = self.app.domains['test-vm']
  522. vm2 = self.app.domains['test-vm2']
  523. self.addCleanup(unittest.mock.patch.stopall)
  524. mock_send_monitor_layout = unittest.mock.Mock()
  525. patch_send_monitor_layout = unittest.mock.patch.object(
  526. self.launcher, 'send_monitor_layout',
  527. functools.partial(self.mock_coroutine, mock_send_monitor_layout))
  528. patch_send_monitor_layout.start()
  529. monitor_layout = ['1920 1080 0 0\n']
  530. mock_get_monior_layout = unittest.mock.patch(
  531. 'qubesadmin.tools.qvm_start_gui.get_monitor_layout').start()
  532. mock_get_monior_layout.return_value = monitor_layout
  533. self.launcher.send_monitor_layout_all()
  534. loop.stop()
  535. loop.run_forever()
  536. # test-vm3 not called b/c feature 'gui' set to false
  537. # test-vm4 not called b/c not running
  538. self.assertCountEqual(mock_send_monitor_layout.mock_calls,
  539. [unittest.mock.call(vm, monitor_layout),
  540. unittest.mock.call(vm2, monitor_layout)])
  541. mock_get_monior_layout.assert_called_once_with()
  542. self.assertAllCalled()