Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 2 | # Copyright (C) 2017 Intel Corporation |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 6 | |
| 7 | import os |
| 8 | import time |
| 9 | import glob |
| 10 | import sys |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 11 | import importlib |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 12 | import subprocess |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 13 | import unittest |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 14 | from random import choice |
| 15 | |
| 16 | import oeqa |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 17 | import oe |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 18 | import bb.utils |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 19 | import bb.tinfoil |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 20 | |
| 21 | from oeqa.core.context import OETestContext, OETestContextExecutor |
| 22 | from oeqa.core.exception import OEQAPreRun, OEQATestNotFound |
| 23 | |
| 24 | from oeqa.utils.commands import runCmd, get_bb_vars, get_test_layer |
| 25 | |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 26 | OESELFTEST_METADATA=["run_all_tests", "run_tests", "skips", "machine", "select_tags", "exclude_tags"] |
| 27 | |
| 28 | def get_oeselftest_metadata(args): |
| 29 | result = {} |
| 30 | raw_args = vars(args) |
| 31 | for metadata in OESELFTEST_METADATA: |
| 32 | if metadata in raw_args: |
| 33 | result[metadata] = raw_args[metadata] |
| 34 | |
| 35 | return result |
| 36 | |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 37 | class NonConcurrentTestSuite(unittest.TestSuite): |
| 38 | def __init__(self, suite, processes, setupfunc, removefunc): |
| 39 | super().__init__([suite]) |
| 40 | self.processes = processes |
| 41 | self.suite = suite |
| 42 | self.setupfunc = setupfunc |
| 43 | self.removefunc = removefunc |
| 44 | |
| 45 | def run(self, result): |
| 46 | (builddir, newbuilddir) = self.setupfunc("-st", None, self.suite) |
| 47 | ret = super().run(result) |
| 48 | os.chdir(builddir) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 49 | if newbuilddir and ret.wasSuccessful() and self.removefunc: |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 50 | self.removefunc(newbuilddir) |
| 51 | |
| 52 | def removebuilddir(d): |
| 53 | delay = 5 |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 54 | while delay and (os.path.exists(d + "/bitbake.lock") or os.path.exists(d + "/cache/hashserv.db-wal")): |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 55 | time.sleep(1) |
| 56 | delay = delay - 1 |
| 57 | # Deleting these directories takes a lot of time, use autobuilder |
| 58 | # clobberdir if its available |
| 59 | clobberdir = os.path.expanduser("~/yocto-autobuilder-helper/janitor/clobberdir") |
| 60 | if os.path.exists(clobberdir): |
| 61 | try: |
| 62 | subprocess.check_call([clobberdir, d]) |
| 63 | return |
| 64 | except subprocess.CalledProcessError: |
| 65 | pass |
| 66 | bb.utils.prunedir(d, ionice=True) |
| 67 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 68 | class OESelftestTestContext(OETestContext): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 69 | def __init__(self, td=None, logger=None, machines=None, config_paths=None, newbuilddir=None, keep_builddir=None): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 70 | super(OESelftestTestContext, self).__init__(td, logger) |
| 71 | |
| 72 | self.machines = machines |
| 73 | self.custommachine = None |
| 74 | self.config_paths = config_paths |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 75 | self.newbuilddir = newbuilddir |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 76 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 77 | if keep_builddir: |
| 78 | self.removebuilddir = None |
| 79 | else: |
| 80 | self.removebuilddir = removebuilddir |
| 81 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 82 | def setup_builddir(self, suffix, selftestdir, suite): |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 83 | # Get SSTATE_DIR from the parent build dir |
| 84 | with bb.tinfoil.Tinfoil(tracking=True) as tinfoil: |
| 85 | tinfoil.prepare(quiet=2, config_only=True) |
| 86 | d = tinfoil.config_data |
| 87 | sstatedir = str(d.getVar('SSTATE_DIR')) |
| 88 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 89 | builddir = os.environ['BUILDDIR'] |
| 90 | if not selftestdir: |
| 91 | selftestdir = get_test_layer() |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 92 | if self.newbuilddir: |
| 93 | newbuilddir = os.path.join(self.newbuilddir, 'build' + suffix) |
| 94 | else: |
| 95 | newbuilddir = builddir + suffix |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 96 | newselftestdir = newbuilddir + "/meta-selftest" |
| 97 | |
| 98 | if os.path.exists(newbuilddir): |
| 99 | self.logger.error("Build directory %s already exists, aborting" % newbuilddir) |
| 100 | sys.exit(1) |
| 101 | |
| 102 | bb.utils.mkdirhier(newbuilddir) |
| 103 | oe.path.copytree(builddir + "/conf", newbuilddir + "/conf") |
| 104 | oe.path.copytree(builddir + "/cache", newbuilddir + "/cache") |
| 105 | oe.path.copytree(selftestdir, newselftestdir) |
| 106 | |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 107 | subprocess.check_output("git init; git add *; git commit -a -m 'initial'", cwd=newselftestdir, shell=True) |
| 108 | |
| 109 | # Tried to used bitbake-layers add/remove but it requires recipe parsing and hence is too slow |
| 110 | subprocess.check_output("sed %s/conf/bblayers.conf -i -e 's#%s#%s#g'" % (newbuilddir, selftestdir, newselftestdir), cwd=newbuilddir, shell=True) |
| 111 | |
| 112 | # Relative paths in BBLAYERS only works when the new build dir share the same ascending node |
| 113 | if self.newbuilddir: |
| 114 | bblayers = subprocess.check_output("bitbake-getvar --value BBLAYERS | tail -1", cwd=builddir, shell=True, text=True) |
| 115 | if '..' in bblayers: |
| 116 | bblayers_abspath = [os.path.abspath(path) for path in bblayers.split()] |
| 117 | with open("%s/conf/bblayers.conf" % newbuilddir, "a") as f: |
| 118 | newbblayers = "# new bblayers to be used by selftest in the new build dir '%s'\n" % newbuilddir |
| 119 | newbblayers += 'BBLAYERS = "%s"\n' % ' '.join(bblayers_abspath) |
| 120 | f.write(newbblayers) |
| 121 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 122 | for e in os.environ: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 123 | if builddir + "/" in os.environ[e]: |
| 124 | os.environ[e] = os.environ[e].replace(builddir + "/", newbuilddir + "/") |
| 125 | if os.environ[e].endswith(builddir): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 126 | os.environ[e] = os.environ[e].replace(builddir, newbuilddir) |
| 127 | |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 128 | # Set SSTATE_DIR to match the parent SSTATE_DIR |
| 129 | subprocess.check_output("echo 'SSTATE_DIR ?= \"%s\"' >> %s/conf/local.conf" % (sstatedir, newbuilddir), cwd=newbuilddir, shell=True) |
| 130 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 131 | os.chdir(newbuilddir) |
| 132 | |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 133 | def patch_test(t): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 134 | if not hasattr(t, "tc"): |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 135 | return |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 136 | cp = t.tc.config_paths |
| 137 | for p in cp: |
| 138 | if selftestdir in cp[p] and newselftestdir not in cp[p]: |
| 139 | cp[p] = cp[p].replace(selftestdir, newselftestdir) |
| 140 | if builddir in cp[p] and newbuilddir not in cp[p]: |
| 141 | cp[p] = cp[p].replace(builddir, newbuilddir) |
| 142 | |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 143 | def patch_suite(s): |
| 144 | for x in s: |
| 145 | if isinstance(x, unittest.TestSuite): |
| 146 | patch_suite(x) |
| 147 | else: |
| 148 | patch_test(x) |
| 149 | |
| 150 | patch_suite(suite) |
| 151 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 152 | return (builddir, newbuilddir) |
| 153 | |
| 154 | def prepareSuite(self, suites, processes): |
| 155 | if processes: |
| 156 | from oeqa.core.utils.concurrencytest import ConcurrentTestSuite |
| 157 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 158 | return ConcurrentTestSuite(suites, processes, self.setup_builddir, self.removebuilddir) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 159 | else: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 160 | return NonConcurrentTestSuite(suites, processes, self.setup_builddir, self.removebuilddir) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 161 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 162 | def runTests(self, processes=None, machine=None, skips=[]): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 163 | if machine: |
| 164 | self.custommachine = machine |
| 165 | if machine == 'random': |
| 166 | self.custommachine = choice(self.machines) |
| 167 | self.logger.info('Run tests with custom MACHINE set to: %s' % \ |
| 168 | self.custommachine) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 169 | return super(OESelftestTestContext, self).runTests(processes, skips) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 170 | |
| 171 | def listTests(self, display_type, machine=None): |
| 172 | return super(OESelftestTestContext, self).listTests(display_type) |
| 173 | |
| 174 | class OESelftestTestContextExecutor(OETestContextExecutor): |
| 175 | _context_class = OESelftestTestContext |
| 176 | _script_executor = 'oe-selftest' |
| 177 | |
| 178 | name = 'oe-selftest' |
| 179 | help = 'oe-selftest test component' |
| 180 | description = 'Executes selftest tests' |
| 181 | |
| 182 | def register_commands(self, logger, parser): |
| 183 | group = parser.add_mutually_exclusive_group(required=True) |
| 184 | |
| 185 | group.add_argument('-a', '--run-all-tests', default=False, |
| 186 | action="store_true", dest="run_all_tests", |
| 187 | help='Run all (unhidden) tests') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 188 | group.add_argument('-r', '--run-tests', required=False, action='store', |
| 189 | nargs='+', dest="run_tests", default=None, |
| 190 | help='Select what tests to run (modules, classes or test methods). Format should be: <module>.<class>.<test_method>') |
| 191 | |
| 192 | group.add_argument('-m', '--list-modules', required=False, |
| 193 | action="store_true", default=False, |
| 194 | help='List all available test modules.') |
| 195 | group.add_argument('--list-classes', required=False, |
| 196 | action="store_true", default=False, |
| 197 | help='List all available test classes.') |
| 198 | group.add_argument('-l', '--list-tests', required=False, |
| 199 | action="store_true", default=False, |
| 200 | help='List all available tests.') |
| 201 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 202 | parser.add_argument('-R', '--skip-tests', required=False, action='store', |
| 203 | nargs='+', dest="skips", default=None, |
| 204 | help='Skip the tests specified. Format should be <module>[.<class>[.<test_method>]]') |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 205 | parser.add_argument('-j', '--num-processes', dest='processes', action='store', |
| 206 | type=int, help="number of processes to execute in parallel with") |
| 207 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 208 | parser.add_argument('--machine', required=False, choices=['random', 'all'], |
| 209 | help='Run tests on different machines (random/all).') |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 210 | |
Brad Bishop | acc069e | 2019-09-13 06:48:36 -0400 | [diff] [blame] | 211 | parser.add_argument('-t', '--select-tag', dest="select_tags", |
| 212 | action='append', default=None, |
| 213 | help='Filter all (unhidden) tests to any that match any of the specified tag(s).') |
| 214 | parser.add_argument('-T', '--exclude-tag', dest="exclude_tags", |
| 215 | action='append', default=None, |
| 216 | help='Exclude all (unhidden) tests that match any of the specified tag(s). (exclude applies before select)') |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 217 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 218 | parser.add_argument('-K', '--keep-builddir', action='store_true', |
| 219 | help='Keep the test build directory even if all tests pass') |
| 220 | |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 221 | parser.add_argument('-B', '--newbuilddir', help='New build directory to use for tests.') |
| 222 | parser.add_argument('-v', '--verbose', action='store_true') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 223 | parser.set_defaults(func=self.run) |
| 224 | |
| 225 | def _get_available_machines(self): |
| 226 | machines = [] |
| 227 | |
| 228 | bbpath = self.tc_kwargs['init']['td']['BBPATH'].split(':') |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame^] | 229 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 230 | for path in bbpath: |
| 231 | found_machines = glob.glob(os.path.join(path, 'conf', 'machine', '*.conf')) |
| 232 | if found_machines: |
| 233 | for i in found_machines: |
| 234 | # eg: '/home/<user>/poky/meta-intel/conf/machine/intel-core2-32.conf' |
| 235 | machines.append(os.path.splitext(os.path.basename(i))[0]) |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame^] | 236 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 237 | return machines |
| 238 | |
| 239 | def _get_cases_paths(self, bbpath): |
| 240 | cases_paths = [] |
| 241 | for layer in bbpath: |
| 242 | cases_dir = os.path.join(layer, 'lib', 'oeqa', 'selftest', 'cases') |
| 243 | if os.path.isdir(cases_dir): |
| 244 | cases_paths.append(cases_dir) |
| 245 | return cases_paths |
| 246 | |
| 247 | def _process_args(self, logger, args): |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 248 | args.test_start_time = time.strftime("%Y%m%d%H%M%S") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 249 | args.test_data_file = None |
| 250 | args.CASES_PATHS = None |
| 251 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 252 | bbvars = get_bb_vars() |
| 253 | logdir = os.environ.get("BUILDDIR") |
| 254 | if 'LOG_DIR' in bbvars: |
| 255 | logdir = bbvars['LOG_DIR'] |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 256 | bb.utils.mkdirhier(logdir) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 257 | args.output_log = logdir + '/%s-results-%s.log' % (self.name, args.test_start_time) |
| 258 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 259 | super(OESelftestTestContextExecutor, self)._process_args(logger, args) |
| 260 | |
| 261 | if args.list_modules: |
| 262 | args.list_tests = 'module' |
| 263 | elif args.list_classes: |
| 264 | args.list_tests = 'class' |
| 265 | elif args.list_tests: |
| 266 | args.list_tests = 'name' |
| 267 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 268 | self.tc_kwargs['init']['td'] = bbvars |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 269 | self.tc_kwargs['init']['machines'] = self._get_available_machines() |
| 270 | |
| 271 | builddir = os.environ.get("BUILDDIR") |
| 272 | self.tc_kwargs['init']['config_paths'] = {} |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 273 | self.tc_kwargs['init']['config_paths']['testlayer_path'] = get_test_layer() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 274 | self.tc_kwargs['init']['config_paths']['builddir'] = builddir |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 275 | self.tc_kwargs['init']['config_paths']['localconf'] = os.path.join(builddir, "conf/local.conf") |
| 276 | self.tc_kwargs['init']['config_paths']['bblayers'] = os.path.join(builddir, "conf/bblayers.conf") |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 277 | self.tc_kwargs['init']['newbuilddir'] = args.newbuilddir |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 278 | self.tc_kwargs['init']['keep_builddir'] = args.keep_builddir |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 279 | |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 280 | def tag_filter(tags): |
| 281 | if args.exclude_tags: |
| 282 | if any(tag in args.exclude_tags for tag in tags): |
| 283 | return True |
| 284 | if args.select_tags: |
| 285 | if not tags or not any(tag in args.select_tags for tag in tags): |
| 286 | return True |
| 287 | return False |
| 288 | |
| 289 | if args.select_tags or args.exclude_tags: |
| 290 | self.tc_kwargs['load']['tags_filter'] = tag_filter |
| 291 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 292 | self.tc_kwargs['run']['skips'] = args.skips |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 293 | self.tc_kwargs['run']['processes'] = args.processes |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 294 | |
| 295 | def _pre_run(self): |
| 296 | def _check_required_env_variables(vars): |
| 297 | for var in vars: |
| 298 | if not os.environ.get(var): |
| 299 | self.tc.logger.error("%s is not set. Did you forget to source your build environment setup script?" % var) |
| 300 | raise OEQAPreRun |
| 301 | |
| 302 | def _check_presence_meta_selftest(): |
| 303 | builddir = os.environ.get("BUILDDIR") |
| 304 | if os.getcwd() != builddir: |
| 305 | self.tc.logger.info("Changing cwd to %s" % builddir) |
| 306 | os.chdir(builddir) |
| 307 | |
| 308 | if not "meta-selftest" in self.tc.td["BBLAYERS"]: |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame^] | 309 | self.tc.logger.info("meta-selftest layer not found in BBLAYERS, adding it") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 310 | meta_selftestdir = os.path.join( |
| 311 | self.tc.td["BBLAYERS_FETCH_DIR"], 'meta-selftest') |
| 312 | if os.path.isdir(meta_selftestdir): |
| 313 | runCmd("bitbake-layers add-layer %s" %meta_selftestdir) |
| 314 | # reload data is needed because a meta-selftest layer was add |
| 315 | self.tc.td = get_bb_vars() |
| 316 | self.tc.config_paths['testlayer_path'] = get_test_layer() |
| 317 | else: |
| 318 | self.tc.logger.error("could not locate meta-selftest in:\n%s" % meta_selftestdir) |
| 319 | raise OEQAPreRun |
| 320 | |
| 321 | def _add_layer_libs(): |
| 322 | bbpath = self.tc.td['BBPATH'].split(':') |
| 323 | layer_libdirs = [p for p in (os.path.join(l, 'lib') \ |
| 324 | for l in bbpath) if os.path.exists(p)] |
| 325 | if layer_libdirs: |
| 326 | self.tc.logger.info("Adding layer libraries:") |
| 327 | for l in layer_libdirs: |
| 328 | self.tc.logger.info("\t%s" % l) |
| 329 | |
| 330 | sys.path.extend(layer_libdirs) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 331 | importlib.reload(oeqa.selftest) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 332 | |
| 333 | _check_required_env_variables(["BUILDDIR"]) |
| 334 | _check_presence_meta_selftest() |
| 335 | |
| 336 | if "buildhistory.bbclass" in self.tc.td["BBINCLUDED"]: |
| 337 | self.tc.logger.error("You have buildhistory enabled already and this isn't recommended for selftest, please disable it first.") |
| 338 | raise OEQAPreRun |
| 339 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 340 | if "rm_work.bbclass" in self.tc.td["BBINCLUDED"]: |
| 341 | self.tc.logger.error("You have rm_work enabled which isn't recommended while running oe-selftest. Please disable it before continuing.") |
| 342 | raise OEQAPreRun |
| 343 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 344 | if "PRSERV_HOST" in self.tc.td: |
| 345 | self.tc.logger.error("Please unset PRSERV_HOST in order to run oe-selftest") |
| 346 | raise OEQAPreRun |
| 347 | |
| 348 | if "SANITY_TESTED_DISTROS" in self.tc.td: |
| 349 | self.tc.logger.error("Please unset SANITY_TESTED_DISTROS in order to run oe-selftest") |
| 350 | raise OEQAPreRun |
| 351 | |
| 352 | _add_layer_libs() |
| 353 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 354 | self.tc.logger.info("Running bitbake -e to test the configuration is valid/parsable") |
| 355 | runCmd("bitbake -e") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 356 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 357 | def get_json_result_dir(self, args): |
| 358 | json_result_dir = os.path.join(self.tc.td["LOG_DIR"], 'oeqa') |
| 359 | if "OEQA_JSON_RESULT_DIR" in self.tc.td: |
| 360 | json_result_dir = self.tc.td["OEQA_JSON_RESULT_DIR"] |
| 361 | |
| 362 | return json_result_dir |
| 363 | |
| 364 | def get_configuration(self, args): |
| 365 | import platform |
| 366 | from oeqa.utils.metadata import metadata_from_bb |
| 367 | metadata = metadata_from_bb() |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 368 | oeselftest_metadata = get_oeselftest_metadata(args) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 369 | configuration = {'TEST_TYPE': 'oeselftest', |
| 370 | 'STARTTIME': args.test_start_time, |
| 371 | 'MACHINE': self.tc.td["MACHINE"], |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 372 | 'HOST_DISTRO': oe.lsb.distro_identifier().replace(' ', '-'), |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 373 | 'HOST_NAME': metadata['hostname'], |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 374 | 'LAYERS': metadata['layers'], |
| 375 | 'OESELFTEST_METADATA': oeselftest_metadata} |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 376 | return configuration |
| 377 | |
| 378 | def get_result_id(self, configuration): |
| 379 | return '%s_%s_%s_%s' % (configuration['TEST_TYPE'], configuration['HOST_DISTRO'], configuration['MACHINE'], configuration['STARTTIME']) |
| 380 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 381 | def _internal_run(self, logger, args): |
| 382 | self.module_paths = self._get_cases_paths( |
| 383 | self.tc_kwargs['init']['td']['BBPATH'].split(':')) |
| 384 | |
| 385 | self.tc = self._context_class(**self.tc_kwargs['init']) |
| 386 | try: |
| 387 | self.tc.loadTests(self.module_paths, **self.tc_kwargs['load']) |
| 388 | except OEQATestNotFound as ex: |
| 389 | logger.error(ex) |
| 390 | sys.exit(1) |
| 391 | |
| 392 | if args.list_tests: |
| 393 | rc = self.tc.listTests(args.list_tests, **self.tc_kwargs['list']) |
| 394 | else: |
| 395 | self._pre_run() |
| 396 | rc = self.tc.runTests(**self.tc_kwargs['run']) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 397 | configuration = self.get_configuration(args) |
| 398 | rc.logDetails(self.get_json_result_dir(args), |
| 399 | configuration, |
| 400 | self.get_result_id(configuration)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 401 | rc.logSummary(self.name) |
| 402 | |
| 403 | return rc |
| 404 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 405 | def run(self, logger, args): |
| 406 | self._process_args(logger, args) |
| 407 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 408 | rc = None |
| 409 | try: |
| 410 | if args.machine: |
| 411 | logger.info('Custom machine mode enabled. MACHINE set to %s' % |
| 412 | args.machine) |
| 413 | |
| 414 | if args.machine == 'all': |
| 415 | results = [] |
| 416 | for m in self.tc_kwargs['init']['machines']: |
| 417 | self.tc_kwargs['run']['machine'] = m |
| 418 | results.append(self._internal_run(logger, args)) |
| 419 | |
| 420 | # XXX: the oe-selftest script only needs to know if one |
| 421 | # machine run fails |
| 422 | for r in results: |
| 423 | rc = r |
| 424 | if not r.wasSuccessful(): |
| 425 | break |
| 426 | |
| 427 | else: |
| 428 | self.tc_kwargs['run']['machine'] = args.machine |
| 429 | return self._internal_run(logger, args) |
| 430 | |
| 431 | else: |
| 432 | self.tc_kwargs['run']['machine'] = args.machine |
| 433 | rc = self._internal_run(logger, args) |
| 434 | finally: |
| 435 | config_paths = self.tc_kwargs['init']['config_paths'] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 436 | |
| 437 | output_link = os.path.join(os.path.dirname(args.output_log), |
| 438 | "%s-results.log" % self.name) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 439 | if os.path.lexists(output_link): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 440 | os.remove(output_link) |
| 441 | os.symlink(args.output_log, output_link) |
| 442 | |
| 443 | return rc |
| 444 | |
| 445 | _executor_class = OESelftestTestContextExecutor |