Skip to contentSkip to conversation

meta: introduce a small baserunner framework

PATCH In review jwang 2 participants 7 messages Sep 13, 2016 – Sep 15, 2016

lore
Share & tools
lore

jwang

author
#0 lore raw

PATCH 1/4 meta: introduce a small baserunner framework

From: zjh <[email protected]>

---
 meta/lib/base/baserunner.py | 60 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100755 meta/lib/base/baserunner.py
meta/lib/base/baserunner.py new file +60 -0
new file mode 100755
index 0000000..56b838e
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+# Copyright (C) 2013 Intel Corporation
+#
+# Released under the MIT license (see COPYING.MIT)
+
+# Base unittest module used by testrunner
+# This provides the common test runner functionalities including manifest input,
+# xunit output, timeout, tag filtering.
+
+"""Base testrunner"""
+
+from __future__ import absolute_import
+import os
+import sys
+import time
+import unittest
+import shutil
+
+class TestContext(object):
+    '''test context which inject into testcase'''
+    def __init__(self):
+        self.target = None
+
+class FakeOptions(object):
+    '''This class just use for configure's defualt arg.
+       Usually, we use this object in a non comandline environment.'''
+    timeout = 0
+    def __getattr__(self, name):
+        return None
+
+class TestRunnerBase(object):
+    '''test runner base '''
+    def __init__(self, context=None):
+        self.tclist = []
+        self.runner = None
+        self.context = context if context else TestContext()
+        self.test_result = None
+        self.run_time = None
+
+
+    def configure(self, options=FakeOptions()):
+        '''configure before testing'''
+        pass
+
+    def result(self):
+        '''output test result '''
+        pass
+
+    def loadtest(self, names=None):
+        '''load test suite'''
+        pass
+
+    def runtest(self, testsuite):
+        '''run test suite'''
+        pass
+
+    def start(self, testsuite):
+        '''start testing'''
+        pass
+
Signature · 1 line
2.1.4
author
#1 lore raw

PATCH 2/4 meta: implement key baserunner features

From: zjh <[email protected]>

Baserunner contains three features:
1. load cases from a manifest file
2. load cases from a package such as "oeqa.runtime"
3. create runner engine based on pyunit textrunner

---
 meta/lib/base/baserunner.py | 44 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 38 insertions(+), 6 deletions(-)
meta/lib/base/baserunner.py +38 -6
index 56b838e..d59872f 100755
@@ -31,30 +31,62 @@ class FakeOptions(object):
 class TestRunnerBase(object):
     '''test runner base '''
     def __init__(self, context=None):
-        self.tclist = []
+        self.testslist = []
         self.runner = None
         self.context = context if context else TestContext()
+        self.test_options = None
         self.test_result = None
         self.run_time = None
 
+    def __del__(self):
+        """
+        Because unittest.TestCase is a class object, it will exist as long as the python virtual machine process.
+        So tc can't be released if we don't release them explicitly.
+        """
+        if hasattr(unittest.TestCase, "tc"):
+            delattr(unittest.TestCase, "tc")
+
+    @staticmethod
+    def get_tc_from_manifest(fname):
+        '''get tc list from manifest format '''
+        with open(fname, "r") as f:
+            tclist = [n.strip() for n in f.readlines() \
+                                if n.strip() and not n.strip().startswith('#')]
+        return tclist
 
     def configure(self, options=FakeOptions()):
         '''configure before testing'''
-        pass
+        self.test_options = options
+        self.runner = unittest.TextTestRunner(stream=sys.stderr, \
+                                                  verbosity=2)
 
     def result(self):
         '''output test result '''
-        pass
+        return self.test_result
 
     def loadtest(self, names=None):
         '''load test suite'''
-        pass
+        if names is None:
+            names = self.testslist
+        testloader = unittest.TestLoader()
+        tclist = []
+        for name in names:
+            tset = testloader.loadTestsFromName(name)
+            if tset.countTestCases() > 0:
+                tclist.append(tset)
+            elif tset._tests == []:
+                tclist.append(testloader.discover(name, "[!_]*.py", os.path.curdir))
+        return testloader.suiteClass(tclist)
 
     def runtest(self, testsuite):
         '''run test suite'''
-        pass
+        starttime = time.time()
+        self.test_result = self.runner.run(testsuite)
+        self.run_time = time.time() - starttime
 
     def start(self, testsuite):
         '''start testing'''
-        pass
+        setattr(unittest.TestCase, "tc", self.context)
+        self.runtest(testsuite)
+        self.result()
 
Signature · 1 line
2.1.4
#2 lore raw

Re: [PATCH 2/4] meta: implement key baserunner features

Quoted 6 lines · jwang

From: zjh <[email protected]>

Baserunner contains three features:
1. load cases from a manifest file
2. load cases from a package such as "oeqa.runtime"
3. create runner engine based on pyunit textrunner

I think this and 1/4 should probably be squashed together?

Quoted 33 lines

---
 meta/lib/base/baserunner.py | 44
++++++++++++++++++++++++++++++++++++++------

 1 file changed, 38 insertions(+), 6 deletions(-)
meta/lib/base/baserunner.py +0 -0
index 56b838e..d59872f 100755
@@ -31,30 +31,62 @@ class FakeOptions(object):

 class TestRunnerBase(object):
     '''test runner base '''
     def __init__(self, context=None):
-        self.tclist = []
+        self.testslist = []
         self.runner = None
         self.context = context if context else TestContext() +        self.test_options = None
         self.test_result = None
         self.run_time = None

+    def __del__(self):
+        """
+        Because unittest.TestCase is a class object, it will exist as long as the python virtual machine process.
+        So tc can't be released if we don't release them explicitly. +        """
+        if hasattr(unittest.TestCase, "tc"):
+            delattr(unittest.TestCase, "tc")
+

Quoted 8 lines

+    @staticmethod
+    def get_tc_from_manifest(fname):
+        '''get tc list from manifest format ''' +        with open(fname, "r") as f:
+            tclist = [n.strip() for n in f.readlines() \
+                                if n.strip() and not n.strip().startswith('#')]
+        return tclist

It might be nice to handle open() failing here? If open() fails we're trying to return an undefined instance.

Quoted 6 lines


     def configure(self, options=FakeOptions()):
         '''configure before testing'''
-        pass
+        self.test_options = options
+        self.runner = unittest.TextTestRunner(stream=sys.stderr, \

There's no need for a backslash here, we can rely on Python's implied continuation.

Quoted 11 lines

+                                                  verbosity=2)
     def result(self):
         '''output test result '''
-        pass
+        return self.test_result

     def loadtest(self, names=None):
         '''load test suite'''
-        pass
+        if names is None:

It's much more idiomatic to write these like:

if not names:

Quoted 8 lines

+            names = self.testslist
+        testloader = unittest.TestLoader()
+        tclist = []
+        for name in names:
+            tset = testloader.loadTestsFromName(name) +            if tset.countTestCases() > 0:
+                tclist.append(tset)
+            elif tset._tests == []:

variable names prefixed with an underscore are, by convention, internal/private to the object.

Is there a case where countTestCases() might not be > 0 and _tests[] != [] ? i.e. can we just use an else here?

Quoted 21 lines

+                tclist.append(testloader.discover(name, "[!_]*.py", os.path.curdir))
+        return testloader.suiteClass(tclist)

     def runtest(self, testsuite):
         '''run test suite'''
-        pass
+        starttime = time.time()
+        self.test_result = self.runner.run(testsuite)
+        self.run_time = time.time() - starttime

     def start(self, testsuite):
         '''start testing'''
-        pass
+        setattr(unittest.TestCase, "tc", self.context)
+        self.runtest(testsuite)
+        self.result()

--
2.1.4

author
#3 lore raw

PATCH 3/4 meta: use baserunner in oetest

In reply to jwang

From: zjh <[email protected]>

enable two features from baserunner:
1. loadtest
2. read test cases from manifest file

---
 meta/lib/oeqa/oetest.py | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
meta/lib/oeqa/oetest.py +14 -10
index 4a740fb..9211cec 100644
@@ -28,6 +28,8 @@ except ImportError:
     pass
 from oeqa.utils.decorators import LogResults, gettag, getResults
 from oeqa.utils import avoid_paths_in_environ
+from base.baserunner import TestRunnerBase
+
 
 logger = logging.getLogger("BitBake")
 
@@ -203,8 +205,10 @@ def custom_verbose(msg, *args, **kwargs):
             logger.info(_buffer_logger.rstrip("\n"), *args, **kwargs)
         _buffer_logger = ""
 
-class TestContext(object):
+class TestContext(TestRunnerBase):
+    _configure_in_init = True
     def __init__(self, d, exported=False):
+        super(TestContext, self).__init__(self)
         self.d = d
 
         self.testsuites = self._get_test_suites()
@@ -223,6 +227,9 @@ class TestContext(object):
         self.imagefeatures = d.getVar("IMAGE_FEATURES", True).split()
         self.distrofeatures = d.getVar("DISTRO_FEATURES", True).split()
 
+        if self._configure_in_init:
+            self.configure()
+
     # get testcase list from specified file
     # if path is a relative path, then relative to build/conf/
     def _read_testlist(self, fpath, builddir):
@@ -329,9 +336,7 @@ class TestContext(object):
     def loadTests(self):
         setattr(oeTest, "tc", self)
 
-        testloader = unittest.TestLoader()
-        testloader.sortTestMethodsUsing = None
-        suites = [testloader.loadTestsFromName(name) for name in self.testslist]
+        suites = self.loadtest(self.testslist)
         suites = filterByTagExp(suites, getattr(self, "tagexp", None))
 
         # Determine dependencies between suites by looking for @skipUnlessPassed
@@ -376,9 +381,9 @@ class TestContext(object):
         def cmpfunc(a, b):
             return cmp((a.depth, a.index), (b.depth, b.index))
 
-        suites.sort(key=functools.cmp_to_key(cmpfunc))
+        suites._tests.sort(key=functools.cmp_to_key(cmpfunc))
 
-        self.suite = testloader.suiteClass(suites)
+        self.suite = suites
 
         return self.suite
 
@@ -387,11 +392,10 @@ class TestContext(object):
         if hasattr(self, "tagexp") and self.tagexp:
             logger.info("Filter test cases by tags: %s" % self.tagexp)
         logger.info("Found %s tests" % self.suite.countTestCases())
-        runner = unittest.TextTestRunner(verbosity=2)
         if 'bb' in sys.modules:
-            runner.stream.write = custom_verbose
-
-        return runner.run(self.suite)
+            self.runner.stream.write = custom_verbose
+        self.start(self.suite)
+        return self.result()
 
 class RuntimeTestContext(TestContext):
     def __init__(self, d, target, exported=False):
Signature · 1 line
2.1.4
#4 lore raw

Re: [PATCH 3/4] meta: use baserunner in oetest

Quoted 29 lines · jwang

From: zjh <[email protected]>

enable two features from baserunner:
1. loadtest
2. read test cases from manifest file

---
 meta/lib/oeqa/oetest.py | 24 ++++++++++++++----------

 1 file changed, 14 insertions(+), 10 deletions(-)
meta/lib/oeqa/oetest.py +0 -0
index 4a740fb..9211cec 100644
@@ -28,6 +28,8 @@ except ImportError:

     pass
 from oeqa.utils.decorators import LogResults, gettag, getResults from oeqa.utils import avoid_paths_in_environ

meta/lib/oeqa/oetest.py +2 -0
+from base.baserunner import TestRunnerBase
+


 logger = logging.getLogger("BitBake")

meta/lib/oeqa/oetest.py +0 -0
@@ -203,8 +205,10 @@ def custom_verbose(msg, *args, **kwargs):
              logger.info(_buffer_logger.rstrip("\n"), *args,
 **kwargs)

         _buffer_logger = ""

meta/lib/oeqa/oetest.py def custom_verbose(msg, *args, **kwargs): · line 209

Quoted 3 lines
meta/lib/oeqa/oetest.py +2 -1
-class TestContext(object):
+class TestContext(TestRunnerBase):
+    _configure_in_init = True

This is a funny variable, when would it not be True and why? Could we add a comment here about what the variable is for?

Should it be an argument passed at object construction, as it's denoted as internal by the _ prefix?

Quoted 10 lines

     def __init__(self, d, exported=False):
+        super(TestContext, self).__init__(self) self.d = d

         self.testsuites = self._get_test_suites()

meta/lib/oeqa/oetest.py +0 -0
@@ -223,6 +227,9 @@ class TestContext(object):
          self.imagefeatures = d.getVar("IMAGE_FEATURES",
 True).split()
          self.distrofeatures = d.getVar("DISTRO_FEATURES",
 True).split()
Quoted 3 lines


+        if self._configure_in_init:
+            self.configure()

If we don't configure during __init__() when do we do it? Why might we not do it during __init__() ?

Quoted 19 lines

+

     # get testcase list from specified file
     # if path is a relative path, then relative to build/conf/
     def _read_testlist(self, fpath, builddir):
meta/lib/oeqa/oetest.py +0 -0
@@ -329,9 +336,7 @@ class TestContext(object):

     def loadTests(self):
         setattr(oeTest, "tc", self)

-        testloader = unittest.TestLoader()
-        testloader.sortTestMethodsUsing = None
-        suites = [testloader.loadTestsFromName(name) for name in self.testslist]
+        suites = self.loadtest(self.testslist)
         suites = filterByTagExp(suites, getattr(self, "tagexp", None))

         # Determine dependencies between suites by looking for @skipUnlessPassed

meta/lib/oeqa/oetest.py +0 -0
@@ -376,9 +381,9 @@ class TestContext(object):
Quoted 5 lines

         def cmpfunc(a, b):
             return cmp((a.depth, a.index), (b.depth, b.index))
-        suites.sort(key=functools.cmp_to_key(cmpfunc)) +        suites._tests.sort(key=functools.cmp_to_key(cmpfunc))

We appear to be using a non-public instance variable, is there something else we could be using here to get the list of tests?

Quoted 25 lines


-        self.suite = testloader.suiteClass(suites) +        self.suite = suites

         return self.suite

meta/lib/oeqa/oetest.py +0 -0
@@ -387,11 +392,10 @@ class TestContext(object):

         if hasattr(self, "tagexp") and self.tagexp:
             logger.info("Filter test cases by tags: %s" % self.tagexp)
         logger.info("Found %s tests" % self.suite.countTestCases()) -        runner = unittest.TextTestRunner(verbosity=2)
         if 'bb' in sys.modules:
-            runner.stream.write = custom_verbose
-
-        return runner.run(self.suite)
+            self.runner.stream.write = custom_verbose
+        self.start(self.suite)
+        return self.result()

 class RuntimeTestContext(TestContext):
     def __init__(self, d, target, exported=False):
--
2.1.4

author
#5 lore raw

PATCH 4/4 meta: modify runexported script to inherit the features from baserunner

In reply to jwang

From: zjh <[email protected]>

---
 meta/classes/testexport.bbclass |  2 ++
 meta/lib/base/baserunner.py     | 21 ++++++++++++++++++++-
 meta/lib/oeqa/runexported.py    | 12 +++++++++++-
 3 files changed, 33 insertions(+), 2 deletions(-)
meta/classes/testexport.bbclass +2 -0
index 15fa470..c65ad55 100644
@@ -128,6 +128,8 @@ def exportTests(d,tc):
         for f in files:
             shutil.copy2(os.path.join(root, f), os.path.join(exportpath, "oeqa/runtime/files"))
 
+    # copy base*
+    shutil.copytree(os.path.join(meta_layer, "lib", "base"), os.path.join(exportpath, "base"))
     # Create tar file for common parts of testexport
     create_tarball(d, "testexport.tar.gz", d.getVar("TEST_EXPORT_DIR", True))
 
meta/lib/base/baserunner.py +20 -1
index d59872f..9b38f1b 100755
@@ -57,9 +57,28 @@ class TestRunnerBase(object):
     def configure(self, options=FakeOptions()):
         '''configure before testing'''
         self.test_options = options
-        self.runner = unittest.TextTestRunner(stream=sys.stderr, \
+        if options.xunit:
+            try:
+                from xmlrunner import XMLTestRunner
+            except ImportError:
+                raise Exception("unittest-xml-reporting not installed")
+            self.runner = XMLTestRunner(stream=sys.stderr, \
+                                        verbosity=2, output=options.xunit)
+        else:
+            self.runner = unittest.TextTestRunner(stream=sys.stderr, \
                                                   verbosity=2)
 
+        if options.manifest:
+            fbname, fext = os.path.splitext(os.path.basename(options.manifest))
+            assert fbname == "manifest" or fext == ".manifest", \
+                  "Please specify file name like xxx.manifest or manifest.xxx"
+            self.tclist = self.get_tc_from_manifest(options.manifest)
+
+        if options.tests:
+            tcs = [t[0:-3] if t.endswith(".py") else t[0:-1] \
+                               if t.endswith("/") else t for t in options.tests]
+            self.tclist.extend([tc.replace("/", ".") for tc in tcs])
+
     def result(self):
         '''output test result '''
         return self.test_result
meta/lib/oeqa/runexported.py +11 -1
index 5886739..18e9543 100755
@@ -69,6 +69,9 @@ class MyDataDict(dict):
     def getVar(self, key, unused = None):
         return self.get(key, "")
 
+class RunExportTestContext(ExportTestContext):
+    _configure_in_init = False
+
 def main():
 
     parser = argparse.ArgumentParser()
@@ -81,6 +84,12 @@ def main():
             specified in the json if that directory actually exists or it will error out.")
     parser.add_argument("-l", "--log-dir", dest="log_dir", help="This sets the path for TEST_LOG_DIR. If not specified \
             the current dir is used. This is used for usually creating a ssh log file and a scp test file.")
+    parser.add_argument("-f", "--manifest", dest="manifest",
+                        help="The test list file"),
+    parser.add_argument("-x", "--xunit", dest="xunit",
+                        help="Output result path of in xUnit XML format"),
+    parser.add_argument("-e", "--tests", dest="tests", action="append",
+                        help="Run tests by dot separated module path"),
     parser.add_argument("json", help="The json file exported by the build system", default="testdata.json", nargs='?')
 
     args = parser.parse_args()
@@ -114,7 +123,8 @@ def main():
         setattr(target, key, loaded["target"][key])
 
     target.exportStart()
-    tc = ExportTestContext(d, target, True)
+    tc = RunExportTestContext(d, target, True)
+    tc.configure(args)
     tc.loadTests()
     tc.runTests()
 
Signature · 1 line
2.1.4
#6 lore raw
In reply to jwang

Hi Jing,

Thanks for your submission.

Quoted 1 line · jwang

From: zjh <[email protected]>

There's no commit message here, which makes this harder to review as I have to try and work out what it's for and why we want it.

A useful commit message would tell me why we are introducing a baserunner framework. This may also make a useful comment in the code itself.

Also, the patch prefix for the series should be "lib/oeqa", i.e

"[PATCH 3/4] lib/oeqa: use baserunner in oetest"

Quoted 14 lines

---
 meta/lib/base/baserunner.py | 60
+++++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 60 insertions(+)

 create mode 100755 meta/lib/base/baserunner.py

meta/lib/base/baserunner.py new file +0 -0
new file mode 100755
index 0000000..56b838e

meta/lib/base/baserunner.py line 1

Quoted 1 line
meta/lib/base/baserunner.py new file +1 -0
@@ -0,0 +1,60 @@
+#!/usr/bin/env python

We're defaulting to Python3 in OE-Core nowadays, do you really want Python2 here (which is the default Python on most Linux distros)

Quoted 26 lines
meta/lib/base/baserunner.py new file +6 -0
+# Copyright (C) 2013 Intel Corporation
+#
+# Released under the MIT license (see COPYING.MIT)
+
+# Base unittest module used by testrunner
+# This provides the common test runner functionalities including

manifest input,

meta/lib/base/baserunner.py new file +19 -0
+# xunit output, timeout, tag filtering.
+
+"""Base testrunner"""
+
+from __future__ import absolute_import
+import os
+import sys
+import time
+import unittest
+import shutil
+
+class TestContext(object):
+    '''test context which inject into testcase'''
+    def __init__(self):
+        self.target = None
+
+class FakeOptions(object):
+    '''This class just use for configure's defualt arg.
+       Usually, we use this object in a non comandline
Quoted 4 lines

environment.'''
+    timeout = 0
+    def __getattr__(self, name):
+        return None

What's the purpose of overloading __getattr__() here?

meta/lib/base/baserunner.py line 32

Quoted 3 lines
meta/lib/base/baserunner.py new file +3 -0
+
+class TestRunnerBase(object):
+    '''test runner base '''

This comment isn't very useful, same for other similar comments in this series.

Quoted 4 lines

+    def __init__(self, context=None):
+        self.tclist = []
+        self.runner = None
+        self.context = context if context else TestContext()

This would probably be better as:
self.context = context or TestContext()

or better yet, pass TestContext() as the default param for context, rather than None?

Quoted 27 lines

+        self.test_result = None
+        self.run_time = None

meta/lib/base/baserunner.py new file +22 -0
+
+
+    def configure(self, options=FakeOptions()):
+        '''configure before testing'''
+        pass
+
+    def result(self):
+        '''output test result '''
+        pass
+
+    def loadtest(self, names=None):
+        '''load test suite'''
+        pass
+
+    def runtest(self, testsuite):
+        '''run test suite'''
+        pass
+
+    def start(self, testsuite):
+        '''start testing'''
+        pass
+

--
2.1.4