Skip to contentSkip to conversation

oe-selftest: support selftests from other layers

PATCH In review LIU, Sharron 2 participants 4 messages Aug 5, 2015 – Aug 7, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 oe-selftest: support selftests from other layers

[YOCTO #7865] https://bugzilla.yoctoproject.org/show_bug.cgi?id=7865

This script is extended to detect and run tests from all BBLAYERS added to BBPATH. Tests can be put in any sub-dir, except for "files" which is treated as test dependent files.

To detect tests, this patch goes through "lib/oeqa/selftest/" of every layer added to BBPATH.
To support sub-dir, this patch list test module with its sub-dirs prefix. To run tests, this patch adds tests package path into "sys.path" then reload "oeqa.selftest".

---
 scripts/oe-selftest | 42 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 34 insertions(+), 8 deletions(-)
scripts/oe-selftest +34 -8
index 91e2dd2..b441a08 100755
@@ -23,7 +23,15 @@
 # Call the script as: "oe-selftest" to run all the tests in in meta/lib/selftest/
 # Call the script as: "oe-selftest <module>.<Class>.<method>" to run just a single test
 # E.g: "oe-selftest bboutput.BitbakeLayers" will run just the BitbakeLayers class from meta/lib/selftest/bboutput.py
-
+#
+# The script is extended to support detecting tests from "lib/oeqa/selftest/*/"
+# from any BBLAYERS added to BBPATH. Tests can be put in any subdir,
+# except for "files" which is treated as test dependent files.
+# Eg.: meta-mylayer/lib/oeqa/selftest/subdirs/mytest.py
+# Call the script as: "oe-selftest --list-modules", you'll have
+#         subdirs.mytest
+# Call the script as: "oe-selftest --runtests subdirs.mytest", you'll have
+# the test run.
 
 import os
 import sys
@@ -41,6 +49,13 @@ import oeqa.utils.ftools as ftools
 from oeqa.utils.commands import runCmd, get_bb_var, get_test_layer
 from oeqa.selftest.base import oeSelfTest
 
+# add test cases location into the python searching path
+for l in get_bb_var('BBPATH').split(':'):
+    if os.path.exists(os.path.abspath(os.path.join(l, 'lib/oeqa/selftest'))):
+        sys.path.insert(0, os.path.abspath(os.path.join(l, 'lib')))
+# reloading this package is necessary due to "sys.path" updated
+reload(oeqa.selftest)
+
 def logger_create():
     log = logging.getLogger("selftest")
     log.setLevel(logging.DEBUG)
@@ -145,12 +160,20 @@ def get_tests(exclusive_modules=[], include_hidden=False):
     for x in exclusive_modules:
         testslist.append('oeqa.selftest.' + x)
     if not testslist:
-        for testpath in oeqa.selftest.__path__:
-            files = sorted([f for f in os.listdir(testpath) if f.endswith('.py') and not (f.startswith('_') and not include_hidden) and not f.startswith('__') and f != 'base.py'])
-            for f in files:
-                module = 'oeqa.selftest.' + f[:-3]
-                if module not in testslist:
-                    testslist.append(module)
+        # add test cases to "testslist" for every subfolder of "oeqa/selftest/"
+        def _addtest(args, dname, fnames):
+            if (os.path.basename(dname) != 'files'): # 'files' folder is skipped
+                # d is sub-dirs name. eg.: "oeqa/selftest/cfg/mytest.py", d=cfg
+                d=dname[args[0]:].replace('/','.')
+                for f in fnames:
+                    if (f.endswith('.py') and not (f.startswith('_') and not include_hidden) and not f.startswith('__') and f != 'base.py'):
+                        # test is added as "oeqa.selftest.cfg.mytest"
+                        testslist.append('oeqa.selftest'+d+'.'+f[:-3])
+        # detect test cases from all layers that added to "BBPATH"
+        for l in get_bb_var('BBPATH').split(':'):
+            testpath = os.path.join(l, 'lib', 'oeqa', 'selftest')
+            if os.path.exists(testpath):
+                os.path.walk(testpath, _addtest, [len(testpath)])
 
     return testslist
 
@@ -172,7 +195,10 @@ def main():
         log.info('Listing all available test modules:')
         testslist = get_tests(include_hidden=True)
         for test in testslist:
-            module = test.split('.')[-1]
+            # list test with its sub-dirs name relative to oeqa.selftest
+            # eg.: "oeqa.selftest.cfg.mytest" is listed as "cfg.mytest" for
+            # passing to "--run-tests <module>"
+            module = test.split('.',2)[-1]
             info = ''
             if module.startswith('_'):
                 info = ' (hidden)'
Signature · 1 line
1.9.1
#1 lore raw
Quoted 15 lines · Sharron

[YOCTO #7865] https://bugzilla.yoctoproject.org/show_bug.cgi?id=7865

This script is extended to detect and run tests from all BBLAYERS added to BBPATH. Tests can be put in any sub-dir, except for "files" which is treated
as test dependent files.

To detect tests, this patch goes through "lib/oeqa/selftest/" of every layer
added to BBPATH.
To support sub-dir, this patch list test module with its sub-dirs prefix. To run tests, this patch adds tests package path into "sys.path" then reload
"oeqa.selftest".

Quoted 2 lines

This was implemented as a part of the work to do the same for devtool & recipetool, see https://bugzilla.yoctoproject.org/show_bug.cgi?id=7865#c10
and https://bugzilla.yoctoproject.org/show_bug.cgi?id=7625#c3:

https://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/?id=d2023c34e4c3735d919c35d80daf991aab579500

https://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/?id=51d760e2a7e22f1b07608fc925da343e56a9ae74

Signature · 5 lines
Christopher Larson
clarson at kergoth dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Maintainer - Tslib
Senior Software Engineer, Mentor Graphics
#2 lore raw

Hi Chris,
Thanks for your inputs. I tested my case with poky master which includes the two commitments you mentioned:

    https://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/?id=d2023c34e4c3735d919c35d80daf991aab579500

https://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/?id=51d760e2a7e22f1b07608fc925da343e56a9ae74
In my observation it still failed at my case.

In my test case I have:
poky_root/meta-mylayer/lib/oeqa/selftest/subdirs/mytest.py

I type:
"oe-selftest --list-modules"
And I expect “subdirs.mytest” being listed.

I type:
"oe-selftest --run-tests subdirs.mytest"
And I expect “mytest” being run.

This case still failed without my patch. Attached the log file for your ref.

Regards,
Sharron

From: [email protected] [mailto:[email protected]] On Behalf Of Christopher Larson
Sent: Wednesday, August 5, 2015 11:07 PM
To: Liu, Sharron
Cc: Patches and discussions about the oe-core layer
Subject: Re: [OE-core] [PATCH] oe-selftest: support selftests from other layers

On Wed, Aug 5, 2015 at 12:17 AM, LIU, Sharron <[email protected]<mailto:[email protected]>> wrote:
[YOCTO #7865] https://bugzilla.yoctoproject.org/show_bug.cgi?id=7865

This script is extended to detect and run tests from all BBLAYERS added to BBPATH. Tests can be put in any sub-dir, except for "files" which is treated as test dependent files.

To detect tests, this patch goes through "lib/oeqa/selftest/" of every layer added to BBPATH.
To support sub-dir, this patch list test module with its sub-dirs prefix. To run tests, this patch adds tests package path into "sys.path" then reload "oeqa.selftest".

This was implemented as a part of the work to do the same for devtool & recipetool, see https://bugzilla.yoctoproject.org/show_bug.cgi?id=7865#c10 and https://bugzilla.yoctoproject.org/show_bug.cgi?id=7625#c3:

    https://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/?id=d2023c34e4c3735d919c35d80daf991aab579500
    https://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/?id=51d760e2a7e22f1b07608fc925da343e56a9ae74

--
Christopher Larson
clarson at kergoth dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Maintainer - Tslib
Senior Software Engineer, Mentor Graphics

###with latest poky master####

sharron@u1304-liuxr:~/workspace/views/iot/poky/poky/build$ oe-selftest --list-modules
2015-08-05 08:29:21,165 - selftest - INFO - Running bitbake -e to get BBPATH
2015-08-05 08:29:21,679 - selftest - INFO - Listing all available test modules:
_sstatetests_noauto (hidden)
_toaster (hidden)
bblayers
bbtests
buildhistory
buildoptions
devtool
imagefeatures
layerappend
lic-checksum
oescripts
pkgdata
prservice
recipetool
sstate
sstatetests
wic
gummiboot

###with Sharron's patch####

sharron@u1304-liuxr:~/workspace/views/iot/poky/poky/build$ oe-selftest --list-modules
2015-08-05 08:30:34,879 - selftest - INFO - Running bitbake -e to get BBPATH
2015-08-05 08:30:35,380 - selftest - INFO - Listing all available test modules:
bblayers
prservice
recipetool
buildhistory
_sstatetests_noauto (hidden)
devtool
buildoptions
_toaster (hidden)
layerappend
sstate
pkgdata
lic-checksum
imagefeatures
sstatetests
oescripts
bbtests
wic
gummiboot
subdirs.mytest    #### this is not listed without my patch
#3 lore raw
Quoted 29 lines · Sharron

Thanks for your inputs. I tested my case with poky master which includes the two commitments you mentioned:

https://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/?id=d2023c34e4c3735d919c35d80daf991aab579500

https://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/?id=51d760e2a7e22f1b07608fc925da343e56a9ae74

In my observation it still failed at my case.

In my test case I have:

poky_root/meta-mylayer/lib/oeqa/selftest/subdirs/mytest.py

I type:

"oe-selftest --list-modules"

And I expect “subdirs.mytest” being listed.

I type:

Quoted 9 lines

"oe-selftest --run-tests subdirs.mytest"

And I expect “mytest” being run.

This case still failed without my patch. Attached the log file for your ref.

Sounds like you didn't properly set up your python packages. It has to be possible to import your tests from python, which is not the case unless __init__.py exists at the root of each python package.

oeqa.selftest is a python namespace package. Copy poky/meta/lib/oeqa/selftest/__init__.py
to poky_root/meta-mylayer/lib/oeqa/selftest/, and either also copy it to poky_root/meta-mylayer/lib/oeqa/selftest/subdirs/ or touch poky_root/meta-mylayer/lib/oeqa/selftest/subdirs/__init__.py.

Signature · 5 lines
Christopher Larson
clarson at kergoth dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Maintainer - Tslib
Senior Software Engineer, Mentor Graphics