__init__.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. # pylint: disable=invalid-name
  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
  7. # Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  8. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. """
  25. .. warning::
  26. The test suite hereby claims any domain whose name starts with
  27. :py:data:`VMPREFIX` as fair game. This is needed to enforce sane
  28. test executing environment. If you have domains named ``test-*``,
  29. don't run the tests.
  30. """
  31. import collections
  32. import functools
  33. import logging
  34. import os
  35. import shutil
  36. import subprocess
  37. import sys
  38. import tempfile
  39. import time
  40. import traceback
  41. import unittest
  42. from distutils import spawn
  43. import lxml.etree
  44. import pkg_resources
  45. import qubes.backup
  46. import qubes.config
  47. import qubes.devices
  48. import qubes.events
  49. import qubes.exc
  50. import qubes.vm.standalonevm
  51. XMLPATH = '/var/lib/qubes/qubes-test.xml'
  52. CLASS_XMLPATH = '/var/lib/qubes/qubes-class-test.xml'
  53. TEMPLATE = 'fedora-23'
  54. VMPREFIX = 'test-inst-'
  55. CLSVMPREFIX = 'test-cls-'
  56. if 'DEFAULT_LVM_POOL' in os.environ.keys():
  57. DEFAULT_LVM_POOL = os.environ['DEFAULT_LVM_POOL']
  58. else:
  59. DEFAULT_LVM_POOL = 'qubes_dom0/pool00'
  60. POOL_CONF = {'name': 'test-lvm',
  61. 'driver': 'lvm_thin',
  62. 'volume_group': DEFAULT_LVM_POOL.split('/')[0],
  63. 'thin_pool': DEFAULT_LVM_POOL.split('/')[1]}
  64. #: :py:obj:`True` if running in dom0, :py:obj:`False` otherwise
  65. in_dom0 = False
  66. #: :py:obj:`False` if outside of git repo,
  67. #: path to root of the directory otherwise
  68. in_git = False
  69. try:
  70. import libvirt
  71. libvirt.openReadOnly(qubes.config.defaults['libvirt_uri']).close()
  72. in_dom0 = True
  73. except libvirt.libvirtError:
  74. pass
  75. try:
  76. in_git = subprocess.check_output(
  77. ['git', 'rev-parse', '--show-toplevel']).decode().strip()
  78. qubes.log.LOGPATH = '/tmp'
  79. qubes.log.LOGFILE = '/tmp/qubes.log'
  80. except subprocess.CalledProcessError:
  81. # git returned nonzero, we are outside git repo
  82. pass
  83. except OSError:
  84. # command not found; let's assume we're outside
  85. pass
  86. def skipUnlessDom0(test_item):
  87. '''Decorator that skips test outside dom0.
  88. Some tests (especially integration tests) have to be run in more or less
  89. working dom0. This is checked by connecting to libvirt.
  90. '''
  91. return unittest.skipUnless(in_dom0, 'outside dom0')(test_item)
  92. def skipUnlessGit(test_item):
  93. '''Decorator that skips test outside git repo.
  94. There are very few tests that an be run only in git. One example is
  95. correctness of example code that won't get included in RPM.
  96. '''
  97. return unittest.skipUnless(in_git, 'outside git tree')(test_item)
  98. class TestEmitter(qubes.events.Emitter):
  99. '''Dummy event emitter which records events fired on it.
  100. Events are counted in :py:attr:`fired_events` attribute, which is
  101. :py:class:`collections.Counter` instance. For each event, ``(event, args,
  102. kwargs)`` object is counted. *event* is event name (a string), *args* is
  103. tuple with positional arguments and *kwargs* is sorted tuple of items from
  104. keyword arguments.
  105. >>> emitter = TestEmitter()
  106. >>> emitter.fired_events
  107. Counter()
  108. >>> emitter.fire_event('event', spam='eggs', foo='bar')
  109. >>> emitter.fired_events
  110. Counter({('event', (1, 2, 3), (('foo', 'bar'), ('spam', 'eggs'))): 1})
  111. '''
  112. def __init__(self, *args, **kwargs):
  113. super(TestEmitter, self).__init__(*args, **kwargs)
  114. #: :py:class:`collections.Counter` instance
  115. self.fired_events = collections.Counter()
  116. def fire_event(self, event, **kwargs):
  117. effects = super(TestEmitter, self).fire_event(event, **kwargs)
  118. ev_kwargs = frozenset(
  119. (key,
  120. frozenset(value.items()) if isinstance(value, dict) else value)
  121. for key, value in kwargs.items()
  122. )
  123. self.fired_events[(event, ev_kwargs)] += 1
  124. return effects
  125. def fire_event_pre(self, event, **kwargs):
  126. effects = super(TestEmitter, self).fire_event_pre(event, **kwargs)
  127. ev_kwargs = frozenset(
  128. (key,
  129. frozenset(value.items()) if isinstance(value, dict) else value)
  130. for key, value in kwargs.items()
  131. )
  132. self.fired_events[(event, ev_kwargs)] += 1
  133. return effects
  134. def expectedFailureIfTemplate(templates):
  135. """
  136. Decorator for marking specific test as expected to fail only for some
  137. templates. Template name is compared as substring, so 'whonix' will
  138. handle both 'whonix-ws' and 'whonix-gw'.
  139. templates can be either a single string, or an iterable
  140. """
  141. def decorator(func):
  142. @functools.wraps(func)
  143. def wrapper(self, *args, **kwargs):
  144. template = self.template
  145. if isinstance(templates, str):
  146. should_expect_fail = template in templates
  147. else:
  148. should_expect_fail = any([template in x for x in templates])
  149. if should_expect_fail:
  150. try:
  151. func(self, *args, **kwargs)
  152. except Exception:
  153. raise unittest.case._ExpectedFailure(sys.exc_info())
  154. raise unittest.case._UnexpectedSuccess()
  155. else:
  156. # Call directly:
  157. func(self, *args, **kwargs)
  158. return wrapper
  159. return decorator
  160. class _AssertNotRaisesContext(object):
  161. """A context manager used to implement TestCase.assertNotRaises methods.
  162. Stolen from unittest and hacked. Regexp support stripped.
  163. """ # pylint: disable=too-few-public-methods
  164. def __init__(self, expected, test_case, expected_regexp=None):
  165. if expected_regexp is not None:
  166. raise NotImplementedError('expected_regexp is unsupported')
  167. self.expected = expected
  168. self.exception = None
  169. self.failureException = test_case.failureException
  170. def __enter__(self):
  171. return self
  172. def __exit__(self, exc_type, exc_value, tb):
  173. if exc_type is None:
  174. return True
  175. if issubclass(exc_type, self.expected):
  176. raise self.failureException(
  177. "{!r} raised, traceback:\n{!s}".format(
  178. exc_value, ''.join(traceback.format_tb(tb))))
  179. else:
  180. # pass through
  181. return False
  182. self.exception = exc_value # store for later retrieval
  183. class substitute_entry_points(object):
  184. '''Monkey-patch pkg_resources to substitute one group in iter_entry_points
  185. This is for testing plugins, like device classes.
  186. :param str group: The group that is to be overloaded.
  187. :param str tempgroup: The substitute group.
  188. Inside this context, if one iterates over entry points in overloaded group,
  189. the iteration actually happens over the other group.
  190. This context manager is stackable. To substitute more than one entry point
  191. group, just nest two contexts.
  192. ''' # pylint: disable=invalid-name
  193. def __init__(self, group, tempgroup):
  194. self.group = group
  195. self.tempgroup = tempgroup
  196. self._orig_iter_entry_points = None
  197. def _iter_entry_points(self, group, *args, **kwargs):
  198. if group == self.group:
  199. group = self.tempgroup
  200. return self._orig_iter_entry_points(group, *args, **kwargs)
  201. def __enter__(self):
  202. self._orig_iter_entry_points = pkg_resources.iter_entry_points
  203. pkg_resources.iter_entry_points = self._iter_entry_points
  204. return self
  205. def __exit__(self, exc_type, exc_value, tb):
  206. pkg_resources.iter_entry_points = self._orig_iter_entry_points
  207. self._orig_iter_entry_points = None
  208. class BeforeCleanExit(BaseException):
  209. '''Raised from :py:meth:`QubesTestCase.tearDown` when
  210. :py:attr:`qubes.tests.run.QubesDNCTestResult.do_not_clean` is set.'''
  211. pass
  212. class QubesTestCase(unittest.TestCase):
  213. '''Base class for Qubes unit tests.
  214. '''
  215. def __init__(self, *args, **kwargs):
  216. super(QubesTestCase, self).__init__(*args, **kwargs)
  217. self.longMessage = True
  218. self.log = logging.getLogger('{}.{}.{}'.format(
  219. self.__class__.__module__,
  220. self.__class__.__name__,
  221. self._testMethodName))
  222. self.addTypeEqualityFunc(qubes.devices.DeviceManager,
  223. self.assertDevicesEqual)
  224. def __str__(self):
  225. return '{}/{}/{}'.format(
  226. self.__class__.__module__,
  227. self.__class__.__name__,
  228. self._testMethodName)
  229. def tearDown(self):
  230. super(QubesTestCase, self).tearDown()
  231. # TODO: find better way in py3
  232. try:
  233. result = self._outcome.result
  234. except:
  235. result = self._resultForDoCleanups
  236. failed_test_cases = result.failures \
  237. + result.errors \
  238. + [(tc, None) for tc in result.unexpectedSuccesses]
  239. if getattr(result, 'do_not_clean', False) \
  240. and any(tc is self for tc, exc in failed_test_cases):
  241. raise BeforeCleanExit()
  242. def assertNotRaises(self, excClass, callableObj=None, *args, **kwargs):
  243. """Fail if an exception of class excClass is raised
  244. by callableObj when invoked with arguments args and keyword
  245. arguments kwargs. If a different type of exception is
  246. raised, it will not be caught, and the test case will be
  247. deemed to have suffered an error, exactly as for an
  248. unexpected exception.
  249. If called with callableObj omitted or None, will return a
  250. context object used like this::
  251. with self.assertRaises(SomeException):
  252. do_something()
  253. The context manager keeps a reference to the exception as
  254. the 'exception' attribute. This allows you to inspect the
  255. exception after the assertion::
  256. with self.assertRaises(SomeException) as cm:
  257. do_something()
  258. the_exception = cm.exception
  259. self.assertEqual(the_exception.error_code, 3)
  260. """
  261. context = _AssertNotRaisesContext(excClass, self)
  262. if callableObj is None:
  263. return context
  264. with context:
  265. callableObj(*args, **kwargs)
  266. def assertXMLEqual(self, xml1, xml2):
  267. '''Check for equality of two XML objects.
  268. :param xml1: first element
  269. :param xml2: second element
  270. :type xml1: :py:class:`lxml.etree._Element`
  271. :type xml2: :py:class:`lxml.etree._Element`
  272. ''' # pylint: disable=invalid-name
  273. self.assertEqual(xml1.tag, xml2.tag)
  274. self.assertEqual(xml1.text, xml2.text)
  275. self.assertCountEqual(xml1.keys(), xml2.keys())
  276. for key in xml1.keys():
  277. self.assertEqual(xml1.get(key), xml2.get(key))
  278. def assertDevicesEqual(self, devices1, devices2, msg=None):
  279. self.assertEqual(devices1.keys(), devices2.keys(), msg)
  280. for dev_class in devices1.keys():
  281. self.assertEqual(
  282. [str(dev) for dev in devices1[dev_class]],
  283. [str(dev) for dev in devices2[dev_class]],
  284. "Devices of class {} differs{}".format(
  285. dev_class, (": " + msg) if msg else "")
  286. )
  287. def assertEventFired(self, subject, event, kwargs=None):
  288. '''Check whether event was fired on given emitter and fail if it did
  289. not.
  290. :param subject: emitter which is being checked
  291. :type emitter: :py:class:`TestEmitter`
  292. :param str event: event identifier
  293. :param list kwargs: when given, all items must appear in kwargs passed \
  294. to an event
  295. '''
  296. will_not_match = object()
  297. for ev, ev_kwargs in subject.fired_events:
  298. if ev != event:
  299. continue
  300. if kwargs is not None:
  301. ev_kwargs = dict(ev_kwargs)
  302. if any(ev_kwargs.get(k, will_not_match) != v
  303. for k, v in kwargs.items()):
  304. continue
  305. return
  306. self.fail('event {!r} {}did not fire on {!r}'.format(
  307. event, ('' if kwargs is None else '{!r} '.format(kwargs)), subject))
  308. def assertEventNotFired(self, subject, event, kwargs=None):
  309. '''Check whether event was fired on given emitter. Fail if it did.
  310. :param subject: emitter which is being checked
  311. :type emitter: :py:class:`TestEmitter`
  312. :param str event: event identifier
  313. :param list kwargs: when given, all items must appear in kwargs passed \
  314. to an event
  315. '''
  316. will_not_match = object()
  317. for ev, ev_kwargs in subject.fired_events:
  318. if ev != event:
  319. continue
  320. if kwargs is not None:
  321. ev_kwargs = dict(ev_kwargs)
  322. if any(ev_kwargs.get(k, will_not_match) != v
  323. for k, v in kwargs.items()):
  324. continue
  325. self.fail('event {!r} {}did fire on {!r}'.format(
  326. event,
  327. ('' if kwargs is None else '{!r} '.format(kwargs)),
  328. subject))
  329. return
  330. def assertXMLIsValid(self, xml, file=None, schema=None):
  331. '''Check whether given XML fulfills Relax NG schema.
  332. Schema can be given in a couple of ways:
  333. - As separate file. This is most common, and also the only way to
  334. handle file inclusion. Call with file name as second argument.
  335. - As string containing actual schema. Put that string in *schema*
  336. keyword argument.
  337. :param lxml.etree._Element xml: XML element instance to check
  338. :param str file: filename of Relax NG schema
  339. :param str schema: optional explicit schema string
  340. ''' # pylint: disable=redefined-builtin
  341. if schema is not None and file is None:
  342. relaxng = schema
  343. if isinstance(relaxng, str):
  344. relaxng = lxml.etree.XML(relaxng)
  345. # pylint: disable=protected-access
  346. if isinstance(relaxng, lxml.etree._Element):
  347. relaxng = lxml.etree.RelaxNG(relaxng)
  348. elif file is not None and schema is None:
  349. if not os.path.isabs(file):
  350. basedirs = ['/usr/share/doc/qubes/relaxng']
  351. if in_git:
  352. basedirs.insert(0, os.path.join(in_git, 'relaxng'))
  353. for basedir in basedirs:
  354. abspath = os.path.join(basedir, file)
  355. if os.path.exists(abspath):
  356. file = abspath
  357. break
  358. relaxng = lxml.etree.RelaxNG(file=file)
  359. else:
  360. raise TypeError("There should be excactly one of 'file' and "
  361. "'schema' arguments specified.")
  362. # We have to be extra careful here in case someone messed up with
  363. # self.failureException. It should by default be AssertionError, just
  364. # what is spewed by RelaxNG(), but who knows what might happen.
  365. try:
  366. relaxng.assert_(xml)
  367. except self.failureException:
  368. raise
  369. except AssertionError as e:
  370. self.fail(str(e))
  371. @staticmethod
  372. def make_vm_name(name, class_teardown=False):
  373. if class_teardown:
  374. return CLSVMPREFIX + name
  375. else:
  376. return VMPREFIX + name
  377. class SystemTestsMixin(object):
  378. """
  379. Mixin for integration tests. All the tests here should use self.app
  380. object and when need qubes.xml path - should use :py:data:`XMLPATH`
  381. defined in this file.
  382. Every VM created by test, must use :py:meth:`SystemTestsMixin.make_vm_name`
  383. for VM name.
  384. By default self.app represents empty collection, if anything is needed
  385. there from the real collection it can be imported from self.host_app in
  386. :py:meth:`SystemTestsMixin.setUp`. But *can not be modified* in any way -
  387. this include both changing attributes in
  388. :py:attr:`SystemTestsMixin.host_app` and modifying files of such imported
  389. VM. If test need to make some modification, it must clone the VM first.
  390. If some group of tests needs class-wide initialization, first of all the
  391. author should consider if it is really needed. But if so, setUpClass can
  392. be used to create Qubes(CLASS_XMLPATH) object and create/import required
  393. stuff there. VMs created in :py:meth:`TestCase.setUpClass` should
  394. use self.make_vm_name('...', class_teardown=True) for name creation.
  395. """
  396. # noinspection PyAttributeOutsideInit
  397. def setUp(self):
  398. if not in_dom0:
  399. self.skipTest('outside dom0')
  400. super(SystemTestsMixin, self).setUp()
  401. self.remove_test_vms()
  402. # need some information from the real qubes.xml - at least installed
  403. # templates; should not be used for testing, only to initialize self.app
  404. self.host_app = qubes.Qubes(os.path.join(
  405. qubes.config.system_path['qubes_base_dir'],
  406. qubes.config.system_path['qubes_store_filename']))
  407. if os.path.exists(CLASS_XMLPATH):
  408. shutil.copy(CLASS_XMLPATH, XMLPATH)
  409. self.app = qubes.Qubes(XMLPATH)
  410. else:
  411. self.app = qubes.Qubes.create_empty_store(qubes.tests.XMLPATH,
  412. default_kernel=self.host_app.default_kernel,
  413. clockvm=None,
  414. updatevm=None
  415. )
  416. os.environ['QUBES_XML_PATH'] = XMLPATH
  417. def init_default_template(self, template=None):
  418. if template is None:
  419. template = self.host_app.default_template
  420. elif isinstance(template, str):
  421. template = self.host_app.domains[template]
  422. used_pools = [vol.pool for vol in template.volumes.values()]
  423. for pool in used_pools:
  424. if pool in self.app.pools:
  425. continue
  426. self.app.add_pool(**self.host_app.pools[pool].config)
  427. template_vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM,
  428. name=template.name,
  429. uuid=template.uuid,
  430. label='black')
  431. for name, volume in template_vm.volumes.items():
  432. if volume.pool != template.volumes[name].pool:
  433. template_vm.storage.init_volume(name,
  434. template.volumes[name].config)
  435. self.app.default_template = template_vm
  436. def init_networking(self):
  437. if not self.app.default_template:
  438. self.skipTest('Default template required for testing networking')
  439. default_netvm = self.host_app.default_netvm
  440. # if testing Whonix Workstation based VMs, try to use sys-whonix instead
  441. if self.app.default_template.name.startswith('whonix-ws'):
  442. if 'sys-whonix' in self.host_app.domains:
  443. default_netvm = self.host_app.domains['sys-whonix']
  444. if default_netvm is None:
  445. self.skipTest('Default netvm required')
  446. if not default_netvm.is_running():
  447. self.skipTest('VM {} required to be running'.format(
  448. default_netvm.name))
  449. # Add NetVM stub to qubes-test.xml matching the one on host.
  450. # Keeping 'qid' the same is critical because IP addresses are
  451. # calculated from it.
  452. # Intentionally don't copy template (use default), as it may be based
  453. # on a different one than actually testing.
  454. netvm_clone = self.app.add_new_vm(default_netvm.__class__,
  455. qid=default_netvm.qid,
  456. name=default_netvm.name,
  457. uuid=default_netvm.uuid,
  458. label=default_netvm.label,
  459. provides_network=True
  460. )
  461. self.app.default_netvm = netvm_clone
  462. def _find_pool(self, volume_group, thin_pool):
  463. ''' Returns the pool matching the specified ``volume_group`` &
  464. ``thin_pool``, or None.
  465. '''
  466. pools = [p for p in self.app.pools
  467. if issubclass(p.__class__, qubes.storage.lvm.ThinPool)]
  468. for pool in pools:
  469. if pool.volume_group == volume_group \
  470. and pool.thin_pool == thin_pool:
  471. return pool
  472. return None
  473. def init_lvm_pool(self):
  474. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  475. path = "/dev/mapper/{!s}-{!s}".format(volume_group, thin_pool)
  476. if not os.path.exists(path):
  477. self.skipTest('LVM thin pool {!r} does not exist'.
  478. format(DEFAULT_LVM_POOL))
  479. self.pool = self._find_pool(volume_group, thin_pool)
  480. if not self.pool:
  481. self.pool = self.app.add_pool(**POOL_CONF)
  482. self.created_pool = True
  483. def reload_db(self):
  484. self.app = qubes.Qubes(qubes.tests.XMLPATH)
  485. def save_and_reload_db(self):
  486. self.app.save()
  487. self.reload_db()
  488. def tearDown(self):
  489. super(SystemTestsMixin, self).tearDown()
  490. self.remove_test_vms()
  491. # remove all references to VM objects, to release resources - most
  492. # importantly file descriptors; this object will live
  493. # during the whole test run, but all the file descriptors would be
  494. # depleted earlier
  495. del self.app
  496. del self.host_app
  497. for attr in dir(self):
  498. if isinstance(getattr(self, attr), qubes.vm.BaseVM):
  499. delattr(self, attr)
  500. @classmethod
  501. def tearDownClass(cls):
  502. super(SystemTestsMixin, cls).tearDownClass()
  503. if not in_dom0:
  504. return
  505. cls.remove_test_vms(xmlpath=CLASS_XMLPATH, prefix=CLSVMPREFIX)
  506. @classmethod
  507. def _remove_vm_qubes(cls, vm):
  508. vmname = vm.name
  509. app = vm.app
  510. try:
  511. # XXX .is_running() may throw libvirtError if undefined
  512. if vm.is_running():
  513. vm.force_shutdown()
  514. except: # pylint: disable=bare-except
  515. pass
  516. try:
  517. vm.remove_from_disk()
  518. except: # pylint: disable=bare-except
  519. pass
  520. del app.domains[vm.qid]
  521. del vm
  522. app.save()
  523. del app
  524. # Now ensure it really went away. This may not have happened,
  525. # for example if vm.libvirt_domain malfunctioned.
  526. try:
  527. conn = libvirt.open(qubes.config.defaults['libvirt_uri'])
  528. dom = conn.lookupByName(vmname)
  529. except: # pylint: disable=bare-except
  530. pass
  531. else:
  532. cls._remove_vm_libvirt(dom)
  533. cls._remove_vm_disk(vmname)
  534. @staticmethod
  535. def _remove_vm_libvirt(dom):
  536. try:
  537. dom.destroy()
  538. except libvirt.libvirtError: # not running
  539. pass
  540. dom.undefine()
  541. @staticmethod
  542. def _remove_vm_disk(vmname):
  543. for dirspec in (
  544. 'qubes_appvms_dir',
  545. 'qubes_servicevms_dir',
  546. 'qubes_templates_dir'):
  547. dirpath = os.path.join(qubes.config.system_path['qubes_base_dir'],
  548. qubes.config.system_path[dirspec], vmname)
  549. if os.path.exists(dirpath):
  550. if os.path.isdir(dirpath):
  551. shutil.rmtree(dirpath)
  552. else:
  553. os.unlink(dirpath)
  554. @staticmethod
  555. def _remove_vm_disk_lvm(prefix=VMPREFIX):
  556. ''' Remove LVM volumes with given prefix
  557. This is "a bit" drastic, as it removes volumes regardless of volume
  558. group, thin pool etc. But we assume no important data on test system.
  559. '''
  560. try:
  561. volumes = subprocess.check_output(
  562. ['sudo', 'lvs', '--noheadings', '-o', 'vg_name,name',
  563. '--separator', '/']).decode()
  564. if ('/' + prefix) not in volumes:
  565. return
  566. subprocess.check_call(['sudo', 'lvremove', '-f'] +
  567. [vol.strip() for vol in volumes.splitlines()
  568. if ('/' + prefix) in vol],
  569. stdout=open(os.devnull, 'w'))
  570. except subprocess.CalledProcessError:
  571. pass
  572. @classmethod
  573. def remove_vms(cls, vms):
  574. for vm in vms:
  575. cls._remove_vm_qubes(vm)
  576. @classmethod
  577. def remove_test_vms(cls, xmlpath=XMLPATH, prefix=VMPREFIX):
  578. '''Aggresively remove any domain that has name in testing namespace.
  579. '''
  580. # first, remove them Qubes-way
  581. if os.path.exists(xmlpath):
  582. try:
  583. cls.remove_vms(vm for vm in qubes.Qubes(xmlpath).domains
  584. if vm.name.startswith(prefix))
  585. except (qubes.exc.QubesException, lxml.etree.XMLSyntaxError):
  586. # If qubes-test.xml is broken that much it doesn't even load,
  587. # simply remove it. VMs will be cleaned up the hard way.
  588. # TODO logging?
  589. pass
  590. os.unlink(xmlpath)
  591. # now remove what was only in libvirt
  592. conn = libvirt.open(qubes.config.defaults['libvirt_uri'])
  593. for dom in conn.listAllDomains():
  594. if dom.name().startswith(prefix):
  595. cls._remove_vm_libvirt(dom)
  596. conn.close()
  597. # finally remove anything that is left on disk
  598. vmnames = set()
  599. for dirspec in (
  600. 'qubes_appvms_dir',
  601. 'qubes_servicevms_dir',
  602. 'qubes_templates_dir'):
  603. dirpath = os.path.join(qubes.config.system_path['qubes_base_dir'],
  604. qubes.config.system_path[dirspec])
  605. for name in os.listdir(dirpath):
  606. if name.startswith(prefix):
  607. vmnames.add(name)
  608. for vmname in vmnames:
  609. cls._remove_vm_disk(vmname)
  610. cls._remove_vm_disk_lvm(prefix)
  611. def qrexec_policy(self, service, source, destination, allow=True):
  612. """
  613. Allow qrexec calls for duration of the test
  614. :param service: service name
  615. :param source: source VM name
  616. :param destination: destination VM name
  617. :return:
  618. """
  619. def add_remove_rule(add=True):
  620. with open('/etc/qubes-rpc/policy/{}'.format(service), 'r+') as policy:
  621. policy_rules = policy.readlines()
  622. rule = "{} {} {}\n".format(source, destination,
  623. 'allow' if allow else 'deny')
  624. if add:
  625. policy_rules.insert(0, rule)
  626. else:
  627. policy_rules.remove(rule)
  628. policy.truncate(0)
  629. policy.seek(0)
  630. policy.write(''.join(policy_rules))
  631. add_remove_rule(add=True)
  632. self.addCleanup(add_remove_rule, add=False)
  633. def wait_for_window(self, title, timeout=30, show=True):
  634. """
  635. Wait for a window with a given title. Depending on show parameter,
  636. it will wait for either window to show or to disappear.
  637. :param title: title of the window to wait for
  638. :param timeout: timeout of the operation, in seconds
  639. :param show: if True - wait for the window to be visible,
  640. otherwise - to not be visible
  641. :return: None
  642. """
  643. wait_count = 0
  644. while subprocess.call(['xdotool', 'search', '--name', title],
  645. stdout=open(os.path.devnull, 'w'), stderr=subprocess.STDOUT) \
  646. == int(show):
  647. wait_count += 1
  648. if wait_count > timeout*10:
  649. self.fail("Timeout while waiting for {} window to {}".format(
  650. title, "show" if show else "hide")
  651. )
  652. time.sleep(0.1)
  653. def enter_keys_in_window(self, title, keys):
  654. """
  655. Search for window with given title, then enter listed keys there.
  656. The function will wait for said window to appear.
  657. :param title: title of window
  658. :param keys: list of keys to enter, as for `xdotool key`
  659. :return: None
  660. """
  661. # 'xdotool search --sync' sometimes crashes on some race when
  662. # accessing window properties
  663. self.wait_for_window(title)
  664. command = ['xdotool', 'search', '--name', title,
  665. 'windowactivate', '--sync',
  666. 'key'] + keys
  667. subprocess.check_call(command)
  668. def shutdown_and_wait(self, vm, timeout=60):
  669. vm.shutdown()
  670. while timeout > 0:
  671. if not vm.is_running():
  672. return
  673. time.sleep(1)
  674. timeout -= 1
  675. self.fail("Timeout while waiting for VM {} shutdown".format(vm.name))
  676. def prepare_hvm_system_linux(self, vm, init_script, extra_files=None):
  677. if not os.path.exists('/usr/lib/grub/i386-pc'):
  678. self.skipTest('grub2 not installed')
  679. if not spawn.find_executable('grub2-install'):
  680. self.skipTest('grub2-tools not installed')
  681. if not spawn.find_executable('dracut'):
  682. self.skipTest('dracut not installed')
  683. # create a single partition
  684. p = subprocess.Popen(['sfdisk', '-q', '-L', vm.storage.root_img],
  685. stdin=subprocess.PIPE,
  686. stdout=open(os.devnull, 'w'),
  687. stderr=subprocess.STDOUT)
  688. p.communicate('2048,\n')
  689. assert p.returncode == 0, 'sfdisk failed'
  690. # TODO: check if root_img is really file, not already block device
  691. p = subprocess.Popen(['sudo', 'losetup', '-f', '-P', '--show',
  692. vm.storage.root_img], stdout=subprocess.PIPE)
  693. (loopdev, _) = p.communicate()
  694. loopdev = loopdev.strip()
  695. looppart = loopdev + 'p1'
  696. assert p.returncode == 0, 'losetup failed'
  697. subprocess.check_call(['sudo', 'mkfs.ext2', '-q', '-F', looppart])
  698. mountpoint = tempfile.mkdtemp()
  699. subprocess.check_call(['sudo', 'mount', looppart, mountpoint])
  700. try:
  701. subprocess.check_call(['sudo', 'grub2-install',
  702. '--target', 'i386-pc',
  703. '--modules', 'part_msdos ext2',
  704. '--boot-directory', mountpoint, loopdev],
  705. stderr=open(os.devnull, 'w')
  706. )
  707. grub_cfg = '{}/grub2/grub.cfg'.format(mountpoint)
  708. subprocess.check_call(
  709. ['sudo', 'chown', '-R', os.getlogin(), mountpoint])
  710. with open(grub_cfg, 'w') as f:
  711. f.write(
  712. "set timeout=1\n"
  713. "menuentry 'Default' {\n"
  714. " linux /vmlinuz root=/dev/xvda1 "
  715. "rd.driver.blacklist=bochs_drm "
  716. "rd.driver.blacklist=uhci_hcd console=hvc0\n"
  717. " initrd /initrd\n"
  718. "}"
  719. )
  720. p = subprocess.Popen(['uname', '-r'], stdout=subprocess.PIPE)
  721. (kernel_version, _) = p.communicate()
  722. kernel_version = kernel_version.strip()
  723. kernel = '/boot/vmlinuz-{}'.format(kernel_version)
  724. shutil.copy(kernel, os.path.join(mountpoint, 'vmlinuz'))
  725. init_path = os.path.join(mountpoint, 'init')
  726. with open(init_path, 'w') as f:
  727. f.write(init_script)
  728. os.chmod(init_path, 0o755)
  729. dracut_args = [
  730. '--kver', kernel_version,
  731. '--include', init_path,
  732. '/usr/lib/dracut/hooks/pre-pivot/initscript.sh',
  733. '--no-hostonly', '--nolvmconf', '--nomdadmconf',
  734. ]
  735. if extra_files:
  736. dracut_args += ['--install', ' '.join(extra_files)]
  737. subprocess.check_call(
  738. ['dracut'] + dracut_args + [os.path.join(mountpoint,
  739. 'initrd')],
  740. stderr=open(os.devnull, 'w')
  741. )
  742. finally:
  743. subprocess.check_call(['sudo', 'umount', mountpoint])
  744. shutil.rmtree(mountpoint)
  745. subprocess.check_call(['sudo', 'losetup', '-d', loopdev])
  746. def load_tests(loader, tests, pattern): # pylint: disable=unused-argument
  747. # discard any tests from this module, because it hosts base classes
  748. tests = unittest.TestSuite()
  749. for modname in (
  750. # unit tests
  751. 'qubes.tests.events',
  752. 'qubes.tests.devices',
  753. 'qubes.tests.firewall',
  754. 'qubes.tests.init',
  755. 'qubes.tests.vm.init',
  756. 'qubes.tests.storage',
  757. 'qubes.tests.storage_file',
  758. 'qubes.tests.storage_lvm',
  759. 'qubes.tests.vm.qubesvm',
  760. 'qubes.tests.vm.mix.net',
  761. 'qubes.tests.vm.adminvm',
  762. 'qubes.tests.app',
  763. 'qubes.tests.tarwriter',
  764. 'qubes.tests.api_admin',
  765. 'qubespolicy.tests',
  766. 'qubes.tests.tools.qubesd',
  767. ):
  768. tests.addTests(loader.loadTestsFromName(modname))
  769. # GTK/Glib is way too old there
  770. if 'TRAVIS' not in os.environ:
  771. for modname in (
  772. 'qubespolicy.tests.gtkhelpers',
  773. 'qubespolicy.tests.rpcconfirmation',
  774. ):
  775. tests.addTests(loader.loadTestsFromName(modname))
  776. tests.addTests(loader.discover(
  777. os.path.join(os.path.dirname(__file__), 'tools')))
  778. if not in_dom0:
  779. return tests
  780. for modname in (
  781. # integration tests
  782. 'qubes.tests.integ.basic',
  783. 'qubes.tests.integ.storage',
  784. 'qubes.tests.integ.devices_pci',
  785. 'qubes.tests.integ.dom0_update',
  786. 'qubes.tests.integ.network',
  787. 'qubes.tests.integ.dispvm',
  788. 'qubes.tests.integ.vm_qrexec_gui',
  789. 'qubes.tests.integ.backup',
  790. 'qubes.tests.integ.backupcompatibility',
  791. # 'qubes.tests.regressions',
  792. # external modules
  793. # 'qubes.tests.extra',
  794. ):
  795. tests.addTests(loader.loadTestsFromName(modname))
  796. return tests