Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # Copyright (C) 2013 Intel Corporation |
| 2 | # |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | |
| 5 | inherit metadata_scm |
| 6 | inherit image-artifact-names |
| 7 | |
| 8 | # testimage.bbclass enables testing of qemu images using python unittests. |
| 9 | # Most of the tests are commands run on target image over ssh. |
| 10 | # To use it add testimage to global inherit and call your target image with -c testimage |
| 11 | # You can try it out like this: |
| 12 | # - first add IMAGE_CLASSES += "testimage" in local.conf |
| 13 | # - build a qemu core-image-sato |
| 14 | # - then bitbake core-image-sato -c testimage. That will run a standard suite of tests. |
| 15 | # |
| 16 | # The tests can be run automatically each time an image is built if you set |
| 17 | # TESTIMAGE_AUTO = "1" |
| 18 | |
| 19 | TESTIMAGE_AUTO ??= "0" |
| 20 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 21 | # When any test fails, TESTIMAGE_FAILED_QA ARTIFACTS will be parsed and for |
| 22 | # each entry in it, if artifact pointed by path description exists on target, |
| 23 | # it will be retrieved onto host |
| 24 | |
| 25 | TESTIMAGE_FAILED_QA_ARTIFACTS ??= "\ |
| 26 | ${localstatedir}/log \ |
| 27 | ${sysconfdir}/version \ |
| 28 | ${sysconfdir}/os-release" |
| 29 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 30 | # You can set (or append to) TEST_SUITES in local.conf to select the tests |
| 31 | # which you want to run for your target. |
| 32 | # The test names are the module names in meta/lib/oeqa/runtime/cases. |
| 33 | # Each name in TEST_SUITES represents a required test for the image. (no skipping allowed) |
| 34 | # Appending "auto" means that it will try to run all tests that are suitable for the image (each test decides that on it's own). |
| 35 | # Note that order in TEST_SUITES is relevant: tests are run in an order such that |
| 36 | # tests mentioned in @skipUnlessPassed run before the tests that depend on them, |
| 37 | # but without such dependencies, tests run in the order in which they are listed |
| 38 | # in TEST_SUITES. |
| 39 | # |
| 40 | # A layer can add its own tests in lib/oeqa/runtime, provided it extends BBPATH as normal in its layer.conf. |
| 41 | |
| 42 | # TEST_LOG_DIR contains a command ssh log and may contain infromation about what command is running, output and return codes and for qemu a boot log till login. |
| 43 | # Booting is handled by this class, and it's not a test in itself. |
| 44 | # TEST_QEMUBOOT_TIMEOUT can be used to set the maximum time in seconds the launch code will wait for the login prompt. |
| 45 | # TEST_OVERALL_TIMEOUT can be used to set the maximum time in seconds the tests will be allowed to run (defaults to no limit). |
| 46 | # TEST_QEMUPARAMS can be used to pass extra parameters to qemu, e.g. "-m 1024" for setting the amount of ram to 1 GB. |
| 47 | # TEST_RUNQEMUPARAMS can be used to pass extra parameters to runqemu, e.g. "gl" to enable OpenGL acceleration. |
| 48 | # QEMU_USE_KVM can be set to "" to disable the use of kvm (by default it is enabled if target_arch == build_arch or both of them are x86 archs) |
| 49 | |
| 50 | # TESTIMAGE_BOOT_PATTERNS can be used to override certain patterns used to communicate with the target when booting, |
| 51 | # if a pattern is not specifically present on this variable a default will be used when booting the target. |
| 52 | # TESTIMAGE_BOOT_PATTERNS[<flag>] overrides the pattern used for that specific flag, where flag comes from a list of accepted flags |
| 53 | # e.g. normally the system boots and waits for a login prompt (login:), after that it sends the command: "root\n" to log as the root user |
| 54 | # if we wanted to log in as the hypothetical "webserver" user for example we could set the following: |
| 55 | # TESTIMAGE_BOOT_PATTERNS = "send_login_user search_login_succeeded" |
| 56 | # TESTIMAGE_BOOT_PATTERNS[send_login_user] = "webserver\n" |
| 57 | # TESTIMAGE_BOOT_PATTERNS[search_login_succeeded] = "webserver@[a-zA-Z0-9\-]+:~#" |
| 58 | # The accepted flags are the following: search_reached_prompt, send_login_user, search_login_succeeded, search_cmd_finished. |
| 59 | # They are prefixed with either search/send, to differentiate if the pattern is meant to be sent or searched to/from the target terminal |
| 60 | |
| 61 | TEST_LOG_DIR ?= "${WORKDIR}/testimage" |
| 62 | |
| 63 | TEST_EXPORT_DIR ?= "${TMPDIR}/testimage/${PN}" |
| 64 | TEST_INSTALL_TMP_DIR ?= "${WORKDIR}/testimage/install_tmp" |
| 65 | TEST_NEEDED_PACKAGES_DIR ?= "${WORKDIR}/testimage/packages" |
| 66 | TEST_EXTRACTED_DIR ?= "${TEST_NEEDED_PACKAGES_DIR}/extracted" |
| 67 | TEST_PACKAGED_DIR ?= "${TEST_NEEDED_PACKAGES_DIR}/packaged" |
| 68 | |
| 69 | BASICTESTSUITE = "\ |
| 70 | ping date df ssh scp python perl gi ptest parselogs \ |
| 71 | logrotate connman systemd oe_syslog pam stap ldd xorg \ |
| 72 | kernelmodule gcc buildcpio buildlzip buildgalculator \ |
| 73 | dnf rpm opkg apt weston go rust" |
| 74 | |
| 75 | DEFAULT_TEST_SUITES = "${BASICTESTSUITE}" |
| 76 | |
| 77 | # musl doesn't support systemtap |
| 78 | DEFAULT_TEST_SUITES:remove:libc-musl = "stap" |
| 79 | |
| 80 | # qemumips is quite slow and has reached the timeout limit several times on the YP build cluster, |
| 81 | # mitigate this by removing build tests for qemumips machines. |
| 82 | MIPSREMOVE ??= "buildcpio buildlzip buildgalculator" |
| 83 | DEFAULT_TEST_SUITES:remove:qemumips = "${MIPSREMOVE}" |
| 84 | DEFAULT_TEST_SUITES:remove:qemumips64 = "${MIPSREMOVE}" |
| 85 | |
| 86 | TEST_SUITES ?= "${DEFAULT_TEST_SUITES}" |
| 87 | |
| 88 | QEMU_USE_KVM ?= "1" |
| 89 | TEST_QEMUBOOT_TIMEOUT ?= "1000" |
| 90 | TEST_OVERALL_TIMEOUT ?= "" |
| 91 | TEST_TARGET ?= "qemu" |
| 92 | TEST_QEMUPARAMS ?= "" |
| 93 | TEST_RUNQEMUPARAMS ?= "" |
| 94 | |
| 95 | TESTIMAGE_BOOT_PATTERNS ?= "" |
| 96 | |
| 97 | TESTIMAGEDEPENDS = "" |
| 98 | TESTIMAGEDEPENDS:append:qemuall = " qemu-native:do_populate_sysroot qemu-helper-native:do_populate_sysroot qemu-helper-native:do_addto_recipe_sysroot" |
| 99 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'cpio-native:do_populate_sysroot', '', d)}" |
| 100 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'dnf-native:do_populate_sysroot', '', d)}" |
| 101 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'createrepo-c-native:do_populate_sysroot', '', d)}" |
| 102 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'ipk', 'opkg-utils-native:do_populate_sysroot package-index:do_package_index', '', d)}" |
| 103 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'deb', 'apt-native:do_populate_sysroot package-index:do_package_index', '', d)}" |
| 104 | |
| 105 | TESTIMAGELOCK = "${TMPDIR}/testimage.lock" |
| 106 | TESTIMAGELOCK:qemuall = "" |
| 107 | |
| 108 | TESTIMAGE_DUMP_DIR ?= "${LOG_DIR}/runtime-hostdump/" |
| 109 | |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 110 | TESTIMAGE_UPDATE_VARS ?= "DL_DIR WORKDIR DEPLOY_DIR_IMAGE IMAGE_LINK_NAME" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 111 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 112 | testimage_dump_monitor () { |
| 113 | query-status |
| 114 | query-block |
| 115 | dump-guest-memory {"paging":false,"protocol":"file:%s.img"} |
| 116 | } |
| 117 | |
| 118 | python do_testimage() { |
| 119 | testimage_main(d) |
| 120 | } |
| 121 | |
| 122 | addtask testimage |
| 123 | do_testimage[nostamp] = "1" |
| 124 | do_testimage[network] = "1" |
| 125 | do_testimage[depends] += "${TESTIMAGEDEPENDS}" |
| 126 | do_testimage[lockfiles] += "${TESTIMAGELOCK}" |
| 127 | |
| 128 | def testimage_sanity(d): |
| 129 | if (d.getVar('TEST_TARGET') == 'simpleremote' |
| 130 | and (not d.getVar('TEST_TARGET_IP') |
| 131 | or not d.getVar('TEST_SERVER_IP'))): |
| 132 | bb.fatal('When TEST_TARGET is set to "simpleremote" ' |
| 133 | 'TEST_TARGET_IP and TEST_SERVER_IP are needed too.') |
| 134 | |
| 135 | def get_testimage_configuration(d, test_type, machine): |
| 136 | import platform |
| 137 | from oeqa.utils.metadata import get_layers |
| 138 | configuration = {'TEST_TYPE': test_type, |
| 139 | 'MACHINE': machine, |
| 140 | 'DISTRO': d.getVar("DISTRO"), |
| 141 | 'IMAGE_BASENAME': d.getVar("IMAGE_BASENAME"), |
| 142 | 'IMAGE_PKGTYPE': d.getVar("IMAGE_PKGTYPE"), |
| 143 | 'STARTTIME': d.getVar("DATETIME"), |
| 144 | 'HOST_DISTRO': oe.lsb.distro_identifier().replace(' ', '-'), |
| 145 | 'LAYERS': get_layers(d.getVar("BBLAYERS"))} |
| 146 | return configuration |
| 147 | get_testimage_configuration[vardepsexclude] = "DATETIME" |
| 148 | |
| 149 | def get_testimage_json_result_dir(d): |
| 150 | json_result_dir = os.path.join(d.getVar("LOG_DIR"), 'oeqa') |
| 151 | custom_json_result_dir = d.getVar("OEQA_JSON_RESULT_DIR") |
| 152 | if custom_json_result_dir: |
| 153 | json_result_dir = custom_json_result_dir |
| 154 | return json_result_dir |
| 155 | |
| 156 | def get_testimage_result_id(configuration): |
| 157 | return '%s_%s_%s_%s' % (configuration['TEST_TYPE'], configuration['IMAGE_BASENAME'], configuration['MACHINE'], configuration['STARTTIME']) |
| 158 | |
| 159 | def get_testimage_boot_patterns(d): |
| 160 | from collections import defaultdict |
| 161 | boot_patterns = defaultdict(str) |
| 162 | # Only accept certain values |
| 163 | accepted_patterns = ['search_reached_prompt', 'send_login_user', 'search_login_succeeded', 'search_cmd_finished'] |
| 164 | # Not all patterns need to be overriden, e.g. perhaps we only want to change the user |
| 165 | boot_patterns_flags = d.getVarFlags('TESTIMAGE_BOOT_PATTERNS') or {} |
| 166 | if boot_patterns_flags: |
| 167 | patterns_set = [p for p in boot_patterns_flags.items() if p[0] in d.getVar('TESTIMAGE_BOOT_PATTERNS').split()] |
| 168 | for flag, flagval in patterns_set: |
| 169 | if flag not in accepted_patterns: |
| 170 | bb.fatal('Testimage: The only accepted boot patterns are: search_reached_prompt,send_login_user, \ |
| 171 | search_login_succeeded,search_cmd_finished\n Make sure your TESTIMAGE_BOOT_PATTERNS=%s \ |
| 172 | contains an accepted flag.' % d.getVar('TESTIMAGE_BOOT_PATTERNS')) |
| 173 | return |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 174 | boot_patterns[flag] = flagval.encode().decode('unicode-escape') |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 175 | return boot_patterns |
| 176 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 177 | def get_artifacts_list(target, raw_list): |
| 178 | result = [] |
| 179 | # Passed list may contains patterns in paths, expand them directly on target |
| 180 | for raw_path in raw_list.split(): |
| 181 | cmd = f"for p in {raw_path}; do if [ -e $p ]; then echo $p; fi; done" |
| 182 | try: |
| 183 | status, output = target.run(cmd) |
| 184 | if status != 0 or not output: |
| 185 | raise Exception() |
| 186 | result += output.split() |
| 187 | except: |
| 188 | bb.note(f"No file/directory matching path {raw_path}") |
| 189 | |
| 190 | return result |
| 191 | |
| 192 | def retrieve_test_artifacts(target, artifacts_list, target_dir): |
| 193 | import shutil |
| 194 | |
| 195 | local_artifacts_dir = os.path.join(target_dir, "artifacts") |
| 196 | if os.path.isdir(local_artifacts_dir): |
| 197 | shutil.rmtree(local_artifacts_dir) |
| 198 | |
| 199 | os.makedirs(local_artifacts_dir) |
| 200 | for artifact_path in artifacts_list: |
| 201 | if not os.path.isabs(artifact_path): |
| 202 | bb.warn(f"{artifact_path} is not an absolute path") |
| 203 | continue |
| 204 | try: |
| 205 | dest_dir = os.path.join(local_artifacts_dir, os.path.dirname(artifact_path[1:])) |
| 206 | os.makedirs(dest_dir, exist_ok=True) |
| 207 | target.copyFrom(artifact_path, dest_dir) |
| 208 | except: |
| 209 | bb.warn(f"Can not retrieve {artifact_path} from test target") |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 210 | |
| 211 | def testimage_main(d): |
| 212 | import os |
| 213 | import json |
| 214 | import signal |
| 215 | import logging |
| 216 | import shutil |
| 217 | |
| 218 | from bb.utils import export_proxies |
| 219 | from oeqa.runtime.context import OERuntimeTestContext |
| 220 | from oeqa.runtime.context import OERuntimeTestContextExecutor |
| 221 | from oeqa.core.target.qemu import supported_fstypes |
| 222 | from oeqa.core.utils.test import getSuiteCases |
| 223 | from oeqa.utils import make_logger_bitbake_compatible |
| 224 | |
| 225 | def sigterm_exception(signum, stackframe): |
| 226 | """ |
| 227 | Catch SIGTERM from worker in order to stop qemu. |
| 228 | """ |
| 229 | os.kill(os.getpid(), signal.SIGINT) |
| 230 | |
| 231 | def handle_test_timeout(timeout): |
| 232 | bb.warn("Global test timeout reached (%s seconds), stopping the tests." %(timeout)) |
| 233 | os.kill(os.getpid(), signal.SIGINT) |
| 234 | |
| 235 | testimage_sanity(d) |
| 236 | |
| 237 | if (d.getVar('IMAGE_PKGTYPE') == 'rpm' |
| 238 | and ('dnf' in d.getVar('TEST_SUITES') or 'auto' in d.getVar('TEST_SUITES'))): |
| 239 | create_rpm_index(d) |
| 240 | |
| 241 | logger = make_logger_bitbake_compatible(logging.getLogger("BitBake")) |
| 242 | pn = d.getVar("PN") |
| 243 | |
| 244 | bb.utils.mkdirhier(d.getVar("TEST_LOG_DIR")) |
| 245 | |
| 246 | image_name = ("%s/%s" % (d.getVar('DEPLOY_DIR_IMAGE'), |
| 247 | d.getVar('IMAGE_LINK_NAME'))) |
| 248 | |
| 249 | tdname = "%s.testdata.json" % image_name |
| 250 | try: |
| 251 | with open(tdname, "r") as f: |
| 252 | td = json.load(f) |
| 253 | except FileNotFoundError as err: |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 254 | bb.fatal('File %s not found (%s).\nHave you built the image with IMAGE_CLASSES += "testimage" in the conf/local.conf?' % (tdname, err)) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 255 | |
| 256 | # Some variables need to be updates (mostly paths) with the |
| 257 | # ones of the current environment because some tests require them. |
| 258 | for var in d.getVar('TESTIMAGE_UPDATE_VARS').split(): |
| 259 | td[var] = d.getVar(var) |
| 260 | |
| 261 | image_manifest = "%s.manifest" % image_name |
| 262 | image_packages = OERuntimeTestContextExecutor.readPackagesManifest(image_manifest) |
| 263 | |
| 264 | extract_dir = d.getVar("TEST_EXTRACTED_DIR") |
| 265 | |
| 266 | # Get machine |
| 267 | machine = d.getVar("MACHINE") |
| 268 | |
| 269 | # Get rootfs |
| 270 | fstypes = d.getVar('IMAGE_FSTYPES').split() |
| 271 | if d.getVar("TEST_TARGET") == "qemu": |
| 272 | fstypes = [fs for fs in fstypes if fs in supported_fstypes] |
| 273 | if not fstypes: |
| 274 | bb.fatal('Unsupported image type built. Add a compatible image to ' |
| 275 | 'IMAGE_FSTYPES. Supported types: %s' % |
| 276 | ', '.join(supported_fstypes)) |
| 277 | qfstype = fstypes[0] |
| 278 | qdeffstype = d.getVar("QB_DEFAULT_FSTYPE") |
| 279 | if qdeffstype: |
| 280 | qfstype = qdeffstype |
| 281 | rootfs = '%s.%s' % (image_name, qfstype) |
| 282 | |
| 283 | # Get tmpdir (not really used, just for compatibility) |
| 284 | tmpdir = d.getVar("TMPDIR") |
| 285 | |
| 286 | # Get deploy_dir_image (not really used, just for compatibility) |
| 287 | dir_image = d.getVar("DEPLOY_DIR_IMAGE") |
| 288 | |
| 289 | # Get bootlog |
| 290 | bootlog = os.path.join(d.getVar("TEST_LOG_DIR"), |
| 291 | 'qemu_boot_log.%s' % d.getVar('DATETIME')) |
| 292 | |
| 293 | # Get display |
| 294 | display = d.getVar("BB_ORIGENV").getVar("DISPLAY") |
| 295 | |
| 296 | # Get kernel |
| 297 | kernel_name = ('%s-%s.bin' % (d.getVar("KERNEL_IMAGETYPE"), machine)) |
| 298 | kernel = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), kernel_name) |
| 299 | |
| 300 | # Get boottime |
| 301 | boottime = int(d.getVar("TEST_QEMUBOOT_TIMEOUT")) |
| 302 | |
| 303 | # Get use_kvm |
| 304 | kvm = oe.types.qemu_use_kvm(d.getVar('QEMU_USE_KVM'), d.getVar('TARGET_ARCH')) |
| 305 | |
| 306 | # Get OVMF |
| 307 | ovmf = d.getVar("QEMU_USE_OVMF") |
| 308 | |
| 309 | slirp = False |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 310 | if bb.utils.contains('TEST_RUNQEMUPARAMS', 'slirp', True, False, d): |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 311 | slirp = True |
| 312 | |
| 313 | # TODO: We use the current implementation of qemu runner because of |
| 314 | # time constrains, qemu runner really needs a refactor too. |
| 315 | target_kwargs = { 'machine' : machine, |
| 316 | 'rootfs' : rootfs, |
| 317 | 'tmpdir' : tmpdir, |
| 318 | 'dir_image' : dir_image, |
| 319 | 'display' : display, |
| 320 | 'kernel' : kernel, |
| 321 | 'boottime' : boottime, |
| 322 | 'bootlog' : bootlog, |
| 323 | 'kvm' : kvm, |
| 324 | 'slirp' : slirp, |
| 325 | 'dump_dir' : d.getVar("TESTIMAGE_DUMP_DIR"), |
| 326 | 'serial_ports': len(d.getVar("SERIAL_CONSOLES").split()), |
| 327 | 'ovmf' : ovmf, |
| 328 | 'tmpfsdir' : d.getVar("RUNQEMU_TMPFS_DIR"), |
| 329 | } |
| 330 | |
| 331 | if d.getVar("TESTIMAGE_BOOT_PATTERNS"): |
| 332 | target_kwargs['boot_patterns'] = get_testimage_boot_patterns(d) |
| 333 | |
| 334 | # hardware controlled targets might need further access |
| 335 | target_kwargs['powercontrol_cmd'] = d.getVar("TEST_POWERCONTROL_CMD") or None |
| 336 | target_kwargs['powercontrol_extra_args'] = d.getVar("TEST_POWERCONTROL_EXTRA_ARGS") or "" |
| 337 | target_kwargs['serialcontrol_cmd'] = d.getVar("TEST_SERIALCONTROL_CMD") or None |
| 338 | target_kwargs['serialcontrol_extra_args'] = d.getVar("TEST_SERIALCONTROL_EXTRA_ARGS") or "" |
| 339 | target_kwargs['testimage_dump_monitor'] = d.getVar("testimage_dump_monitor") or "" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 340 | |
| 341 | def export_ssh_agent(d): |
| 342 | import os |
| 343 | |
| 344 | variables = ['SSH_AGENT_PID', 'SSH_AUTH_SOCK'] |
| 345 | for v in variables: |
| 346 | if v not in os.environ.keys(): |
| 347 | val = d.getVar(v) |
| 348 | if val is not None: |
| 349 | os.environ[v] = val |
| 350 | |
| 351 | export_ssh_agent(d) |
| 352 | |
| 353 | # runtime use network for download projects for build |
| 354 | export_proxies(d) |
| 355 | |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 356 | if slirp: |
| 357 | # Default to 127.0.0.1 and let the runner identify the port forwarding |
| 358 | # (as OEQemuTarget does), but allow overriding. |
| 359 | target_ip = d.getVar("TEST_TARGET_IP") or "127.0.0.1" |
| 360 | # Default to 10.0.2.2 as this is the IP that the guest has with the |
| 361 | # default qemu slirp networking configuration, but allow overriding. |
| 362 | server_ip = d.getVar("TEST_SERVER_IP") or "10.0.2.2" |
| 363 | else: |
| 364 | target_ip = d.getVar("TEST_TARGET_IP") |
| 365 | server_ip = d.getVar("TEST_SERVER_IP") |
| 366 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 367 | # the robot dance |
| 368 | target = OERuntimeTestContextExecutor.getTarget( |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 369 | d.getVar("TEST_TARGET"), logger, target_ip, |
| 370 | server_ip, **target_kwargs) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 371 | |
| 372 | # test context |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 373 | tc = OERuntimeTestContext(td, logger, target, image_packages, extract_dir) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 374 | |
| 375 | # Load tests before starting the target |
| 376 | test_paths = get_runtime_paths(d) |
| 377 | test_modules = d.getVar('TEST_SUITES').split() |
| 378 | if not test_modules: |
| 379 | bb.fatal('Empty test suite, please verify TEST_SUITES variable') |
| 380 | |
| 381 | tc.loadTests(test_paths, modules=test_modules) |
| 382 | |
| 383 | suitecases = getSuiteCases(tc.suites) |
| 384 | if not suitecases: |
| 385 | bb.fatal('Empty test suite, please verify TEST_SUITES variable') |
| 386 | else: |
| 387 | bb.debug(2, 'test suites:\n\t%s' % '\n\t'.join([str(c) for c in suitecases])) |
| 388 | |
| 389 | package_extraction(d, tc.suites) |
| 390 | |
| 391 | results = None |
| 392 | complete = False |
| 393 | orig_sigterm_handler = signal.signal(signal.SIGTERM, sigterm_exception) |
| 394 | try: |
| 395 | # We need to check if runqemu ends unexpectedly |
| 396 | # or if the worker send us a SIGTERM |
| 397 | tc.target.start(params=d.getVar("TEST_QEMUPARAMS"), runqemuparams=d.getVar("TEST_RUNQEMUPARAMS")) |
| 398 | import threading |
| 399 | try: |
| 400 | threading.Timer(int(d.getVar("TEST_OVERALL_TIMEOUT")), handle_test_timeout, (int(d.getVar("TEST_OVERALL_TIMEOUT")),)).start() |
| 401 | except ValueError: |
| 402 | pass |
| 403 | results = tc.runTests() |
| 404 | complete = True |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 405 | if results.hasAnyFailingTest(): |
| 406 | artifacts_list = get_artifacts_list(tc.target, d.getVar("TESTIMAGE_FAILED_QA_ARTIFACTS")) |
| 407 | if not artifacts_list: |
| 408 | bb.warn("Could not load artifacts list, skip artifacts retrieval") |
| 409 | else: |
| 410 | retrieve_test_artifacts(tc.target, artifacts_list, get_testimage_json_result_dir(d)) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 411 | except (KeyboardInterrupt, BlockingIOError) as err: |
| 412 | if isinstance(err, KeyboardInterrupt): |
| 413 | bb.error('testimage interrupted, shutting down...') |
| 414 | else: |
| 415 | bb.error('runqemu failed, shutting down...') |
| 416 | if results: |
| 417 | results.stop() |
| 418 | results = tc.results |
| 419 | finally: |
| 420 | signal.signal(signal.SIGTERM, orig_sigterm_handler) |
| 421 | tc.target.stop() |
| 422 | |
| 423 | # Show results (if we have them) |
| 424 | if results: |
| 425 | configuration = get_testimage_configuration(d, 'runtime', machine) |
| 426 | results.logDetails(get_testimage_json_result_dir(d), |
| 427 | configuration, |
| 428 | get_testimage_result_id(configuration), |
| 429 | dump_streams=d.getVar('TESTREPORT_FULLLOGS')) |
| 430 | results.logSummary(pn) |
| 431 | |
| 432 | # Copy additional logs to tmp/log/oeqa so it's easier to find them |
| 433 | targetdir = os.path.join(get_testimage_json_result_dir(d), d.getVar("PN")) |
| 434 | os.makedirs(targetdir, exist_ok=True) |
| 435 | os.symlink(bootlog, os.path.join(targetdir, os.path.basename(bootlog))) |
| 436 | os.symlink(d.getVar("BB_LOGFILE"), os.path.join(targetdir, os.path.basename(d.getVar("BB_LOGFILE") + "." + d.getVar('DATETIME')))) |
| 437 | |
| 438 | if not results or not complete: |
| 439 | bb.fatal('%s - FAILED - tests were interrupted during execution, check the logs in %s' % (pn, d.getVar("LOG_DIR")), forcelog=True) |
| 440 | if not results.wasSuccessful(): |
| 441 | bb.fatal('%s - FAILED - also check the logs in %s' % (pn, d.getVar("LOG_DIR")), forcelog=True) |
| 442 | |
| 443 | def get_runtime_paths(d): |
| 444 | """ |
| 445 | Returns a list of paths where runtime test must reside. |
| 446 | |
| 447 | Runtime tests are expected in <LAYER_DIR>/lib/oeqa/runtime/cases/ |
| 448 | """ |
| 449 | paths = [] |
| 450 | |
| 451 | for layer in d.getVar('BBLAYERS').split(): |
| 452 | path = os.path.join(layer, 'lib/oeqa/runtime/cases') |
| 453 | if os.path.isdir(path): |
| 454 | paths.append(path) |
| 455 | return paths |
| 456 | |
| 457 | def create_index(arg): |
| 458 | import subprocess |
| 459 | |
| 460 | index_cmd = arg |
| 461 | try: |
| 462 | bb.note("Executing '%s' ..." % index_cmd) |
| 463 | result = subprocess.check_output(index_cmd, |
| 464 | stderr=subprocess.STDOUT, |
| 465 | shell=True) |
| 466 | result = result.decode('utf-8') |
| 467 | except subprocess.CalledProcessError as e: |
| 468 | return("Index creation command '%s' failed with return code " |
| 469 | '%d:\n%s' % (e.cmd, e.returncode, e.output.decode("utf-8"))) |
| 470 | if result: |
| 471 | bb.note(result) |
| 472 | return None |
| 473 | |
| 474 | def create_rpm_index(d): |
| 475 | import glob |
| 476 | # Index RPMs |
| 477 | rpm_createrepo = bb.utils.which(os.getenv('PATH'), "createrepo_c") |
| 478 | index_cmds = [] |
| 479 | archs = (d.getVar('ALL_MULTILIB_PACKAGE_ARCHS') or '').replace('-', '_') |
| 480 | |
| 481 | for arch in archs.split(): |
| 482 | rpm_dir = os.path.join(d.getVar('DEPLOY_DIR_RPM'), arch) |
| 483 | idx_path = os.path.join(d.getVar('WORKDIR'), 'oe-testimage-repo', arch) |
| 484 | |
| 485 | if not os.path.isdir(rpm_dir): |
| 486 | continue |
| 487 | |
| 488 | lockfilename = os.path.join(d.getVar('DEPLOY_DIR_RPM'), 'rpm.lock') |
| 489 | lf = bb.utils.lockfile(lockfilename, False) |
| 490 | oe.path.copyhardlinktree(rpm_dir, idx_path) |
| 491 | # Full indexes overload a 256MB image so reduce the number of rpms |
| 492 | # in the feed by filtering to specific packages needed by the tests. |
| 493 | package_list = glob.glob(idx_path + "*/*.rpm") |
| 494 | |
| 495 | for pkg in package_list: |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 496 | if not os.path.basename(pkg).startswith(("dnf-test-", "busybox", "update-alternatives", "libc6", "musl")): |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 497 | bb.utils.remove(pkg) |
| 498 | |
| 499 | bb.utils.unlockfile(lf) |
| 500 | cmd = '%s --update -q %s' % (rpm_createrepo, idx_path) |
| 501 | |
| 502 | # Create repodata |
| 503 | result = create_index(cmd) |
| 504 | if result: |
| 505 | bb.fatal('%s' % ('\n'.join(result))) |
| 506 | |
| 507 | def package_extraction(d, test_suites): |
| 508 | from oeqa.utils.package_manager import find_packages_to_extract |
| 509 | from oeqa.utils.package_manager import extract_packages |
| 510 | |
| 511 | bb.utils.remove(d.getVar("TEST_NEEDED_PACKAGES_DIR"), recurse=True) |
| 512 | packages = find_packages_to_extract(test_suites) |
| 513 | if packages: |
| 514 | bb.utils.mkdirhier(d.getVar("TEST_INSTALL_TMP_DIR")) |
| 515 | bb.utils.mkdirhier(d.getVar("TEST_PACKAGED_DIR")) |
| 516 | bb.utils.mkdirhier(d.getVar("TEST_EXTRACTED_DIR")) |
| 517 | extract_packages(d, packages) |
| 518 | |
| 519 | testimage_main[vardepsexclude] += "BB_ORIGENV DATETIME" |
| 520 | |
| 521 | python () { |
| 522 | if oe.types.boolean(d.getVar("TESTIMAGE_AUTO") or "False"): |
| 523 | bb.build.addtask("testimage", "do_build", "do_image_complete", d) |
| 524 | } |
| 525 | |
| 526 | inherit testsdk |