diff options
Diffstat (limited to 'scripts/oe-selftest')
| -rwxr-xr-x | scripts/oe-selftest | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/scripts/oe-selftest b/scripts/oe-selftest new file mode 100755 index 0000000000..db42e73470 --- /dev/null +++ b/scripts/oe-selftest | |||
| @@ -0,0 +1,148 @@ | |||
| 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 | |||
| 28 | import os | ||
| 29 | import sys | ||
| 30 | import unittest | ||
| 31 | import logging | ||
| 32 | |||
| 33 | sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'meta/lib'))) | ||
| 34 | |||
| 35 | import oeqa.selftest | ||
| 36 | import oeqa.utils.ftools as ftools | ||
| 37 | from oeqa.utils.commands import runCmd, get_bb_var, get_test_layer | ||
| 38 | from oeqa.selftest.base import oeSelfTest | ||
| 39 | |||
| 40 | def logger_create(): | ||
| 41 | log = logging.getLogger("selftest") | ||
| 42 | log.setLevel(logging.DEBUG) | ||
| 43 | |||
| 44 | fh = logging.FileHandler(filename='oe-selftest.log', mode='w') | ||
| 45 | fh.setLevel(logging.DEBUG) | ||
| 46 | |||
| 47 | ch = logging.StreamHandler(sys.stdout) | ||
| 48 | ch.setLevel(logging.INFO) | ||
| 49 | |||
| 50 | formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | ||
| 51 | fh.setFormatter(formatter) | ||
| 52 | ch.setFormatter(formatter) | ||
| 53 | |||
| 54 | log.addHandler(fh) | ||
| 55 | log.addHandler(ch) | ||
| 56 | |||
| 57 | return log | ||
| 58 | |||
| 59 | log = logger_create() | ||
| 60 | |||
| 61 | def preflight_check(): | ||
| 62 | |||
| 63 | log.info("Checking that everything is in order before running the tests") | ||
| 64 | |||
| 65 | if not os.environ.get("BUILDDIR"): | ||
| 66 | log.error("BUILDDIR isn't set. Did you forget to source your build environment setup script?") | ||
| 67 | return False | ||
| 68 | |||
| 69 | builddir = os.environ.get("BUILDDIR") | ||
| 70 | if os.getcwd() != builddir: | ||
| 71 | log.info("Changing cwd to %s" % builddir) | ||
| 72 | os.chdir(builddir) | ||
| 73 | |||
| 74 | if not "meta-selftest" in get_bb_var("BBLAYERS"): | ||
| 75 | log.error("You don't seem to have the meta-selftest layer in BBLAYERS") | ||
| 76 | return False | ||
| 77 | |||
| 78 | log.info("Running bitbake -p") | ||
| 79 | runCmd("bitbake -p") | ||
| 80 | |||
| 81 | return True | ||
| 82 | |||
| 83 | def add_include(): | ||
| 84 | builddir = os.environ.get("BUILDDIR") | ||
| 85 | if "#include added by oe-selftest.py" \ | ||
| 86 | not in ftools.read_file(os.path.join(builddir, "conf/local.conf")): | ||
| 87 | log.info("Adding: \"include selftest.inc\" in local.conf") | ||
| 88 | ftools.append_file(os.path.join(builddir, "conf/local.conf"), \ | ||
| 89 | "\n#include added by oe-selftest.py\ninclude selftest.inc") | ||
| 90 | |||
| 91 | |||
| 92 | def remove_include(): | ||
| 93 | builddir = os.environ.get("BUILDDIR") | ||
| 94 | if "#include added by oe-selftest.py" \ | ||
| 95 | in ftools.read_file(os.path.join(builddir, "conf/local.conf")): | ||
| 96 | log.info("Removing the include from local.conf") | ||
| 97 | ftools.remove_from_file(os.path.join(builddir, "conf/local.conf"), \ | ||
| 98 | "#include added by oe-selftest.py\ninclude selftest.inc") | ||
| 99 | |||
| 100 | def get_tests(): | ||
| 101 | testslist = [] | ||
| 102 | for x in sys.argv[1:]: | ||
| 103 | testslist.append('oeqa.selftest.' + x) | ||
| 104 | if not testslist: | ||
| 105 | testpath = os.path.abspath(os.path.dirname(oeqa.selftest.__file__)) | ||
| 106 | files = sorted([f for f in os.listdir(testpath) if f.endswith('.py') and not f.startswith('_') and f != 'base.py']) | ||
| 107 | for f in files: | ||
| 108 | module = 'oeqa.selftest.' + f[:-3] | ||
| 109 | testslist.append(module) | ||
| 110 | |||
| 111 | return testslist | ||
| 112 | |||
| 113 | def main(): | ||
| 114 | if not preflight_check(): | ||
| 115 | return 1 | ||
| 116 | |||
| 117 | testslist = get_tests() | ||
| 118 | suite = unittest.TestSuite() | ||
| 119 | loader = unittest.TestLoader() | ||
| 120 | loader.sortTestMethodsUsing = None | ||
| 121 | runner = unittest.TextTestRunner(verbosity=2) | ||
| 122 | # we need to do this here, otherwise just loading the tests | ||
| 123 | # will take 2 minutes (bitbake -e calls) | ||
| 124 | oeSelfTest.testlayer_path = get_test_layer() | ||
| 125 | for test in testslist: | ||
| 126 | log.info("Loading tests from: %s" % test) | ||
| 127 | try: | ||
| 128 | suite.addTests(loader.loadTestsFromName(test)) | ||
| 129 | except AttributeError as e: | ||
| 130 | log.error("Failed to import %s" % test) | ||
| 131 | log.error(e) | ||
| 132 | return 1 | ||
| 133 | add_include() | ||
| 134 | result = runner.run(suite) | ||
| 135 | log.info("Finished") | ||
| 136 | |||
| 137 | return 0 | ||
| 138 | |||
| 139 | if __name__ == "__main__": | ||
| 140 | try: | ||
| 141 | ret = main() | ||
| 142 | except Exception: | ||
| 143 | ret = 1 | ||
| 144 | import traceback | ||
| 145 | traceback.print_exc(5) | ||
| 146 | finally: | ||
| 147 | remove_include() | ||
| 148 | sys.exit(ret) | ||
