qvm_template_postprocess.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 asyncio
  21. import os
  22. import subprocess
  23. import tempfile
  24. from unittest import mock
  25. import qubesadmin.tests
  26. import qubesadmin.tools.qvm_template_postprocess
  27. class QubesLocalMock(qubesadmin.tests.QubesTest):
  28. def __init__(self):
  29. super(QubesLocalMock, self).__init__()
  30. self.__class__ = qubesadmin.app.QubesLocal
  31. qubesd_call = qubesadmin.tests.QubesTest.qubesd_call
  32. run_service = qubesadmin.tests.QubesTest.run_service
  33. class TC_00_qvm_template_postprocess(qubesadmin.tests.QubesTestCase):
  34. def setUp(self):
  35. super(TC_00_qvm_template_postprocess, self).setUp()
  36. self.source_dir = tempfile.TemporaryDirectory()
  37. def tearDown(self):
  38. try:
  39. self.source_dir.cleanup()
  40. except FileNotFoundError:
  41. pass
  42. super(TC_00_qvm_template_postprocess, self).tearDown()
  43. def test_000_import_root_img_raw(self):
  44. root_img = os.path.join(self.source_dir.name, 'root.img')
  45. volume_data = b'volume data'
  46. with open(root_img, 'wb') as f:
  47. f.write(volume_data)
  48. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  49. b'0\0test-vm class=TemplateVM state=Halted\n'
  50. self.app.expected_calls[('test-vm', 'admin.vm.volume.List', None,
  51. None)] = \
  52. b'0\0root\nprivate\nvolatile\nkernel\n'
  53. self.app.expected_calls[('test-vm', 'admin.vm.volume.Resize', 'root',
  54. str(len(volume_data)).encode())] = \
  55. b'0\0'
  56. self.app.expected_calls[('test-vm', 'admin.vm.volume.Import', 'root',
  57. volume_data)] = b'0\0'
  58. vm = self.app.domains['test-vm']
  59. qubesadmin.tools.qvm_template_postprocess.import_root_img(
  60. vm, self.source_dir.name)
  61. self.assertAllCalled()
  62. def test_001_import_root_img_tar(self):
  63. root_img = os.path.join(self.source_dir.name, 'root.img')
  64. volume_data = b'volume data' * 1000
  65. with open(root_img, 'wb') as f:
  66. f.write(volume_data)
  67. subprocess.check_call(['tar', 'cf', 'root.img.tar', 'root.img'],
  68. cwd=self.source_dir.name)
  69. subprocess.check_call(['split', '-d', '-b', '1024', 'root.img.tar',
  70. 'root.img.part.'], cwd=self.source_dir.name)
  71. os.unlink(root_img)
  72. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  73. b'0\0test-vm class=TemplateVM state=Halted\n'
  74. self.app.expected_calls[('test-vm', 'admin.vm.volume.List', None,
  75. None)] = \
  76. b'0\0root\nprivate\nvolatile\nkernel\n'
  77. self.app.expected_calls[('test-vm', 'admin.vm.volume.Resize', 'root',
  78. str(len(volume_data)).encode())] = \
  79. b'0\0'
  80. self.app.expected_calls[('test-vm', 'admin.vm.volume.Import', 'root',
  81. volume_data)] = b'0\0'
  82. vm = self.app.domains['test-vm']
  83. qubesadmin.tools.qvm_template_postprocess.import_root_img(
  84. vm, self.source_dir.name)
  85. self.assertAllCalled()
  86. def test_002_import_root_img_no_overwrite(self):
  87. self.app.qubesd_connection_type = 'socket'
  88. template_dir = os.path.join(self.source_dir.name, 'vm-templates',
  89. 'test-vm')
  90. os.makedirs(template_dir)
  91. root_img = os.path.join(template_dir, 'root.img')
  92. volume_data = b'volume data'
  93. with open(root_img, 'wb') as f:
  94. f.write(volume_data)
  95. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  96. b'0\0test-vm class=TemplateVM state=Halted\n'
  97. self.app.expected_calls[
  98. ('test-vm', 'admin.vm.volume.List', None, None)] = \
  99. b'0\0root\nprivate\nvolatile\nkernel\n'
  100. self.app.expected_calls[
  101. ('test-vm', 'admin.vm.volume.Info', 'root', None)] = \
  102. b'0\0pool=default\nvid=vm-templates/test-vm/root\n'
  103. self.app.expected_calls[
  104. ('dom0', 'admin.pool.List', None, None)] = \
  105. b'0\0default\n'
  106. self.app.expected_calls[
  107. ('dom0', 'admin.pool.Info', 'default', None)] = \
  108. b'0\0driver=file\ndir_path=' + self.source_dir.name.encode() + b'\n'
  109. self.app.expected_calls[
  110. ('test-vm', 'admin.vm.volume.Resize', 'root',
  111. str(len(volume_data)).encode())] = \
  112. b'0\0'
  113. vm = self.app.domains['test-vm']
  114. qubesadmin.tools.qvm_template_postprocess.import_root_img(
  115. vm, template_dir)
  116. self.assertAllCalled()
  117. def test_010_import_appmenus(self):
  118. with open(os.path.join(self.source_dir.name,
  119. 'vm-whitelisted-appmenus.list'), 'w') as f:
  120. f.write('org.gnome.Terminal.desktop\n')
  121. f.write('firefox.desktop\n')
  122. with open(os.path.join(self.source_dir.name,
  123. 'whitelisted-appmenus.list'), 'w') as f:
  124. f.write('org.gnome.Terminal.desktop\n')
  125. f.write('org.gnome.Software.desktop\n')
  126. f.write('gnome-control-center.desktop\n')
  127. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  128. b'0\0test-vm class=TemplateVM state=Halted\n'
  129. vm = self.app.domains['test-vm']
  130. with mock.patch('subprocess.check_call') as mock_proc:
  131. qubesadmin.tools.qvm_template_postprocess.import_appmenus(
  132. vm, self.source_dir.name)
  133. self.assertEqual(mock_proc.mock_calls, [
  134. mock.call(['qvm-appmenus',
  135. '--set-default-whitelist=' + os.path.join(self.source_dir.name,
  136. 'vm-whitelisted-appmenus.list'), 'test-vm']),
  137. mock.call(['qvm-appmenus', '--set-whitelist=' + os.path.join(
  138. self.source_dir.name, 'whitelisted-appmenus.list'), 'test-vm']),
  139. ])
  140. self.assertAllCalled()
  141. @mock.patch('grp.getgrnam')
  142. @mock.patch('os.getuid')
  143. def test_011_import_appmenus_as_root(self, mock_getuid, mock_getgrnam):
  144. with open(os.path.join(self.source_dir.name,
  145. 'vm-whitelisted-appmenus.list'), 'w') as f:
  146. f.write('org.gnome.Terminal.desktop\n')
  147. f.write('firefox.desktop\n')
  148. with open(os.path.join(self.source_dir.name,
  149. 'whitelisted-appmenus.list'), 'w') as f:
  150. f.write('org.gnome.Terminal.desktop\n')
  151. f.write('org.gnome.Software.desktop\n')
  152. f.write('gnome-control-center.desktop\n')
  153. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  154. b'0\0test-vm class=TemplateVM state=Halted\n'
  155. mock_getuid.return_value = 0
  156. mock_getgrnam.configure_mock(**{
  157. 'return_value.gr_mem.__getitem__.return_value': 'user'
  158. })
  159. vm = self.app.domains['test-vm']
  160. with mock.patch('subprocess.check_call') as mock_proc:
  161. qubesadmin.tools.qvm_template_postprocess.import_appmenus(
  162. vm, self.source_dir.name)
  163. self.assertEqual(mock_proc.mock_calls, [
  164. mock.call(['runuser', '-u', 'user', '--', 'env', 'DISPLAY=:0',
  165. 'qvm-appmenus',
  166. '--set-default-whitelist=' + os.path.join(self.source_dir.name,
  167. 'vm-whitelisted-appmenus.list'), 'test-vm']),
  168. mock.call(['runuser', '-u', 'user', '--', 'env', 'DISPLAY=:0',
  169. 'qvm-appmenus', '--set-whitelist=' + os.path.join(
  170. self.source_dir.name, 'whitelisted-appmenus.list'), 'test-vm']),
  171. ])
  172. self.assertAllCalled()
  173. @mock.patch('grp.getgrnam')
  174. @mock.patch('os.getuid')
  175. def test_012_import_appmenus_missing_user(self, mock_getuid, mock_getgrnam):
  176. with open(os.path.join(self.source_dir.name,
  177. 'vm-whitelisted-appmenus.list'), 'w') as f:
  178. f.write('org.gnome.Terminal.desktop\n')
  179. f.write('firefox.desktop\n')
  180. with open(os.path.join(self.source_dir.name,
  181. 'whitelisted-appmenus.list'), 'w') as f:
  182. f.write('org.gnome.Terminal.desktop\n')
  183. f.write('org.gnome.Software.desktop\n')
  184. f.write('gnome-control-center.desktop\n')
  185. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  186. b'0\0test-vm class=TemplateVM state=Halted\n'
  187. mock_getuid.return_value = 0
  188. mock_getgrnam.side_effect = KeyError
  189. vm = self.app.domains['test-vm']
  190. with mock.patch('subprocess.check_call') as mock_proc:
  191. qubesadmin.tools.qvm_template_postprocess.import_appmenus(
  192. vm, self.source_dir.name)
  193. self.assertEqual(mock_proc.mock_calls, [])
  194. self.assertAllCalled()
  195. def add_new_vm_side_effect(self, *args, **kwargs):
  196. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  197. b'0\0test-vm class=TemplateVM state=Halted\n'
  198. self.app.domains.clear_cache()
  199. return self.app.domains['test-vm']
  200. @asyncio.coroutine
  201. def wait_for_shutdown(self, vm):
  202. pass
  203. @mock.patch('qubesadmin.tools.qvm_template_postprocess.import_appmenus')
  204. @mock.patch('qubesadmin.tools.qvm_template_postprocess.import_root_img')
  205. def test_020_post_install(self, mock_import_root_img,
  206. mock_import_appmenus):
  207. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  208. b'0\0'
  209. self.app.add_new_vm = mock.Mock(side_effect=self.add_new_vm_side_effect)
  210. self.app.expected_calls[
  211. ('test-vm', 'admin.vm.property.Set', 'netvm', b'')] = b'0\0'
  212. self.app.expected_calls[
  213. ('test-vm', 'admin.vm.property.Set', 'installed_by_rpm', b'True')] \
  214. = b'0\0'
  215. self.app.expected_calls[
  216. ('test-vm', 'admin.vm.property.Reset', 'netvm', None)] = b'0\0'
  217. self.app.expected_calls[
  218. ('test-vm', 'admin.vm.feature.Set', 'qrexec', b'1')] = b'0\0'
  219. self.app.expected_calls[
  220. ('test-vm', 'admin.vm.Start', None, None)] = b'0\0'
  221. self.app.expected_calls[
  222. ('test-vm', 'admin.vm.Shutdown', None, None)] = b'0\0'
  223. if qubesadmin.tools.qvm_template_postprocess.have_events:
  224. patch_domain_shutdown = mock.patch(
  225. 'qubesadmin.events.utils.wait_for_domain_shutdown')
  226. self.addCleanup(patch_domain_shutdown.stop)
  227. mock_domain_shutdown = patch_domain_shutdown.start()
  228. mock_domain_shutdown.side_effect = self.wait_for_shutdown
  229. else:
  230. self.app.expected_calls[
  231. ('test-vm', 'admin.vm.List', None, None)] = \
  232. b'0\0test-vm class=TemplateVM state=Halted\n'
  233. asyncio.set_event_loop(asyncio.new_event_loop())
  234. ret = qubesadmin.tools.qvm_template_postprocess.main([
  235. '--really', 'post-install', 'test-vm', self.source_dir.name],
  236. app=self.app)
  237. self.assertEqual(ret, 0)
  238. self.app.add_new_vm.assert_called_once_with('TemplateVM',
  239. name='test-vm', label='black')
  240. mock_import_root_img.assert_called_once_with(self.app.domains[
  241. 'test-vm'], self.source_dir.name)
  242. mock_import_appmenus.assert_called_once_with(self.app.domains[
  243. 'test-vm'], self.source_dir.name)
  244. if qubesadmin.tools.qvm_template_postprocess.have_events:
  245. mock_domain_shutdown.assert_called_once_with([self.app.domains[
  246. 'test-vm']])
  247. self.assertEqual(self.app.service_calls, [
  248. ('test-vm', 'qubes.PostInstall', {}),
  249. ('test-vm', 'qubes.PostInstall', b''),
  250. ])
  251. self.assertAllCalled()
  252. @mock.patch('qubesadmin.tools.qvm_template_postprocess.import_appmenus')
  253. @mock.patch('qubesadmin.tools.qvm_template_postprocess.import_root_img')
  254. def test_021_post_install_reinstall(self, mock_import_root_img,
  255. mock_import_appmenus):
  256. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  257. b'0\0test-vm class=TemplateVM state=Halted\n'
  258. self.app.add_new_vm = mock.Mock()
  259. self.app.expected_calls[
  260. ('test-vm', 'admin.vm.property.Set', 'netvm', b'')] = b'0\0'
  261. self.app.expected_calls[
  262. ('test-vm', 'admin.vm.property.Set', 'installed_by_rpm', b'True')] \
  263. = b'0\0'
  264. self.app.expected_calls[
  265. ('test-vm', 'admin.vm.property.Reset', 'netvm', None)] = b'0\0'
  266. self.app.expected_calls[
  267. ('test-vm', 'admin.vm.feature.Set', 'qrexec', b'1')] = b'0\0'
  268. self.app.expected_calls[
  269. ('test-vm', 'admin.vm.Start', None, None)] = b'0\0'
  270. self.app.expected_calls[
  271. ('test-vm', 'admin.vm.Shutdown', None, None)] = b'0\0'
  272. if qubesadmin.tools.qvm_template_postprocess.have_events:
  273. patch_domain_shutdown = mock.patch(
  274. 'qubesadmin.events.utils.wait_for_domain_shutdown')
  275. self.addCleanup(patch_domain_shutdown.stop)
  276. mock_domain_shutdown = patch_domain_shutdown.start()
  277. mock_domain_shutdown.side_effect = self.wait_for_shutdown
  278. else:
  279. self.app.expected_calls[
  280. ('test-vm', 'admin.vm.List', None, None)] = \
  281. b'0\0test-vm class=TemplateVM state=Halted\n'
  282. asyncio.set_event_loop(asyncio.new_event_loop())
  283. ret = qubesadmin.tools.qvm_template_postprocess.main([
  284. '--really', 'post-install', 'test-vm', self.source_dir.name],
  285. app=self.app)
  286. self.assertEqual(ret, 0)
  287. self.assertFalse(self.app.add_new_vm.called)
  288. mock_import_root_img.assert_called_once_with(self.app.domains[
  289. 'test-vm'], self.source_dir.name)
  290. mock_import_appmenus.assert_called_once_with(self.app.domains[
  291. 'test-vm'], self.source_dir.name)
  292. if qubesadmin.tools.qvm_template_postprocess.have_events:
  293. mock_domain_shutdown.assert_called_once_with([self.app.domains[
  294. 'test-vm']])
  295. self.assertEqual(self.app.service_calls, [
  296. ('test-vm', 'qubes.PostInstall', {}),
  297. ('test-vm', 'qubes.PostInstall', b''),
  298. ])
  299. self.assertAllCalled()
  300. @mock.patch('qubesadmin.tools.qvm_template_postprocess.import_appmenus')
  301. @mock.patch('qubesadmin.tools.qvm_template_postprocess.import_root_img')
  302. def test_022_post_install_skip_start(self, mock_import_root_img,
  303. mock_import_appmenus):
  304. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  305. b'0\0test-vm class=TemplateVM state=Halted\n'
  306. self.app.expected_calls[
  307. ('test-vm', 'admin.vm.property.Set', 'installed_by_rpm', b'True')] \
  308. = b'0\0'
  309. self.app.add_new_vm = mock.Mock()
  310. if qubesadmin.tools.qvm_template_postprocess.have_events:
  311. patch_domain_shutdown = mock.patch(
  312. 'qubesadmin.events.utils.wait_for_domain_shutdown')
  313. self.addCleanup(patch_domain_shutdown.stop)
  314. mock_domain_shutdown = patch_domain_shutdown.start()
  315. mock_domain_shutdown.side_effect = self.wait_for_shutdown
  316. asyncio.set_event_loop(asyncio.new_event_loop())
  317. ret = qubesadmin.tools.qvm_template_postprocess.main([
  318. '--really', '--skip-start', 'post-install', 'test-vm',
  319. self.source_dir.name],
  320. app=self.app)
  321. self.assertEqual(ret, 0)
  322. self.assertFalse(self.app.add_new_vm.called)
  323. mock_import_root_img.assert_called_once_with(self.app.domains[
  324. 'test-vm'], self.source_dir.name)
  325. mock_import_appmenus.assert_called_once_with(self.app.domains[
  326. 'test-vm'], self.source_dir.name)
  327. if qubesadmin.tools.qvm_template_postprocess.have_events:
  328. self.assertFalse(mock_domain_shutdown.called)
  329. self.assertEqual(self.app.service_calls, [])
  330. self.assertAllCalled()
  331. def test_030_pre_remove(self):
  332. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  333. b'0\0test-vm class=TemplateVM state=Halted\n'
  334. self.app.expected_calls[('test-vm', 'admin.vm.Remove', None, None)] = \
  335. b'0\0'
  336. self.app.expected_calls[
  337. ('test-vm', 'admin.vm.property.Set', 'installed_by_rpm', b'False')]\
  338. = b'0\0'
  339. self.app.expected_calls[
  340. ('test-vm', 'admin.vm.property.Get', 'template', None)] = \
  341. b'2\0QubesNoSuchPropertyError\0\0invalid property \'template\' of ' \
  342. b'test-vm\0'
  343. ret = qubesadmin.tools.qvm_template_postprocess.main([
  344. '--really', 'pre-remove', 'test-vm',
  345. self.source_dir.name],
  346. app=self.app)
  347. self.assertEqual(ret, 0)
  348. self.assertEqual(self.app.service_calls, [])
  349. self.assertAllCalled()
  350. def test_031_pre_remove_existing_appvm(self):
  351. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  352. b'0\0test-vm class=TemplateVM state=Halted\n' \
  353. b'test-vm2 class=AppVM state=Halted\n'
  354. self.app.expected_calls[
  355. ('test-vm', 'admin.vm.property.Get', 'template', None)] = \
  356. b'2\0QubesNoSuchPropertyError\0\0invalid property \'template\' of ' \
  357. b'test-vm\0'
  358. self.app.expected_calls[
  359. ('test-vm2', 'admin.vm.property.Get', 'template', None)] = \
  360. b'0\0default=no type=vm test-vm'
  361. with self.assertRaises(SystemExit):
  362. qubesadmin.tools.qvm_template_postprocess.main([
  363. '--really', 'pre-remove', 'test-vm',
  364. self.source_dir.name],
  365. app=self.app)
  366. self.assertEqual(self.app.service_calls, [])
  367. self.assertAllCalled()
  368. def test_040_missing_really(self):
  369. with self.assertRaises(SystemExit):
  370. qubesadmin.tools.qvm_template_postprocess.main([
  371. 'post-install', 'test-vm', self.source_dir.name],
  372. app=self.app)
  373. self.assertAllCalled()