2015-01-19 18:03:23 +01:00
|
|
|
#!/usr/bin/python2 -O
|
|
|
|
# vim: fileencoding=utf-8
|
2015-10-17 00:10:15 +02:00
|
|
|
# pylint: disable=invalid-name
|
2015-01-19 18:03:23 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
2015-10-17 00:10:15 +02:00
|
|
|
# Copyright (C) 2014-2015
|
|
|
|
# Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
|
2015-01-19 18:03:23 +01:00
|
|
|
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
2015-01-05 14:41:59 +01:00
|
|
|
|
2015-01-05 15:39:14 +01:00
|
|
|
import collections
|
2015-10-17 00:10:15 +02:00
|
|
|
import multiprocessing
|
|
|
|
import logging
|
2015-01-13 23:17:18 +01:00
|
|
|
import os
|
2015-10-17 00:10:15 +02:00
|
|
|
import shutil
|
2015-01-13 23:17:18 +01:00
|
|
|
import subprocess
|
2015-10-17 00:10:15 +02:00
|
|
|
import sys
|
2015-01-05 14:41:59 +01:00
|
|
|
import unittest
|
|
|
|
|
2015-01-11 01:19:03 +01:00
|
|
|
import lxml.etree
|
|
|
|
|
2015-01-05 17:01:13 +01:00
|
|
|
import qubes.config
|
2015-01-05 15:39:14 +01:00
|
|
|
import qubes.events
|
|
|
|
|
2015-10-27 11:39:59 +01:00
|
|
|
XMLPATH = '/var/lib/qubes/qubes-test.xml'
|
2015-12-29 01:53:47 +01:00
|
|
|
TEMPLATE = 'fedora-23'
|
2015-10-17 00:10:15 +02:00
|
|
|
VMPREFIX = 'test-'
|
|
|
|
|
2015-01-05 17:01:13 +01:00
|
|
|
|
|
|
|
#: :py:obj:`True` if running in dom0, :py:obj:`False` otherwise
|
|
|
|
in_dom0 = False
|
|
|
|
|
2015-01-19 17:06:30 +01:00
|
|
|
#: :py:obj:`False` if outside of git repo,
|
|
|
|
#: path to root of the directory otherwise
|
2015-01-13 23:17:18 +01:00
|
|
|
in_git = False
|
|
|
|
|
2015-01-05 17:01:13 +01:00
|
|
|
try:
|
|
|
|
import libvirt
|
|
|
|
libvirt.openReadOnly(qubes.config.defaults['libvirt_uri']).close()
|
|
|
|
in_dom0 = True
|
|
|
|
except libvirt.libvirtError:
|
|
|
|
pass
|
|
|
|
|
2015-01-13 23:17:18 +01:00
|
|
|
try:
|
2015-01-19 17:06:30 +01:00
|
|
|
in_git = subprocess.check_output(
|
|
|
|
['git', 'rev-parse', '--show-toplevel']).strip()
|
2015-09-23 16:25:53 +02:00
|
|
|
qubes.log.LOGPATH = '/tmp'
|
|
|
|
qubes.log.LOGFILE = '/tmp/qubes.log'
|
2015-01-13 23:17:18 +01:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
# git returned nonzero, we are outside git repo
|
|
|
|
pass
|
|
|
|
except OSError:
|
|
|
|
# command not found; let's assume we're outside
|
|
|
|
pass
|
|
|
|
|
2015-01-05 17:01:13 +01:00
|
|
|
|
|
|
|
def skipUnlessDom0(test_item):
|
|
|
|
'''Decorator that skips test outside dom0.
|
|
|
|
|
|
|
|
Some tests (especially integration tests) have to be run in more or less
|
|
|
|
working dom0. This is checked by connecting to libvirt.
|
2015-10-17 00:10:15 +02:00
|
|
|
'''
|
2015-01-05 19:15:32 +01:00
|
|
|
|
2015-01-05 17:01:13 +01:00
|
|
|
return unittest.skipUnless(in_dom0, 'outside dom0')(test_item)
|
|
|
|
|
|
|
|
|
2015-01-13 23:17:18 +01:00
|
|
|
def skipUnlessGit(test_item):
|
|
|
|
'''Decorator that skips test outside git repo.
|
|
|
|
|
|
|
|
There are very few tests that an be run only in git. One example is
|
|
|
|
correctness of example code that won't get included in RPM.
|
2015-10-17 00:10:15 +02:00
|
|
|
'''
|
2015-01-13 23:17:18 +01:00
|
|
|
|
|
|
|
return unittest.skipUnless(in_git, 'outside git tree')(test_item)
|
|
|
|
|
|
|
|
|
2015-01-05 15:39:14 +01:00
|
|
|
class TestEmitter(qubes.events.Emitter):
|
|
|
|
'''Dummy event emitter which records events fired on it.
|
|
|
|
|
|
|
|
Events are counted in :py:attr:`fired_events` attribute, which is
|
|
|
|
:py:class:`collections.Counter` instance. For each event, ``(event, args,
|
|
|
|
kwargs)`` object is counted. *event* is event name (a string), *args* is
|
|
|
|
tuple with positional arguments and *kwargs* is sorted tuple of items from
|
|
|
|
keyword arguments.
|
|
|
|
|
|
|
|
>>> emitter = TestEmitter()
|
|
|
|
>>> emitter.fired_events
|
|
|
|
Counter()
|
|
|
|
>>> emitter.fire_event('event', 1, 2, 3, spam='eggs', foo='bar')
|
|
|
|
>>> emitter.fired_events
|
|
|
|
Counter({('event', (1, 2, 3), (('foo', 'bar'), ('spam', 'eggs'))): 1})
|
|
|
|
'''
|
2015-01-05 19:15:32 +01:00
|
|
|
|
2015-01-05 15:39:14 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(TestEmitter, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
#: :py:class:`collections.Counter` instance
|
|
|
|
self.fired_events = collections.Counter()
|
|
|
|
|
|
|
|
def fire_event(self, event, *args, **kwargs):
|
|
|
|
super(TestEmitter, self).fire_event(event, *args, **kwargs)
|
|
|
|
self.fired_events[(event, args, tuple(sorted(kwargs.items())))] += 1
|
|
|
|
|
|
|
|
def fire_event_pre(self, event, *args, **kwargs):
|
|
|
|
super(TestEmitter, self).fire_event_pre(event, *args, **kwargs)
|
|
|
|
self.fired_events[(event, args, tuple(sorted(kwargs.items())))] += 1
|
|
|
|
|
|
|
|
|
2015-10-17 00:10:15 +02:00
|
|
|
class _AssertNotRaisesContext(object):
|
|
|
|
"""A context manager used to implement TestCase.assertNotRaises methods.
|
|
|
|
|
|
|
|
Stolen from unittest and hacked. Regexp support stripped.
|
|
|
|
""" # pylint: disable=too-few-public-methods
|
|
|
|
|
|
|
|
def __init__(self, expected, test_case, expected_regexp=None):
|
|
|
|
if expected_regexp is not None:
|
|
|
|
raise NotImplementedError('expected_regexp is unsupported')
|
|
|
|
|
|
|
|
self.expected = expected
|
|
|
|
self.exception = None
|
|
|
|
|
|
|
|
self.failureException = test_case.failureException
|
|
|
|
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, tb):
|
|
|
|
if exc_type is None:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if issubclass(exc_type, self.expected):
|
|
|
|
raise self.failureException(
|
2016-02-10 17:06:45 +01:00
|
|
|
"{!r} raised".format(exc_value))
|
2015-10-17 00:10:15 +02:00
|
|
|
else:
|
|
|
|
# pass through
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.exception = exc_value # store for later retrieval
|
|
|
|
|
|
|
|
|
|
|
|
class BeforeCleanExit(BaseException):
|
|
|
|
'''Raised from :py:meth:`QubesTestCase.tearDown` when
|
|
|
|
:py:attr:`qubes.tests.run.QubesDNCTestResult.do_not_clean` is set.'''
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-01-05 14:41:59 +01:00
|
|
|
class QubesTestCase(unittest.TestCase):
|
2015-01-05 15:39:14 +01:00
|
|
|
'''Base class for Qubes unit tests.
|
|
|
|
'''
|
2015-01-05 19:15:32 +01:00
|
|
|
|
2015-10-17 00:10:15 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(QubesTestCase, self).__init__(*args, **kwargs)
|
|
|
|
self.longMessage = True
|
|
|
|
self.log = logging.getLogger('{}.{}.{}'.format(
|
|
|
|
self.__class__.__module__,
|
|
|
|
self.__class__.__name__,
|
|
|
|
self._testMethodName))
|
|
|
|
|
|
|
|
|
2015-01-05 14:41:59 +01:00
|
|
|
def __str__(self):
|
|
|
|
return '{}/{}/{}'.format(
|
|
|
|
'.'.join(self.__class__.__module__.split('.')[2:]),
|
|
|
|
self.__class__.__name__,
|
|
|
|
self._testMethodName)
|
2015-01-05 15:39:14 +01:00
|
|
|
|
|
|
|
|
2015-10-17 00:10:15 +02:00
|
|
|
def tearDown(self):
|
|
|
|
super(QubesTestCase, self).tearDown()
|
|
|
|
|
|
|
|
result = self._resultForDoCleanups
|
|
|
|
failed_test_cases = result.failures \
|
|
|
|
+ result.errors \
|
|
|
|
+ [(tc, None) for tc in result.unexpectedSuccesses]
|
|
|
|
|
|
|
|
if getattr(result, 'do_not_clean', False) \
|
|
|
|
and any(tc is self for tc, exc in failed_test_cases):
|
|
|
|
raise BeforeCleanExit()
|
|
|
|
|
|
|
|
|
|
|
|
def assertNotRaises(self, excClass, callableObj=None, *args, **kwargs):
|
|
|
|
"""Fail if an exception of class excClass is raised
|
|
|
|
by callableObj when invoked with arguments args and keyword
|
|
|
|
arguments kwargs. If a different type of exception is
|
|
|
|
raised, it will not be caught, and the test case will be
|
|
|
|
deemed to have suffered an error, exactly as for an
|
|
|
|
unexpected exception.
|
|
|
|
|
|
|
|
If called with callableObj omitted or None, will return a
|
|
|
|
context object used like this::
|
|
|
|
|
|
|
|
with self.assertRaises(SomeException):
|
|
|
|
do_something()
|
|
|
|
|
|
|
|
The context manager keeps a reference to the exception as
|
|
|
|
the 'exception' attribute. This allows you to inspect the
|
|
|
|
exception after the assertion::
|
|
|
|
|
|
|
|
with self.assertRaises(SomeException) as cm:
|
|
|
|
do_something()
|
|
|
|
the_exception = cm.exception
|
|
|
|
self.assertEqual(the_exception.error_code, 3)
|
|
|
|
"""
|
|
|
|
context = _AssertNotRaisesContext(excClass, self)
|
|
|
|
if callableObj is None:
|
|
|
|
return context
|
|
|
|
with context:
|
|
|
|
callableObj(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2015-01-08 17:32:45 +01:00
|
|
|
def assertXMLEqual(self, xml1, xml2):
|
|
|
|
'''Check for equality of two XML objects.
|
|
|
|
|
|
|
|
:param xml1: first element
|
|
|
|
:param xml2: second element
|
|
|
|
:type xml1: :py:class:`lxml.etree._Element`
|
|
|
|
:type xml2: :py:class:`lxml.etree._Element`
|
2016-03-03 01:05:23 +01:00
|
|
|
''' # pylint: disable=invalid-name
|
2015-01-19 19:02:28 +01:00
|
|
|
|
2015-01-08 17:32:45 +01:00
|
|
|
self.assertEqual(xml1.tag, xml2.tag)
|
|
|
|
self.assertEqual(xml1.text, xml2.text)
|
|
|
|
self.assertItemsEqual(xml1.keys(), xml2.keys())
|
|
|
|
for key in xml1.keys():
|
|
|
|
self.assertEqual(xml1.get(key), xml2.get(key))
|
|
|
|
|
|
|
|
|
2015-01-20 16:32:25 +01:00
|
|
|
def assertEventFired(self, emitter, event, args=None, kwargs=None):
|
2015-01-05 15:39:14 +01:00
|
|
|
'''Check whether event was fired on given emitter and fail if it did
|
|
|
|
not.
|
|
|
|
|
2015-01-05 19:15:32 +01:00
|
|
|
:param emitter: emitter which is being checked
|
|
|
|
:type emitter: :py:class:`TestEmitter`
|
2015-01-05 15:39:14 +01:00
|
|
|
:param str event: event identifier
|
2015-01-19 17:06:30 +01:00
|
|
|
:param list args: when given, all items must appear in args passed to \
|
|
|
|
an event
|
|
|
|
:param list kwargs: when given, all items must appear in kwargs passed \
|
|
|
|
to an event
|
2015-10-17 00:10:15 +02:00
|
|
|
'''
|
2015-01-05 15:39:14 +01:00
|
|
|
|
|
|
|
for ev, ev_args, ev_kwargs in emitter.fired_events:
|
|
|
|
if ev != event:
|
|
|
|
continue
|
2015-01-20 16:32:25 +01:00
|
|
|
if args is not None and any(i not in ev_args for i in args):
|
2015-01-05 15:39:14 +01:00
|
|
|
continue
|
2015-01-20 16:32:25 +01:00
|
|
|
if kwargs is not None and any(i not in ev_kwargs for i in kwargs):
|
2015-01-05 15:39:14 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
self.fail('event {!r} did not fire on {!r}'.format(event, emitter))
|
|
|
|
|
|
|
|
|
2015-01-20 16:32:25 +01:00
|
|
|
def assertEventNotFired(self, emitter, event, args=None, kwargs=None):
|
2015-01-05 15:39:14 +01:00
|
|
|
'''Check whether event was fired on given emitter. Fail if it did.
|
|
|
|
|
2015-01-05 19:15:32 +01:00
|
|
|
:param emitter: emitter which is being checked
|
|
|
|
:type emitter: :py:class:`TestEmitter`
|
2015-01-05 15:39:14 +01:00
|
|
|
:param str event: event identifier
|
2015-01-19 17:06:30 +01:00
|
|
|
:param list args: when given, all items must appear in args passed to \
|
|
|
|
an event
|
|
|
|
:param list kwargs: when given, all items must appear in kwargs passed \
|
|
|
|
to an event
|
2015-10-17 00:10:15 +02:00
|
|
|
'''
|
2015-01-05 15:39:14 +01:00
|
|
|
|
|
|
|
for ev, ev_args, ev_kwargs in emitter.fired_events:
|
|
|
|
if ev != event:
|
|
|
|
continue
|
2015-01-20 16:32:25 +01:00
|
|
|
if args is not None and any(i not in ev_args for i in args):
|
2015-01-05 15:39:14 +01:00
|
|
|
continue
|
2015-01-20 16:32:25 +01:00
|
|
|
if kwargs is not None and any(i not in ev_kwargs for i in kwargs):
|
2015-01-05 15:39:14 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
self.fail('event {!r} did fire on {!r}'.format(event, emitter))
|
|
|
|
|
|
|
|
return
|
2015-01-11 01:19:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
def assertXMLIsValid(self, xml, file=None, schema=None):
|
|
|
|
'''Check whether given XML fulfills Relax NG schema.
|
|
|
|
|
|
|
|
Schema can be given in a couple of ways:
|
|
|
|
|
|
|
|
- As separate file. This is most common, and also the only way to
|
2015-01-19 17:06:30 +01:00
|
|
|
handle file inclusion. Call with file name as second argument.
|
2015-01-11 01:19:03 +01:00
|
|
|
|
|
|
|
- As string containing actual schema. Put that string in *schema*
|
|
|
|
keyword argument.
|
|
|
|
|
|
|
|
:param lxml.etree._Element xml: XML element instance to check
|
|
|
|
:param str file: filename of Relax NG schema
|
|
|
|
:param str schema: optional explicit schema string
|
2015-10-17 00:10:15 +02:00
|
|
|
''' # pylint: disable=redefined-builtin
|
2015-01-11 01:19:03 +01:00
|
|
|
|
|
|
|
if schema is not None and file is None:
|
|
|
|
relaxng = schema
|
|
|
|
if isinstance(relaxng, str):
|
|
|
|
relaxng = lxml.etree.XML(relaxng)
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=protected-access
|
2015-01-11 01:19:03 +01:00
|
|
|
if isinstance(relaxng, lxml.etree._Element):
|
|
|
|
relaxng = lxml.etree.RelaxNG(relaxng)
|
|
|
|
|
|
|
|
elif file is not None and schema is None:
|
2015-01-13 23:17:18 +01:00
|
|
|
if not os.path.isabs(file):
|
|
|
|
basedirs = ['/usr/share/doc/qubes/relaxng']
|
|
|
|
if in_git:
|
|
|
|
basedirs.insert(0, os.path.join(in_git, 'relaxng'))
|
|
|
|
for basedir in basedirs:
|
|
|
|
abspath = os.path.join(basedir, file)
|
|
|
|
if os.path.exists(abspath):
|
|
|
|
file = abspath
|
|
|
|
break
|
2015-01-11 01:19:03 +01:00
|
|
|
relaxng = lxml.etree.RelaxNG(file=file)
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise TypeError("There should be excactly one of 'file' and "
|
|
|
|
"'schema' arguments specified.")
|
|
|
|
|
|
|
|
# We have to be extra careful here in case someone messed up with
|
|
|
|
# self.failureException. It should by default be AssertionError, just
|
|
|
|
# what is spewed by RelaxNG(), but who knows what might happen.
|
|
|
|
try:
|
|
|
|
relaxng.assert_(xml)
|
|
|
|
except self.failureException:
|
|
|
|
raise
|
|
|
|
except AssertionError as e:
|
|
|
|
self.fail(str(e))
|
2015-10-17 00:10:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SystemTestsMixin(object):
|
|
|
|
def setUp(self):
|
|
|
|
super(SystemTestsMixin, self).setUp()
|
|
|
|
self.remove_test_vms()
|
|
|
|
|
2016-02-10 17:11:50 +01:00
|
|
|
# need some information from the real qubes.xml - at least installed
|
|
|
|
# templates; should not be used for testing, only to initialize self.app
|
|
|
|
self.host_app = qubes.Qubes()
|
|
|
|
self.app = qubes.Qubes.create_empty_store(qubes.tests.XMLPATH,
|
|
|
|
default_kernel=self.host_app.default_kernel,
|
|
|
|
clockvm=None,
|
|
|
|
updatevm=None
|
|
|
|
)
|
|
|
|
|
|
|
|
def init_default_template(self, template=None):
|
|
|
|
if template is None:
|
|
|
|
template = self.host_app.default_template
|
|
|
|
|
|
|
|
template_vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM,
|
|
|
|
name=template.name,
|
|
|
|
uuid=template.uuid,
|
|
|
|
label='black')
|
|
|
|
self.app.default_template = template_vm
|
|
|
|
|
|
|
|
def reload_db(self):
|
|
|
|
self.app = qubes.Qubes(qubes.tests.XMLPATH)
|
|
|
|
|
|
|
|
def save_and_reload_db(self):
|
|
|
|
self.app.save()
|
|
|
|
self.reload_db()
|
|
|
|
|
2015-12-29 16:49:17 +01:00
|
|
|
def tearDown(self):
|
|
|
|
super(SystemTestsMixin, self).tearDown()
|
|
|
|
self.remove_test_vms()
|
2016-02-10 17:14:47 +01:00
|
|
|
# remove all references to VM objects, to release resources - most
|
|
|
|
# importantly file descriptors; this object will live
|
|
|
|
# during the whole test run, but all the file descriptors would be
|
|
|
|
# depleted earlier
|
|
|
|
del self.app
|
|
|
|
del self.host_app
|
|
|
|
for attr in dir(self):
|
|
|
|
if isinstance(getattr(self, attr), qubes.vm.BaseVM):
|
|
|
|
delattr(self, attr)
|
2015-10-17 00:10:15 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def make_vm_name(name):
|
|
|
|
return VMPREFIX + name
|
|
|
|
|
|
|
|
|
2015-12-23 14:22:40 +01:00
|
|
|
def _remove_vm_qubes(self, vm):
|
2015-10-17 00:10:15 +02:00
|
|
|
vmname = vm.name
|
2015-10-27 11:39:59 +01:00
|
|
|
app = vm.app
|
2015-10-17 00:10:15 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
# XXX .is_running() may throw libvirtError if undefined
|
|
|
|
if vm.is_running():
|
|
|
|
vm.force_shutdown()
|
|
|
|
except: # pylint: disable=bare-except
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
vm.remove_from_disk()
|
|
|
|
except: # pylint: disable=bare-except
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
vm.libvirt_domain.undefine()
|
2015-10-27 11:39:59 +01:00
|
|
|
except (AttributeError, libvirt.libvirtError):
|
2015-10-17 00:10:15 +02:00
|
|
|
pass
|
|
|
|
|
2015-10-27 11:39:59 +01:00
|
|
|
del app.domains[vm]
|
2015-10-17 00:10:15 +02:00
|
|
|
del vm
|
|
|
|
|
2015-10-27 11:39:59 +01:00
|
|
|
app.save()
|
|
|
|
del app
|
|
|
|
|
2015-10-17 00:10:15 +02:00
|
|
|
# Now ensure it really went away. This may not have happened,
|
|
|
|
# for example if vm.libvirt_domain malfunctioned.
|
|
|
|
try:
|
2015-10-27 11:39:59 +01:00
|
|
|
conn = libvirt.open(qubes.config.defaults['libvirt_uri'])
|
|
|
|
dom = conn.lookupByName(vmname)
|
2015-10-17 00:10:15 +02:00
|
|
|
except: # pylint: disable=bare-except
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self._remove_vm_libvirt(dom)
|
|
|
|
|
|
|
|
self._remove_vm_disk(vmname)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _remove_vm_libvirt(dom):
|
|
|
|
try:
|
|
|
|
dom.destroy()
|
|
|
|
except libvirt.libvirtError: # not running
|
|
|
|
pass
|
|
|
|
dom.undefine()
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _remove_vm_disk(vmname):
|
|
|
|
for dirspec in (
|
|
|
|
'qubes_appvms_dir',
|
|
|
|
'qubes_servicevms_dir',
|
|
|
|
'qubes_templates_dir'):
|
|
|
|
dirpath = os.path.join(qubes.config.system_path['qubes_base_dir'],
|
|
|
|
qubes.config.system_path[dirspec], vmname)
|
|
|
|
if os.path.exists(dirpath):
|
|
|
|
if os.path.isdir(dirpath):
|
|
|
|
shutil.rmtree(dirpath)
|
|
|
|
else:
|
|
|
|
os.unlink(dirpath)
|
|
|
|
|
|
|
|
|
|
|
|
def remove_vms(self, vms):
|
|
|
|
for vm in vms:
|
|
|
|
self._remove_vm_qubes(vm)
|
|
|
|
|
|
|
|
|
|
|
|
def remove_test_vms(self):
|
|
|
|
'''Aggresively remove any domain that has name in testing namespace.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
The test suite hereby claims any domain whose name starts with
|
|
|
|
:py:data:`VMPREFIX` as fair game. This is needed to enforce sane
|
|
|
|
test executing environment. If you have domains named ``test-*``,
|
|
|
|
don't run the tests.
|
|
|
|
'''
|
|
|
|
|
|
|
|
# first, remove them Qubes-way
|
2015-10-27 11:39:59 +01:00
|
|
|
if os.path.exists(XMLPATH):
|
|
|
|
self.remove_vms(vm for vm in qubes.Qubes(XMLPATH).domains
|
|
|
|
if vm.name != TEMPLATE)
|
|
|
|
os.unlink(XMLPATH)
|
2015-10-17 00:10:15 +02:00
|
|
|
|
|
|
|
# now remove what was only in libvirt
|
2015-10-27 11:39:59 +01:00
|
|
|
conn = libvirt.open(qubes.config.defaults['libvirt_uri'])
|
|
|
|
for dom in conn.listAllDomains():
|
2015-10-17 00:10:15 +02:00
|
|
|
if dom.name().startswith(VMPREFIX):
|
|
|
|
self._remove_vm_libvirt(dom)
|
2016-02-10 17:14:47 +01:00
|
|
|
conn.close()
|
2015-10-17 00:10:15 +02:00
|
|
|
|
|
|
|
# finally remove anything that is left on disk
|
|
|
|
vmnames = set()
|
|
|
|
for dirspec in (
|
|
|
|
'qubes_appvms_dir',
|
|
|
|
'qubes_servicevms_dir',
|
|
|
|
'qubes_templates_dir'):
|
|
|
|
dirpath = os.path.join(qubes.config.system_path['qubes_base_dir'],
|
|
|
|
qubes.config.system_path[dirspec])
|
|
|
|
for name in os.listdir(dirpath):
|
|
|
|
if name.startswith(VMPREFIX):
|
|
|
|
vmnames.add(name)
|
|
|
|
for vmname in vmnames:
|
|
|
|
self._remove_vm_disk(vmname)
|
|
|
|
|
|
|
|
|
|
|
|
def load_tests(loader, tests, pattern): # pylint: disable=unused-argument
|
|
|
|
# discard any tests from this module, because it hosts base classes
|
|
|
|
tests = unittest.TestSuite()
|
|
|
|
|
|
|
|
for modname in (
|
|
|
|
# unit tests
|
|
|
|
'qubes.tests.events',
|
2015-12-30 00:22:47 +01:00
|
|
|
'qubes.tests.init1',
|
2015-10-17 00:10:15 +02:00
|
|
|
'qubes.tests.vm.init',
|
|
|
|
'qubes.tests.vm.qubesvm',
|
|
|
|
'qubes.tests.vm.adminvm',
|
2015-12-30 00:22:47 +01:00
|
|
|
'qubes.tests.init2',
|
2015-10-17 00:10:15 +02:00
|
|
|
'qubes.tests.tools',
|
|
|
|
|
|
|
|
# integration tests
|
2015-10-27 11:39:59 +01:00
|
|
|
# 'qubes.tests.int.basic',
|
2015-10-17 00:10:15 +02:00
|
|
|
# 'qubes.tests.dom0_update',
|
|
|
|
# 'qubes.tests.network',
|
|
|
|
# 'qubes.tests.vm_qrexec_gui',
|
|
|
|
# 'qubes.tests.backup',
|
|
|
|
# 'qubes.tests.backupcompatibility',
|
|
|
|
# 'qubes.tests.regressions',
|
2015-10-27 11:39:59 +01:00
|
|
|
|
|
|
|
# tool tests
|
|
|
|
'qubes.tests.int.tools.qubes_create',
|
2015-12-29 01:53:47 +01:00
|
|
|
'qubes.tests.int.tools.qvm_run',
|
2015-10-17 00:10:15 +02:00
|
|
|
):
|
|
|
|
tests.addTests(loader.loadTestsFromName(modname))
|
|
|
|
|
|
|
|
return tests
|