app.py 29 KB

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