app.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. # pylint: disable=protected-access,pointless-statement
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library 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 GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. import os
  22. import unittest.mock as mock
  23. import lxml.etree
  24. import qubes
  25. import qubes.events
  26. import qubes.tests
  27. import qubes.tests.init
  28. import qubes.tests.storage_reflink
  29. class TestApp(qubes.tests.TestEmitter):
  30. pass
  31. class TC_20_QubesHost(qubes.tests.QubesTestCase):
  32. sample_xc_domain_getinfo = [
  33. {'paused': 0, 'cpu_time': 243951379111104, 'ssidref': 0,
  34. 'hvm': 0, 'shutdown_reason': 255, 'dying': 0,
  35. 'mem_kb': 3733212, 'domid': 0, 'max_vcpu_id': 7,
  36. 'crashed': 0, 'running': 1, 'maxmem_kb': 3734236,
  37. 'shutdown': 0, 'online_vcpus': 8,
  38. 'handle': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  39. 'cpupool': 0, 'blocked': 0},
  40. {'paused': 0, 'cpu_time': 2849496569205, 'ssidref': 0,
  41. 'hvm': 0, 'shutdown_reason': 255, 'dying': 0,
  42. 'mem_kb': 303916, 'domid': 1, 'max_vcpu_id': 0,
  43. 'crashed': 0, 'running': 0, 'maxmem_kb': 308224,
  44. 'shutdown': 0, 'online_vcpus': 1,
  45. 'handle': [116, 174, 229, 207, 17, 1, 79, 39, 191, 37, 41,
  46. 186, 205, 158, 219, 8],
  47. 'cpupool': 0, 'blocked': 1},
  48. {'paused': 0, 'cpu_time': 249658663079978, 'ssidref': 0,
  49. 'hvm': 0, 'shutdown_reason': 255, 'dying': 0,
  50. 'mem_kb': 3782668, 'domid': 11, 'max_vcpu_id': 7,
  51. 'crashed': 0, 'running': 0, 'maxmem_kb': 3783692,
  52. 'shutdown': 0, 'online_vcpus': 8,
  53. 'handle': [169, 95, 55, 127, 140, 94, 79, 220, 186, 210,
  54. 117, 5, 148, 11, 185, 206],
  55. 'cpupool': 0, 'blocked': 1}]
  56. def setUp(self):
  57. super(TC_20_QubesHost, self).setUp()
  58. self.app = TestApp()
  59. self.app.vmm = mock.Mock()
  60. self.qubes_host = qubes.app.QubesHost(self.app)
  61. def test_000_get_vm_stats_single(self):
  62. self.app.vmm.configure_mock(**{
  63. 'xc.domain_getinfo.return_value': self.sample_xc_domain_getinfo
  64. })
  65. info_time, info = self.qubes_host.get_vm_stats()
  66. self.assertEqual(self.app.vmm.mock_calls, [
  67. ('xc.domain_getinfo', (0, 1024), {}),
  68. ])
  69. self.assertIsNotNone(info_time)
  70. expected_info = {
  71. 0: {
  72. 'cpu_time': 243951379111104,
  73. 'cpu_usage': 0,
  74. 'cpu_usage_raw': 0,
  75. 'memory_kb': 3733212,
  76. },
  77. 1: {
  78. 'cpu_time': 2849496569205,
  79. 'cpu_usage': 0,
  80. 'cpu_usage_raw': 0,
  81. 'memory_kb': 303916,
  82. },
  83. 11: {
  84. 'cpu_time': 249658663079978,
  85. 'cpu_usage': 0,
  86. 'cpu_usage_raw': 0,
  87. 'memory_kb': 3782668,
  88. },
  89. }
  90. self.assertEqual(info, expected_info)
  91. def test_001_get_vm_stats_twice(self):
  92. self.app.vmm.configure_mock(**{
  93. 'xc.domain_getinfo.return_value': self.sample_xc_domain_getinfo
  94. })
  95. prev_time, prev_info = self.qubes_host.get_vm_stats()
  96. prev_time -= 1
  97. prev_info[0]['cpu_time'] -= 8 * 10 ** 8 # 0.8s
  98. prev_info[1]['cpu_time'] -= 10 ** 9 # 1s
  99. prev_info[11]['cpu_time'] -= 10 ** 9 # 1s
  100. info_time, info = self.qubes_host.get_vm_stats(prev_time, prev_info)
  101. self.assertIsNotNone(info_time)
  102. expected_info = {
  103. 0: {
  104. 'cpu_time': 243951379111104,
  105. 'cpu_usage': 9,
  106. 'cpu_usage_raw': 79,
  107. 'memory_kb': 3733212,
  108. },
  109. 1: {
  110. 'cpu_time': 2849496569205,
  111. 'cpu_usage': 99,
  112. 'cpu_usage_raw': 99,
  113. 'memory_kb': 303916,
  114. },
  115. 11: {
  116. 'cpu_time': 249658663079978,
  117. 'cpu_usage': 12,
  118. 'cpu_usage_raw': 99,
  119. 'memory_kb': 3782668,
  120. },
  121. }
  122. self.assertEqual(info, expected_info)
  123. self.assertEqual(self.app.vmm.mock_calls, [
  124. ('xc.domain_getinfo', (0, 1024), {}),
  125. ('xc.domain_getinfo', (0, 1024), {}),
  126. ])
  127. def test_002_get_vm_stats_one_vm(self):
  128. self.app.vmm.configure_mock(**{
  129. 'xc.domain_getinfo.return_value': [self.sample_xc_domain_getinfo[1]]
  130. })
  131. vm = mock.Mock
  132. vm.xid = 1
  133. vm.name = 'somevm'
  134. info_time, info = self.qubes_host.get_vm_stats(only_vm=vm)
  135. self.assertIsNotNone(info_time)
  136. self.assertEqual(self.app.vmm.mock_calls, [
  137. ('xc.domain_getinfo', (1, 1), {}),
  138. ])
  139. def test_010_iommu_supported(self):
  140. self.app.vmm.configure_mock(**{
  141. 'xc.physinfo.return_value': {
  142. 'hw_caps': '...',
  143. 'scrub_memory': 0,
  144. 'virt_caps': 'hvm hvm_directio',
  145. 'nr_cpus': 4,
  146. 'threads_per_core': 1,
  147. 'cpu_khz': 3400001,
  148. 'nr_nodes': 1,
  149. 'free_memory': 234752,
  150. 'cores_per_socket': 4,
  151. 'total_memory': 16609720
  152. }
  153. })
  154. self.assertEqual(self.qubes_host.is_iommu_supported(), True)
  155. def test_011_iommu_supported(self):
  156. self.app.vmm.configure_mock(**{
  157. 'xc.physinfo.return_value': {
  158. 'hw_caps': '...',
  159. 'scrub_memory': 0,
  160. 'virt_caps': 'hvm hvm_directio pv pv_directio',
  161. 'nr_cpus': 4,
  162. 'threads_per_core': 1,
  163. 'cpu_khz': 3400001,
  164. 'nr_nodes': 1,
  165. 'free_memory': 234752,
  166. 'cores_per_socket': 4,
  167. 'total_memory': 16609720
  168. }
  169. })
  170. self.assertEqual(self.qubes_host.is_iommu_supported(), True)
  171. def test_010_iommu_supported(self):
  172. self.app.vmm.configure_mock(**{
  173. 'xc.physinfo.return_value': {
  174. 'hw_caps': '...',
  175. 'scrub_memory': 0,
  176. 'virt_caps': 'hvm pv',
  177. 'nr_cpus': 4,
  178. 'threads_per_core': 1,
  179. 'cpu_khz': 3400001,
  180. 'nr_nodes': 1,
  181. 'free_memory': 234752,
  182. 'cores_per_socket': 4,
  183. 'total_memory': 16609720
  184. }
  185. })
  186. self.assertEqual(self.qubes_host.is_iommu_supported(), False)
  187. class TC_30_VMCollection(qubes.tests.QubesTestCase):
  188. def setUp(self):
  189. super().setUp()
  190. self.app = TestApp()
  191. self.vms = qubes.app.VMCollection(self.app)
  192. self.testvm1 = qubes.tests.init.TestVM(
  193. None, None, qid=1, name='testvm1')
  194. self.testvm2 = qubes.tests.init.TestVM(
  195. None, None, qid=2, name='testvm2')
  196. self.addCleanup(self.cleanup_vmcollection)
  197. def cleanup_vmcollection(self):
  198. self.testvm1.close()
  199. self.testvm2.close()
  200. self.vms.close()
  201. del self.testvm1
  202. del self.testvm2
  203. del self.vms
  204. del self.app
  205. def test_000_contains(self):
  206. self.vms._dict = {1: self.testvm1}
  207. self.assertIn(1, self.vms)
  208. self.assertIn('testvm1', self.vms)
  209. self.assertIn(self.testvm1, self.vms)
  210. self.assertNotIn(2, self.vms)
  211. self.assertNotIn('testvm2', self.vms)
  212. self.assertNotIn(self.testvm2, self.vms)
  213. def test_001_getitem(self):
  214. self.vms._dict = {1: self.testvm1}
  215. self.assertIs(self.vms[1], self.testvm1)
  216. self.assertIs(self.vms['testvm1'], self.testvm1)
  217. self.assertIs(self.vms[self.testvm1], self.testvm1)
  218. def test_002_add(self):
  219. self.vms.add(self.testvm1)
  220. self.assertIn(1, self.vms)
  221. self.assertEventFired(self.app, 'domain-add',
  222. kwargs={'vm': self.testvm1})
  223. with self.assertRaises(TypeError):
  224. self.vms.add(object())
  225. testvm_qid_collision = qubes.tests.init.TestVM(
  226. None, None, name='testvm2', qid=1)
  227. testvm_name_collision = qubes.tests.init.TestVM(
  228. None, None, name='testvm1', qid=2)
  229. with self.assertRaises(ValueError):
  230. self.vms.add(testvm_qid_collision)
  231. with self.assertRaises(ValueError):
  232. self.vms.add(testvm_name_collision)
  233. def test_003_qids(self):
  234. self.vms.add(self.testvm1)
  235. self.vms.add(self.testvm2)
  236. self.assertCountEqual(self.vms.qids(), [1, 2])
  237. self.assertCountEqual(self.vms.keys(), [1, 2])
  238. def test_004_names(self):
  239. self.vms.add(self.testvm1)
  240. self.vms.add(self.testvm2)
  241. self.assertCountEqual(self.vms.names(), ['testvm1', 'testvm2'])
  242. def test_005_vms(self):
  243. self.vms.add(self.testvm1)
  244. self.vms.add(self.testvm2)
  245. self.assertCountEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  246. self.assertCountEqual(self.vms.values(), [self.testvm1, self.testvm2])
  247. def test_006_items(self):
  248. self.vms.add(self.testvm1)
  249. self.vms.add(self.testvm2)
  250. self.assertCountEqual(self.vms.items(),
  251. [(1, self.testvm1), (2, self.testvm2)])
  252. def test_007_len(self):
  253. self.vms.add(self.testvm1)
  254. self.vms.add(self.testvm2)
  255. self.assertEqual(len(self.vms), 2)
  256. def test_008_delitem(self):
  257. self.vms.add(self.testvm1)
  258. self.vms.add(self.testvm2)
  259. del self.vms['testvm2']
  260. self.assertCountEqual(self.vms.vms(), [self.testvm1])
  261. self.assertEventFired(self.app, 'domain-delete',
  262. kwargs={'vm': self.testvm2})
  263. def test_100_get_new_unused_qid(self):
  264. self.vms.add(self.testvm1)
  265. self.vms.add(self.testvm2)
  266. self.vms.get_new_unused_qid()
  267. # def test_200_get_vms_based_on(self):
  268. # pass
  269. # def test_201_get_vms_connected_to(self):
  270. # pass
  271. class TC_80_QubesInitialPools(qubes.tests.QubesTestCase):
  272. def setUp(self):
  273. super().setUp()
  274. self.app = qubes.Qubes('/tmp/qubestest.xml', load=False,
  275. offline_mode=True)
  276. self.test_dir = '/var/tmp/test-varlibqubes'
  277. self.test_patch = mock.patch.dict(
  278. qubes.config.defaults['pool_configs']['varlibqubes'],
  279. {'dir_path': self.test_dir})
  280. self.test_patch.start()
  281. def tearDown(self):
  282. self.test_patch.stop()
  283. self.app.close()
  284. del self.app
  285. def get_driver(self, fs_type, accessible):
  286. qubes.tests.storage_reflink.mkdir_fs(self.test_dir, fs_type,
  287. accessible=accessible,
  288. cleanup_via=self.addCleanup)
  289. self.app.load_initial_values()
  290. varlibqubes = self.app.pools['varlibqubes']
  291. self.assertEqual(varlibqubes.dir_path, self.test_dir)
  292. return varlibqubes.driver
  293. def test_100_varlibqubes_btrfs_accessible(self):
  294. self.assertEqual(self.get_driver('btrfs', True), 'file-reflink')
  295. def test_101_varlibqubes_btrfs_inaccessible(self):
  296. self.assertEqual(self.get_driver('btrfs', False), 'file')
  297. def test_102_varlibqubes_ext4_accessible(self):
  298. self.assertEqual(self.get_driver('ext4', True), 'file')
  299. def test_103_varlibqubes_ext4_inaccessible(self):
  300. self.assertEqual(self.get_driver('ext4', False), 'file')
  301. class TC_89_QubesEmpty(qubes.tests.QubesTestCase):
  302. def tearDown(self):
  303. try:
  304. os.unlink('/tmp/qubestest.xml')
  305. except:
  306. pass
  307. try:
  308. self.app.close()
  309. del self.app
  310. except AttributeError:
  311. pass
  312. super().tearDown()
  313. @qubes.tests.skipUnlessDom0
  314. def test_000_init_empty(self):
  315. # pylint: disable=no-self-use,unused-variable,bare-except
  316. try:
  317. os.unlink('/tmp/qubestest.xml')
  318. except FileNotFoundError:
  319. pass
  320. qubes.Qubes.create_empty_store('/tmp/qubestest.xml').close()
  321. def test_100_property_migrate_default_fw_netvm(self):
  322. xml_template = """<?xml version="1.0" encoding="utf-8" ?>
  323. <qubes version="3.0">
  324. <properties>
  325. <property name="default_netvm">{default_netvm}</property>
  326. <property name="default_fw_netvm">{default_fw_netvm}</property>
  327. </properties>
  328. <labels>
  329. <label id="label-1" color="#cc0000">red</label>
  330. </labels>
  331. <pools>
  332. <pool driver="file" dir_path="/tmp/qubes-test" name="default"/>
  333. </pools>
  334. <domains>
  335. <domain class="StandaloneVM" id="domain-1">
  336. <properties>
  337. <property name="qid">1</property>
  338. <property name="name">sys-net</property>
  339. <property name="provides_network">True</property>
  340. <property name="label" ref="label-1" />
  341. <property name="netvm"></property>
  342. <property name="uuid">2fcfc1f4-b2fe-4361-931a-c5294b35edfa</property>
  343. </properties>
  344. <features/>
  345. <devices class="pci"/>
  346. </domain>
  347. <domain class="StandaloneVM" id="domain-2">
  348. <properties>
  349. <property name="qid">2</property>
  350. <property name="name">sys-firewall</property>
  351. <property name="provides_network">True</property>
  352. <property name="label" ref="label-1" />
  353. <property name="uuid">9a6d9689-25f7-48c9-a15f-8205d6c5b7c6</property>
  354. </properties>
  355. </domain>
  356. <domain class="StandaloneVM" id="domain-3">
  357. <properties>
  358. <property name="qid">3</property>
  359. <property name="name">appvm</property>
  360. <property name="label" ref="label-1" />
  361. <property name="uuid">1d6aab41-3262-400a-b3d3-21aae8fdbec8</property>
  362. </properties>
  363. </domain>
  364. </domains>
  365. </qubes>
  366. """
  367. with self.subTest('default_setup'):
  368. with open('/tmp/qubestest.xml', 'w') as xml_file:
  369. xml_file.write(xml_template.format(
  370. default_netvm='sys-firewall',
  371. default_fw_netvm='sys-net'))
  372. self.app = qubes.Qubes('/tmp/qubestest.xml', offline_mode=True)
  373. self.assertEqual(
  374. self.app.domains['sys-net'].netvm, None)
  375. self.assertEqual(
  376. self.app.domains['sys-firewall'].netvm,
  377. self.app.domains['sys-net'])
  378. # property is no longer "default"
  379. self.assertFalse(
  380. self.app.domains['sys-firewall'].property_is_default('netvm'))
  381. # verify that appvm.netvm is unaffected
  382. self.assertTrue(
  383. self.app.domains['appvm'].property_is_default('netvm'))
  384. self.assertEqual(
  385. self.app.domains['appvm'].netvm,
  386. self.app.domains['sys-firewall'])
  387. with self.assertRaises(AttributeError):
  388. self.app.default_fw_netvm
  389. self.app.close()
  390. del self.app
  391. with self.subTest('same'):
  392. with open('/tmp/qubestest.xml', 'w') as xml_file:
  393. xml_file.write(xml_template.format(
  394. default_netvm='sys-net',
  395. default_fw_netvm='sys-net'))
  396. self.app = qubes.Qubes('/tmp/qubestest.xml', offline_mode=True)
  397. self.assertEqual(
  398. self.app.domains['sys-net'].netvm, None)
  399. self.assertEqual(
  400. self.app.domains['sys-firewall'].netvm,
  401. self.app.domains['sys-net'])
  402. self.assertTrue(
  403. self.app.domains['sys-firewall'].property_is_default('netvm'))
  404. # verify that appvm.netvm is unaffected
  405. self.assertTrue(
  406. self.app.domains['appvm'].property_is_default('netvm'))
  407. self.assertEqual(
  408. self.app.domains['appvm'].netvm,
  409. self.app.domains['sys-net'])
  410. with self.assertRaises(AttributeError):
  411. self.app.default_fw_netvm
  412. self.app.close()
  413. del self.app
  414. with self.subTest('loop'):
  415. with open('/tmp/qubestest.xml', 'w') as xml_file:
  416. xml_file.write(xml_template.format(
  417. default_netvm='sys-firewall',
  418. default_fw_netvm='sys-firewall'))
  419. self.app = qubes.Qubes('/tmp/qubestest.xml', offline_mode=True)
  420. self.assertEqual(
  421. self.app.domains['sys-net'].netvm, None)
  422. # this was netvm loop, better set to none, to not crash qubesd
  423. self.assertEqual(
  424. self.app.domains['sys-firewall'].netvm, None)
  425. self.assertFalse(
  426. self.app.domains['sys-firewall'].property_is_default('netvm'))
  427. # verify that appvm.netvm is unaffected
  428. self.assertTrue(
  429. self.app.domains['appvm'].property_is_default('netvm'))
  430. self.assertEqual(
  431. self.app.domains['appvm'].netvm,
  432. self.app.domains['sys-firewall'])
  433. with self.assertRaises(AttributeError):
  434. self.app.default_fw_netvm
  435. self.app.close()
  436. del self.app
  437. def test_101_property_migrate_label(self):
  438. xml_template = """<?xml version="1.0" encoding="utf-8" ?>
  439. <qubes version="3.0">
  440. <labels>
  441. <label id="label-1" color="{old_gray}">gray</label>
  442. </labels>
  443. <pools>
  444. <pool driver="file" dir_path="/tmp/qubes-test" name="default"/>
  445. </pools>
  446. <domains>
  447. <domain class="StandaloneVM" id="domain-1">
  448. <properties>
  449. <property name="qid">1</property>
  450. <property name="name">sys-net</property>
  451. <property name="provides_network">True</property>
  452. <property name="label" ref="label-1" />
  453. <property name="netvm"></property>
  454. <property name="uuid">2fcfc1f4-b2fe-4361-931a-c5294b35edfa</property>
  455. </properties>
  456. <features/>
  457. <devices class="pci"/>
  458. </domain>
  459. </domains>
  460. </qubes>
  461. """
  462. with self.subTest('replace_label'):
  463. with open('/tmp/qubestest.xml', 'w') as xml_file:
  464. xml_file.write(xml_template.format(old_gray='0x555753'))
  465. self.app = qubes.Qubes('/tmp/qubestest.xml', offline_mode=True)
  466. self.assertEqual(
  467. self.app.get_label('gray').color, '0x555555')
  468. self.app.close()
  469. del self.app
  470. with self.subTest('dont_replace_label'):
  471. with open('/tmp/qubestest.xml', 'w') as xml_file:
  472. xml_file.write(xml_template.format(old_gray='0x123456'))
  473. self.app = qubes.Qubes('/tmp/qubestest.xml', offline_mode=True)
  474. self.assertEqual(
  475. self.app.get_label('gray').color, '0x123456')
  476. self.app.close()
  477. del self.app
  478. class TC_90_Qubes(qubes.tests.QubesTestCase):
  479. def tearDown(self):
  480. try:
  481. os.unlink('/tmp/qubestest.xml')
  482. except:
  483. pass
  484. super().tearDown()
  485. def setUp(self):
  486. super(TC_90_Qubes, self).setUp()
  487. self.app = qubes.Qubes('/tmp/qubestest.xml', load=False,
  488. offline_mode=True)
  489. self.addCleanup(self.cleanup_qubes)
  490. self.app.load_initial_values()
  491. self.template = self.app.add_new_vm('TemplateVM', name='test-template',
  492. label='green')
  493. def cleanup_qubes(self):
  494. self.app.close()
  495. del self.app
  496. try:
  497. del self.template
  498. except AttributeError:
  499. pass
  500. def test_100_clockvm(self):
  501. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  502. template=self.template,
  503. label='red')
  504. self.assertIsNone(self.app.clockvm)
  505. self.assertNotIn('service.clocksync', appvm.features)
  506. self.assertNotIn('service.clocksync', self.template.features)
  507. self.app.clockvm = appvm
  508. self.assertIn('service.clocksync', appvm.features)
  509. self.assertTrue(appvm.features['service.clocksync'])
  510. self.app.clockvm = self.template
  511. self.assertNotIn('service.clocksync', appvm.features)
  512. self.assertIn('service.clocksync', self.template.features)
  513. self.assertTrue(self.template.features['service.clocksync'])
  514. def test_110_netvm_loop(self):
  515. """Netvm loop through default_netvm"""
  516. netvm = self.app.add_new_vm('AppVM', name='test-net',
  517. template=self.template, label='red')
  518. try:
  519. self.app.default_netvm = None
  520. netvm.netvm = qubes.property.DEFAULT
  521. with self.assertRaises(ValueError):
  522. self.app.default_netvm = netvm
  523. finally:
  524. del netvm
  525. def test_111_netvm_loop(self):
  526. """Netvm loop through default_netvm"""
  527. netvm = self.app.add_new_vm('AppVM', name='test-net',
  528. template=self.template, label='red')
  529. try:
  530. netvm.netvm = None
  531. self.app.default_netvm = netvm
  532. with self.assertRaises(ValueError):
  533. netvm.netvm = qubes.property.DEFAULT
  534. finally:
  535. del netvm
  536. def test_112_default_guivm(self):
  537. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  538. default_guivm = qubes.property('default_guivm',
  539. default=(lambda self: 'dom0'))
  540. holder = MyTestHolder(None)
  541. guivm = self.app.add_new_vm('AppVM', name='sys-gui', guivm='dom0',
  542. template=self.template, label='red')
  543. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  544. template=self.template, label='red')
  545. holder.default_guivm = 'sys-gui'
  546. self.assertEqual(holder.default_guivm, 'sys-gui')
  547. self.assertIsNotNone(self.app.default_guivm)
  548. self.assertTrue(appvm.property_is_default('guivm'))
  549. self.app.default_guivm = guivm
  550. self.assertEventFired(holder, 'property-set:default_guivm',
  551. kwargs={'name': 'default_guivm',
  552. 'newvalue': 'sys-gui'})
  553. self.assertIn('guivm-sys-gui', appvm.tags)
  554. def test_113_guivm(self):
  555. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  556. guivm = qubes.property('guivm',
  557. default=(lambda self: 'dom0'))
  558. holder = MyTestHolder(None)
  559. guivm = self.app.add_new_vm('AppVM', name='sys-gui', guivm='dom0',
  560. template=self.template, label='red')
  561. vncvm = self.app.add_new_vm('AppVM', name='sys-vnc', guivm='dom0',
  562. template=self.template, label='red')
  563. appvm = self.app.add_new_vm('AppVM', name='test-vm', guivm='dom0',
  564. template=self.template, label='red')
  565. holder.guivm = 'sys-gui'
  566. self.assertEqual(holder.guivm, 'sys-gui')
  567. self.assertEventFired(holder, 'property-set:guivm',
  568. kwargs={'name': 'guivm',
  569. 'newvalue': 'sys-gui'})
  570. # Set GuiVM
  571. self.assertFalse(appvm.property_is_default('guivm'))
  572. appvm.guivm = guivm
  573. self.assertIn('guivm-sys-gui', appvm.tags)
  574. # Change GuiVM
  575. appvm.guivm = vncvm
  576. self.assertIn('guivm-sys-vnc', appvm.tags)
  577. self.assertNotIn('guivm-sys-gui', appvm.tags)
  578. # Empty GuiVM
  579. del appvm.guivm
  580. self.assertNotIn('guivm-sys-vnc', appvm.tags)
  581. self.assertNotIn('guivm-sys-gui', appvm.tags)
  582. self.assertNotIn('guivm-', appvm.tags)
  583. def test_114_default_audiovm(self):
  584. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  585. default_audiovm = qubes.property('default_audiovm',
  586. default=(lambda self: 'dom0'))
  587. holder = MyTestHolder(None)
  588. audiovm = self.app.add_new_vm('AppVM', name='sys-audio', audiovm='dom0',
  589. template=self.template, label='red')
  590. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  591. template=self.template, label='red')
  592. holder.default_audiovm = 'sys-audio'
  593. self.assertEqual(holder.default_audiovm, 'sys-audio')
  594. self.assertIsNotNone(self.app.default_audiovm)
  595. self.assertTrue(appvm.property_is_default('audiovm'))
  596. self.app.default_audiovm = audiovm
  597. self.assertEventFired(holder, 'property-set:default_audiovm',
  598. kwargs={'name': 'default_audiovm',
  599. 'newvalue': 'sys-audio'})
  600. self.assertIn('audiovm-sys-audio', appvm.tags)
  601. def test_115_audiovm(self):
  602. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  603. audiovm = qubes.property('audiovm',
  604. default=(lambda self: 'dom0'))
  605. holder = MyTestHolder(None)
  606. audiovm = self.app.add_new_vm('AppVM', name='sys-audio', audiovm='dom0',
  607. template=self.template, label='red')
  608. guivm = self.app.add_new_vm('AppVM', name='sys-gui', audiovm='dom0',
  609. template=self.template, label='red')
  610. appvm = self.app.add_new_vm('AppVM', name='test-vm', audiovm='dom0',
  611. template=self.template, label='red')
  612. holder.audiovm = 'sys-audio'
  613. self.assertEqual(holder.audiovm, 'sys-audio')
  614. self.assertEventFired(holder, 'property-set:audiovm',
  615. kwargs={'name': 'audiovm',
  616. 'newvalue': 'sys-audio'})
  617. # Set AudioVM
  618. self.assertFalse(appvm.property_is_default('audiovm'))
  619. appvm.audiovm = audiovm
  620. self.assertIn('audiovm-sys-audio', appvm.tags)
  621. # Change AudioVM
  622. appvm.audiovm = guivm
  623. self.assertIn('audiovm-sys-gui', appvm.tags)
  624. self.assertNotIn('audiovm-sys-audio', appvm.tags)
  625. # Empty AudioVM
  626. del appvm.audiovm
  627. self.assertNotIn('audiovm-sys-gui', appvm.tags)
  628. self.assertNotIn('audiovm-sys-audio', appvm.tags)
  629. self.assertNotIn('audiovm-', appvm.tags)
  630. def test_200_remove_template(self):
  631. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  632. template=self.template,
  633. label='red')
  634. with mock.patch.object(self.app, 'vmm'):
  635. with self.assertRaises(qubes.exc.QubesException):
  636. del self.app.domains[self.template]
  637. def test_201_remove_netvm(self):
  638. netvm = self.app.add_new_vm('AppVM', name='test-netvm',
  639. template=self.template,
  640. provides_network=True,
  641. label='red')
  642. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  643. template=self.template,
  644. label='red')
  645. appvm.netvm = netvm
  646. with mock.patch.object(self.app, 'vmm'):
  647. with self.assertRaises(qubes.exc.QubesVMInUseError):
  648. del self.app.domains[netvm]
  649. def test_202_remove_default_netvm(self):
  650. netvm = self.app.add_new_vm('AppVM', name='test-netvm',
  651. template=self.template,
  652. provides_network=True,
  653. label='red')
  654. netvm.netvm = None
  655. self.app.default_netvm = netvm
  656. with mock.patch.object(self.app, 'vmm'):
  657. with self.assertRaises(qubes.exc.QubesVMInUseError):
  658. del self.app.domains[netvm]
  659. def test_203_remove_default_dispvm(self):
  660. appvm = self.app.add_new_vm('AppVM', name='test-appvm',
  661. template=self.template,
  662. label='red')
  663. self.app.default_dispvm = appvm
  664. with mock.patch.object(self.app, 'vmm'):
  665. with self.assertRaises(qubes.exc.QubesVMInUseError):
  666. del self.app.domains[appvm]
  667. def test_204_remove_appvm_dispvm(self):
  668. dispvm = self.app.add_new_vm('AppVM', name='test-appvm',
  669. template=self.template,
  670. label='red')
  671. appvm = self.app.add_new_vm('AppVM', name='test-appvm2',
  672. template=self.template,
  673. default_dispvm=dispvm,
  674. label='red')
  675. with mock.patch.object(self.app, 'vmm'):
  676. with self.assertRaises(qubes.exc.QubesVMInUseError):
  677. del self.app.domains[dispvm]
  678. def test_205_remove_appvm_dispvm(self):
  679. appvm = self.app.add_new_vm('AppVM', name='test-appvm',
  680. template=self.template,
  681. template_for_dispvms=True,
  682. label='red')
  683. dispvm = self.app.add_new_vm('DispVM', name='test-dispvm',
  684. template=appvm,
  685. label='red')
  686. with mock.patch.object(self.app, 'vmm'):
  687. with self.assertRaises(qubes.exc.QubesVMInUseError):
  688. del self.app.domains[appvm]
  689. def test_206_remove_attached(self):
  690. # See also qubes.tests.api_admin.
  691. vm = self.app.add_new_vm(
  692. 'AppVM', name='test-vm', template=self.template, label='red')
  693. assignment = mock.Mock(ident='1234')
  694. vm.get_provided_assignments = lambda: [assignment]
  695. with self.assertRaises(qubes.exc.QubesVMInUseError):
  696. del self.app.domains[vm]
  697. @qubes.tests.skipUnlessGit
  698. def test_900_example_xml_in_doc(self):
  699. self.assertXMLIsValid(
  700. lxml.etree.parse(open(
  701. os.path.join(qubes.tests.in_git, 'doc/example.xml'), 'rb')),
  702. 'qubes.rng')