qubes.tests.list_templates(), and .close() the app

This commit is contained in:
Wojtek Porczyk 2017-08-28 14:28:17 +02:00
parent de8ff20976
commit 25670fbd61
7 changed files with 32 additions and 48 deletions

View File

@ -52,6 +52,7 @@ import gc
import lxml.etree import lxml.etree
import pkg_resources import pkg_resources
import qubes
import qubes.api import qubes.api
import qubes.api.admin import qubes.api.admin
import qubes.api.internal import qubes.api.internal
@ -61,6 +62,7 @@ import qubes.devices
import qubes.events import qubes.events
import qubes.exc import qubes.exc
import qubes.vm.standalonevm import qubes.vm.standalonevm
import qubes.vm.templatevm
XMLPATH = '/var/lib/qubes/qubes-test.xml' XMLPATH = '/var/lib/qubes/qubes-test.xml'
CLASS_XMLPATH = '/var/lib/qubes/qubes-class-test.xml' CLASS_XMLPATH = '/var/lib/qubes/qubes-class-test.xml'
@ -992,6 +994,22 @@ class SystemTestCase(QubesTestCase):
timeout=30) timeout=30)
_templates = None
def list_templates():
'''Returns tuple of template names available in the system.'''
global _templates
if _templates is None:
try:
app = qubes.Qubes()
_templates = tuple(vm.name for vm in app.domains
if isinstance(vm, qubes.vm.templatevm.TemplateVM))
app.close()
del app
except OSError:
_templates = ()
return _templates
def load_tests(loader, tests, pattern): # pylint: disable=unused-argument def load_tests(loader, tests, pattern): # pylint: disable=unused-argument
# discard any tests from this module, because it hosts base classes # discard any tests from this module, because it hosts base classes
tests = unittest.TestSuite() tests = unittest.TestSuite()

View File

@ -23,7 +23,6 @@ import sys
import pkg_resources import pkg_resources
import qubes.tests import qubes.tests
import qubes.vm.appvm import qubes.vm.appvm
import qubes.vm.templatevm
class ExtraTestCase(qubes.tests.SystemTestCase): class ExtraTestCase(qubes.tests.SystemTestCase):
@ -80,18 +79,11 @@ def load_tests(loader, tests, pattern):
{entry.name: runTest}) {entry.name: runTest})
tests.addTest(ExtraLoadFailure(entry.name)) tests.addTest(ExtraLoadFailure(entry.name))
try:
app = qubes.Qubes()
templates = [vm.name for vm in app.domains if
isinstance(vm, qubes.vm.templatevm.TemplateVM)]
except OSError:
templates = []
for entry in pkg_resources.iter_entry_points( for entry in pkg_resources.iter_entry_points(
'qubes.tests.extra.for_template'): 'qubes.tests.extra.for_template'):
try: try:
for test_case in entry.load()(): for test_case in entry.load()():
for template in templates: for template in qubes.tests.list_templates():
tests.addTests(loader.loadTestsFromTestCase( tests.addTests(loader.loadTestsFromTestCase(
type( type(
'{}_{}_{}'.format( '{}_{}_{}'.format(

View File

@ -541,13 +541,7 @@ class TC_10_BackupVMMixin(BackupTestsMixin):
def load_tests(loader, tests, pattern): def load_tests(loader, tests, pattern):
try: for template in qubes.tests.list_templates():
app = qubes.Qubes()
templates = [vm.name for vm in app.domains if
isinstance(vm, qubes.vm.templatevm.TemplateVM)]
except OSError:
templates = []
for template in templates:
tests.addTests(loader.loadTestsFromTestCase( tests.addTests(loader.loadTestsFromTestCase(
type( type(
'TC_10_BackupVM_' + template, 'TC_10_BackupVM_' + template,

View File

@ -19,13 +19,15 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# #
from distutils import spawn import os
import qubes.tests
import subprocess import subprocess
import tempfile import tempfile
import unittest
import os
import time import time
import unittest
from distutils import spawn
import qubes.tests
class TC_04_DispVM(qubes.tests.SystemTestCase): class TC_04_DispVM(qubes.tests.SystemTestCase):
@ -254,13 +256,7 @@ class TC_20_DispVMMixin(object):
self.assertEqual(test_txt_content, b"Test test 2\ntest1\n") self.assertEqual(test_txt_content, b"Test test 2\ntest1\n")
def load_tests(loader, tests, pattern): def load_tests(loader, tests, pattern):
try: for template in qubes.tests.list_templates():
app = qubes.Qubes()
templates = [vm.name for vm in app.domains if
isinstance(vm, qubes.vm.templatevm.TemplateVM)]
except OSError:
templates = []
for template in templates:
tests.addTests(loader.loadTestsFromTestCase( tests.addTests(loader.loadTestsFromTestCase(
type( type(
'TC_20_DispVM_' + template, 'TC_20_DispVM_' + template,

View File

@ -26,6 +26,7 @@ import tempfile
import unittest import unittest
import qubes import qubes
import qubes.tests
VM_PREFIX = "test-" VM_PREFIX = "test-"
@ -359,13 +360,7 @@ Test package
def load_tests(loader, tests, pattern): def load_tests(loader, tests, pattern):
try: for template in qubes.tests.list_templates():
app = qubes.Qubes()
templates = [vm.name for vm in app.domains if
isinstance(vm, qubes.vm.templatevm.TemplateVM)]
except OSError:
templates = []
for template in templates:
tests.addTests(loader.loadTestsFromTestCase( tests.addTests(loader.loadTestsFromTestCase(
type( type(
'TC_00_Dom0Upgrade_' + template, 'TC_00_Dom0Upgrade_' + template,

View File

@ -934,13 +934,7 @@ SHA256:
'{}: {}\n{}'.format(self.update_cmd, stdout, stderr)) '{}: {}\n{}'.format(self.update_cmd, stdout, stderr))
def load_tests(loader, tests, pattern): def load_tests(loader, tests, pattern):
try: for template in qubes.tests.list_templates():
app = qubes.Qubes()
templates = [vm.name for vm in app.domains if
isinstance(vm, qubes.vm.templatevm.TemplateVM)]
except OSError:
templates = []
for template in templates:
tests.addTests(loader.loadTestsFromTestCase( tests.addTests(loader.loadTestsFromTestCase(
type( type(
'VmNetworking_' + template, 'VmNetworking_' + template,

View File

@ -25,6 +25,7 @@ import multiprocessing
import os import os
import subprocess import subprocess
import unittest import unittest
from distutils import spawn from distutils import spawn
import qubes.config import qubes.config
@ -1001,13 +1002,7 @@ class TC_10_Generic(qubes.tests.SystemTestCase):
def load_tests(loader, tests, pattern): def load_tests(loader, tests, pattern):
try: for template in qubes.tests.list_templates():
app = qubes.Qubes()
templates = [vm.name for vm in app.domains if
isinstance(vm, qubes.vm.templatevm.TemplateVM)]
except OSError:
templates = []
for template in templates:
tests.addTests(loader.loadTestsFromTestCase( tests.addTests(loader.loadTestsFromTestCase(
type( type(
'TC_00_AppVM_' + template, 'TC_00_AppVM_' + template,