qubesvm.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. # pylint: disable=protected-access
  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 program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 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 General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. import os
  23. import unittest
  24. import uuid
  25. import datetime
  26. import lxml.etree
  27. import unittest.mock
  28. import qubes
  29. import qubes.exc
  30. import qubes.config
  31. import qubes.vm
  32. import qubes.vm.qubesvm
  33. import qubes.tests
  34. import qubes.tests.vm
  35. class TestApp(object):
  36. labels = {1: qubes.Label(1, '0xcc0000', 'red')}
  37. def __init__(self):
  38. self.domains = {}
  39. class TestProp(object):
  40. # pylint: disable=too-few-public-methods
  41. __name__ = 'testprop'
  42. class TestVM(object):
  43. # pylint: disable=too-few-public-methods
  44. app = TestApp()
  45. def __init__(self, **kwargs):
  46. self.running = False
  47. self.installed_by_rpm = False
  48. for k, v in kwargs.items():
  49. setattr(self, k, v)
  50. def is_running(self):
  51. return self.running
  52. class TC_00_setters(qubes.tests.QubesTestCase):
  53. def setUp(self):
  54. super().setUp()
  55. self.vm = TestVM()
  56. self.prop = TestProp()
  57. def test_000_setter_qid(self):
  58. self.assertEqual(
  59. qubes.vm.qubesvm._setter_qid(self.vm, self.prop, 5), 5)
  60. def test_001_setter_qid_lt_0(self):
  61. with self.assertRaises(ValueError):
  62. qubes.vm.qubesvm._setter_qid(self.vm, self.prop, -1)
  63. def test_002_setter_qid_gt_max(self):
  64. with self.assertRaises(ValueError):
  65. qubes.vm.qubesvm._setter_qid(self.vm,
  66. self.prop, qubes.config.max_qid + 5)
  67. @unittest.skip('test not implemented')
  68. def test_020_setter_kernel(self):
  69. pass
  70. def test_030_setter_label_object(self):
  71. label = TestApp.labels[1]
  72. self.assertIs(label,
  73. qubes.vm.setter_label(self.vm, self.prop, label))
  74. def test_031_setter_label_getitem(self):
  75. label = TestApp.labels[1]
  76. self.assertIs(label,
  77. qubes.vm.setter_label(self.vm, self.prop, 'label-1'))
  78. # there is no check for self.app.get_label()
  79. def test_040_setter_virt_mode(self):
  80. self.assertEqual(
  81. qubes.vm.qubesvm._setter_virt_mode(self.vm, self.prop, 'hvm'),
  82. 'hvm')
  83. self.assertEqual(
  84. qubes.vm.qubesvm._setter_virt_mode(self.vm, self.prop, 'HVM'),
  85. 'hvm')
  86. self.assertEqual(
  87. qubes.vm.qubesvm._setter_virt_mode(self.vm, self.prop, 'PV'),
  88. 'pv')
  89. with self.assertRaises(ValueError):
  90. qubes.vm.qubesvm._setter_virt_mode(self.vm, self.prop, 'True')
  91. class QubesVMTestsMixin(object):
  92. property_no_default = object()
  93. def setUp(self):
  94. super(QubesVMTestsMixin, self).setUp()
  95. self.app = qubes.tests.vm.TestApp()
  96. self.app.vmm.offline_mode = True
  97. def get_vm(self, **kwargs):
  98. return qubes.vm.qubesvm.QubesVM(self.app, None,
  99. qid=1, name=qubes.tests.VMPREFIX + 'test',
  100. **kwargs)
  101. def assertPropertyValue(self, vm, prop_name, set_value, expected_value,
  102. expected_xml_content=None):
  103. # FIXME: any better exception list? or maybe all of that should be a
  104. # single exception?
  105. with self.assertNotRaises((ValueError, TypeError, KeyError)):
  106. setattr(vm, prop_name, set_value)
  107. self.assertEqual(getattr(vm, prop_name), expected_value)
  108. if expected_xml_content is not None:
  109. xml = vm.__xml__()
  110. prop_xml = xml.xpath(
  111. './properties/property[@name=\'{}\']'.format(prop_name))
  112. self.assertEqual(len(prop_xml), 1, "Property not found in XML")
  113. self.assertEqual(prop_xml[0].text, expected_xml_content)
  114. def assertPropertyInvalidValue(self, vm, prop_name, set_value):
  115. orig_value_set = True
  116. orig_value = None
  117. try:
  118. orig_value = getattr(vm, prop_name)
  119. except AttributeError:
  120. orig_value_set = False
  121. # FIXME: any better exception list? or maybe all of that should be a
  122. # single exception?
  123. with self.assertRaises((ValueError, TypeError, KeyError)):
  124. setattr(vm, prop_name, set_value)
  125. if orig_value_set:
  126. self.assertEqual(getattr(vm, prop_name), orig_value)
  127. else:
  128. with self.assertRaises(AttributeError):
  129. getattr(vm, prop_name)
  130. def assertPropertyDefaultValue(self, vm, prop_name,
  131. expected_default=property_no_default):
  132. if expected_default is self.property_no_default:
  133. with self.assertRaises(AttributeError):
  134. getattr(vm, prop_name)
  135. else:
  136. with self.assertNotRaises(AttributeError):
  137. self.assertEqual(getattr(vm, prop_name), expected_default)
  138. xml = vm.__xml__()
  139. prop_xml = xml.xpath(
  140. './properties/property[@name=\'{}\']'.format(prop_name))
  141. self.assertEqual(len(prop_xml), 0, "Property still found in XML")
  142. def _test_generic_bool_property(self, vm, prop_name, default=False):
  143. self.assertPropertyDefaultValue(vm, prop_name, default)
  144. self.assertPropertyValue(vm, prop_name, False, False, 'False')
  145. self.assertPropertyValue(vm, prop_name, True, True, 'True')
  146. delattr(vm, prop_name)
  147. self.assertPropertyDefaultValue(vm, prop_name, default)
  148. self.assertPropertyValue(vm, prop_name, 'True', True, 'True')
  149. self.assertPropertyValue(vm, prop_name, 'False', False, 'False')
  150. self.assertPropertyInvalidValue(vm, prop_name, 'xxx')
  151. self.assertPropertyValue(vm, prop_name, 123, True)
  152. self.assertPropertyInvalidValue(vm, prop_name, '')
  153. class TC_90_QubesVM(QubesVMTestsMixin, qubes.tests.QubesTestCase):
  154. def test_000_init(self):
  155. self.get_vm()
  156. def test_001_init_no_qid_or_name(self):
  157. with self.assertRaises(AssertionError):
  158. qubes.vm.qubesvm.QubesVM(self.app, None,
  159. name=qubes.tests.VMPREFIX + 'test')
  160. with self.assertRaises(AssertionError):
  161. qubes.vm.qubesvm.QubesVM(self.app, None,
  162. qid=1)
  163. def test_003_init_fire_domain_init(self):
  164. class TestVM2(qubes.vm.qubesvm.QubesVM):
  165. event_fired = False
  166. @qubes.events.handler('domain-init')
  167. def on_domain_init(self, event): # pylint: disable=unused-argument
  168. self.__class__.event_fired = True
  169. TestVM2(self.app, None, qid=1, name=qubes.tests.VMPREFIX + 'test')
  170. self.assertTrue(TestVM2.event_fired)
  171. def test_004_uuid_autogen(self):
  172. vm = self.get_vm()
  173. self.assertTrue(hasattr(vm, 'uuid'))
  174. def test_100_qid(self):
  175. vm = self.get_vm()
  176. self.assertIsInstance(vm.qid, int)
  177. with self.assertRaises(AttributeError):
  178. vm.qid = 2
  179. def test_110_name(self):
  180. vm = self.get_vm()
  181. self.assertIsInstance(vm.name, str)
  182. def test_120_uuid(self):
  183. my_uuid = uuid.uuid4()
  184. vm = self.get_vm(uuid=my_uuid)
  185. self.assertIsInstance(vm.uuid, uuid.UUID)
  186. self.assertIs(vm.uuid, my_uuid)
  187. with self.assertRaises(AttributeError):
  188. vm.uuid = uuid.uuid4()
  189. @unittest.skip('TODO: how to not fail on making an icon symlink here?')
  190. def test_130_label(self):
  191. vm = self.get_vm()
  192. self.assertPropertyDefaultValue(vm, 'label')
  193. self.assertPropertyValue(vm, 'label', self.app.labels[1],
  194. self.app.labels[1], 'label-1')
  195. del vm.label
  196. self.assertPropertyDefaultValue(vm, 'label')
  197. self.assertPropertyValue(vm, 'label', 'red',
  198. self.app.labels[1], 'label-1')
  199. self.assertPropertyValue(vm, 'label', 'label-1',
  200. self.app.labels[1], 'label-1')
  201. def test_131_label_invalid(self):
  202. vm = self.get_vm()
  203. self.assertPropertyInvalidValue(vm, 'label', 'invalid')
  204. self.assertPropertyInvalidValue(vm, 'label', 123)
  205. def test_160_memory(self):
  206. vm = self.get_vm()
  207. self.assertPropertyDefaultValue(vm, 'memory', 400)
  208. self.assertPropertyValue(vm, 'memory', 500, 500, '500')
  209. del vm.memory
  210. self.assertPropertyDefaultValue(vm, 'memory', 400)
  211. self.assertPropertyValue(vm, 'memory', '500', 500, '500')
  212. def test_161_memory_invalid(self):
  213. vm = self.get_vm()
  214. self.assertPropertyInvalidValue(vm, 'memory', -100)
  215. self.assertPropertyInvalidValue(vm, 'memory', '-100')
  216. self.assertPropertyInvalidValue(vm, 'memory', '')
  217. # TODO: higher than maxmem
  218. # TODO: human readable setter (500M, 4G)?
  219. def test_170_maxmem(self):
  220. vm = self.get_vm()
  221. self.assertPropertyDefaultValue(vm, 'maxmem',
  222. self.app.host.memory_total / 1024 / 2)
  223. self.assertPropertyValue(vm, 'maxmem', 500, 500, '500')
  224. del vm.maxmem
  225. self.assertPropertyDefaultValue(vm, 'maxmem',
  226. self.app.host.memory_total / 1024 / 2)
  227. self.assertPropertyValue(vm, 'maxmem', '500', 500, '500')
  228. def test_171_maxmem_invalid(self):
  229. vm = self.get_vm()
  230. self.assertPropertyInvalidValue(vm, 'maxmem', -100)
  231. self.assertPropertyInvalidValue(vm, 'maxmem', '-100')
  232. self.assertPropertyInvalidValue(vm, 'maxmem', '')
  233. # TODO: lower than memory
  234. # TODO: human readable setter (500M, 4G)?
  235. def test_190_vcpus(self):
  236. vm = self.get_vm()
  237. self.assertPropertyDefaultValue(vm, 'vcpus', 2)
  238. self.assertPropertyValue(vm, 'vcpus', 3, 3, '3')
  239. del vm.vcpus
  240. self.assertPropertyDefaultValue(vm, 'vcpus', 2)
  241. self.assertPropertyValue(vm, 'vcpus', '3', 3, '3')
  242. def test_191_vcpus_invalid(self):
  243. vm = self.get_vm()
  244. self.assertPropertyInvalidValue(vm, 'vcpus', 0)
  245. self.assertPropertyInvalidValue(vm, 'vcpus', -2)
  246. self.assertPropertyInvalidValue(vm, 'vcpus', '-2')
  247. self.assertPropertyInvalidValue(vm, 'vcpus', '')
  248. def test_200_debug(self):
  249. vm = self.get_vm()
  250. self._test_generic_bool_property(vm, 'debug', False)
  251. def test_210_installed_by_rpm(self):
  252. vm = self.get_vm()
  253. self._test_generic_bool_property(vm, 'installed_by_rpm', False)
  254. def test_220_include_in_backups(self):
  255. vm = self.get_vm()
  256. self._test_generic_bool_property(vm, 'include_in_backups', True)
  257. @qubes.tests.skipUnlessDom0
  258. def test_250_kernel(self):
  259. kernels = os.listdir(os.path.join(
  260. qubes.config.qubes_base_dir,
  261. qubes.config.system_path['qubes_kernels_base_dir']))
  262. if not len(kernels):
  263. self.skipTest('Needs at least one kernel installed')
  264. self.app.default_kernel = kernels[0]
  265. vm = self.get_vm()
  266. self.assertPropertyDefaultValue(vm, 'kernel', kernels[0])
  267. self.assertPropertyValue(vm, 'kernel', kernels[-1], kernels[-1],
  268. kernels[-1])
  269. del vm.kernel
  270. self.assertPropertyDefaultValue(vm, 'kernel', kernels[0])
  271. @qubes.tests.skipUnlessDom0
  272. def test_251_kernel_invalid(self):
  273. vm = self.get_vm()
  274. self.assertPropertyInvalidValue(vm, 'kernel', 123)
  275. self.assertPropertyInvalidValue(vm, 'kernel', 'invalid')
  276. def test_252_kernel_empty(self):
  277. vm = self.get_vm()
  278. self.assertPropertyValue(vm, 'kernel', '', '', '')
  279. self.assertPropertyValue(vm, 'kernel', None, '', '')
  280. def test_260_kernelopts(self):
  281. vm = self.get_vm()
  282. self.assertPropertyDefaultValue(vm, 'kernelopts',
  283. qubes.config.defaults['kernelopts'])
  284. self.assertPropertyValue(vm, 'kernelopts', 'some options',
  285. 'some options', 'some options')
  286. del vm.kernelopts
  287. self.assertPropertyDefaultValue(vm, 'kernelopts',
  288. qubes.config.defaults['kernelopts'])
  289. self.assertPropertyValue(vm, 'kernelopts', '',
  290. '', '')
  291. # TODO?
  292. # self.assertPropertyInvalidValue(vm, 'kernelopts', None),
  293. @unittest.skip('test not implemented')
  294. def test_261_kernelopts_pcidevs(self):
  295. vm = self.get_vm()
  296. # how to do that here? use dummy DeviceManager/DeviceCollection?
  297. # Disable events?
  298. vm.devices['pci'].attach('something')
  299. self.assertPropertyDefaultValue(vm, 'kernelopts',
  300. qubes.config.defaults['kernelopts_pcidevs'])
  301. def test_270_qrexec_timeout(self):
  302. vm = self.get_vm()
  303. self.assertPropertyDefaultValue(vm, 'qrexec_timeout', 60)
  304. self.assertPropertyValue(vm, 'qrexec_timeout', 3, 3, '3')
  305. del vm.qrexec_timeout
  306. self.assertPropertyDefaultValue(vm, 'qrexec_timeout', 60)
  307. self.assertPropertyValue(vm, 'qrexec_timeout', '3', 3, '3')
  308. def test_271_qrexec_timeout_invalid(self):
  309. vm = self.get_vm()
  310. self.assertPropertyInvalidValue(vm, 'qrexec_timeout', -2)
  311. self.assertPropertyInvalidValue(vm, 'qrexec_timeout', '-2')
  312. self.assertPropertyInvalidValue(vm, 'qrexec_timeout', '')
  313. def test_280_autostart(self):
  314. vm = self.get_vm()
  315. # FIXME any better idea to not involve systemctl call at this stage?
  316. vm.events_enabled = False
  317. self._test_generic_bool_property(vm, 'autostart', False)
  318. @qubes.tests.skipUnlessDom0
  319. def test_281_autostart_systemd(self):
  320. vm = self.get_vm()
  321. self.assertFalse(os.path.exists(
  322. '/etc/systemd/system/multi-user.target.wants/'
  323. 'qubes-vm@{}.service'.format(vm.name)),
  324. "systemd service enabled before setting autostart")
  325. vm.autostart = True
  326. self.assertTrue(os.path.exists(
  327. '/etc/systemd/system/multi-user.target.wants/'
  328. 'qubes-vm@{}.service'.format(vm.name)),
  329. "systemd service not enabled by autostart=True")
  330. vm.autostart = False
  331. self.assertFalse(os.path.exists(
  332. '/etc/systemd/system/multi-user.target.wants/'
  333. 'qubes-vm@{}.service'.format(vm.name)),
  334. "systemd service not disabled by autostart=False")
  335. vm.autostart = True
  336. del vm.autostart
  337. self.assertFalse(os.path.exists(
  338. '/etc/systemd/system/multi-user.target.wants/'
  339. 'qubes-vm@{}.service'.format(vm.name)),
  340. "systemd service not disabled by resetting autostart")
  341. @unittest.skip('TODO')
  342. def test_320_seamless_gui_mode(self):
  343. vm = self.get_vm()
  344. self._test_generic_bool_property(vm, 'seamless_gui_mode')
  345. # TODO: reject setting to True when guiagent_installed is false
  346. def test_330_mac(self):
  347. vm = self.get_vm()
  348. # TODO: calculate proper default here
  349. default_mac = vm.mac
  350. self.assertIsNotNone(default_mac)
  351. self.assertPropertyDefaultValue(vm, 'mac', default_mac)
  352. self.assertPropertyValue(vm, 'mac', '00:11:22:33:44:55',
  353. '00:11:22:33:44:55', '00:11:22:33:44:55')
  354. del vm.mac
  355. self.assertPropertyDefaultValue(vm, 'mac', default_mac)
  356. def test_331_mac_invalid(self):
  357. vm = self.get_vm()
  358. self.assertPropertyInvalidValue(vm, 'mac', 123)
  359. self.assertPropertyInvalidValue(vm, 'mac', 'invalid')
  360. self.assertPropertyInvalidValue(vm, 'mac', '00:11:22:33:44:55:66')
  361. def test_340_default_user(self):
  362. vm = self.get_vm()
  363. self.assertPropertyDefaultValue(vm, 'default_user', 'user')
  364. self.assertPropertyValue(vm, 'default_user', 'someuser', 'someuser',
  365. 'someuser')
  366. del vm.default_user
  367. self.assertPropertyDefaultValue(vm, 'default_user', 'user')
  368. self.assertPropertyValue(vm, 'default_user', 123, '123', '123')
  369. # TODO: check propagation for template-based VMs
  370. @unittest.skip('TODO')
  371. def test_350_timezone(self):
  372. vm = self.get_vm()
  373. self.assertPropertyDefaultValue(vm, 'timezone', 'localtime')
  374. self.assertPropertyValue(vm, 'timezone', 0, 0, '0')
  375. del vm.timezone
  376. self.assertPropertyDefaultValue(vm, 'timezone', 'localtime')
  377. self.assertPropertyValue(vm, 'timezone', '0', 0, '0')
  378. self.assertPropertyValue(vm, 'timezone', -3600, -3600, '-3600')
  379. self.assertPropertyValue(vm, 'timezone', 7200, 7200, '7200')
  380. @unittest.skip('TODO')
  381. def test_350_timezone_invalid(self):
  382. vm = self.get_vm()
  383. self.assertPropertyInvalidValue(vm, 'timezone', 'xxx')
  384. @unittest.skip('TODO')
  385. def test_360_drive(self):
  386. vm = self.get_vm()
  387. self.assertPropertyDefaultValue(vm, 'drive', None)
  388. # self.execute_tests('drive', [
  389. # ('hd:dom0:/tmp/drive.img', 'hd:dom0:/tmp/drive.img', True),
  390. # ('hd:/tmp/drive.img', 'hd:dom0:/tmp/drive.img', True),
  391. # ('cdrom:dom0:/tmp/drive.img', 'cdrom:dom0:/tmp/drive.img', True),
  392. # ('cdrom:/tmp/drive.img', 'cdrom:dom0:/tmp/drive.img', True),
  393. # ('/tmp/drive.img', 'cdrom:dom0:/tmp/drive.img', True),
  394. # ('hd:drive.img', '', False),
  395. # ('drive.img', '', False),
  396. # ])
  397. def test_400_backup_timestamp(self):
  398. vm = self.get_vm()
  399. timestamp = datetime.datetime(2016, 1, 1, 12, 14, 2)
  400. timestamp_str = timestamp.strftime('%s')
  401. self.assertPropertyDefaultValue(vm, 'backup_timestamp', None)
  402. self.assertPropertyValue(vm, 'backup_timestamp', timestamp,
  403. timestamp, timestamp_str)
  404. del vm.backup_timestamp
  405. self.assertPropertyDefaultValue(vm, 'backup_timestamp', None)
  406. self.assertPropertyValue(vm, 'backup_timestamp', timestamp_str,
  407. timestamp)
  408. def test_401_backup_timestamp_invalid(self):
  409. vm = self.get_vm()
  410. self.assertPropertyInvalidValue(vm, 'backup_timestamp', 'xxx')
  411. self.assertPropertyInvalidValue(vm, 'backup_timestamp', None)
  412. def test_500_property_migrate_virt_mode(self):
  413. xml_template = '''
  414. <domain class="QubesVM" id="domain-1">
  415. <properties>
  416. <property name="qid">1</property>
  417. <property name="name">testvm</property>
  418. <property name="label" ref="label-1" />
  419. <property name="hvm">{hvm_value}</property>
  420. </properties>
  421. </domain>
  422. '''
  423. xml = lxml.etree.XML(xml_template.format(hvm_value='True'))
  424. vm = qubes.vm.qubesvm.QubesVM(self.app, xml)
  425. self.assertEqual(vm.virt_mode, 'hvm')
  426. with self.assertRaises(AttributeError):
  427. vm.hvm
  428. xml = lxml.etree.XML(xml_template.format(hvm_value='False'))
  429. vm = qubes.vm.qubesvm.QubesVM(self.app, xml)
  430. self.assertEqual(vm.virt_mode, 'pv')
  431. with self.assertRaises(AttributeError):
  432. vm.hvm
  433. def test_600_libvirt_xml_pv(self):
  434. expected = '''<domain type="xen">
  435. <name>test-inst-test</name>
  436. <uuid>7db78950-c467-4863-94d1-af59806384ea</uuid>
  437. <memory unit="MiB">500</memory>
  438. <currentMemory unit="MiB">400</currentMemory>
  439. <vcpu placement="static">2</vcpu>
  440. <os>
  441. <type arch="x86_64" machine="xenpv">linux</type>
  442. <kernel>/tmp/kernel/vmlinuz</kernel>
  443. <initrd>/tmp/kernel/initramfs</initrd>
  444. <cmdline>root=/dev/mapper/dmroot ro nomodeset console=hvc0 rd_NO_PLYMOUTH rd.plymouth.enable=0 plymouth.enable=0 nopat</cmdline>
  445. </os>
  446. <features>
  447. </features>
  448. <clock offset='utc' adjustment='reset'>
  449. <timer name="tsc" mode="native"/>
  450. </clock>
  451. <on_poweroff>destroy</on_poweroff>
  452. <on_reboot>destroy</on_reboot>
  453. <on_crash>destroy</on_crash>
  454. <devices>
  455. <disk type="block" device="disk">
  456. <driver name="phy" />
  457. <source dev="/tmp/kernel/modules.img" />
  458. <target dev="xvdd" />
  459. <backenddomain name="dom0" />
  460. </disk>
  461. <console type="pty">
  462. <target type="xen" port="0"/>
  463. </console>
  464. </devices>
  465. </domain>
  466. '''
  467. my_uuid = '7db78950-c467-4863-94d1-af59806384ea'
  468. vm = self.get_vm(uuid=my_uuid)
  469. vm.netvm = None
  470. vm.virt_mode = 'pv'
  471. # tests for storage are later
  472. vm.volumes['kernel'] = unittest.mock.Mock(**{
  473. 'kernels_dir': '/tmp/kernel',
  474. 'block_device.return_value.domain': 'dom0',
  475. 'block_device.return_value.script': None,
  476. 'block_device.return_value.path': '/tmp/kernel/modules.img',
  477. 'block_device.return_value.devtype': 'disk',
  478. 'block_device.return_value.name': 'kernel',
  479. })
  480. libvirt_xml = vm.create_config_file()
  481. self.assertXMLEqual(lxml.etree.XML(libvirt_xml),
  482. lxml.etree.XML(expected))
  483. def test_600_libvirt_xml_hvm(self):
  484. expected = '''<domain type="xen">
  485. <name>test-inst-test</name>
  486. <uuid>7db78950-c467-4863-94d1-af59806384ea</uuid>
  487. <memory unit="MiB">500</memory>
  488. <currentMemory unit="MiB">400</currentMemory>
  489. <vcpu placement="static">2</vcpu>
  490. <cpu mode='host-passthrough'>
  491. <!-- disable nested HVM -->
  492. <feature name='vmx' policy='disable'/>
  493. <feature name='svm' policy='disable'/>
  494. <!-- disable SMAP inside VM, because of Linux bug -->
  495. <feature name='smap' policy='disable'/>
  496. </cpu>
  497. <os>
  498. <type arch="x86_64" machine="xenfv">hvm</type>
  499. <loader>hvmloader</loader>
  500. <boot dev="cdrom" />
  501. <boot dev="hd" />
  502. <!-- server_ip is the address of stubdomain. It hosts it's own DNS server. -->
  503. </os>
  504. <features>
  505. <pae/>
  506. <acpi/>
  507. <apic/>
  508. <viridian/>
  509. </features>
  510. <clock offset="variable" adjustment="0" basis="localtime" />
  511. <on_poweroff>destroy</on_poweroff>
  512. <on_reboot>destroy</on_reboot>
  513. <on_crash>destroy</on_crash>
  514. <devices>
  515. <emulator type="stubdom-linux" />
  516. <input type="tablet" bus="usb"/>
  517. <video type="cirrus"/>
  518. <graphics type="qubes"/>
  519. </devices>
  520. </domain>
  521. '''
  522. my_uuid = '7db78950-c467-4863-94d1-af59806384ea'
  523. vm = self.get_vm(uuid=my_uuid)
  524. vm.netvm = None
  525. vm.virt_mode = 'hvm'
  526. libvirt_xml = vm.create_config_file()
  527. self.assertXMLEqual(lxml.etree.XML(libvirt_xml),
  528. lxml.etree.XML(expected))