__init__.py 35 KB

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