__init__.py 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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 library is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU Lesser General Public
  12. # License as published by the Free Software Foundation; either
  13. # version 2.1 of the License, or (at your option) any later version.
  14. #
  15. # This library 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 GNU
  18. # Lesser General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public
  21. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  22. #
  23. """
  24. .. warning::
  25. The test suite hereby claims any domain whose name starts with
  26. :py:data:`VMPREFIX` as fair game. This is needed to enforce sane
  27. test executing environment. If you have domains named ``test-*``,
  28. don't run the tests.
  29. """
  30. import asyncio
  31. import collections
  32. import functools
  33. import logging
  34. import os
  35. import pathlib
  36. import shlex
  37. import shutil
  38. import subprocess
  39. import sys
  40. import tempfile
  41. import time
  42. import traceback
  43. import unittest
  44. import warnings
  45. from distutils import spawn
  46. import gc
  47. import lxml.etree
  48. import pkg_resources
  49. import qubes
  50. import qubes.api
  51. import qubes.api.admin
  52. import qubes.api.internal
  53. import qubes.api.misc
  54. import qubes.backup
  55. import qubes.config
  56. import qubes.devices
  57. import qubes.events
  58. import qubes.exc
  59. import qubes.ext.pci
  60. import qubes.vm.standalonevm
  61. import qubes.vm.templatevm
  62. XMLPATH = '/var/lib/qubes/qubes-test.xml'
  63. CLASS_XMLPATH = '/var/lib/qubes/qubes-class-test.xml'
  64. TEMPLATE = 'fedora-23'
  65. VMPREFIX = 'test-inst-'
  66. CLSVMPREFIX = 'test-cls-'
  67. if 'DEFAULT_LVM_POOL' in os.environ.keys():
  68. DEFAULT_LVM_POOL = os.environ['DEFAULT_LVM_POOL']
  69. else:
  70. DEFAULT_LVM_POOL = 'qubes_dom0/pool00'
  71. POOL_CONF = {'name': 'test-lvm',
  72. 'driver': 'lvm_thin',
  73. 'volume_group': DEFAULT_LVM_POOL.split('/')[0],
  74. 'thin_pool': DEFAULT_LVM_POOL.split('/')[1]}
  75. #: :py:obj:`True` if running in dom0, :py:obj:`False` otherwise
  76. in_dom0 = False
  77. #: :py:obj:`False` if outside of git repo,
  78. #: path to root of the directory otherwise
  79. in_git = False
  80. try:
  81. import libvirt
  82. libvirt.openReadOnly(qubes.config.defaults['libvirt_uri']).close()
  83. in_dom0 = True
  84. except libvirt.libvirtError:
  85. pass
  86. if in_dom0:
  87. import libvirtaio
  88. libvirt_event_impl = None
  89. try:
  90. in_git = subprocess.check_output(
  91. ['git', 'rev-parse', '--show-toplevel'], stderr=subprocess.DEVNULL
  92. ).decode().strip()
  93. qubes.log.LOGPATH = '/tmp'
  94. qubes.log.LOGFILE = '/tmp/qubes.log'
  95. except subprocess.CalledProcessError:
  96. # git returned nonzero, we are outside git repo
  97. pass
  98. except OSError:
  99. # command not found; let's assume we're outside
  100. pass
  101. ha_syslog = None
  102. def skipUnlessDom0(test_item):
  103. """Decorator that skips test outside dom0.
  104. Some tests (especially integration tests) have to be run in more or less
  105. working dom0. This is checked by connecting to libvirt.
  106. """
  107. return unittest.skipUnless(in_dom0, 'outside dom0')(test_item)
  108. def skipUnlessGit(test_item):
  109. """Decorator that skips test outside git repo.
  110. There are very few tests that an be run only in git. One example is
  111. correctness of example code that won't get included in RPM.
  112. """
  113. return unittest.skipUnless(in_git, 'outside git tree')(test_item)
  114. def skipUnlessEnv(varname):
  115. """Decorator generator for skipping tests without environment variable set.
  116. Some tests require working X11 display, like those using GTK library, which
  117. segfaults without connection to X.
  118. Other require their own, custom variables.
  119. """
  120. return unittest.skipUnless(os.getenv(varname), 'no {} set'.format(varname))
  121. class TestEmitter(qubes.events.Emitter):
  122. """Dummy event emitter which records events fired on it.
  123. Events are counted in :py:attr:`fired_events` attribute, which is
  124. :py:class:`collections.Counter` instance. For each event, ``(event, args,
  125. kwargs)`` object is counted. *event* is event name (a string), *args* is
  126. tuple with positional arguments and *kwargs* is sorted tuple of items from
  127. keyword arguments.
  128. >>> emitter = TestEmitter()
  129. >>> emitter.fired_events
  130. Counter()
  131. >>> emitter.fire_event('event', spam='eggs', foo='bar')
  132. >>> emitter.fired_events
  133. Counter({('event', (1, 2, 3), (('foo', 'bar'), ('spam', 'eggs'))): 1})
  134. """
  135. def __init__(self, *args, **kwargs):
  136. super(TestEmitter, self).__init__(*args, **kwargs)
  137. #: :py:class:`collections.Counter` instance
  138. self.fired_events = collections.Counter()
  139. def fire_event(self, event, **kwargs):
  140. effects = super(TestEmitter, self).fire_event(event, **kwargs)
  141. ev_kwargs = frozenset(
  142. (key,
  143. frozenset(value.items()) if isinstance(value, dict)
  144. else tuple(value) if isinstance(value, list)
  145. else value)
  146. for key, value in kwargs.items()
  147. )
  148. self.fired_events[(event, ev_kwargs)] += 1
  149. return effects
  150. @asyncio.coroutine
  151. def fire_event_async(self, event, pre_event=False, **kwargs):
  152. effects = yield from super(TestEmitter, self).fire_event_async(
  153. event, pre_event=pre_event, **kwargs)
  154. ev_kwargs = frozenset(
  155. (key,
  156. frozenset(value.items()) if isinstance(value, dict) else value)
  157. for key, value in kwargs.items()
  158. )
  159. self.fired_events[(event, ev_kwargs)] += 1
  160. return effects
  161. def expectedFailureIfTemplate(templates):
  162. """
  163. Decorator for marking specific test as expected to fail only for some
  164. templates. Template name is compared as substring, so 'whonix' will
  165. handle both 'whonix-ws' and 'whonix-gw'.
  166. templates can be either a single string, or an iterable
  167. """
  168. def decorator(func):
  169. @functools.wraps(func)
  170. def wrapper(self, *args, **kwargs):
  171. template = self.template
  172. if isinstance(templates, str):
  173. should_expect_fail = template in templates
  174. else:
  175. should_expect_fail = any([template in x for x in templates])
  176. if should_expect_fail:
  177. try:
  178. func(self, *args, **kwargs)
  179. except Exception:
  180. raise unittest.case._ExpectedFailure(sys.exc_info())
  181. raise unittest.case._UnexpectedSuccess()
  182. else:
  183. # Call directly:
  184. func(self, *args, **kwargs)
  185. return wrapper
  186. return decorator
  187. def wait_on_fail(func):
  188. """Test decorator for debugging. It pause test execution on failure and wait
  189. for user input. It's useful to manually inspect system state just after test
  190. fails, before executing any cleanup.
  191. Usage: decorate a test you are debugging.
  192. DO IT ONLY TEMPORARILY, DO NOT COMMIT!
  193. """
  194. @functools.wraps(func)
  195. def wrapper(self, *args, **kwargs):
  196. try:
  197. func(self, *args, **kwargs)
  198. except:
  199. print('FAIL\n')
  200. traceback.print_exc()
  201. print('Press return to continue:', end='')
  202. sys.stdout.flush()
  203. reader = asyncio.StreamReader(loop=self.loop)
  204. transport, protocol = self.loop.run_until_complete(
  205. self.loop.connect_read_pipe(
  206. lambda: asyncio.StreamReaderProtocol(reader),
  207. os.fdopen(os.dup(sys.stdin.fileno()))))
  208. self.loop.run_until_complete(reader.readline())
  209. transport.close()
  210. raise
  211. return wrapper
  212. class _AssertNotRaisesContext(object):
  213. """A context manager used to implement TestCase.assertNotRaises methods.
  214. Stolen from unittest and hacked. Regexp support stripped.
  215. """ # pylint: disable=too-few-public-methods
  216. def __init__(self, expected, test_case, expected_regexp=None):
  217. if expected_regexp is not None:
  218. raise NotImplementedError('expected_regexp is unsupported')
  219. self.expected = expected
  220. self.exception = None
  221. self.failureException = test_case.failureException
  222. def __enter__(self):
  223. return self
  224. def __exit__(self, exc_type, exc_value, tb):
  225. if exc_type is None:
  226. return True
  227. if issubclass(exc_type, self.expected):
  228. raise self.failureException(
  229. "{!r} raised, traceback:\n{!s}".format(
  230. exc_value, ''.join(traceback.format_tb(tb))))
  231. else:
  232. # pass through
  233. return False
  234. self.exception = exc_value # store for later retrieval
  235. class _QrexecPolicyContext(object):
  236. """Context manager for SystemTestCase.qrexec_policy"""
  237. def __init__(self, service, source, destination, allow=True, action=None):
  238. try:
  239. source = source.name
  240. except AttributeError:
  241. pass
  242. try:
  243. destination = destination.name
  244. except AttributeError:
  245. pass
  246. self._filename = pathlib.Path('/etc/qubes-rpc/policy') / service
  247. if action is None:
  248. action = 'allow' if allow else 'deny'
  249. self._rule = '{} {} {}\n'.format(source, destination, action)
  250. self._did_create = False
  251. self._handle = None
  252. def load(self):
  253. if self._handle is None:
  254. try:
  255. self._handle = self._filename.open('r+')
  256. except FileNotFoundError:
  257. self._handle = self._filename.open('w+')
  258. self._did_create = True
  259. self._handle.seek(0)
  260. return self._handle.readlines()
  261. def save(self, rules):
  262. assert self._handle is not None
  263. self._handle.truncate(0)
  264. self._handle.seek(0)
  265. self._handle.write(''.join(rules))
  266. self._handle.flush()
  267. def close(self):
  268. assert self._handle is not None
  269. self._handle.close()
  270. self._handle = None
  271. def __enter__(self):
  272. rules = self.load()
  273. rules.insert(0, self._rule)
  274. self.save(rules)
  275. return self
  276. def __exit__(self, exc_type, exc_value, tb):
  277. if not self._did_create:
  278. try:
  279. rules = self.load()
  280. rules.remove(self._rule)
  281. self.save(rules)
  282. finally:
  283. self.close()
  284. else:
  285. self.close()
  286. self._filename.unlink()
  287. class substitute_entry_points(object):
  288. """Monkey-patch pkg_resources to substitute one group in iter_entry_points
  289. This is for testing plugins, like device classes.
  290. :param str group: The group that is to be overloaded.
  291. :param str tempgroup: The substitute group.
  292. Inside this context, if one iterates over entry points in overloaded group,
  293. the iteration actually happens over the other group.
  294. This context manager is stackable. To substitute more than one entry point
  295. group, just nest two contexts.
  296. """ # pylint: disable=invalid-name
  297. def __init__(self, group, tempgroup):
  298. self.group = group
  299. self.tempgroup = tempgroup
  300. self._orig_iter_entry_points = None
  301. def _iter_entry_points(self, group, *args, **kwargs):
  302. if group == self.group:
  303. group = self.tempgroup
  304. return self._orig_iter_entry_points(group, *args, **kwargs)
  305. def __enter__(self):
  306. self._orig_iter_entry_points = pkg_resources.iter_entry_points
  307. pkg_resources.iter_entry_points = self._iter_entry_points
  308. return self
  309. def __exit__(self, exc_type, exc_value, tb):
  310. pkg_resources.iter_entry_points = self._orig_iter_entry_points
  311. self._orig_iter_entry_points = None
  312. class QubesTestCase(unittest.TestCase):
  313. """Base class for Qubes unit tests.
  314. """
  315. def __init__(self, *args, **kwargs):
  316. super(QubesTestCase, self).__init__(*args, **kwargs)
  317. self.longMessage = True
  318. self.log = logging.getLogger('{}.{}.{}'.format(
  319. self.__class__.__module__,
  320. self.__class__.__name__,
  321. self._testMethodName))
  322. self.addTypeEqualityFunc(qubes.devices.DeviceManager,
  323. self.assertDevicesEqual)
  324. self.loop = None
  325. global libvirt_event_impl
  326. if in_dom0 and not libvirt_event_impl:
  327. libvirt_event_impl = libvirtaio.virEventRegisterAsyncIOImpl()
  328. def __str__(self):
  329. return '{}/{}/{}'.format(
  330. self.__class__.__module__,
  331. self.__class__.__name__,
  332. self._testMethodName)
  333. def setUp(self):
  334. super().setUp()
  335. self.addCleanup(self.cleanup_gc)
  336. self.loop = asyncio.get_event_loop()
  337. self.addCleanup(self.cleanup_loop)
  338. self.addCleanup(self.cleanup_traceback)
  339. def cleanup_traceback(self):
  340. """Remove local variables reference from tracebacks to allow garbage
  341. collector to clean all Qubes*() objects, otherwise file descriptors
  342. held by them will leak"""
  343. exc_infos = [e for test_case, e in self._outcome.errors
  344. if test_case is self]
  345. if self._outcome.expectedFailure:
  346. exc_infos.append(self._outcome.expectedFailure)
  347. for exc_info in exc_infos:
  348. if exc_info is None:
  349. continue
  350. ex = exc_info[1]
  351. while ex is not None:
  352. if isinstance(ex, qubes.exc.QubesVMError):
  353. ex.vm = None
  354. traceback.clear_frames(ex.__traceback__)
  355. ex = ex.__context__
  356. def cleanup_gc(self):
  357. gc.collect()
  358. leaked = [obj for obj in gc.get_objects() + gc.garbage
  359. if isinstance(obj,
  360. (qubes.Qubes, qubes.vm.BaseVM,
  361. libvirt.virConnect, libvirt.virDomain))]
  362. if leaked:
  363. try:
  364. import objgraph
  365. objgraph.show_backrefs(leaked,
  366. max_depth=15, extra_info=extra_info,
  367. filename='/tmp/objgraph-{}.png'.format(
  368. self.id()))
  369. except ImportError:
  370. pass
  371. # do not keep leaked object references in locals()
  372. leaked = bool(leaked)
  373. assert not leaked
  374. def cleanup_loop(self):
  375. """Check if the loop is empty"""
  376. # XXX BEWARE this is touching undocumented, implementation-specific
  377. # attributes of the loop. This is most certainly unsupported and likely
  378. # will break when messing with: Python version, kernel family, loop
  379. # implementation, a combination thereof, or other things.
  380. # KEYWORDS for searching:
  381. # win32, SelectorEventLoop, ProactorEventLoop, uvloop, gevent
  382. global libvirt_event_impl
  383. # really destroy all objects that could have used loop and/or libvirt
  384. gc.collect()
  385. # Check for unfinished libvirt business.
  386. if libvirt_event_impl is not None:
  387. try:
  388. self.loop.run_until_complete(asyncio.wait_for(
  389. libvirt_event_impl.drain(), timeout=4))
  390. except asyncio.TimeoutError:
  391. raise AssertionError('libvirt event impl drain timeout')
  392. # this is stupid, but apparently it requires two passes
  393. # to cleanup SIGCHLD handlers
  394. self.loop.stop()
  395. self.loop.run_forever()
  396. self.loop.stop()
  397. self.loop.run_forever()
  398. # Check there are no Tasks left.
  399. assert not self.loop._ready
  400. assert not self.loop._scheduled
  401. # Check the loop watches no descriptors.
  402. # NOTE the loop has a pipe for self-interrupting, created once per
  403. # lifecycle, and it is unwatched only at loop.close(); so we cannot just
  404. # check selector for non-emptiness
  405. assert len(self.loop._selector.get_map()) \
  406. == int(self.loop._ssock is not None)
  407. del self.loop
  408. def assertNotRaises(self, excClass, callableObj=None, *args, **kwargs):
  409. """Fail if an exception of class excClass is raised
  410. by callableObj when invoked with arguments args and keyword
  411. arguments kwargs. If a different type of exception is
  412. raised, it will not be caught, and the test case will be
  413. deemed to have suffered an error, exactly as for an
  414. unexpected exception.
  415. If called with callableObj omitted or None, will return a
  416. context object used like this::
  417. with self.assertRaises(SomeException):
  418. do_something()
  419. The context manager keeps a reference to the exception as
  420. the 'exception' attribute. This allows you to inspect the
  421. exception after the assertion::
  422. with self.assertRaises(SomeException) as cm:
  423. do_something()
  424. the_exception = cm.exception
  425. self.assertEqual(the_exception.error_code, 3)
  426. """
  427. context = _AssertNotRaisesContext(excClass, self)
  428. if callableObj is None:
  429. return context
  430. with context:
  431. callableObj(*args, **kwargs)
  432. def assertXMLEqual(self, xml1, xml2, msg=''):
  433. """Check for equality of two XML objects.
  434. :param xml1: first element
  435. :param xml2: second element
  436. :type xml1: :py:class:`lxml.etree._Element`
  437. :type xml2: :py:class:`lxml.etree._Element`
  438. """
  439. self.assertEqual(xml1.tag, xml2.tag)
  440. msg += '/' + str(xml1.tag)
  441. if xml1.text is not None and xml2.text is not None:
  442. self.assertEqual(xml1.text.strip(), xml2.text.strip(), msg)
  443. else:
  444. self.assertEqual(xml1.text, xml2.text, msg)
  445. self.assertCountEqual(xml1.keys(), xml2.keys(), msg)
  446. for key in xml1.keys():
  447. self.assertEqual(xml1.get(key), xml2.get(key), msg)
  448. self.assertEqual(len(xml1), len(xml2), msg + ' children count')
  449. for child1, child2 in zip(xml1, xml2):
  450. self.assertXMLEqual(child1, child2, msg=msg)
  451. def assertDevicesEqual(self, devices1, devices2, msg=None):
  452. self.assertEqual(devices1.keys(), devices2.keys(), msg)
  453. for dev_class in devices1.keys():
  454. self.assertEqual(
  455. [str(dev) for dev in devices1[dev_class]],
  456. [str(dev) for dev in devices2[dev_class]],
  457. "Devices of class {} differs{}".format(
  458. dev_class, (": " + msg) if msg else "")
  459. )
  460. def assertEventFired(self, subject, event, kwargs=None):
  461. """Check whether event was fired on given emitter and fail if it did
  462. not.
  463. :param subject: emitter which is being checked
  464. :type emitter: :py:class:`TestEmitter`
  465. :param str event: event identifier
  466. :param dict kwargs: when given, all items must appear in kwargs passed \
  467. to an event
  468. """
  469. will_not_match = object()
  470. for ev, ev_kwargs in subject.fired_events:
  471. if ev != event:
  472. continue
  473. if kwargs is not None:
  474. ev_kwargs = dict(ev_kwargs)
  475. if any(ev_kwargs.get(k, will_not_match) != v
  476. for k, v in kwargs.items()):
  477. continue
  478. return
  479. self.fail('event {!r} {}did not fire on {!r}'.format(
  480. event, ('' if kwargs is None else '{!r} '.format(kwargs)), subject))
  481. def assertEventNotFired(self, subject, event, kwargs=None):
  482. """Check whether event was fired on given emitter. Fail if it did.
  483. :param subject: emitter which is being checked
  484. :type emitter: :py:class:`TestEmitter`
  485. :param str event: event identifier
  486. :param list kwargs: when given, all items must appear in kwargs passed \
  487. to an event
  488. """
  489. will_not_match = object()
  490. for ev, ev_kwargs in subject.fired_events:
  491. if ev != event:
  492. continue
  493. if kwargs is not None:
  494. ev_kwargs = dict(ev_kwargs)
  495. if any(ev_kwargs.get(k, will_not_match) != v
  496. for k, v in kwargs.items()):
  497. continue
  498. self.fail('event {!r} {}did fire on {!r}'.format(
  499. event,
  500. ('' if kwargs is None else '{!r} '.format(kwargs)),
  501. subject))
  502. return
  503. def assertXMLIsValid(self, xml, file=None, schema=None):
  504. """Check whether given XML fulfills Relax NG schema.
  505. Schema can be given in a couple of ways:
  506. - As separate file. This is most common, and also the only way to
  507. handle file inclusion. Call with file name as second argument.
  508. - As string containing actual schema. Put that string in *schema*
  509. keyword argument.
  510. :param lxml.etree._Element xml: XML element instance to check
  511. :param str file: filename of Relax NG schema
  512. :param str schema: optional explicit schema string
  513. """ # pylint: disable=redefined-builtin
  514. if schema is not None and file is None:
  515. relaxng = schema
  516. if isinstance(relaxng, str):
  517. relaxng = lxml.etree.XML(relaxng)
  518. # pylint: disable=protected-access
  519. if isinstance(relaxng, lxml.etree._Element):
  520. relaxng = lxml.etree.RelaxNG(relaxng)
  521. elif file is not None and schema is None:
  522. if not os.path.isabs(file):
  523. basedirs = ['/usr/share/doc/qubes/relaxng']
  524. if in_git:
  525. basedirs.insert(0, os.path.join(in_git, 'relaxng'))
  526. for basedir in basedirs:
  527. abspath = os.path.join(basedir, file)
  528. if os.path.exists(abspath):
  529. file = abspath
  530. break
  531. relaxng = lxml.etree.RelaxNG(file=file)
  532. else:
  533. raise TypeError("There should be excactly one of 'file' and "
  534. "'schema' arguments specified.")
  535. # We have to be extra careful here in case someone messed up with
  536. # self.failureException. It should by default be AssertionError, just
  537. # what is spewed by RelaxNG(), but who knows what might happen.
  538. try:
  539. relaxng.assert_(xml)
  540. except self.failureException:
  541. raise
  542. except AssertionError as e:
  543. self.fail(str(e))
  544. @staticmethod
  545. def make_vm_name(name, class_teardown=False):
  546. if class_teardown:
  547. return CLSVMPREFIX + name
  548. else:
  549. return VMPREFIX + name
  550. class SystemTestCase(QubesTestCase):
  551. """
  552. Mixin for integration tests. All the tests here should use self.app
  553. object and when need qubes.xml path - should use :py:data:`XMLPATH`
  554. defined in this file.
  555. Every VM created by test, must use :py:meth:`SystemTestCase.make_vm_name`
  556. for VM name.
  557. By default self.app represents empty collection, if anything is needed
  558. there from the real collection it can be imported from self.host_app in
  559. :py:meth:`SystemTestCase.setUp`. But *can not be modified* in any way -
  560. this include both changing attributes in
  561. :py:attr:`SystemTestCase.host_app` and modifying files of such imported
  562. VM. If test need to make some modification, it must clone the VM first.
  563. If some group of tests needs class-wide initialization, first of all the
  564. author should consider if it is really needed. But if so, setUpClass can
  565. be used to create Qubes(CLASS_XMLPATH) object and create/import required
  566. stuff there. VMs created in :py:meth:`TestCase.setUpClass` should
  567. use self.make_vm_name('...', class_teardown=True) for name creation.
  568. Such (group of) test need to take care about
  569. :py:meth:`TestCase.tearDownClass` implementation itself.
  570. """
  571. # noinspection PyAttributeOutsideInit
  572. def setUp(self):
  573. if not in_dom0:
  574. self.skipTest('outside dom0')
  575. super(SystemTestCase, self).setUp()
  576. self.remove_test_vms()
  577. global ha_syslog
  578. if ha_syslog is None:
  579. ha_syslog = logging.handlers.SysLogHandler('/dev/log')
  580. ha_syslog.setFormatter(
  581. logging.Formatter('%(name)s[%(process)d]: %(message)s'))
  582. logging.root.addHandler(ha_syslog)
  583. self.log.critical('starting')
  584. # need some information from the real qubes.xml - at least installed
  585. # templates; should not be used for testing, only to initialize self.app
  586. self.host_app = qubes.Qubes(os.path.join(
  587. qubes.config.qubes_base_dir,
  588. qubes.config.system_path['qubes_store_filename']))
  589. if os.path.exists(CLASS_XMLPATH):
  590. shutil.copy(CLASS_XMLPATH, XMLPATH)
  591. else:
  592. shutil.copy(self.host_app.store, XMLPATH)
  593. self.app = qubes.Qubes(XMLPATH)
  594. os.environ['QUBES_XML_PATH'] = XMLPATH
  595. self.app.register_event_handlers()
  596. self.qubesd = self.loop.run_until_complete(
  597. qubes.api.create_servers(
  598. qubes.api.admin.QubesAdminAPI,
  599. qubes.api.internal.QubesInternalAPI,
  600. qubes.api.misc.QubesMiscAPI,
  601. app=self.app, debug=True))
  602. self.addCleanup(self.cleanup_app)
  603. self.app.add_handler('domain-delete', self.close_qdb_on_remove)
  604. def close_qdb_on_remove(self, app, event, vm, **kwargs):
  605. # only close QubesDB connection, do not perform other (destructive)
  606. # actions of vm.close()
  607. if vm._qdb_connection_watch is not None:
  608. asyncio.get_event_loop().remove_reader(
  609. vm._qdb_connection_watch.watch_fd())
  610. vm._qdb_connection_watch.close()
  611. vm._qdb_connection_watch = None
  612. def cleanup_app(self):
  613. self.remove_test_vms()
  614. server = None
  615. for server in self.qubesd:
  616. for sock in server.sockets:
  617. os.unlink(sock.getsockname())
  618. server.close()
  619. del server
  620. # close all existing connections, especially this will interrupt
  621. # running admin.Events calls, which do keep reference to Qubes() and
  622. # libvirt connection
  623. conn = None
  624. for conn in qubes.api.QubesDaemonProtocol.connections:
  625. if conn.transport:
  626. conn.transport.abort()
  627. del conn
  628. self.loop.run_until_complete(asyncio.wait([
  629. server.wait_closed() for server in self.qubesd]))
  630. del self.qubesd
  631. # remove all references to any complex qubes objects, to release
  632. # resources - most importantly file descriptors; this object will live
  633. # during the whole test run, but all the file descriptors would be
  634. # depleted earlier
  635. self.app.close()
  636. self.host_app.close()
  637. del self.app
  638. del self.host_app
  639. for attr in dir(self):
  640. obj_type = type(getattr(self, attr))
  641. if obj_type.__module__.startswith('qubes'):
  642. delattr(self, attr)
  643. # then trigger garbage collector to really destroy those objects
  644. gc.collect()
  645. def init_default_template(self, template=None):
  646. if template is None:
  647. template = self.host_app.default_template
  648. elif isinstance(template, str):
  649. template = self.host_app.domains[template]
  650. self.app.default_template = str(template)
  651. def init_networking(self):
  652. if not self.app.default_template:
  653. self.skipTest('Default template required for testing networking')
  654. default_netvm = self.host_app.default_netvm
  655. # if testing Whonix Workstation based VMs, try to use sys-whonix instead
  656. if self.app.default_template.name.startswith('whonix-ws'):
  657. if 'sys-whonix' in self.host_app.domains:
  658. default_netvm = self.host_app.domains['sys-whonix']
  659. if default_netvm is None:
  660. self.skipTest('Default netvm required')
  661. if not default_netvm.is_running():
  662. self.skipTest('VM {} required to be running'.format(
  663. default_netvm.name))
  664. self.app.default_netvm = str(default_netvm)
  665. def _find_pool(self, volume_group, thin_pool):
  666. """ Returns the pool matching the specified ``volume_group`` &
  667. ``thin_pool``, or None.
  668. """
  669. pools = [p for p in self.app.pools
  670. if issubclass(p.__class__, qubes.storage.lvm.ThinPool)]
  671. for pool in pools:
  672. if pool.volume_group == volume_group \
  673. and pool.thin_pool == thin_pool:
  674. return pool
  675. return None
  676. def init_lvm_pool(self):
  677. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  678. path = "/dev/mapper/{!s}-{!s}".format(volume_group, thin_pool)
  679. if not os.path.exists(path):
  680. self.skipTest('LVM thin pool {!r} does not exist'.
  681. format(DEFAULT_LVM_POOL))
  682. self.pool = self._find_pool(volume_group, thin_pool)
  683. if not self.pool:
  684. self.pool = self.loop.run_until_complete(
  685. self.app.add_pool(**POOL_CONF))
  686. self.created_pool = True
  687. def _remove_vm_qubes(self, vm):
  688. vmname = vm.name
  689. app = vm.app
  690. try:
  691. del app.domains[vm.qid]
  692. except KeyError:
  693. pass
  694. try:
  695. self.loop.run_until_complete(vm.remove_from_disk())
  696. except: # pylint: disable=bare-except
  697. pass
  698. vm.close()
  699. del vm
  700. app.save()
  701. del app
  702. # Now ensure it really went away. This may not have happened,
  703. # for example if vm.libvirt_domain malfunctioned.
  704. try:
  705. conn = libvirt.open(qubes.config.defaults['libvirt_uri'])
  706. except: # pylint: disable=bare-except
  707. pass
  708. else:
  709. try:
  710. dom = conn.lookupByName(vmname)
  711. except: # pylint: disable=bare-except
  712. pass
  713. else:
  714. self._remove_vm_libvirt(dom)
  715. conn.close()
  716. self._remove_vm_disk(vmname)
  717. @staticmethod
  718. def _remove_vm_libvirt(dom):
  719. try:
  720. dom.destroy()
  721. except libvirt.libvirtError: # not running
  722. pass
  723. dom.undefine()
  724. @staticmethod
  725. def _remove_vm_disk(vmname):
  726. for dirspec in (
  727. 'qubes_appvms_dir',
  728. 'qubes_templates_dir'):
  729. dirpath = os.path.join(qubes.config.qubes_base_dir,
  730. qubes.config.system_path[dirspec], vmname)
  731. if os.path.exists(dirpath):
  732. if os.path.isdir(dirpath):
  733. shutil.rmtree(dirpath)
  734. else:
  735. os.unlink(dirpath)
  736. @staticmethod
  737. def _remove_vm_disk_lvm(prefix=VMPREFIX):
  738. """ Remove LVM volumes with given prefix
  739. This is "a bit" drastic, as it removes volumes regardless of volume
  740. group, thin pool etc. But we assume no important data on test system.
  741. """
  742. try:
  743. volumes = subprocess.check_output(
  744. ['lvs', '--noheadings', '-o', 'vg_name,name',
  745. '--separator', '/']).decode()
  746. if ('/vm-' + prefix) not in volumes:
  747. return
  748. subprocess.check_call(['sudo', 'lvremove', '-f'] +
  749. [vol.strip() for vol in volumes.splitlines()
  750. if ('/vm-' + prefix) in vol],
  751. stdout=subprocess.DEVNULL)
  752. except subprocess.CalledProcessError:
  753. pass
  754. def remove_vms(self, vms):
  755. vms = list(vms)
  756. if not vms:
  757. return
  758. # workaround for https://phabricator.whonix.org/T930
  759. # unregister all the VMs from sys-whonix, otherwise it will start them
  760. # again (possibly in further test)
  761. if hasattr(self, 'app') and 'whonix' in self.app.default_netvm.name:
  762. for vm in vms:
  763. try:
  764. self.loop.run_until_complete(
  765. self.app.default_netvm.run_service_for_stdio(
  766. 'whonix.NewStatus+{}_shutdown'.format(vm.name)))
  767. except:
  768. pass
  769. locked_vms = set()
  770. # first take startup lock
  771. for vm in vms:
  772. self.loop.run_until_complete(vm.startup_lock.acquire())
  773. locked_vms.add(vm)
  774. # first kill all the domains, to avoid side effects of changing netvm
  775. for vm in vms:
  776. try:
  777. # XXX .is_running() may throw libvirtError if undefined
  778. if vm.is_running():
  779. self.loop.run_until_complete(vm._kill_locked())
  780. except: # pylint: disable=bare-except
  781. pass
  782. # break dependencies
  783. for vm in vms:
  784. vm.default_dispvm = None
  785. vm.netvm = None
  786. # take app instance from any VM to be removed
  787. app = vms[0].app
  788. if app.default_dispvm in vms:
  789. app.default_dispvm = None
  790. if app.default_netvm in vms:
  791. app.default_netvm = None
  792. del app
  793. # then remove in reverse topological order (wrt template), using naive
  794. # algorithm
  795. # this heavily depends on lack of template loops, but those are
  796. # impossible
  797. while vms:
  798. vm = vms.pop(0)
  799. # make sure that all connected VMs are going to be removed,
  800. # otherwise this will loop forever
  801. child_vms = list(getattr(vm, 'appvms', []))
  802. assert all(x in vms for x in child_vms)
  803. if child_vms:
  804. # if still something use this VM, put it at the end of queue
  805. # and try next one
  806. vms.append(vm)
  807. continue
  808. self._remove_vm_qubes(vm)
  809. # release startup_lock, if anything was waiting at vm.start(),
  810. # it will detect the VM is gone
  811. for vm in locked_vms:
  812. vm.startup_lock.release()
  813. def remove_test_vms(self, xmlpath=XMLPATH, prefix=VMPREFIX):
  814. """Aggressively remove any domain that has name in testing namespace.
  815. :param prefix: name prefix of VMs to remove, can be a list of prefixes
  816. """
  817. if isinstance(prefix, str):
  818. prefixes = [prefix]
  819. else:
  820. prefixes = prefix
  821. del prefix
  822. # first, remove them Qubes-way
  823. if os.path.exists(xmlpath):
  824. try:
  825. try:
  826. app = self.app
  827. except AttributeError:
  828. app = qubes.Qubes(xmlpath)
  829. try:
  830. host_app = self.host_app
  831. except AttributeError:
  832. host_app = qubes.Qubes()
  833. self.remove_vms([vm for vm in app.domains
  834. if any(
  835. vm.name.startswith(prefix) for prefix in prefixes) or
  836. (isinstance(vm,
  837. qubes.vm.dispvm.DispVM) and vm.name
  838. not in host_app.domains)])
  839. if not hasattr(self, 'host_app'):
  840. host_app.close()
  841. del host_app
  842. if not hasattr(self, 'app'):
  843. app.close()
  844. del app
  845. except qubes.exc.QubesException:
  846. pass
  847. os.unlink(xmlpath)
  848. # now remove what was only in libvirt
  849. conn = libvirt.open(qubes.config.defaults['libvirt_uri'])
  850. for dom in conn.listAllDomains():
  851. if any(dom.name().startswith(prefix) for prefix in prefixes):
  852. self._remove_vm_libvirt(dom)
  853. conn.close()
  854. # finally remove anything that is left on disk
  855. vmnames = set()
  856. for dirspec in (
  857. 'qubes_appvms_dir',
  858. 'qubes_templates_dir'):
  859. dirpath = os.path.join(qubes.config.qubes_base_dir,
  860. qubes.config.system_path[dirspec])
  861. if not os.path.exists(dirpath):
  862. continue
  863. for name in os.listdir(dirpath):
  864. if any(name.startswith(prefix) for prefix in prefixes):
  865. vmnames.add(name)
  866. for vmname in vmnames:
  867. self._remove_vm_disk(vmname)
  868. for prefix in prefixes:
  869. self._remove_vm_disk_lvm(prefix)
  870. def qrexec_policy(self, service, source, destination, allow=True,
  871. action=None):
  872. """
  873. Allow qrexec calls for duration of the test
  874. :param service: service name
  875. :param source: source VM name
  876. :param destination: destination VM name
  877. :param allow: add rule with 'allow' action, otherwise 'deny'
  878. :param action: custom action, if specified *allow* argument is ignored
  879. :return:
  880. """
  881. return _QrexecPolicyContext(service, source, destination,
  882. allow=allow, action=action)
  883. @asyncio.coroutine
  884. def wait_for_window_hide_coro(self, title, winid, timeout=30):
  885. """
  886. Wait for window do disappear
  887. :param winid: window id
  888. :return:
  889. """
  890. wait_count = 0
  891. while subprocess.call(['xdotool', 'getwindowname', str(winid)],
  892. stdout=subprocess.DEVNULL,
  893. stderr=subprocess.STDOUT) == 0:
  894. wait_count += 1
  895. if wait_count > timeout * 10:
  896. self.fail("Timeout while waiting for {}({}) window to "
  897. "disappear".format(title, winid))
  898. yield from asyncio.sleep(0.1)
  899. @asyncio.coroutine
  900. def wait_for_window_coro(self, title, search_class=False, timeout=30,
  901. show=True):
  902. """
  903. Wait for a window with a given title. Depending on show parameter,
  904. it will wait for either window to show or to disappear.
  905. :param title: title of the window to wait for
  906. :param timeout: timeout of the operation, in seconds
  907. :param show: if True - wait for the window to be visible,
  908. otherwise - to not be visible
  909. :param search_class: search based on window class instead of title
  910. :return: window id of found window, if show=True
  911. """
  912. xdotool_search = ['xdotool', 'search', '--onlyvisible']
  913. if search_class:
  914. xdotool_search.append('--class')
  915. else:
  916. xdotool_search.append('--name')
  917. if show:
  918. xdotool_search.append('--sync')
  919. if not show:
  920. try:
  921. winid = subprocess.check_output(xdotool_search + [title],
  922. stderr=subprocess.DEVNULL).decode()
  923. except subprocess.CalledProcessError:
  924. # already gone
  925. return
  926. yield from self.wait_for_window_hide_coro(winid, title,
  927. timeout=timeout)
  928. return
  929. winid = None
  930. while not winid:
  931. p = yield from asyncio.create_subprocess_exec(
  932. *xdotool_search, title,
  933. stderr=subprocess.DEVNULL, stdout=subprocess.PIPE)
  934. try:
  935. (winid, _) = yield from asyncio.wait_for(
  936. p.communicate(), timeout)
  937. # don't check exit code, getting winid on stdout is enough
  938. # indicator of success; specifically ignore xdotool failing
  939. # with BadWindow or such - when some window appears only for a
  940. # moment by xdotool didn't manage to get its properties
  941. except asyncio.TimeoutError:
  942. self.fail(
  943. "Timeout while waiting for {} window to show".format(title))
  944. return winid.decode().strip()
  945. def wait_for_window(self, *args, **kwargs):
  946. """
  947. Wait for a window with a given title. Depending on show parameter,
  948. it will wait for either window to show or to disappear.
  949. :param title: title of the window to wait for
  950. :param timeout: timeout of the operation, in seconds
  951. :param show: if True - wait for the window to be visible,
  952. otherwise - to not be visible
  953. :param search_class: search based on window class instead of title
  954. :return: window id of found window, if show=True
  955. """
  956. return self.loop.run_until_complete(
  957. self.wait_for_window_coro(*args, **kwargs))
  958. def enter_keys_in_window(self, title, keys):
  959. """
  960. Search for window with given title, then enter listed keys there.
  961. The function will wait for said window to appear.
  962. :param title: title of window
  963. :param keys: list of keys to enter, as for `xdotool key`
  964. :return: None
  965. """
  966. # 'xdotool search --sync' sometimes crashes on some race when
  967. # accessing window properties
  968. self.wait_for_window(title)
  969. command = ['xdotool', 'search', '--name', title,
  970. 'windowactivate', '--sync',
  971. 'key'] + keys
  972. subprocess.check_call(command)
  973. def shutdown_and_wait(self, vm, timeout=60):
  974. try:
  975. self.loop.run_until_complete(
  976. vm.shutdown(wait=True, timeout=timeout))
  977. except qubes.exc.QubesException:
  978. name = vm.name
  979. del vm
  980. self.fail("Timeout while waiting for VM {} shutdown".format(name))
  981. def prepare_hvm_system_linux(self, vm, init_script, extra_files=None):
  982. if not os.path.exists('/usr/lib/grub/i386-pc'):
  983. self.skipTest('grub2 not installed')
  984. if not spawn.find_executable('grub2-install'):
  985. self.skipTest('grub2-tools not installed')
  986. if not spawn.find_executable('dracut'):
  987. self.skipTest('dracut not installed')
  988. # create a single partition
  989. p = subprocess.Popen(['sfdisk', '-q', '-L', vm.storage.root_img],
  990. stdin=subprocess.PIPE,
  991. stdout=subprocess.DEVNULL,
  992. stderr=subprocess.STDOUT)
  993. p.communicate('2048,\n')
  994. assert p.returncode == 0, 'sfdisk failed'
  995. # TODO: check if root_img is really file, not already block device
  996. p = subprocess.Popen(['sudo', 'losetup', '-f', '-P', '--show',
  997. vm.storage.root_img], stdout=subprocess.PIPE)
  998. (loopdev, _) = p.communicate()
  999. loopdev = loopdev.strip()
  1000. looppart = loopdev + 'p1'
  1001. assert p.returncode == 0, 'losetup failed'
  1002. subprocess.check_call(['sudo', 'mkfs.ext2', '-q', '-F', looppart])
  1003. mountpoint = tempfile.mkdtemp()
  1004. subprocess.check_call(['sudo', 'mount', looppart, mountpoint])
  1005. try:
  1006. subprocess.check_call(['sudo', 'grub2-install',
  1007. '--target', 'i386-pc',
  1008. '--modules', 'part_msdos ext2',
  1009. '--boot-directory', mountpoint, loopdev],
  1010. stderr=subprocess.DEVNULL
  1011. )
  1012. grub_cfg = '{}/grub2/grub.cfg'.format(mountpoint)
  1013. subprocess.check_call(
  1014. ['sudo', 'chown', '-R', os.getlogin(), mountpoint])
  1015. with open(grub_cfg, 'w') as f:
  1016. f.write(
  1017. "set timeout=1\n"
  1018. "menuentry 'Default' {\n"
  1019. " linux /vmlinuz root=/dev/xvda1 "
  1020. "rd.driver.blacklist=bochs_drm "
  1021. "rd.driver.blacklist=uhci_hcd console=hvc0\n"
  1022. " initrd /initrd\n"
  1023. "}"
  1024. )
  1025. p = subprocess.Popen(['uname', '-r'], stdout=subprocess.PIPE)
  1026. (kernel_version, _) = p.communicate()
  1027. kernel_version = kernel_version.strip()
  1028. kernel = '/boot/vmlinuz-{}'.format(kernel_version)
  1029. shutil.copy(kernel, os.path.join(mountpoint, 'vmlinuz'))
  1030. init_path = os.path.join(mountpoint, 'init')
  1031. with open(init_path, 'w') as f:
  1032. f.write(init_script)
  1033. os.chmod(init_path, 0o755)
  1034. dracut_args = [
  1035. '--kver', kernel_version,
  1036. '--include', init_path,
  1037. '/usr/lib/dracut/hooks/pre-pivot/initscript.sh',
  1038. '--no-hostonly', '--nolvmconf', '--nomdadmconf',
  1039. ]
  1040. if extra_files:
  1041. dracut_args += ['--install', ' '.join(extra_files)]
  1042. subprocess.check_call(
  1043. ['dracut'] + dracut_args + [os.path.join(mountpoint,
  1044. 'initrd')],
  1045. stderr=subprocess.DEVNULL
  1046. )
  1047. finally:
  1048. subprocess.check_call(['sudo', 'umount', mountpoint])
  1049. shutil.rmtree(mountpoint)
  1050. subprocess.check_call(['sudo', 'losetup', '-d', loopdev])
  1051. def create_bootable_iso(self):
  1052. """Create simple bootable ISO image.
  1053. Type 'poweroff' to it to terminate that VM.
  1054. """
  1055. isolinux_cfg = (
  1056. 'prompt 1\n'
  1057. 'label poweroff\n'
  1058. ' kernel poweroff.c32\n'
  1059. )
  1060. output_fd, output_path = tempfile.mkstemp('.iso')
  1061. with tempfile.TemporaryDirectory() as tmp_dir:
  1062. try:
  1063. shutil.copy('/usr/share/syslinux/isolinux.bin', tmp_dir)
  1064. shutil.copy('/usr/share/syslinux/ldlinux.c32', tmp_dir)
  1065. shutil.copy('/usr/share/syslinux/poweroff.c32', tmp_dir)
  1066. with open(os.path.join(tmp_dir, 'isolinux.cfg'), 'w') as cfg:
  1067. cfg.write(isolinux_cfg)
  1068. subprocess.check_call(['genisoimage', '-o', output_path,
  1069. '-c', 'boot.cat',
  1070. '-b', 'isolinux.bin',
  1071. '-no-emul-boot',
  1072. '-boot-load-size', '4',
  1073. '-boot-info-table',
  1074. '-q',
  1075. tmp_dir])
  1076. except FileNotFoundError:
  1077. self.skipTest('syslinux or genisoimage not installed')
  1078. os.close(output_fd)
  1079. self.addCleanup(os.unlink, output_path)
  1080. return output_path
  1081. def create_local_file(self, filename, content, mode='w'):
  1082. with open(filename, mode) as file:
  1083. file.write(content)
  1084. self.addCleanup(os.unlink, filename)
  1085. def create_remote_file(self, vm, filename, content, mode=0o755):
  1086. self.loop.run_until_complete(vm.run_for_stdio(
  1087. 'cat > {0}; chmod {1:o} {0}'.format(shlex.quote(filename), mode),
  1088. user='root', input=content.encode('utf-8')))
  1089. @asyncio.coroutine
  1090. def wait_for_session(self, vm):
  1091. timeout = vm.qrexec_timeout
  1092. if getattr(vm, 'template', None) and 'whonix-ws' in vm.template.name:
  1093. # first boot of whonix-ws takes more time because of /home
  1094. # initialization, including Tor Browser copying
  1095. timeout = 120
  1096. yield from asyncio.wait_for(
  1097. vm.run_service_for_stdio(
  1098. 'qubes.WaitForSession', input=vm.default_user.encode()),
  1099. timeout=timeout)
  1100. @asyncio.coroutine
  1101. def start_vm(self, vm):
  1102. """Start a VM and wait for it to be fully up"""
  1103. yield from vm.start()
  1104. yield from self.wait_for_session(vm)
  1105. _templates = None
  1106. def list_templates():
  1107. """Returns tuple of template names available in the system."""
  1108. global _templates
  1109. if _templates is None:
  1110. if 'QUBES_TEST_TEMPLATES' in os.environ:
  1111. _templates = os.environ['QUBES_TEST_TEMPLATES'].split()
  1112. if _templates is None:
  1113. try:
  1114. app = qubes.Qubes()
  1115. _templates = tuple(vm.name for vm in app.domains
  1116. if isinstance(vm,
  1117. qubes.vm.templatevm.TemplateVM) and
  1118. vm.features.get('os', None) != 'Windows')
  1119. app.close()
  1120. del app
  1121. except OSError:
  1122. _templates = ()
  1123. return _templates
  1124. def create_testcases_for_templates(name, *bases, module, **kwds):
  1125. """Do-it-all helper for generating per-template tests via load_tests proto
  1126. This does several things:
  1127. - creates per-template classes
  1128. - adds them to module's :py:func:`globals`
  1129. - returns an iterable suitable for passing to loader.loadTestsFromNames
  1130. TestCase classes created by this function have implicit `.template`
  1131. attribute, which contains name of the respective template. They are also
  1132. named with given prefix, underscore and template name. If template name
  1133. contains characters not valid as part of Python identifier, they are
  1134. impossible to get via standard ``.`` operator, though :py:func:`getattr` is
  1135. still usable.
  1136. >>> class MyTestsMixIn:
  1137. ... def test_000_my_test(self):
  1138. ... assert self.template.startswith('debian')
  1139. >>> def load_tests(loader, tests, pattern):
  1140. ... tests.addTests(loader.loadTestsFromNames(
  1141. ... qubes.tests.create_testcases_for_templates(
  1142. ... 'TC_00_MyTests', MyTestsMixIn, qubes.tests.SystemTestCase,
  1143. ... module=sys.modules[__name__])))
  1144. *NOTE* adding ``module=sys.modules[__name__]`` is *mandatory*, and to allow
  1145. enforcing this, it uses keyword-only argument syntax, which is only in
  1146. Python 3.
  1147. """
  1148. # Do not attempt to grab the module from traceback, since we are actually
  1149. # a generator and loadTestsFromNames may also be a generator, so it's not
  1150. # possible to correctly guess frame from stack. Explicit is better than
  1151. # implicit!
  1152. for template in list_templates():
  1153. clsname = name + '_' + template
  1154. if hasattr(module, clsname):
  1155. continue
  1156. cls = type(clsname, bases, {'template': template, **kwds})
  1157. cls.__module__ = module.__name__
  1158. # XXX I wonder what other __dunder__ attrs did I miss
  1159. setattr(module, clsname, cls)
  1160. yield '.'.join((module.__name__, clsname))
  1161. def maybe_create_testcases_on_import(create_testcases_gen):
  1162. """If certain conditions are met, call *create_testcases_gen* to create
  1163. testcases for templates tests. The purpose is to use it on integration
  1164. tests module(s) import, so the test runner could discover tests without
  1165. using load tests protocol.
  1166. The conditions - any of:
  1167. - QUBES_TEST_TEMPLATES present in the environment (it's possible to
  1168. create test cases without opening qubes.xml)
  1169. - QUBES_TEST_LOAD_ALL present in the environment
  1170. """
  1171. if 'QUBES_TEST_TEMPLATES' in os.environ or \
  1172. 'QUBES_TEST_LOAD_ALL' in os.environ:
  1173. list(create_testcases_gen())
  1174. def extra_info(obj):
  1175. """Return short info identifying object.
  1176. For example, if obj is a qube, return its name. This is for use with
  1177. :py:mod:`objgraph` package.
  1178. """
  1179. # Feel free to extend to other cases.
  1180. if isinstance(obj, qubes.vm.qubesvm.QubesVM):
  1181. try:
  1182. return obj.name
  1183. except AttributeError:
  1184. pass
  1185. if isinstance(obj, unittest.TestCase):
  1186. return obj.id()
  1187. return ''
  1188. def load_tests(loader, tests, pattern): # pylint: disable=unused-argument
  1189. # discard any tests from this module, because it hosts base classes
  1190. tests = unittest.TestSuite()
  1191. for modname in (
  1192. # unit tests
  1193. 'qubes.tests.events',
  1194. 'qubes.tests.devices',
  1195. 'qubes.tests.devices_block',
  1196. 'qubes.tests.firewall',
  1197. 'qubes.tests.init',
  1198. 'qubes.tests.vm.init',
  1199. 'qubes.tests.storage',
  1200. 'qubes.tests.storage_file',
  1201. 'qubes.tests.storage_reflink',
  1202. 'qubes.tests.storage_lvm',
  1203. 'qubes.tests.storage_callback',
  1204. 'qubes.tests.storage_kernels',
  1205. 'qubes.tests.ext',
  1206. 'qubes.tests.vm.qubesvm',
  1207. 'qubes.tests.vm.mix.net',
  1208. 'qubes.tests.vm.adminvm',
  1209. 'qubes.tests.vm.appvm',
  1210. 'qubes.tests.vm.dispvm',
  1211. 'qubes.tests.app',
  1212. 'qubes.tests.tarwriter',
  1213. 'qubes.tests.api',
  1214. 'qubes.tests.api_admin',
  1215. 'qubes.tests.api_misc',
  1216. 'qubes.tests.api_internal',
  1217. 'qubes.tests.rpc_import',
  1218. ):
  1219. tests.addTests(loader.loadTestsFromName(modname))
  1220. tests.addTests(loader.discover(
  1221. os.path.join(os.path.dirname(__file__), 'tools')))
  1222. if not in_dom0:
  1223. return tests
  1224. for modname in (
  1225. 'qrexec.tests',
  1226. 'qrexec.tests.cli',
  1227. 'qrexec.tests.gtkhelpers',
  1228. 'qrexec.tests.rpcconfirmation',
  1229. # integration tests
  1230. 'qubes.tests.integ.basic',
  1231. 'qubes.tests.integ.storage',
  1232. 'qubes.tests.integ.grub',
  1233. 'qubes.tests.integ.devices_block',
  1234. 'qubes.tests.integ.devices_pci',
  1235. 'qubes.tests.integ.qrexec',
  1236. 'qubes.tests.integ.dom0_update',
  1237. 'qubes.tests.integ.vm_update',
  1238. 'qubes.tests.integ.network',
  1239. 'qubes.tests.integ.network_ipv6',
  1240. 'qubes.tests.integ.dispvm',
  1241. 'qubes.tests.integ.vm_qrexec_gui',
  1242. 'qubes.tests.integ.mime',
  1243. 'qubes.tests.integ.salt',
  1244. 'qubes.tests.integ.backup',
  1245. 'qubes.tests.integ.backupcompatibility',
  1246. 'qubes.tests.integ.backupdispvm',
  1247. # external modules
  1248. 'qubes.tests.extra',
  1249. ):
  1250. tests.addTests(loader.loadTestsFromName(modname))
  1251. return tests