Yocto 2.4
Move OpenBMC to Yocto 2.4(rocko)
Tested: Built and verified Witherspoon and Palmetto images
Change-Id: I12057b18610d6fb0e6903c60213690301e9b0c67
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/README b/import-layers/yocto-poky/meta/lib/oeqa/core/README
index 0c859fd..d4fcda4 100644
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/README
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/README
@@ -1,38 +1,76 @@
-= OEQA Framework =
+= OEQA (v2) Framework =
== Introduction ==
-This is the new OEQA framework the base clases of the framework
-are in this module oeqa/core the subsequent components needs to
-extend this classes.
+This is version 2 of the OEQA framework. Base clases are located in the
+'oeqa/core' directory and subsequent components must extend from these.
-A new/unique runner was created called oe-test and is under scripts/
-oe-test, this new runner scans over oeqa module searching for test
-components that supports OETestContextExecutor implemented in context
-module (i.e. oeqa/core/context.py).
+The main design consideration was to implement the needed functionality on
+top of the Python unittest framework. To achieve this goal, the following
+modules are used:
-For execute an example:
+ * oeqa/core/runner.py: Provides OETestResult and OETestRunner base
+ classes extending the unittest class. These classes support exporting
+ results to different formats; currently RAW and XML support exist.
-$ source oe-init-build-env
-$ oe-test core
+ * oeqa/core/loader.py: Provides OETestLoader extending the unittest class.
+ It also features a unified implementation of decorator support and
+ filtering test cases.
-For list supported components:
+ * oeqa/core/case.py: Provides OETestCase base class extending
+ unittest.TestCase and provides access to the Test data (td), Test context
+ and Logger functionality.
-$ oe-test -h
+ * oeqa/core/decorator: Provides OETestDecorator, a new class to implement
+ decorators for Test cases.
-== Create new Test component ==
+ * oeqa/core/context: Provides OETestContext, a high-level API for
+ loadTests and runTests of certain Test component and
+ OETestContextExecutor a base class to enable oe-test to discover/use
+ the Test component.
-Usally for add a new Test component the developer needs to extend
-OETestContext/OETestContextExecutor in context.py and OETestCase in
-case.py.
+Also, a new 'oe-test' runner is located under 'scripts', allowing scans for components
+that supports OETestContextExecutor (see below).
-== How to run the testing of the OEQA framework ==
+== Terminology ==
+
+ * Test component: The area of testing in the Project, for example: runtime, SDK, eSDK, selftest.
+
+ * Test data: Data associated with the Test component. Currently we use bitbake datastore as
+ a Test data input.
+
+ * Test context: A context of what tests needs to be run and how to do it; this additionally
+ provides access to the Test data and could have custom methods and/or attrs.
+
+== oe-test ==
+
+The new tool, oe-test, has the ability to scan the code base for test components and provide
+a unified way to run test cases. Internally it scans folders inside oeqa module in order to find
+specific classes that implement a test component.
+
+== Usage ==
+
+Executing the example test component
+
+ $ source oe-init-build-env
+ $ oe-test core
+
+Getting help
+
+ $ oe-test -h
+
+== Creating new Test Component ==
+
+Adding a new test component the developer needs to extend OETestContext/OETestContextExecutor
+(from context.py) and OETestCase (from case.py)
+
+== Selftesting the framework ==
Run all tests:
-$ PATH=$PATH:../../ python3 -m unittest discover -s tests
+ $ PATH=$PATH:../../ python3 -m unittest discover -s tests
Run some test:
-$ cd tests/
-$ ./test_data.py
+ $ cd tests/
+ $ ./test_data.py
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/case.py b/import-layers/yocto-poky/meta/lib/oeqa/core/case.py
index d2dbf20..917a2aa 100644
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/case.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/case.py
@@ -23,7 +23,7 @@
# td_vars has the variables needed by a test class
# or test case instance, if some var isn't into td a
- # OEMissingVariable exception is raised
+ # OEQAMissingVariable exception is raised
td_vars = None
@classmethod
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/context.py b/import-layers/yocto-poky/meta/lib/oeqa/core/context.py
index 4476750..acd5474 100644
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/context.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/context.py
@@ -7,15 +7,14 @@
import time
import logging
import collections
-import re
from oeqa.core.loader import OETestLoader
-from oeqa.core.runner import OETestRunner, OEStreamLogger, xmlEnabled
+from oeqa.core.runner import OETestRunner
+from oeqa.core.exception import OEQAMissingManifest, OEQATestNotFound
class OETestContext(object):
loaderClass = OETestLoader
runnerClass = OETestRunner
- streamLoggerClass = OEStreamLogger
files_dir = os.path.abspath(os.path.join(os.path.dirname(
os.path.abspath(__file__)), "../files"))
@@ -32,7 +31,7 @@
def _read_modules_from_manifest(self, manifest):
if not os.path.exists(manifest):
- raise
+ raise OEQAMissingManifest("Manifest does not exist on %s" % manifest)
modules = []
for line in open(manifest).readlines():
@@ -42,6 +41,14 @@
return modules
+ def skipTests(self, skips):
+ if not skips:
+ return
+ for test in self.suites:
+ for skip in skips:
+ if test.id().startswith(skip):
+ setattr(test, 'setUp', lambda: test.skipTest('Skip by the command line argument "%s"' % skip))
+
def loadTests(self, module_paths, modules=[], tests=[],
modules_manifest="", modules_required=[], filters={}):
if modules_manifest:
@@ -51,9 +58,11 @@
modules_required, filters)
self.suites = self.loader.discover()
- def runTests(self):
- streamLogger = self.streamLoggerClass(self.logger)
- self.runner = self.runnerClass(self, stream=streamLogger, verbosity=2)
+ def runTests(self, skips=[]):
+ self.runner = self.runnerClass(self, descriptions=False, verbosity=2)
+
+ # Dinamically skip those tests specified though arguments
+ self.skipTests(skips)
self._run_start_time = time.time()
result = self.runner.run(self.suites)
@@ -61,94 +70,13 @@
return result
- def logSummary(self, result, component, context_msg=''):
- self.logger.info("SUMMARY:")
- self.logger.info("%s (%s) - Ran %d test%s in %.3fs" % (component,
- context_msg, result.testsRun, result.testsRun != 1 and "s" or "",
- (self._run_end_time - self._run_start_time)))
-
- if result.wasSuccessful():
- msg = "%s - OK - All required tests passed" % component
- else:
- msg = "%s - FAIL - Required tests failed" % component
- skipped = len(self._results['skipped'])
- if skipped:
- msg += " (skipped=%d)" % skipped
- self.logger.info(msg)
-
- def _getDetailsNotPassed(self, case, type, desc):
- found = False
-
- for (scase, msg) in self._results[type]:
- # XXX: When XML reporting is enabled scase is
- # xmlrunner.result._TestInfo instance instead of
- # string.
- if xmlEnabled:
- if case.id() == scase.test_id:
- found = True
- break
- scase_str = scase.test_id
- else:
- if case == scase:
- found = True
- break
- scase_str = str(scase)
-
- # When fails at module or class level the class name is passed as string
- # so figure out to see if match
- m = re.search("^setUpModule \((?P<module_name>.*)\)$", scase_str)
- if m:
- if case.__class__.__module__ == m.group('module_name'):
- found = True
- break
-
- m = re.search("^setUpClass \((?P<class_name>.*)\)$", scase_str)
- if m:
- class_name = "%s.%s" % (case.__class__.__module__,
- case.__class__.__name__)
-
- if class_name == m.group('class_name'):
- found = True
- break
-
- if found:
- return (found, msg)
-
- return (found, None)
-
- def logDetails(self):
- self.logger.info("RESULTS:")
- for case_name in self._registry['cases']:
- case = self._registry['cases'][case_name]
-
- result_types = ['failures', 'errors', 'skipped', 'expectedFailures']
- result_desc = ['FAILED', 'ERROR', 'SKIPPED', 'EXPECTEDFAIL']
-
- fail = False
- desc = None
- for idx, name in enumerate(result_types):
- (fail, msg) = self._getDetailsNotPassed(case, result_types[idx],
- result_desc[idx])
- if fail:
- desc = result_desc[idx]
- break
-
- oeid = -1
- for d in case.decorators:
- if hasattr(d, 'oeid'):
- oeid = d.oeid
-
- if fail:
- self.logger.info("RESULTS - %s - Testcase %s: %s" % (case.id(),
- oeid, desc))
- if msg:
- self.logger.info(msg)
- else:
- self.logger.info("RESULTS - %s - Testcase %s: %s" % (case.id(),
- oeid, 'PASSED'))
+ def listTests(self, display_type):
+ self.runner = self.runnerClass(self, verbosity=2)
+ return self.runner.list_tests(self.suites, display_type)
class OETestContextExecutor(object):
_context_class = OETestContext
+ _script_executor = 'oe-test'
name = 'core'
help = 'core test component example'
@@ -168,9 +96,14 @@
self.parser.add_argument('--output-log', action='store',
default=self.default_output_log,
help="results output log, default: %s" % self.default_output_log)
- self.parser.add_argument('--run-tests', action='store',
+
+ group = self.parser.add_mutually_exclusive_group()
+ group.add_argument('--run-tests', action='store', nargs='+',
default=self.default_tests,
- help="tests to run in <module>[.<class>[.<name>]] format. Just works for modules now")
+ help="tests to run in <module>[.<class>[.<name>]]")
+ group.add_argument('--list-tests', action='store',
+ choices=('module', 'class', 'name'),
+ help="lists available tests")
if self.default_test_data:
self.parser.add_argument('--test-data-file', action='store',
@@ -206,7 +139,8 @@
self.tc_kwargs = {}
self.tc_kwargs['init'] = {}
self.tc_kwargs['load'] = {}
- self.tc_kwargs['run'] = {}
+ self.tc_kwargs['list'] = {}
+ self.tc_kwargs['run'] = {}
self.tc_kwargs['init']['logger'] = self._setup_logger(logger, args)
if args.test_data_file:
@@ -215,22 +149,36 @@
else:
self.tc_kwargs['init']['td'] = {}
-
if args.run_tests:
- self.tc_kwargs['load']['modules'] = args.run_tests.split()
+ self.tc_kwargs['load']['modules'] = args.run_tests
+ self.tc_kwargs['load']['modules_required'] = args.run_tests
else:
- self.tc_kwargs['load']['modules'] = None
+ self.tc_kwargs['load']['modules'] = []
+
+ self.tc_kwargs['run']['skips'] = []
self.module_paths = args.CASES_PATHS
+ def _pre_run(self):
+ pass
+
def run(self, logger, args):
self._process_args(logger, args)
self.tc = self._context_class(**self.tc_kwargs['init'])
- self.tc.loadTests(self.module_paths, **self.tc_kwargs['load'])
- rc = self.tc.runTests(**self.tc_kwargs['run'])
- self.tc.logSummary(rc, self.name)
- self.tc.logDetails()
+ try:
+ self.tc.loadTests(self.module_paths, **self.tc_kwargs['load'])
+ except OEQATestNotFound as ex:
+ logger.error(ex)
+ sys.exit(1)
+
+ if args.list_tests:
+ rc = self.tc.listTests(args.list_tests, **self.tc_kwargs['list'])
+ else:
+ self._pre_run()
+ rc = self.tc.runTests(**self.tc_kwargs['run'])
+ rc.logDetails()
+ rc.logSummary(self.name)
output_link = os.path.join(os.path.dirname(args.output_log),
"%s-results.log" % self.name)
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/decorator/depends.py b/import-layers/yocto-poky/meta/lib/oeqa/core/decorator/depends.py
index 195711c..baa0434 100644
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/decorator/depends.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/decorator/depends.py
@@ -3,6 +3,7 @@
from unittest import SkipTest
+from oeqa.core.threaded import OETestRunnerThreaded
from oeqa.core.exception import OEQADependency
from . import OETestDiscover, registerDecorator
@@ -63,7 +64,12 @@
return [cases[case_id] for case_id in cases_ordered]
def _skipTestDependency(case, depends):
- results = case.tc._results
+ if isinstance(case.tc.runner, OETestRunnerThreaded):
+ import threading
+ results = case.tc._results[threading.get_ident()]
+ else:
+ results = case.tc._results
+
skipReasons = ['errors', 'failures', 'skipped']
for reason in skipReasons:
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/decorator/oetimeout.py b/import-layers/yocto-poky/meta/lib/oeqa/core/decorator/oetimeout.py
index a247583..f85e7d9 100644
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/decorator/oetimeout.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/decorator/oetimeout.py
@@ -1,8 +1,12 @@
# Copyright (C) 2016 Intel Corporation
# Released under the MIT license (see COPYING.MIT)
-import signal
from . import OETestDecorator, registerDecorator
+
+import signal
+from threading import Timer
+
+from oeqa.core.threaded import OETestRunnerThreaded
from oeqa.core.exception import OEQATimeoutError
@registerDecorator
@@ -10,16 +14,32 @@
attrs = ('oetimeout',)
def setUpDecorator(self):
- timeout = self.oetimeout
- def _timeoutHandler(signum, frame):
- raise OEQATimeoutError("Timed out after %s "
+ self.logger.debug("Setting up a %d second(s) timeout" % self.oetimeout)
+
+ if isinstance(self.case.tc.runner, OETestRunnerThreaded):
+ self.timeouted = False
+ def _timeoutHandler():
+ self.timeouted = True
+
+ self.timer = Timer(self.oetimeout, _timeoutHandler)
+ self.timer.start()
+ else:
+ timeout = self.oetimeout
+ def _timeoutHandler(signum, frame):
+ raise OEQATimeoutError("Timed out after %s "
"seconds of execution" % timeout)
- self.logger.debug("Setting up a %d second(s) timeout" % self.oetimeout)
- self.alarmSignal = signal.signal(signal.SIGALRM, _timeoutHandler)
- signal.alarm(self.oetimeout)
+ self.alarmSignal = signal.signal(signal.SIGALRM, _timeoutHandler)
+ signal.alarm(self.oetimeout)
def tearDownDecorator(self):
- signal.alarm(0)
- signal.signal(signal.SIGALRM, self.alarmSignal)
- self.logger.debug("Removed SIGALRM handler")
+ if isinstance(self.case.tc.runner, OETestRunnerThreaded):
+ self.timer.cancel()
+ self.logger.debug("Removed Timer handler")
+ if self.timeouted:
+ raise OEQATimeoutError("Timed out after %s "
+ "seconds of execution" % self.oetimeout)
+ else:
+ signal.alarm(0)
+ signal.signal(signal.SIGALRM, self.alarmSignal)
+ self.logger.debug("Removed SIGALRM handler")
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/exception.py b/import-layers/yocto-poky/meta/lib/oeqa/core/exception.py
index 2dfd840..732f2ef 100644
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/exception.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/exception.py
@@ -12,3 +12,12 @@
class OEQADependency(OEQAException):
pass
+
+class OEQAMissingManifest(OEQAException):
+ pass
+
+class OEQAPreRun(OEQAException):
+ pass
+
+class OEQATestNotFound(OEQAException):
+ pass
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/loader.py b/import-layers/yocto-poky/meta/lib/oeqa/core/loader.py
index 63a1703..975a081 100644
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/loader.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/loader.py
@@ -2,25 +2,30 @@
# Released under the MIT license (see COPYING.MIT)
import os
+import re
import sys
import unittest
+import inspect
from oeqa.core.utils.path import findFile
from oeqa.core.utils.test import getSuiteModules, getCaseID
+from oeqa.core.exception import OEQATestNotFound
from oeqa.core.case import OETestCase
from oeqa.core.decorator import decoratorClasses, OETestDecorator, \
OETestFilter, OETestDiscover
-def _make_failed_test(classname, methodname, exception, suiteClass):
- """
- When loading tests unittest framework stores the exception in a new
- class created for be displayed into run().
-
- For our purposes will be better to raise the exception in loading
- step instead of wait to run the test suite.
- """
- raise exception
+# When loading tests, the unittest framework stores any exceptions and
+# displays them only when the run method is called.
+#
+# For our purposes, it is better to raise the exceptions in the loading
+# step rather than waiting to run the test suite.
+#
+# Generate the function definition because this differ across python versions
+# Python >= 3.4.4 uses tree parameters instead four but for example Python 3.5.3
+# ueses four parameters so isn't incremental.
+_failed_test_args = inspect.getargspec(unittest.loader._make_failed_test).args
+exec("""def _make_failed_test(%s): raise exception""" % ', '.join(_failed_test_args))
unittest.loader._make_failed_test = _make_failed_test
def _find_duplicated_modules(suite, directory):
@@ -29,6 +34,28 @@
if path:
raise ImportError("Duplicated %s module found in %s" % (module, path))
+def _built_modules_dict(modules):
+ modules_dict = {}
+
+ if modules == None:
+ return modules_dict
+
+ for module in modules:
+ # Assumption: package and module names do not contain upper case
+ # characters, whereas class names do
+ m = re.match(r'^([^A-Z]+)(?:\.([A-Z][^.]*)(?:\.([^.]+))?)?$', module)
+
+ module_name, class_name, test_name = m.groups()
+
+ if module_name and module_name not in modules_dict:
+ modules_dict[module_name] = {}
+ if class_name and class_name not in modules_dict[module_name]:
+ modules_dict[module_name][class_name] = []
+ if test_name and test_name not in modules_dict[module_name][class_name]:
+ modules_dict[module_name][class_name].append(test_name)
+
+ return modules_dict
+
class OETestLoader(unittest.TestLoader):
caseClass = OETestCase
@@ -39,7 +66,8 @@
filters, *args, **kwargs):
self.tc = tc
- self.modules = modules
+ self.modules = _built_modules_dict(modules)
+
self.tests = tests
self.modules_required = modules_required
@@ -63,6 +91,8 @@
self._patchCaseClass(self.caseClass)
+ super(OETestLoader, self).__init__()
+
def _patchCaseClass(self, testCaseClass):
# Adds custom attributes to the OETestCase class
setattr(testCaseClass, 'tc', self.tc)
@@ -116,7 +146,35 @@
"""
Returns True if test case must be filtered, False otherwise.
"""
- if self.filters:
+ # XXX; If the module has more than one namespace only use
+ # the first to support run the whole module specifying the
+ # <module_name>.[test_class].[test_name]
+ module_name_small = case.__module__.split('.')[0]
+ module_name = case.__module__
+
+ class_name = case.__class__.__name__
+ test_name = case._testMethodName
+
+ if self.modules:
+ module = None
+ try:
+ module = self.modules[module_name_small]
+ except KeyError:
+ try:
+ module = self.modules[module_name]
+ except KeyError:
+ return True
+
+ if module:
+ if not class_name in module:
+ return True
+
+ if module[class_name]:
+ if test_name not in module[class_name]:
+ return True
+
+ # Decorator filters
+ if self.filters and isinstance(case, OETestCase):
filters = self.filters.copy()
case_decorators = [cd for cd in case.decorators
if cd.__class__ in self.used_filters]
@@ -134,7 +192,8 @@
return False
def _getTestCase(self, testCaseClass, tcName):
- if not hasattr(testCaseClass, '__oeqa_loader'):
+ if not hasattr(testCaseClass, '__oeqa_loader') and \
+ issubclass(testCaseClass, OETestCase):
# In order to support data_vars validation
# monkey patch the default setUp/tearDown{Class} to use
# the ones provided by OETestCase
@@ -161,7 +220,8 @@
setattr(testCaseClass, '__oeqa_loader', True)
case = testCaseClass(tcName)
- setattr(case, 'decorators', [])
+ if isinstance(case, OETestCase):
+ setattr(case, 'decorators', [])
return case
@@ -173,9 +233,9 @@
raise TypeError("Test cases should not be derived from TestSuite." \
" Maybe you meant to derive %s from TestCase?" \
% testCaseClass.__name__)
- if not issubclass(testCaseClass, self.caseClass):
+ if not issubclass(testCaseClass, unittest.case.TestCase):
raise TypeError("Test %s is not derived from %s" % \
- (testCaseClass.__name__, self.caseClass.__name__))
+ (testCaseClass.__name__, unittest.case.TestCase.__name__))
testCaseNames = self.getTestCaseNames(testCaseClass)
if not testCaseNames and hasattr(testCaseClass, 'runTest'):
@@ -196,6 +256,28 @@
return self.suiteClass(suite)
+ def _required_modules_validation(self):
+ """
+ Search in Test context registry if a required
+ test is found, raise an exception when not found.
+ """
+
+ for module in self.modules_required:
+ found = False
+
+ # The module name is splitted to only compare the
+ # first part of a test case id.
+ comp_len = len(module.split('.'))
+ for case in self.tc._registry['cases']:
+ case_comp = '.'.join(case.split('.')[0:comp_len])
+ if module == case_comp:
+ found = True
+ break
+
+ if not found:
+ raise OEQATestNotFound("Not found %s in loaded test cases" % \
+ module)
+
def discover(self):
big_suite = self.suiteClass()
for path in self.module_paths:
@@ -210,8 +292,41 @@
for clss in discover_classes:
cases = clss.discover(self.tc._registry)
+ if self.modules_required:
+ self._required_modules_validation()
+
return self.suiteClass(cases) if cases else big_suite
+ def _filterModule(self, module):
+ if module.__name__ in sys.builtin_module_names:
+ msg = 'Tried to import %s test module but is a built-in'
+ raise ImportError(msg % module.__name__)
+
+ # XXX; If the module has more than one namespace only use
+ # the first to support run the whole module specifying the
+ # <module_name>.[test_class].[test_name]
+ module_name_small = module.__name__.split('.')[0]
+ module_name = module.__name__
+
+ # Normal test modules are loaded if no modules were specified,
+ # if module is in the specified module list or if 'all' is in
+ # module list.
+ # Underscore modules are loaded only if specified in module list.
+ load_module = True if not module_name.startswith('_') \
+ and (not self.modules \
+ or module_name in self.modules \
+ or module_name_small in self.modules \
+ or 'all' in self.modules) \
+ else False
+
+ load_underscore = True if module_name.startswith('_') \
+ and (module_name in self.modules or \
+ module_name_small in self.modules) \
+ else False
+
+ return (load_module, load_underscore)
+
+
# XXX After Python 3.5, remove backward compatibility hacks for
# use_load_tests deprecation via *args and **kws. See issue 16662.
if sys.version_info >= (3,5):
@@ -219,23 +334,7 @@
"""
Returns a suite of all tests cases contained in module.
"""
- if module.__name__ in sys.builtin_module_names:
- msg = 'Tried to import %s test module but is a built-in'
- raise ImportError(msg % module.__name__)
-
- # Normal test modules are loaded if no modules were specified,
- # if module is in the specified module list or if 'all' is in
- # module list.
- # Underscore modules are loaded only if specified in module list.
- load_module = True if not module.__name__.startswith('_') \
- and (not self.modules \
- or module.__name__ in self.modules \
- or 'all' in self.modules) \
- else False
-
- load_underscore = True if module.__name__.startswith('_') \
- and module.__name__ in self.modules \
- else False
+ load_module, load_underscore = self._filterModule(module)
if load_module or load_underscore:
return super(OETestLoader, self).loadTestsFromModule(
@@ -247,23 +346,7 @@
"""
Returns a suite of all tests cases contained in module.
"""
- if module.__name__ in sys.builtin_module_names:
- msg = 'Tried to import %s test module but is a built-in'
- raise ImportError(msg % module.__name__)
-
- # Normal test modules are loaded if no modules were specified,
- # if module is in the specified module list or if 'all' is in
- # module list.
- # Underscore modules are loaded only if specified in module list.
- load_module = True if not module.__name__.startswith('_') \
- and (not self.modules \
- or module.__name__ in self.modules \
- or 'all' in self.modules) \
- else False
-
- load_underscore = True if module.__name__.startswith('_') \
- and module.__name__ in self.modules \
- else False
+ load_module, load_underscore = self._filterModule(module)
if load_module or load_underscore:
return super(OETestLoader, self).loadTestsFromModule(
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/runner.py b/import-layers/yocto-poky/meta/lib/oeqa/core/runner.py
index 44ffecb..13cdf5b 100644
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/runner.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/runner.py
@@ -5,6 +5,7 @@
import time
import unittest
import logging
+import re
xmlEnabled = False
try:
@@ -24,10 +25,14 @@
def write(self, msg):
if len(msg) > 1 and msg[0] != '\n':
- self.buffer += msg
- else:
- self.logger.log(logging.INFO, self.buffer.rstrip("\n"))
- self.buffer = ""
+ if '...' in msg:
+ self.buffer += msg
+ elif self.buffer:
+ self.buffer += msg
+ self.logger.log(logging.INFO, self.buffer)
+ self.buffer = ""
+ else:
+ self.logger.log(logging.INFO, msg)
def flush(self):
for handler in self.logger.handlers:
@@ -38,22 +43,122 @@
super(OETestResult, self).__init__(*args, **kwargs)
self.tc = tc
+ self._tc_map_results()
+ def startTest(self, test):
+ # Allow us to trigger the testcase buffer mode on a per test basis
+ # so stdout/stderr are only printed upon failure. Enables debugging
+ # but clean output
+ if hasattr(test, "buffer"):
+ self.buffer = test.buffer
+ super(OETestResult, self).startTest(test)
+
+ def _tc_map_results(self):
self.tc._results['failures'] = self.failures
self.tc._results['errors'] = self.errors
self.tc._results['skipped'] = self.skipped
self.tc._results['expectedFailures'] = self.expectedFailures
- def startTest(self, test):
- super(OETestResult, self).startTest(test)
+ def logSummary(self, component, context_msg=''):
+ elapsed_time = self.tc._run_end_time - self.tc._run_start_time
+ self.tc.logger.info("SUMMARY:")
+ self.tc.logger.info("%s (%s) - Ran %d test%s in %.3fs" % (component,
+ context_msg, self.testsRun, self.testsRun != 1 and "s" or "",
+ elapsed_time))
+
+ if self.wasSuccessful():
+ msg = "%s - OK - All required tests passed" % component
+ else:
+ msg = "%s - FAIL - Required tests failed" % component
+ skipped = len(self.tc._results['skipped'])
+ if skipped:
+ msg += " (skipped=%d)" % skipped
+ self.tc.logger.info(msg)
+
+ def _getDetailsNotPassed(self, case, type, desc):
+ found = False
+
+ for (scase, msg) in self.tc._results[type]:
+ # XXX: When XML reporting is enabled scase is
+ # xmlrunner.result._TestInfo instance instead of
+ # string.
+ if xmlEnabled:
+ if case.id() == scase.test_id:
+ found = True
+ break
+ scase_str = scase.test_id
+ else:
+ if case == scase:
+ found = True
+ break
+ scase_str = str(scase)
+
+ # When fails at module or class level the class name is passed as string
+ # so figure out to see if match
+ m = re.search("^setUpModule \((?P<module_name>.*)\)$", scase_str)
+ if m:
+ if case.__class__.__module__ == m.group('module_name'):
+ found = True
+ break
+
+ m = re.search("^setUpClass \((?P<class_name>.*)\)$", scase_str)
+ if m:
+ class_name = "%s.%s" % (case.__class__.__module__,
+ case.__class__.__name__)
+
+ if class_name == m.group('class_name'):
+ found = True
+ break
+
+ if found:
+ return (found, msg)
+
+ return (found, None)
+
+ def logDetails(self):
+ self.tc.logger.info("RESULTS:")
+ for case_name in self.tc._registry['cases']:
+ case = self.tc._registry['cases'][case_name]
+
+ result_types = ['failures', 'errors', 'skipped', 'expectedFailures']
+ result_desc = ['FAILED', 'ERROR', 'SKIPPED', 'EXPECTEDFAIL']
+
+ fail = False
+ desc = None
+ for idx, name in enumerate(result_types):
+ (fail, msg) = self._getDetailsNotPassed(case, result_types[idx],
+ result_desc[idx])
+ if fail:
+ desc = result_desc[idx]
+ break
+
+ oeid = -1
+ if hasattr(case, 'decorators'):
+ for d in case.decorators:
+ if hasattr(d, 'oeid'):
+ oeid = d.oeid
+
+ if fail:
+ self.tc.logger.info("RESULTS - %s - Testcase %s: %s" % (case.id(),
+ oeid, desc))
+ else:
+ self.tc.logger.info("RESULTS - %s - Testcase %s: %s" % (case.id(),
+ oeid, 'PASSED'))
+
+class OEListTestsResult(object):
+ def wasSuccessful(self):
+ return True
class OETestRunner(_TestRunner):
+ streamLoggerClass = OEStreamLogger
+
def __init__(self, tc, *args, **kwargs):
if xmlEnabled:
if not kwargs.get('output'):
kwargs['output'] = os.path.join(os.getcwd(),
'TestResults_%s_%s' % (time.strftime("%Y%m%d%H%M%S"), os.getpid()))
+ kwargs['stream'] = self.streamLoggerClass(tc.logger)
super(OETestRunner, self).__init__(*args, **kwargs)
self.tc = tc
self.resultclass = OETestResult
@@ -74,3 +179,99 @@
def _makeResult(self):
return self.resultclass(self.tc, self.stream, self.descriptions,
self.verbosity)
+
+
+ def _walk_suite(self, suite, func):
+ for obj in suite:
+ if isinstance(obj, unittest.suite.TestSuite):
+ if len(obj._tests):
+ self._walk_suite(obj, func)
+ elif isinstance(obj, unittest.case.TestCase):
+ func(self.tc.logger, obj)
+ self._walked_cases = self._walked_cases + 1
+
+ def _list_tests_name(self, suite):
+ from oeqa.core.decorator.oeid import OETestID
+ from oeqa.core.decorator.oetag import OETestTag
+
+ self._walked_cases = 0
+
+ def _list_cases_without_id(logger, case):
+
+ found_id = False
+ if hasattr(case, 'decorators'):
+ for d in case.decorators:
+ if isinstance(d, OETestID):
+ found_id = True
+
+ if not found_id:
+ logger.info('oeid missing for %s' % case.id())
+
+ def _list_cases(logger, case):
+ oeid = None
+ oetag = None
+
+ if hasattr(case, 'decorators'):
+ for d in case.decorators:
+ if isinstance(d, OETestID):
+ oeid = d.oeid
+ elif isinstance(d, OETestTag):
+ oetag = d.oetag
+
+ logger.info("%s\t%s\t\t%s" % (oeid, oetag, case.id()))
+
+ self.tc.logger.info("Listing test cases that don't have oeid ...")
+ self._walk_suite(suite, _list_cases_without_id)
+ self.tc.logger.info("-" * 80)
+
+ self.tc.logger.info("Listing all available tests:")
+ self._walked_cases = 0
+ self.tc.logger.info("id\ttag\t\ttest")
+ self.tc.logger.info("-" * 80)
+ self._walk_suite(suite, _list_cases)
+ self.tc.logger.info("-" * 80)
+ self.tc.logger.info("Total found:\t%s" % self._walked_cases)
+
+ def _list_tests_class(self, suite):
+ self._walked_cases = 0
+
+ curr = {}
+ def _list_classes(logger, case):
+ if not 'module' in curr or curr['module'] != case.__module__:
+ curr['module'] = case.__module__
+ logger.info(curr['module'])
+
+ if not 'class' in curr or curr['class'] != \
+ case.__class__.__name__:
+ curr['class'] = case.__class__.__name__
+ logger.info(" -- %s" % curr['class'])
+
+ logger.info(" -- -- %s" % case._testMethodName)
+
+ self.tc.logger.info("Listing all available test classes:")
+ self._walk_suite(suite, _list_classes)
+
+ def _list_tests_module(self, suite):
+ self._walked_cases = 0
+
+ listed = []
+ def _list_modules(logger, case):
+ if not case.__module__ in listed:
+ if case.__module__.startswith('_'):
+ logger.info("%s (hidden)" % case.__module__)
+ else:
+ logger.info(case.__module__)
+ listed.append(case.__module__)
+
+ self.tc.logger.info("Listing all available test modules:")
+ self._walk_suite(suite, _list_modules)
+
+ def list_tests(self, suite, display_type):
+ if display_type == 'name':
+ self._list_tests_name(suite)
+ elif display_type == 'class':
+ self._list_tests_class(suite)
+ elif display_type == 'module':
+ self._list_tests_module(suite)
+
+ return OEListTestsResult()
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/target/qemu.py b/import-layers/yocto-poky/meta/lib/oeqa/core/target/qemu.py
index 2dc521c..d359bf9 100644
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/target/qemu.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/target/qemu.py
@@ -31,7 +31,7 @@
deploy_dir_image=dir_image, display=display,
logfile=bootlog, boottime=boottime,
use_kvm=kvm, dump_dir=dump_dir,
- dump_host_cmds=dump_host_cmds)
+ dump_host_cmds=dump_host_cmds, logger=logger)
def start(self, params=None, extra_bootparams=None):
if self.runner.start(params, extra_bootparams=extra_bootparams):
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/target/ssh.py b/import-layers/yocto-poky/meta/lib/oeqa/core/target/ssh.py
index b80939c..151b99a 100644
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/target/ssh.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/target/ssh.py
@@ -6,6 +6,7 @@
import select
import logging
import subprocess
+import codecs
from . import OETarget
@@ -82,7 +83,7 @@
processTimeout = self.timeout
status, output = self._run(sshCmd, processTimeout, True)
- self.logger.info('\nCommand: %s\nOutput: %s\n' % (command, output))
+ self.logger.debug('Command: %s\nOutput: %s\n' % (command, output))
return (status, output)
def copyTo(self, localSrc, remoteDst):
@@ -206,12 +207,12 @@
logger.debug('time: %s, endtime: %s' % (time.time(), endtime))
try:
if select.select([process.stdout], [], [], 5)[0] != []:
- data = os.read(process.stdout.fileno(), 1024)
+ reader = codecs.getreader('utf-8')(process.stdout)
+ data = reader.read(1024, 1024)
if not data:
process.stdout.close()
eof = True
else:
- data = data.decode("utf-8")
output += data
logger.debug('Partial data from SSH call: %s' % data)
endtime = time.time() + timeout
@@ -233,7 +234,7 @@
output += lastline
else:
- output = process.communicate()[0].decode("utf-8")
+ output = process.communicate()[0].decode("utf-8", errors='replace')
logger.debug('Data from SSH call: %s' % output.rstrip())
options = {
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded.py b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded.py
new file mode 100644
index 0000000..0fe4cb3
--- /dev/null
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded.py
@@ -0,0 +1,12 @@
+# Copyright (C) 2017 Intel Corporation
+# Released under the MIT license (see COPYING.MIT)
+
+from oeqa.core.case import OETestCase
+
+class ThreadedTest(OETestCase):
+ def test_threaded_no_depends(self):
+ self.assertTrue(True, msg='How is this possible?')
+
+class ThreadedTest2(OETestCase):
+ def test_threaded_same_module(self):
+ self.assertTrue(True, msg='How is this possible?')
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded_alone.py b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded_alone.py
new file mode 100644
index 0000000..905f397
--- /dev/null
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded_alone.py
@@ -0,0 +1,8 @@
+# Copyright (C) 2017 Intel Corporation
+# Released under the MIT license (see COPYING.MIT)
+
+from oeqa.core.case import OETestCase
+
+class ThreadedTestAlone(OETestCase):
+ def test_threaded_alone(self):
+ self.assertTrue(True, msg='How is this possible?')
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded_depends.py b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded_depends.py
new file mode 100644
index 0000000..0c158d3
--- /dev/null
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded_depends.py
@@ -0,0 +1,10 @@
+# Copyright (C) 2017 Intel Corporation
+# Released under the MIT license (see COPYING.MIT)
+
+from oeqa.core.case import OETestCase
+from oeqa.core.decorator.depends import OETestDepends
+
+class ThreadedTest3(OETestCase):
+ @OETestDepends(['threaded.ThreadedTest.test_threaded_no_depends'])
+ def test_threaded_depends(self):
+ self.assertTrue(True, msg='How is this possible?')
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded_module.py b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded_module.py
new file mode 100644
index 0000000..63d17e0
--- /dev/null
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/cases/loader/threaded/threaded_module.py
@@ -0,0 +1,12 @@
+# Copyright (C) 2017 Intel Corporation
+# Released under the MIT license (see COPYING.MIT)
+
+from oeqa.core.case import OETestCase
+
+class ThreadedTestModule(OETestCase):
+ def test_threaded_module(self):
+ self.assertTrue(True, msg='How is this possible?')
+
+class ThreadedTestModule2(OETestCase):
+ def test_threaded_module2(self):
+ self.assertTrue(True, msg='How is this possible?')
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/tests/common.py b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/common.py
index 52b18a1..1932323 100644
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/tests/common.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/common.py
@@ -33,3 +33,13 @@
tc.loadTests(self.cases_path, modules=modules, tests=tests,
filters=filters)
return tc
+
+ def _testLoaderThreaded(self, d={}, modules=[],
+ tests=[], filters={}):
+ from oeqa.core.threaded import OETestContextThreaded
+
+ tc = OETestContextThreaded(d, self.logger)
+ tc.loadTests(self.cases_path, modules=modules, tests=tests,
+ filters=filters)
+
+ return tc
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/tests/test_decorators.py b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/test_decorators.py
index f7d11e8..cf99e0d 100755
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/tests/test_decorators.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/test_decorators.py
@@ -131,5 +131,17 @@
msg = "OETestTimeout didn't restore SIGALRM"
self.assertIs(alarm_signal, signal.getsignal(signal.SIGALRM), msg=msg)
+ def test_timeout_thread(self):
+ tests = ['timeout.TimeoutTest.testTimeoutPass']
+ msg = 'Failed to run test using OETestTimeout'
+ tc = self._testLoaderThreaded(modules=self.modules, tests=tests)
+ self.assertTrue(tc.runTests().wasSuccessful(), msg=msg)
+
+ def test_timeout_threaded_fail(self):
+ tests = ['timeout.TimeoutTest.testTimeoutFail']
+ msg = "OETestTimeout test didn't timeout as expected"
+ tc = self._testLoaderThreaded(modules=self.modules, tests=tests)
+ self.assertFalse(tc.runTests().wasSuccessful(), msg=msg)
+
if __name__ == '__main__':
unittest.main()
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/tests/test_loader.py b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/test_loader.py
index b79b8ba..e0d917d 100755
--- a/import-layers/yocto-poky/meta/lib/oeqa/core/tests/test_loader.py
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/tests/test_loader.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
-# Copyright (C) 2016 Intel Corporation
+# Copyright (C) 2016-2017 Intel Corporation
# Released under the MIT license (see COPYING.MIT)
import os
@@ -82,5 +82,33 @@
msg = 'Expected modules from two different paths'
self.assertEqual(modules, expected_modules, msg=msg)
+ def test_loader_threaded(self):
+ cases_path = self.cases_path
+
+ self.cases_path = [os.path.join(self.cases_path, 'loader', 'threaded')]
+
+ tc = self._testLoaderThreaded()
+ self.assertEqual(len(tc.suites), 3, "Expected to be 3 suites")
+
+ case_ids = ['threaded.ThreadedTest.test_threaded_no_depends',
+ 'threaded.ThreadedTest2.test_threaded_same_module',
+ 'threaded_depends.ThreadedTest3.test_threaded_depends']
+ for case in tc.suites[0]._tests:
+ self.assertEqual(case.id(),
+ case_ids[tc.suites[0]._tests.index(case)])
+
+ case_ids = ['threaded_alone.ThreadedTestAlone.test_threaded_alone']
+ for case in tc.suites[1]._tests:
+ self.assertEqual(case.id(),
+ case_ids[tc.suites[1]._tests.index(case)])
+
+ case_ids = ['threaded_module.ThreadedTestModule.test_threaded_module',
+ 'threaded_module.ThreadedTestModule2.test_threaded_module2']
+ for case in tc.suites[2]._tests:
+ self.assertEqual(case.id(),
+ case_ids[tc.suites[2]._tests.index(case)])
+
+ self.cases_path = cases_path
+
if __name__ == '__main__':
unittest.main()
diff --git a/import-layers/yocto-poky/meta/lib/oeqa/core/threaded.py b/import-layers/yocto-poky/meta/lib/oeqa/core/threaded.py
new file mode 100644
index 0000000..2cafe03
--- /dev/null
+++ b/import-layers/yocto-poky/meta/lib/oeqa/core/threaded.py
@@ -0,0 +1,275 @@
+# Copyright (C) 2017 Intel Corporation
+# Released under the MIT license (see COPYING.MIT)
+
+import threading
+import multiprocessing
+import queue
+import time
+
+from unittest.suite import TestSuite
+
+from oeqa.core.loader import OETestLoader
+from oeqa.core.runner import OEStreamLogger, OETestResult, OETestRunner
+from oeqa.core.context import OETestContext
+
+class OETestLoaderThreaded(OETestLoader):
+ def __init__(self, tc, module_paths, modules, tests, modules_required,
+ filters, process_num=0, *args, **kwargs):
+ super(OETestLoaderThreaded, self).__init__(tc, module_paths, modules,
+ tests, modules_required, filters, *args, **kwargs)
+
+ self.process_num = process_num
+
+ def discover(self):
+ suite = super(OETestLoaderThreaded, self).discover()
+
+ if self.process_num <= 0:
+ self.process_num = min(multiprocessing.cpu_count(),
+ len(suite._tests))
+
+ suites = []
+ for _ in range(self.process_num):
+ suites.append(self.suiteClass())
+
+ def _search_for_module_idx(suites, case):
+ """
+ Cases in the same module needs to be run
+ in the same thread because PyUnit keeps track
+ of setUp{Module, Class,} and tearDown{Module, Class,}.
+ """
+
+ for idx in range(self.process_num):
+ suite = suites[idx]
+ for c in suite._tests:
+ if case.__module__ == c.__module__:
+ return idx
+
+ return -1
+
+ def _search_for_depend_idx(suites, depends):
+ """
+ Dependency cases needs to be run in the same
+ thread, because OEQA framework look at the state
+ of dependant test to figure out if skip or not.
+ """
+
+ for idx in range(self.process_num):
+ suite = suites[idx]
+
+ for case in suite._tests:
+ if case.id() in depends:
+ return idx
+ return -1
+
+ def _get_best_idx(suites):
+ sizes = [len(suite._tests) for suite in suites]
+ return sizes.index(min(sizes))
+
+ def _fill_suites(suite):
+ idx = -1
+ for case in suite:
+ if isinstance(case, TestSuite):
+ _fill_suites(case)
+ else:
+ idx = _search_for_module_idx(suites, case)
+
+ depends = {}
+ if 'depends' in self.tc._registry:
+ depends = self.tc._registry['depends']
+
+ if idx == -1 and case.id() in depends:
+ case_depends = depends[case.id()]
+ idx = _search_for_depend_idx(suites, case_depends)
+
+ if idx == -1:
+ idx = _get_best_idx(suites)
+
+ suites[idx].addTest(case)
+ _fill_suites(suite)
+
+ suites_tmp = suites
+ suites = []
+ for suite in suites_tmp:
+ if len(suite._tests) > 0:
+ suites.append(suite)
+
+ return suites
+
+class OEStreamLoggerThreaded(OEStreamLogger):
+ _lock = threading.Lock()
+ buffers = {}
+
+ def write(self, msg):
+ tid = threading.get_ident()
+
+ if not tid in self.buffers:
+ self.buffers[tid] = ""
+
+ if msg:
+ self.buffers[tid] += msg
+
+ def finish(self):
+ tid = threading.get_ident()
+
+ self._lock.acquire()
+ self.logger.info('THREAD: %d' % tid)
+ self.logger.info('-' * 70)
+ for line in self.buffers[tid].split('\n'):
+ self.logger.info(line)
+ self._lock.release()
+
+class OETestResultThreadedInternal(OETestResult):
+ def _tc_map_results(self):
+ tid = threading.get_ident()
+
+ # PyUnit generates a result for every test module run, test
+ # if the thread already has an entry to avoid lose the previous
+ # test module results.
+ if not tid in self.tc._results:
+ self.tc._results[tid] = {}
+ self.tc._results[tid]['failures'] = self.failures
+ self.tc._results[tid]['errors'] = self.errors
+ self.tc._results[tid]['skipped'] = self.skipped
+ self.tc._results[tid]['expectedFailures'] = self.expectedFailures
+
+class OETestResultThreaded(object):
+ _results = {}
+ _lock = threading.Lock()
+
+ def __init__(self, tc):
+ self.tc = tc
+
+ def _fill_tc_results(self):
+ tids = list(self.tc._results.keys())
+ fields = ['failures', 'errors', 'skipped', 'expectedFailures']
+
+ for tid in tids:
+ result = self.tc._results[tid]
+ for field in fields:
+ if not field in self.tc._results:
+ self.tc._results[field] = []
+ self.tc._results[field].extend(result[field])
+
+ def addResult(self, result, run_start_time, run_end_time):
+ tid = threading.get_ident()
+
+ self._lock.acquire()
+ self._results[tid] = {}
+ self._results[tid]['result'] = result
+ self._results[tid]['run_start_time'] = run_start_time
+ self._results[tid]['run_end_time'] = run_end_time
+ self._results[tid]['result'] = result
+ self._lock.release()
+
+ def wasSuccessful(self):
+ wasSuccessful = True
+ for tid in self._results.keys():
+ wasSuccessful = wasSuccessful and \
+ self._results[tid]['result'].wasSuccessful()
+ return wasSuccessful
+
+ def stop(self):
+ for tid in self._results.keys():
+ self._results[tid]['result'].stop()
+
+ def logSummary(self, component, context_msg=''):
+ elapsed_time = (self.tc._run_end_time - self.tc._run_start_time)
+
+ self.tc.logger.info("SUMMARY:")
+ self.tc.logger.info("%s (%s) - Ran %d tests in %.3fs" % (component,
+ context_msg, len(self.tc._registry['cases']), elapsed_time))
+ if self.wasSuccessful():
+ msg = "%s - OK - All required tests passed" % component
+ else:
+ msg = "%s - FAIL - Required tests failed" % component
+ self.tc.logger.info(msg)
+
+ def logDetails(self):
+ if list(self._results):
+ tid = list(self._results)[0]
+ result = self._results[tid]['result']
+ result.logDetails()
+
+class _Worker(threading.Thread):
+ """Thread executing tasks from a given tasks queue"""
+ def __init__(self, tasks, result, stream):
+ threading.Thread.__init__(self)
+ self.tasks = tasks
+
+ self.result = result
+ self.stream = stream
+
+ def run(self):
+ while True:
+ try:
+ func, args, kargs = self.tasks.get(block=False)
+ except queue.Empty:
+ break
+
+ try:
+ run_start_time = time.time()
+ rc = func(*args, **kargs)
+ run_end_time = time.time()
+ self.result.addResult(rc, run_start_time, run_end_time)
+ self.stream.finish()
+ except Exception as e:
+ print(e)
+ finally:
+ self.tasks.task_done()
+
+class _ThreadedPool:
+ """Pool of threads consuming tasks from a queue"""
+ def __init__(self, num_workers, num_tasks, stream=None, result=None):
+ self.tasks = queue.Queue(num_tasks)
+ self.workers = []
+
+ for _ in range(num_workers):
+ worker = _Worker(self.tasks, result, stream)
+ self.workers.append(worker)
+
+ def start(self):
+ for worker in self.workers:
+ worker.start()
+
+ def add_task(self, func, *args, **kargs):
+ """Add a task to the queue"""
+ self.tasks.put((func, args, kargs))
+
+ def wait_completion(self):
+ """Wait for completion of all the tasks in the queue"""
+ self.tasks.join()
+ for worker in self.workers:
+ worker.join()
+
+class OETestRunnerThreaded(OETestRunner):
+ streamLoggerClass = OEStreamLoggerThreaded
+
+ def __init__(self, tc, *args, **kwargs):
+ super(OETestRunnerThreaded, self).__init__(tc, *args, **kwargs)
+ self.resultclass = OETestResultThreadedInternal # XXX: XML reporting overrides at __init__
+
+ def run(self, suites):
+ result = OETestResultThreaded(self.tc)
+
+ pool = _ThreadedPool(len(suites), len(suites), stream=self.stream,
+ result=result)
+ for s in suites:
+ pool.add_task(super(OETestRunnerThreaded, self).run, s)
+ pool.start()
+ pool.wait_completion()
+ result._fill_tc_results()
+
+ return result
+
+class OETestContextThreaded(OETestContext):
+ loaderClass = OETestLoaderThreaded
+ runnerClass = OETestRunnerThreaded
+
+ def loadTests(self, module_paths, modules=[], tests=[],
+ modules_manifest="", modules_required=[], filters={}, process_num=0):
+ if modules_manifest:
+ modules = self._read_modules_from_manifest(modules_manifest)
+
+ self.loader = self.loaderClass(self, module_paths, modules, tests,
+ modules_required, filters, process_num)
+ self.suites = self.loader.discover()