From bc240bd742e28837aaccea4e2d1866abc23641a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Thu, 3 Mar 2016 11:48:21 +0100 Subject: [PATCH] tests: move external test loader to qubes.tests.extra module This way, tests will appear as from 'extra' module. Besides the move, some minor changes: - add missing self.qc.unlock_db() in create_vms() - suffix per-template tests with template name QubesOS/qubes-issues#1800 --- tests/Makefile | 2 + tests/__init__.py | 78 +------------------------------- tests/extra.py | 110 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 112 insertions(+), 78 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 696ef568..993c741a 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -33,3 +33,5 @@ endif cp storage_xen.py[co] $(DESTDIR)$(PYTHON_TESTSPATH) cp hardware.py $(DESTDIR)$(PYTHON_TESTSPATH) cp hardware.py[co] $(DESTDIR)$(PYTHON_TESTSPATH) + cp extra.py $(DESTDIR)$(PYTHON_TESTSPATH) + cp extra.py[co] $(DESTDIR)$(PYTHON_TESTSPATH) diff --git a/tests/__init__.py b/tests/__init__.py index 02d660e3..1aff0021 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -740,48 +740,6 @@ class BackupTestsMixin(SystemTestsMixin): f.truncate(size) f.close() -class ExtraTestMixin(SystemTestsMixin): - - template = None - - def setUp(self): - super(ExtraTestMixin, self).setUp() - self.qc.unlock_db() - - 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 - """ - self.qc.lock_db_for_writing() - self.qc.load() - if self.template: - template = self.qc.get_vm_by_name(self.template) - else: - template = self.qc.get_default_template() - for vmname in names: - vm = self.qc.add_new_vm("QubesAppVm", - name=self.make_vm_name(vmname), - template=template) - vm.create_on_disk(verbose=False) - self.save_and_reload_db() - - # get objects after reload - vms = [] - for vmname in names: - vms.append(self.qc.get_vm_by_name(self.make_vm_name(vmname))) - return vms - - def enable_network(self): - """ - Enable access to the network. Must be called before creating VMs. - """ - # nothing to do in core2 - pass - def load_tests(loader, tests, pattern): # discard any tests from this module, because it hosts base classes @@ -798,43 +756,9 @@ def load_tests(loader, tests, pattern): 'qubes.tests.storage', 'qubes.tests.storage_xen', 'qubes.tests.hardware', + 'qubes.tests.extra', ): tests.addTests(loader.loadTestsFromName(modname)) - - for entry in pkg_resources.iter_entry_points('qubes.tests.extra'): - for test_case in entry(): - tests.addTests(loader.loadTestsFromTestCase( - type( - entry.name + '_' + test_case.__name__, - (test_case, ExtraTestMixin, QubesTestCase), - {} - ) - )) - - - try: - qc = qubes.qubes.QubesVmCollection() - qc.lock_db_for_reading() - qc.load() - qc.unlock_db() - templates = [vm.name for vm in qc.values() if - isinstance(vm, qubes.qubes.QubesTemplateVm)] - except OSError: - templates = [] - - for entry in pkg_resources.iter_entry_points( - 'qubes.tests.extra.for_template'): - for test_case in entry(): - for template in templates: - tests.addTests(loader.loadTestsFromTestCase( - type( - entry.name + '_' + test_case.__name__, - (test_case, ExtraTestMixin, - QubesTestCase), - {'template': template} - ) - )) - return tests diff --git a/tests/extra.py b/tests/extra.py index e6b28fd8..31943b3c 100644 --- a/tests/extra.py +++ b/tests/extra.py @@ -1 +1,109 @@ -__author__ = 'user' +#!/usr/bin/python2 -O +# vim: fileencoding=utf-8 + +# +# The Qubes OS Project, https://www.qubes-os.org/ +# +# Copyright (C) 2016 +# Marek Marczykowski-Górecki +# +# 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. +# + +import pkg_resources +import qubes.tests +import qubes.qubes + + +class ExtraTestMixin(qubes.tests.SystemTestsMixin): + + template = None + + def setUp(self): + super(ExtraTestMixin, self).setUp() + self.qc.unlock_db() + + 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 + """ + self.qc.lock_db_for_writing() + self.qc.load() + if self.template: + template = self.qc.get_vm_by_name(self.template) + else: + template = self.qc.get_default_template() + for vmname in names: + vm = self.qc.add_new_vm("QubesAppVm", + name=self.make_vm_name(vmname), + template=template) + vm.create_on_disk(verbose=False) + self.save_and_reload_db() + self.qc.unlock_db() + + # get objects after reload + vms = [] + for vmname in names: + vms.append(self.qc.get_vm_by_name(self.make_vm_name(vmname))) + return vms + + def enable_network(self): + """ + Enable access to the network. Must be called before creating VMs. + """ + # nothing to do in core2 + pass + + +def load_tests(loader, tests, pattern): + for entry in pkg_resources.iter_entry_points('qubes.tests.extra'): + for test_case in entry(): + tests.addTests(loader.loadTestsFromTestCase( + type( + entry.name + '_' + test_case.__name__, + (test_case, ExtraTestMixin, qubes.tests.QubesTestCase), + {} + ) + )) + + try: + qc = qubes.qubes.QubesVmCollection() + qc.lock_db_for_reading() + qc.load() + qc.unlock_db() + templates = [vm.name for vm in qc.values() if + isinstance(vm, qubes.qubes.QubesTemplateVm)] + except OSError: + templates = [] + + for entry in pkg_resources.iter_entry_points( + 'qubes.tests.extra.for_template'): + for test_case in entry.load()(): + for template in templates: + tests.addTests(loader.loadTestsFromTestCase( + type( + '{}_{}_{}'.format( + entry.name, test_case.__name__, template), + (test_case, ExtraTestMixin, + qubes.tests.QubesTestCase), + {'template': template} + ) + )) + + return tests