app.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. class TC_90_Qubes(qubes.tests.QubesTestCase):
  438. def tearDown(self):
  439. try:
  440. os.unlink('/tmp/qubestest.xml')
  441. except:
  442. pass
  443. super().tearDown()
  444. def setUp(self):
  445. super(TC_90_Qubes, self).setUp()
  446. self.app = qubes.Qubes('/tmp/qubestest.xml', load=False,
  447. offline_mode=True)
  448. self.addCleanup(self.cleanup_qubes)
  449. self.app.load_initial_values()
  450. self.template = self.app.add_new_vm('TemplateVM', name='test-template',
  451. label='green')
  452. def cleanup_qubes(self):
  453. self.app.close()
  454. del self.app
  455. try:
  456. del self.template
  457. except AttributeError:
  458. pass
  459. def test_100_clockvm(self):
  460. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  461. template=self.template,
  462. label='red')
  463. self.assertIsNone(self.app.clockvm)
  464. self.assertNotIn('service.clocksync', appvm.features)
  465. self.assertNotIn('service.clocksync', self.template.features)
  466. self.app.clockvm = appvm
  467. self.assertIn('service.clocksync', appvm.features)
  468. self.assertTrue(appvm.features['service.clocksync'])
  469. self.app.clockvm = self.template
  470. self.assertNotIn('service.clocksync', appvm.features)
  471. self.assertIn('service.clocksync', self.template.features)
  472. self.assertTrue(self.template.features['service.clocksync'])
  473. def test_110_netvm_loop(self):
  474. """Netvm loop through default_netvm"""
  475. netvm = self.app.add_new_vm('AppVM', name='test-net',
  476. template=self.template, label='red')
  477. try:
  478. self.app.default_netvm = None
  479. netvm.netvm = qubes.property.DEFAULT
  480. with self.assertRaises(ValueError):
  481. self.app.default_netvm = netvm
  482. finally:
  483. del netvm
  484. def test_111_netvm_loop(self):
  485. """Netvm loop through default_netvm"""
  486. netvm = self.app.add_new_vm('AppVM', name='test-net',
  487. template=self.template, label='red')
  488. try:
  489. netvm.netvm = None
  490. self.app.default_netvm = netvm
  491. with self.assertRaises(ValueError):
  492. netvm.netvm = qubes.property.DEFAULT
  493. finally:
  494. del netvm
  495. def test_112_default_guivm(self):
  496. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  497. default_guivm = qubes.property('default_guivm',
  498. default=(lambda self: 'dom0'))
  499. holder = MyTestHolder(None)
  500. guivm = self.app.add_new_vm('AppVM', name='sys-gui', guivm='dom0',
  501. template=self.template, label='red')
  502. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  503. template=self.template, label='red')
  504. holder.default_guivm = 'sys-gui'
  505. self.assertEqual(holder.default_guivm, 'sys-gui')
  506. self.assertIsNotNone(self.app.default_guivm)
  507. self.assertTrue(appvm.property_is_default('guivm'))
  508. self.app.default_guivm = guivm
  509. self.assertEventFired(holder, 'property-set:default_guivm',
  510. kwargs={'name': 'default_guivm',
  511. 'newvalue': 'sys-gui'})
  512. self.assertIn('guivm-sys-gui', appvm.tags)
  513. def test_113_guivm(self):
  514. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  515. guivm = qubes.property('guivm',
  516. default=(lambda self: 'dom0'))
  517. holder = MyTestHolder(None)
  518. guivm = self.app.add_new_vm('AppVM', name='sys-gui', guivm='dom0',
  519. template=self.template, label='red')
  520. vncvm = self.app.add_new_vm('AppVM', name='sys-vnc', guivm='dom0',
  521. template=self.template, label='red')
  522. appvm = self.app.add_new_vm('AppVM', name='test-vm', guivm='dom0',
  523. template=self.template, label='red')
  524. holder.guivm = 'sys-gui'
  525. self.assertEqual(holder.guivm, 'sys-gui')
  526. self.assertEventFired(holder, 'property-set:guivm',
  527. kwargs={'name': 'guivm',
  528. 'newvalue': 'sys-gui'})
  529. # Set GuiVM
  530. self.assertFalse(appvm.property_is_default('guivm'))
  531. appvm.guivm = guivm
  532. self.assertIn('guivm-sys-gui', appvm.tags)
  533. # Change GuiVM
  534. appvm.guivm = vncvm
  535. self.assertIn('guivm-sys-vnc', appvm.tags)
  536. self.assertNotIn('guivm-sys-gui', appvm.tags)
  537. # Empty GuiVM
  538. del appvm.guivm
  539. self.assertNotIn('guivm-sys-vnc', appvm.tags)
  540. self.assertNotIn('guivm-sys-gui', appvm.tags)
  541. self.assertNotIn('guivm-', appvm.tags)
  542. def test_114_default_audiovm(self):
  543. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  544. default_audiovm = qubes.property('default_audiovm',
  545. default=(lambda self: 'dom0'))
  546. holder = MyTestHolder(None)
  547. audiovm = self.app.add_new_vm('AppVM', name='sys-audio', audiovm='dom0',
  548. template=self.template, label='red')
  549. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  550. template=self.template, label='red')
  551. holder.default_audiovm = 'sys-audio'
  552. self.assertEqual(holder.default_audiovm, 'sys-audio')
  553. self.assertIsNotNone(self.app.default_audiovm)
  554. self.assertTrue(appvm.property_is_default('audiovm'))
  555. self.app.default_audiovm = audiovm
  556. self.assertEventFired(holder, 'property-set:default_audiovm',
  557. kwargs={'name': 'default_audiovm',
  558. 'newvalue': 'sys-audio'})
  559. self.assertIn('audiovm-sys-audio', appvm.tags)
  560. def test_115_audiovm(self):
  561. class MyTestHolder(qubes.tests.TestEmitter, qubes.PropertyHolder):
  562. audiovm = qubes.property('audiovm',
  563. default=(lambda self: 'dom0'))
  564. holder = MyTestHolder(None)
  565. audiovm = self.app.add_new_vm('AppVM', name='sys-audio', audiovm='dom0',
  566. template=self.template, label='red')
  567. guivm = self.app.add_new_vm('AppVM', name='sys-gui', audiovm='dom0',
  568. template=self.template, label='red')
  569. appvm = self.app.add_new_vm('AppVM', name='test-vm', audiovm='dom0',
  570. template=self.template, label='red')
  571. holder.audiovm = 'sys-audio'
  572. self.assertEqual(holder.audiovm, 'sys-audio')
  573. self.assertEventFired(holder, 'property-set:audiovm',
  574. kwargs={'name': 'audiovm',
  575. 'newvalue': 'sys-audio'})
  576. # Set AudioVM
  577. self.assertFalse(appvm.property_is_default('audiovm'))
  578. appvm.audiovm = audiovm
  579. self.assertIn('audiovm-sys-audio', appvm.tags)
  580. # Change AudioVM
  581. appvm.audiovm = guivm
  582. self.assertIn('audiovm-sys-gui', appvm.tags)
  583. self.assertNotIn('audiovm-sys-audio', appvm.tags)
  584. # Empty AudioVM
  585. del appvm.audiovm
  586. self.assertNotIn('audiovm-sys-gui', appvm.tags)
  587. self.assertNotIn('audiovm-sys-audio', appvm.tags)
  588. self.assertNotIn('audiovm-', appvm.tags)
  589. def test_200_remove_template(self):
  590. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  591. template=self.template,
  592. label='red')
  593. with mock.patch.object(self.app, 'vmm'):
  594. with self.assertRaises(qubes.exc.QubesException):
  595. del self.app.domains[self.template]
  596. def test_201_remove_netvm(self):
  597. netvm = self.app.add_new_vm('AppVM', name='test-netvm',
  598. template=self.template,
  599. provides_network=True,
  600. label='red')
  601. appvm = self.app.add_new_vm('AppVM', name='test-vm',
  602. template=self.template,
  603. label='red')
  604. appvm.netvm = netvm
  605. with mock.patch.object(self.app, 'vmm'):
  606. with self.assertRaises(qubes.exc.QubesVMInUseError):
  607. del self.app.domains[netvm]
  608. def test_202_remove_default_netvm(self):
  609. netvm = self.app.add_new_vm('AppVM', name='test-netvm',
  610. template=self.template,
  611. provides_network=True,
  612. label='red')
  613. netvm.netvm = None
  614. self.app.default_netvm = netvm
  615. with mock.patch.object(self.app, 'vmm'):
  616. with self.assertRaises(qubes.exc.QubesVMInUseError):
  617. del self.app.domains[netvm]
  618. def test_203_remove_default_dispvm(self):
  619. appvm = self.app.add_new_vm('AppVM', name='test-appvm',
  620. template=self.template,
  621. label='red')
  622. self.app.default_dispvm = appvm
  623. with mock.patch.object(self.app, 'vmm'):
  624. with self.assertRaises(qubes.exc.QubesVMInUseError):
  625. del self.app.domains[appvm]
  626. def test_204_remove_appvm_dispvm(self):
  627. dispvm = self.app.add_new_vm('AppVM', name='test-appvm',
  628. template=self.template,
  629. label='red')
  630. appvm = self.app.add_new_vm('AppVM', name='test-appvm2',
  631. template=self.template,
  632. default_dispvm=dispvm,
  633. label='red')
  634. with mock.patch.object(self.app, 'vmm'):
  635. with self.assertRaises(qubes.exc.QubesVMInUseError):
  636. del self.app.domains[dispvm]
  637. def test_205_remove_appvm_dispvm(self):
  638. appvm = self.app.add_new_vm('AppVM', name='test-appvm',
  639. template=self.template,
  640. template_for_dispvms=True,
  641. label='red')
  642. dispvm = self.app.add_new_vm('DispVM', name='test-dispvm',
  643. template=appvm,
  644. label='red')
  645. with mock.patch.object(self.app, 'vmm'):
  646. with self.assertRaises(qubes.exc.QubesVMInUseError):
  647. del self.app.domains[appvm]
  648. def test_206_remove_attached(self):
  649. # See also qubes.tests.api_admin.
  650. vm = self.app.add_new_vm(
  651. 'AppVM', name='test-vm', template=self.template, label='red')
  652. assignment = mock.Mock(ident='1234')
  653. vm.get_provided_assignments = lambda: [assignment]
  654. with self.assertRaises(qubes.exc.QubesVMInUseError):
  655. del self.app.domains[vm]
  656. @qubes.tests.skipUnlessGit
  657. def test_900_example_xml_in_doc(self):
  658. self.assertXMLIsValid(
  659. lxml.etree.parse(open(
  660. os.path.join(qubes.tests.in_git, 'doc/example.xml'), 'rb')),
  661. 'qubes.rng')