summaryrefslogtreecommitdiffstats
path: root/scripts/oe-selftest
diff options
context:
space:
mode:
authorTudor Florea <tudor.florea@enea.com>2015-10-09 22:59:03 +0200
committerTudor Florea <tudor.florea@enea.com>2015-10-09 22:59:03 +0200
commit972dcfcdbfe75dcfeb777150c136576cf1a71e99 (patch)
tree97a61cd7e293d7ae9d56ef7ed0f81253365bb026 /scripts/oe-selftest
downloadpoky-972dcfcdbfe75dcfeb777150c136576cf1a71e99.tar.gz
initial commit for Enea Linux 5.0 arm
Signed-off-by: Tudor Florea <tudor.florea@enea.com>
Diffstat (limited to 'scripts/oe-selftest')
-rwxr-xr-xscripts/oe-selftest187
1 files changed, 187 insertions, 0 deletions
diff --git a/scripts/oe-selftest b/scripts/oe-selftest
new file mode 100755
index 0000000000..2332b224ee
--- /dev/null
+++ b/scripts/oe-selftest
@@ -0,0 +1,187 @@
1#!/usr/bin/env python
2
3# Copyright (c) 2013 Intel Corporation
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License version 2 as
7# published by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18# DESCRIPTION
19# This script runs tests defined in meta/lib/selftest/
20# It's purpose is to automate the testing of different bitbake tools.
21# To use it you just need to source your build environment setup script and
22# add the meta-selftest layer to your BBLAYERS.
23# Call the script as: "oe-selftest" to run all the tests in in meta/lib/selftest/
24# Call the script as: "oe-selftest <module>.<Class>.<method>" to run just a single test
25# E.g: "oe-selftest bboutput.BitbakeLayers" will run just the BitbakeLayers class from meta/lib/selftest/bboutput.py
26
27
28import os
29import sys
30import unittest
31import logging
32import argparse
33
34sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'meta/lib')))
35
36import oeqa.selftest
37import oeqa.utils.ftools as ftools
38from oeqa.utils.commands import runCmd, get_bb_var, get_test_layer
39from oeqa.selftest.base import oeSelfTest
40
41def logger_create():
42 log = logging.getLogger("selftest")
43 log.setLevel(logging.DEBUG)
44
45 fh = logging.FileHandler(filename='oe-selftest.log', mode='w')
46 fh.setLevel(logging.DEBUG)
47
48 ch = logging.StreamHandler(sys.stdout)
49 ch.setLevel(logging.INFO)
50
51 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
52 fh.setFormatter(formatter)
53 ch.setFormatter(formatter)
54
55 log.addHandler(fh)
56 log.addHandler(ch)
57
58 return log
59
60log = logger_create()
61
62def get_args_parser():
63 description = "Script that runs unit tests agains bitbake and other Yocto related tools. The goal is to validate tools functionality and metadata integrity. Refer to https://wiki.yoctoproject.org/wiki/Oe-selftest for more information."
64 parser = argparse.ArgumentParser(description=description)
65 group = parser.add_mutually_exclusive_group(required=True)
66 group.add_argument('--run-tests', required=False, action='store', nargs='*', dest="run_tests", default=None, help='Select what tests to run (modules, classes or test methods). Format should be: <module>.<class>.<test_method>')
67 group.add_argument('--run-all-tests', required=False, action="store_true", dest="run_all_tests", default=False, help='Run all (unhidden) tests')
68 group.add_argument('--list-modules', required=False, action="store_true", dest="list_modules", default=False, help='List all available test modules.')
69 return parser
70
71
72def preflight_check():
73
74 log.info("Checking that everything is in order before running the tests")
75
76 if not os.environ.get("BUILDDIR"):
77 log.error("BUILDDIR isn't set. Did you forget to source your build environment setup script?")
78 return False
79
80 builddir = os.environ.get("BUILDDIR")
81 if os.getcwd() != builddir:
82 log.info("Changing cwd to %s" % builddir)
83 os.chdir(builddir)
84
85 if not "meta-selftest" in get_bb_var("BBLAYERS"):
86 log.error("You don't seem to have the meta-selftest layer in BBLAYERS")
87 return False
88
89 log.info("Running bitbake -p")
90 runCmd("bitbake -p")
91
92 return True
93
94def add_include():
95 builddir = os.environ.get("BUILDDIR")
96 if "#include added by oe-selftest.py" \
97 not in ftools.read_file(os.path.join(builddir, "conf/local.conf")):
98 log.info("Adding: \"include selftest.inc\" in local.conf")
99 ftools.append_file(os.path.join(builddir, "conf/local.conf"), \
100 "\n#include added by oe-selftest.py\ninclude selftest.inc")
101
102
103def remove_include():
104 builddir = os.environ.get("BUILDDIR")
105 if "#include added by oe-selftest.py" \
106 in ftools.read_file(os.path.join(builddir, "conf/local.conf")):
107 log.info("Removing the include from local.conf")
108 ftools.remove_from_file(os.path.join(builddir, "conf/local.conf"), \
109 "#include added by oe-selftest.py\ninclude selftest.inc")
110
111
112def remove_inc_files():
113 try:
114 os.remove(os.path.join(os.environ.get("BUILDDIR"), "conf/selftest.inc"))
115 for root, _, files in os.walk(get_test_layer()):
116 for f in files:
117 if f == 'test_recipe.inc':
118 os.remove(os.path.join(root, f))
119 except OSError as e:
120 pass
121
122def get_tests(exclusive_modules=[], include_hidden=False):
123 testslist = []
124 for x in exclusive_modules:
125 testslist.append('oeqa.selftest.' + x)
126 if not testslist:
127 testpath = os.path.abspath(os.path.dirname(oeqa.selftest.__file__))
128 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'])
129 for f in files:
130 module = 'oeqa.selftest.' + f[:-3]
131 testslist.append(module)
132
133 return testslist
134
135def main():
136 parser = get_args_parser()
137 args = parser.parse_args()
138
139 if args.list_modules:
140 log.info('Listing all available test modules:')
141 testslist = get_tests(include_hidden=True)
142 for test in testslist:
143 module = test.split('.')[-1]
144 info = ''
145 if module.startswith('_'):
146 info = ' (hidden)'
147 print module + info
148
149 if args.run_tests or args.run_all_tests:
150 if not preflight_check():
151 return 1
152
153 testslist = get_tests(exclusive_modules=(args.run_tests or []), include_hidden=False)
154 suite = unittest.TestSuite()
155 loader = unittest.TestLoader()
156 loader.sortTestMethodsUsing = None
157 runner = unittest.TextTestRunner(verbosity=2)
158 # we need to do this here, otherwise just loading the tests
159 # will take 2 minutes (bitbake -e calls)
160 oeSelfTest.testlayer_path = get_test_layer()
161 for test in testslist:
162 log.info("Loading tests from: %s" % test)
163 try:
164 suite.addTests(loader.loadTestsFromName(test))
165 except AttributeError as e:
166 log.error("Failed to import %s" % test)
167 log.error(e)
168 return 1
169 add_include()
170 result = runner.run(suite)
171 log.info("Finished")
172 if result.wasSuccessful():
173 return 0
174 else:
175 return 1
176
177if __name__ == "__main__":
178 try:
179 ret = main()
180 except Exception:
181 ret = 1
182 import traceback
183 traceback.print_exc(5)
184 finally:
185 remove_include()
186 remove_inc_files()
187 sys.exit(ret)