2016-03-03 11:48:21 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016
|
|
|
|
# Marek Marczykowski-Górecki <marmarek@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.
|
|
|
|
#
|
|
|
|
|
2016-07-04 16:51:35 +02:00
|
|
|
import sys
|
2016-03-03 11:48:21 +01:00
|
|
|
import pkg_resources
|
|
|
|
import qubes.tests
|
2016-04-11 13:50:19 +02:00
|
|
|
import qubes.vm.appvm
|
|
|
|
import qubes.vm.templatevm
|
2016-03-03 11:48:21 +01:00
|
|
|
|
|
|
|
|
2016-04-11 13:50:19 +02:00
|
|
|
class ExtraTestCase(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase):
|
2016-03-03 11:48:21 +01:00
|
|
|
|
|
|
|
template = None
|
|
|
|
|
2016-04-20 02:26:16 +02:00
|
|
|
def setUp(self):
|
|
|
|
super(ExtraTestCase, self).setUp()
|
|
|
|
self.init_default_template(self.template)
|
|
|
|
|
2016-03-03 11:48:21 +01:00
|
|
|
def create_vms(self, names):
|
|
|
|
"""
|
|
|
|
Create AppVMs for the duration of the test. Will be automatically
|
|
|
|
removed after completing the test.
|
|
|
|
:param names: list of VM names to create (each of them will be
|
|
|
|
prefixed with some test specific string)
|
|
|
|
:return: list of created VM objects
|
|
|
|
"""
|
|
|
|
if self.template:
|
2016-04-11 13:50:19 +02:00
|
|
|
template = self.app.domains[self.template]
|
2016-03-03 11:48:21 +01:00
|
|
|
else:
|
2016-04-11 13:50:19 +02:00
|
|
|
template = self.app.default_template
|
2016-03-03 11:48:21 +01:00
|
|
|
for vmname in names:
|
2016-04-11 13:50:19 +02:00
|
|
|
vm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
|
2016-03-03 11:48:21 +01:00
|
|
|
name=self.make_vm_name(vmname),
|
2016-04-20 02:26:16 +02:00
|
|
|
template=template,
|
|
|
|
label='red')
|
|
|
|
vm.create_on_disk()
|
2016-03-03 11:48:21 +01:00
|
|
|
self.save_and_reload_db()
|
|
|
|
|
|
|
|
# get objects after reload
|
|
|
|
vms = []
|
|
|
|
for vmname in names:
|
2016-04-20 02:26:16 +02:00
|
|
|
vms.append(self.app.domains[self.make_vm_name(vmname)])
|
2016-03-03 11:48:21 +01:00
|
|
|
return vms
|
|
|
|
|
|
|
|
def enable_network(self):
|
|
|
|
"""
|
|
|
|
Enable access to the network. Must be called before creating VMs.
|
|
|
|
"""
|
2016-04-11 13:50:19 +02:00
|
|
|
self.init_networking()
|
2016-03-03 11:48:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def load_tests(loader, tests, pattern):
|
|
|
|
for entry in pkg_resources.iter_entry_points('qubes.tests.extra'):
|
2016-07-04 16:51:35 +02:00
|
|
|
try:
|
|
|
|
for test_case in entry.load()():
|
|
|
|
tests.addTests(loader.loadTestsFromTestCase(test_case))
|
|
|
|
except Exception as err: # pylint: disable=broad-except
|
|
|
|
def runTest(self):
|
2017-01-18 22:16:46 +01:00
|
|
|
raise err
|
2016-07-04 16:51:35 +02:00
|
|
|
ExtraLoadFailure = type('ExtraLoadFailure',
|
|
|
|
(qubes.tests.QubesTestCase,),
|
|
|
|
{entry.name: runTest})
|
|
|
|
tests.addTest(ExtraLoadFailure(entry.name))
|
2016-03-03 11:48:21 +01:00
|
|
|
|
|
|
|
try:
|
2016-04-11 13:50:19 +02:00
|
|
|
app = qubes.Qubes()
|
|
|
|
templates = [vm.name for vm in app.domains if
|
|
|
|
isinstance(vm, qubes.vm.templatevm.TemplateVM)]
|
2016-03-03 11:48:21 +01:00
|
|
|
except OSError:
|
|
|
|
templates = []
|
|
|
|
|
|
|
|
for entry in pkg_resources.iter_entry_points(
|
|
|
|
'qubes.tests.extra.for_template'):
|
2016-07-04 16:51:35 +02:00
|
|
|
try:
|
|
|
|
for test_case in entry.load()():
|
|
|
|
for template in templates:
|
|
|
|
tests.addTests(loader.loadTestsFromTestCase(
|
|
|
|
type(
|
|
|
|
'{}_{}_{}'.format(
|
|
|
|
entry.name, test_case.__name__, template),
|
|
|
|
(test_case,),
|
|
|
|
{'template': template}
|
|
|
|
)
|
|
|
|
))
|
|
|
|
except Exception as err: # pylint: disable=broad-except
|
|
|
|
def runTest(self):
|
2017-01-18 22:16:46 +01:00
|
|
|
raise err
|
2016-07-04 16:51:35 +02:00
|
|
|
ExtraForTemplateLoadFailure = type('ExtraForTemplateLoadFailure',
|
|
|
|
(qubes.tests.QubesTestCase,),
|
|
|
|
{entry.name: runTest})
|
|
|
|
tests.addTest(ExtraLoadFailure(entry.name))
|
2016-03-03 11:48:21 +01:00
|
|
|
|
|
|
|
return tests
|