Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # Copyright (C) 2013 Intel Corporation |
| 2 | # |
| 3 | # Released under the MIT license (see COPYING.MIT) |
| 4 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 5 | inherit metadata_scm |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 6 | inherit image-artifact-names |
| 7 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 12 | # - first add IMAGE_CLASSES += "testimage" in local.conf |
| 13 | # - build a qemu core-image-sato |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 | # - then bitbake core-image-sato -c testimage. That will run a standard suite of tests. |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 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" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | |
| 21 | # You can set (or append to) TEST_SUITES in local.conf to select the tests |
| 22 | # which you want to run for your target. |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 23 | # The test names are the module names in meta/lib/oeqa/runtime/cases. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | # Each name in TEST_SUITES represents a required test for the image. (no skipping allowed) |
| 25 | # 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). |
| 26 | # Note that order in TEST_SUITES is relevant: tests are run in an order such that |
| 27 | # tests mentioned in @skipUnlessPassed run before the tests that depend on them, |
| 28 | # but without such dependencies, tests run in the order in which they are listed |
| 29 | # in TEST_SUITES. |
| 30 | # |
| 31 | # A layer can add its own tests in lib/oeqa/runtime, provided it extends BBPATH as normal in its layer.conf. |
| 32 | |
| 33 | # 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. |
| 34 | # Booting is handled by this class, and it's not a test in itself. |
| 35 | # TEST_QEMUBOOT_TIMEOUT can be used to set the maximum time in seconds the launch code will wait for the login prompt. |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 36 | # TEST_OVERALL_TIMEOUT can be used to set the maximum time in seconds the tests will be allowed to run (defaults to no limit). |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 37 | # TEST_QEMUPARAMS can be used to pass extra parameters to qemu, e.g. "-m 1024" for setting the amount of ram to 1 GB. |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 38 | # TEST_RUNQEMUPARAMS can be used to pass extra parameters to runqemu, e.g. "gl" to enable OpenGL acceleration. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 40 | # TESTIMAGE_BOOT_PATTERNS can be used to override certain patterns used to communicate with the target when booting, |
| 41 | # if a pattern is not specifically present on this variable a default will be used when booting the target. |
| 42 | # TESTIMAGE_BOOT_PATTERNS[<flag>] overrides the pattern used for that specific flag, where flag comes from a list of accepted flags |
| 43 | # 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 |
| 44 | # if we wanted to log in as the hypothetical "webserver" user for example we could set the following: |
| 45 | # TESTIMAGE_BOOT_PATTERNS = "send_login_user search_login_succeeded" |
| 46 | # TESTIMAGE_BOOT_PATTERNS[send_login_user] = "webserver\n" |
| 47 | # TESTIMAGE_BOOT_PATTERNS[search_login_succeeded] = "webserver@[a-zA-Z0-9\-]+:~#" |
| 48 | # The accepted flags are the following: search_reached_prompt, send_login_user, search_login_succeeded, search_cmd_finished. |
| 49 | # They are prefixed with either search/send, to differentiate if the pattern is meant to be sent or searched to/from the target terminal |
| 50 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 | TEST_LOG_DIR ?= "${WORKDIR}/testimage" |
| 52 | |
| 53 | TEST_EXPORT_DIR ?= "${TMPDIR}/testimage/${PN}" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 54 | TEST_INSTALL_TMP_DIR ?= "${WORKDIR}/testimage/install_tmp" |
| 55 | TEST_NEEDED_PACKAGES_DIR ?= "${WORKDIR}/testimage/packages" |
| 56 | TEST_EXTRACTED_DIR ?= "${TEST_NEEDED_PACKAGES_DIR}/extracted" |
| 57 | TEST_PACKAGED_DIR ?= "${TEST_NEEDED_PACKAGES_DIR}/packaged" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 59 | BASICTESTSUITE = "\ |
| 60 | ping date df ssh scp python perl gi ptest parselogs \ |
| 61 | logrotate connman systemd oe_syslog pam stap ldd xorg \ |
| 62 | kernelmodule gcc buildcpio buildlzip buildgalculator \ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 63 | dnf rpm opkg apt weston" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 65 | DEFAULT_TEST_SUITES = "${BASICTESTSUITE}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | |
| 67 | # aarch64 has no graphics |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 68 | DEFAULT_TEST_SUITES_remove_aarch64 = "xorg" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 69 | # musl doesn't support systemtap |
| 70 | DEFAULT_TEST_SUITES_remove_libc-musl = "stap" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 72 | # qemumips is quite slow and has reached the timeout limit several times on the YP build cluster, |
| 73 | # mitigate this by removing build tests for qemumips machines. |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 74 | MIPSREMOVE ??= "buildcpio buildlzip buildgalculator" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 75 | DEFAULT_TEST_SUITES_remove_qemumips = "${MIPSREMOVE}" |
| 76 | DEFAULT_TEST_SUITES_remove_qemumips64 = "${MIPSREMOVE}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | |
| 78 | TEST_SUITES ?= "${DEFAULT_TEST_SUITES}" |
| 79 | |
| 80 | TEST_QEMUBOOT_TIMEOUT ?= "1000" |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 81 | TEST_OVERALL_TIMEOUT ?= "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | TEST_TARGET ?= "qemu" |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 83 | TEST_QEMUPARAMS ?= "" |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 84 | TEST_RUNQEMUPARAMS ?= "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 86 | TESTIMAGE_BOOT_PATTERNS ?= "" |
| 87 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | TESTIMAGEDEPENDS = "" |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 89 | TESTIMAGEDEPENDS_append_qemuall = " qemu-native:do_populate_sysroot qemu-helper-native:do_populate_sysroot qemu-helper-native:do_addto_recipe_sysroot" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 90 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'cpio-native:do_populate_sysroot', '', d)}" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 91 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'dnf-native:do_populate_sysroot', '', d)}" |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 92 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'createrepo-c-native:do_populate_sysroot', '', d)}" |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 93 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'ipk', 'opkg-utils-native:do_populate_sysroot package-index:do_package_index', '', d)}" |
| 94 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'deb', 'apt-native:do_populate_sysroot package-index:do_package_index', '', d)}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | |
| 96 | TESTIMAGELOCK = "${TMPDIR}/testimage.lock" |
| 97 | TESTIMAGELOCK_qemuall = "" |
| 98 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 99 | TESTIMAGE_DUMP_DIR ?= "${LOG_DIR}/runtime-hostdump/" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 101 | TESTIMAGE_UPDATE_VARS ?= "DL_DIR WORKDIR DEPLOY_DIR" |
| 102 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | testimage_dump_target () { |
| 104 | top -bn1 |
| 105 | ps |
| 106 | free |
| 107 | df |
| 108 | # The next command will export the default gateway IP |
| 109 | export DEFAULT_GATEWAY=$(ip route | awk '/default/ { print $3}') |
| 110 | ping -c3 $DEFAULT_GATEWAY |
| 111 | dmesg |
| 112 | netstat -an |
| 113 | ip address |
| 114 | # Next command will dump logs from /var/log/ |
| 115 | find /var/log/ -type f 2>/dev/null -exec echo "====================" \; -exec echo {} \; -exec echo "====================" \; -exec cat {} \; -exec echo "" \; |
| 116 | } |
| 117 | |
| 118 | testimage_dump_host () { |
| 119 | top -bn1 |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 120 | iostat -x -z -N -d -p ALL 20 2 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 121 | ps -ef |
| 122 | free |
| 123 | df |
| 124 | memstat |
| 125 | dmesg |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 126 | ip -s link |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 127 | netstat -an |
| 128 | } |
| 129 | |
| 130 | python do_testimage() { |
| 131 | testimage_main(d) |
| 132 | } |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 133 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 134 | addtask testimage |
| 135 | do_testimage[nostamp] = "1" |
| 136 | do_testimage[depends] += "${TESTIMAGEDEPENDS}" |
| 137 | do_testimage[lockfiles] += "${TESTIMAGELOCK}" |
| 138 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 139 | def testimage_sanity(d): |
| 140 | if (d.getVar('TEST_TARGET') == 'simpleremote' |
| 141 | and (not d.getVar('TEST_TARGET_IP') |
| 142 | or not d.getVar('TEST_SERVER_IP'))): |
| 143 | bb.fatal('When TEST_TARGET is set to "simpleremote" ' |
| 144 | 'TEST_TARGET_IP and TEST_SERVER_IP are needed too.') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 145 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 146 | def get_testimage_configuration(d, test_type, machine): |
| 147 | import platform |
| 148 | from oeqa.utils.metadata import get_layers |
| 149 | configuration = {'TEST_TYPE': test_type, |
| 150 | 'MACHINE': machine, |
| 151 | 'DISTRO': d.getVar("DISTRO"), |
| 152 | 'IMAGE_BASENAME': d.getVar("IMAGE_BASENAME"), |
| 153 | 'IMAGE_PKGTYPE': d.getVar("IMAGE_PKGTYPE"), |
| 154 | 'STARTTIME': d.getVar("DATETIME"), |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 155 | 'HOST_DISTRO': oe.lsb.distro_identifier().replace(' ', '-'), |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 156 | 'LAYERS': get_layers(d.getVar("BBLAYERS"))} |
| 157 | return configuration |
| 158 | get_testimage_configuration[vardepsexclude] = "DATETIME" |
| 159 | |
| 160 | def get_testimage_json_result_dir(d): |
| 161 | json_result_dir = os.path.join(d.getVar("LOG_DIR"), 'oeqa') |
| 162 | custom_json_result_dir = d.getVar("OEQA_JSON_RESULT_DIR") |
| 163 | if custom_json_result_dir: |
| 164 | json_result_dir = custom_json_result_dir |
| 165 | return json_result_dir |
| 166 | |
| 167 | def get_testimage_result_id(configuration): |
| 168 | return '%s_%s_%s_%s' % (configuration['TEST_TYPE'], configuration['IMAGE_BASENAME'], configuration['MACHINE'], configuration['STARTTIME']) |
| 169 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 170 | def get_testimage_boot_patterns(d): |
| 171 | from collections import defaultdict |
| 172 | boot_patterns = defaultdict(str) |
| 173 | # Only accept certain values |
| 174 | accepted_patterns = ['search_reached_prompt', 'send_login_user', 'search_login_succeeded', 'search_cmd_finished'] |
| 175 | # Not all patterns need to be overriden, e.g. perhaps we only want to change the user |
| 176 | boot_patterns_flags = d.getVarFlags('TESTIMAGE_BOOT_PATTERNS') or {} |
| 177 | if boot_patterns_flags: |
| 178 | patterns_set = [p for p in boot_patterns_flags.items() if p[0] in d.getVar('TESTIMAGE_BOOT_PATTERNS').split()] |
| 179 | for flag, flagval in patterns_set: |
| 180 | if flag not in accepted_patterns: |
| 181 | bb.fatal('Testimage: The only accepted boot patterns are: search_reached_prompt,send_login_user, \ |
| 182 | search_login_succeeded,search_cmd_finished\n Make sure your TESTIMAGE_BOOT_PATTERNS=%s \ |
| 183 | contains an accepted flag.' % d.getVar('TESTIMAGE_BOOT_PATTERNS')) |
| 184 | return |
| 185 | # We know boot prompt is searched through in binary format, others might be expressions |
| 186 | if flag == 'search_reached_prompt': |
| 187 | boot_patterns[flag] = flagval.encode() |
| 188 | else: |
| 189 | boot_patterns[flag] = flagval.encode().decode('unicode-escape') |
| 190 | return boot_patterns |
| 191 | |
| 192 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 193 | def testimage_main(d): |
| 194 | import os |
| 195 | import json |
| 196 | import signal |
| 197 | import logging |
| 198 | |
| 199 | from bb.utils import export_proxies |
| 200 | from oeqa.core.utils.misc import updateTestData |
| 201 | from oeqa.runtime.context import OERuntimeTestContext |
| 202 | from oeqa.runtime.context import OERuntimeTestContextExecutor |
| 203 | from oeqa.core.target.qemu import supported_fstypes |
| 204 | from oeqa.core.utils.test import getSuiteCases |
| 205 | from oeqa.utils import make_logger_bitbake_compatible |
| 206 | |
| 207 | def sigterm_exception(signum, stackframe): |
| 208 | """ |
| 209 | Catch SIGTERM from worker in order to stop qemu. |
| 210 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 211 | os.kill(os.getpid(), signal.SIGINT) |
| 212 | |
| 213 | def handle_test_timeout(timeout): |
| 214 | bb.warn("Global test timeout reached (%s seconds), stopping the tests." %(timeout)) |
| 215 | os.kill(os.getpid(), signal.SIGINT) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 216 | |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 217 | testimage_sanity(d) |
| 218 | |
| 219 | if (d.getVar('IMAGE_PKGTYPE') == 'rpm' |
| 220 | and ('dnf' in d.getVar('TEST_SUITES') or 'auto' in d.getVar('TEST_SUITES'))): |
| 221 | create_rpm_index(d) |
| 222 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 223 | logger = make_logger_bitbake_compatible(logging.getLogger("BitBake")) |
| 224 | pn = d.getVar("PN") |
| 225 | |
| 226 | bb.utils.mkdirhier(d.getVar("TEST_LOG_DIR")) |
| 227 | |
| 228 | image_name = ("%s/%s" % (d.getVar('DEPLOY_DIR_IMAGE'), |
| 229 | d.getVar('IMAGE_LINK_NAME'))) |
| 230 | |
| 231 | tdname = "%s.testdata.json" % image_name |
| 232 | try: |
| 233 | td = json.load(open(tdname, "r")) |
| 234 | except (FileNotFoundError) as err: |
| 235 | bb.fatal('File %s Not Found. Have you built the image with INHERIT+="testimage" in the conf/local.conf?' % tdname) |
| 236 | |
| 237 | # Some variables need to be updates (mostly paths) with the |
| 238 | # ones of the current environment because some tests require them. |
| 239 | updateTestData(d, td, d.getVar('TESTIMAGE_UPDATE_VARS').split()) |
| 240 | |
| 241 | image_manifest = "%s.manifest" % image_name |
| 242 | image_packages = OERuntimeTestContextExecutor.readPackagesManifest(image_manifest) |
| 243 | |
| 244 | extract_dir = d.getVar("TEST_EXTRACTED_DIR") |
| 245 | |
| 246 | # Get machine |
| 247 | machine = d.getVar("MACHINE") |
| 248 | |
| 249 | # Get rootfs |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 250 | fstypes = d.getVar('IMAGE_FSTYPES').split() |
| 251 | if d.getVar("TEST_TARGET") == "qemu": |
| 252 | fstypes = [fs for fs in fstypes if fs in supported_fstypes] |
| 253 | if not fstypes: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 254 | bb.fatal('Unsupported image type built. Add a compatible image to ' |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 255 | 'IMAGE_FSTYPES. Supported types: %s' % |
| 256 | ', '.join(supported_fstypes)) |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 257 | qfstype = fstypes[0] |
| 258 | qdeffstype = d.getVar("QB_DEFAULT_FSTYPE") |
| 259 | if qdeffstype: |
| 260 | qfstype = qdeffstype |
| 261 | rootfs = '%s.%s' % (image_name, qfstype) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 262 | |
| 263 | # Get tmpdir (not really used, just for compatibility) |
| 264 | tmpdir = d.getVar("TMPDIR") |
| 265 | |
| 266 | # Get deploy_dir_image (not really used, just for compatibility) |
| 267 | dir_image = d.getVar("DEPLOY_DIR_IMAGE") |
| 268 | |
| 269 | # Get bootlog |
| 270 | bootlog = os.path.join(d.getVar("TEST_LOG_DIR"), |
| 271 | 'qemu_boot_log.%s' % d.getVar('DATETIME')) |
| 272 | |
| 273 | # Get display |
| 274 | display = d.getVar("BB_ORIGENV").getVar("DISPLAY") |
| 275 | |
| 276 | # Get kernel |
| 277 | kernel_name = ('%s-%s.bin' % (d.getVar("KERNEL_IMAGETYPE"), machine)) |
| 278 | kernel = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), kernel_name) |
| 279 | |
| 280 | # Get boottime |
| 281 | boottime = int(d.getVar("TEST_QEMUBOOT_TIMEOUT")) |
| 282 | |
| 283 | # Get use_kvm |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 284 | kvm = oe.types.qemu_use_kvm(d.getVar('QEMU_USE_KVM'), d.getVar('TARGET_ARCH')) |
| 285 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 286 | # Get OVMF |
| 287 | ovmf = d.getVar("QEMU_USE_OVMF") |
| 288 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 289 | slirp = False |
| 290 | if d.getVar("QEMU_USE_SLIRP"): |
| 291 | slirp = True |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 292 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 293 | # TODO: We use the current implementation of qemu runner because of |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 294 | # time constrains, qemu runner really needs a refactor too. |
| 295 | target_kwargs = { 'machine' : machine, |
| 296 | 'rootfs' : rootfs, |
| 297 | 'tmpdir' : tmpdir, |
| 298 | 'dir_image' : dir_image, |
| 299 | 'display' : display, |
| 300 | 'kernel' : kernel, |
| 301 | 'boottime' : boottime, |
| 302 | 'bootlog' : bootlog, |
| 303 | 'kvm' : kvm, |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 304 | 'slirp' : slirp, |
| 305 | 'dump_dir' : d.getVar("TESTIMAGE_DUMP_DIR"), |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 306 | 'serial_ports': len(d.getVar("SERIAL_CONSOLES").split()), |
| 307 | 'ovmf' : ovmf, |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 310 | if d.getVar("TESTIMAGE_BOOT_PATTERNS"): |
| 311 | target_kwargs['boot_patterns'] = get_testimage_boot_patterns(d) |
| 312 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 313 | # TODO: Currently BBPATH is needed for custom loading of targets. |
| 314 | # It would be better to find these modules using instrospection. |
| 315 | target_kwargs['target_modules_path'] = d.getVar('BBPATH') |
| 316 | |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 317 | # hardware controlled targets might need further access |
| 318 | target_kwargs['powercontrol_cmd'] = d.getVar("TEST_POWERCONTROL_CMD") or None |
| 319 | target_kwargs['powercontrol_extra_args'] = d.getVar("TEST_POWERCONTROL_EXTRA_ARGS") or "" |
| 320 | target_kwargs['serialcontrol_cmd'] = d.getVar("TEST_SERIALCONTROL_CMD") or None |
| 321 | target_kwargs['serialcontrol_extra_args'] = d.getVar("TEST_SERIALCONTROL_EXTRA_ARGS") or "" |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame^] | 322 | target_kwargs['testimage_dump_target'] = d.getVar("testimage_dump_target") or "" |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 323 | |
| 324 | def export_ssh_agent(d): |
| 325 | import os |
| 326 | |
| 327 | variables = ['SSH_AGENT_PID', 'SSH_AUTH_SOCK'] |
| 328 | for v in variables: |
| 329 | if v not in os.environ.keys(): |
| 330 | val = d.getVar(v) |
| 331 | if val is not None: |
| 332 | os.environ[v] = val |
| 333 | |
| 334 | export_ssh_agent(d) |
| 335 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 336 | # runtime use network for download projects for build |
| 337 | export_proxies(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 338 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 339 | # we need the host dumper in test context |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 340 | host_dumper = OERuntimeTestContextExecutor.getHostDumper( |
| 341 | d.getVar("testimage_dump_host"), |
| 342 | d.getVar("TESTIMAGE_DUMP_DIR")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 343 | |
| 344 | # the robot dance |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 345 | target = OERuntimeTestContextExecutor.getTarget( |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 346 | d.getVar("TEST_TARGET"), logger, d.getVar("TEST_TARGET_IP"), |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 347 | d.getVar("TEST_SERVER_IP"), **target_kwargs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 348 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 349 | # test context |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 350 | tc = OERuntimeTestContext(td, logger, target, host_dumper, |
| 351 | image_packages, extract_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 352 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 353 | # Load tests before starting the target |
| 354 | test_paths = get_runtime_paths(d) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 355 | test_modules = d.getVar('TEST_SUITES').split() |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 356 | if not test_modules: |
| 357 | bb.fatal('Empty test suite, please verify TEST_SUITES variable') |
| 358 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 359 | tc.loadTests(test_paths, modules=test_modules) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 360 | |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 361 | suitecases = getSuiteCases(tc.suites) |
| 362 | if not suitecases: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 363 | bb.fatal('Empty test suite, please verify TEST_SUITES variable') |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 364 | else: |
| 365 | bb.debug(2, 'test suites:\n\t%s' % '\n\t'.join([str(c) for c in suitecases])) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 366 | |
| 367 | package_extraction(d, tc.suites) |
| 368 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 369 | results = None |
| 370 | orig_sigterm_handler = signal.signal(signal.SIGTERM, sigterm_exception) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 371 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 372 | # We need to check if runqemu ends unexpectedly |
| 373 | # or if the worker send us a SIGTERM |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 374 | tc.target.start(params=d.getVar("TEST_QEMUPARAMS"), runqemuparams=d.getVar("TEST_RUNQEMUPARAMS")) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 375 | import threading |
| 376 | try: |
| 377 | threading.Timer(int(d.getVar("TEST_OVERALL_TIMEOUT")), handle_test_timeout, (int(d.getVar("TEST_OVERALL_TIMEOUT")),)).start() |
| 378 | except ValueError: |
| 379 | pass |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 380 | results = tc.runTests() |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 381 | except (KeyboardInterrupt, BlockingIOError) as err: |
| 382 | if isinstance(err, KeyboardInterrupt): |
| 383 | bb.error('testimage interrupted, shutting down...') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 384 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 385 | bb.error('runqemu failed, shutting down...') |
| 386 | if results: |
| 387 | results.stop() |
| 388 | results = None |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 389 | finally: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 390 | signal.signal(signal.SIGTERM, orig_sigterm_handler) |
| 391 | tc.target.stop() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 392 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 393 | # Show results (if we have them) |
| 394 | if not results: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 395 | bb.fatal('%s - FAILED - tests were interrupted during execution' % pn, forcelog=True) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 396 | configuration = get_testimage_configuration(d, 'runtime', machine) |
| 397 | results.logDetails(get_testimage_json_result_dir(d), |
| 398 | configuration, |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 399 | get_testimage_result_id(configuration), |
| 400 | dump_streams=d.getVar('TESTREPORT_FULLLOGS')) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 401 | results.logSummary(pn) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 402 | if not results.wasSuccessful(): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 403 | bb.fatal('%s - FAILED - check the task log and the ssh log' % pn, forcelog=True) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 404 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 405 | def get_runtime_paths(d): |
| 406 | """ |
| 407 | Returns a list of paths where runtime test must reside. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 408 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 409 | Runtime tests are expected in <LAYER_DIR>/lib/oeqa/runtime/cases/ |
| 410 | """ |
| 411 | paths = [] |
| 412 | |
| 413 | for layer in d.getVar('BBLAYERS').split(): |
| 414 | path = os.path.join(layer, 'lib/oeqa/runtime/cases') |
| 415 | if os.path.isdir(path): |
| 416 | paths.append(path) |
| 417 | return paths |
| 418 | |
| 419 | def create_index(arg): |
| 420 | import subprocess |
| 421 | |
| 422 | index_cmd = arg |
| 423 | try: |
| 424 | bb.note("Executing '%s' ..." % index_cmd) |
| 425 | result = subprocess.check_output(index_cmd, |
| 426 | stderr=subprocess.STDOUT, |
| 427 | shell=True) |
| 428 | result = result.decode('utf-8') |
| 429 | except subprocess.CalledProcessError as e: |
| 430 | return("Index creation command '%s' failed with return code " |
| 431 | '%d:\n%s' % (e.cmd, e.returncode, e.output.decode("utf-8"))) |
| 432 | if result: |
| 433 | bb.note(result) |
| 434 | return None |
| 435 | |
| 436 | def create_rpm_index(d): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 437 | import glob |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 438 | # Index RPMs |
| 439 | rpm_createrepo = bb.utils.which(os.getenv('PATH'), "createrepo_c") |
| 440 | index_cmds = [] |
| 441 | archs = (d.getVar('ALL_MULTILIB_PACKAGE_ARCHS') or '').replace('-', '_') |
| 442 | |
| 443 | for arch in archs.split(): |
| 444 | rpm_dir = os.path.join(d.getVar('DEPLOY_DIR_RPM'), arch) |
| 445 | idx_path = os.path.join(d.getVar('WORKDIR'), 'oe-testimage-repo', arch) |
| 446 | |
| 447 | if not os.path.isdir(rpm_dir): |
| 448 | continue |
| 449 | |
| 450 | lockfilename = os.path.join(d.getVar('DEPLOY_DIR_RPM'), 'rpm.lock') |
| 451 | lf = bb.utils.lockfile(lockfilename, False) |
| 452 | oe.path.copyhardlinktree(rpm_dir, idx_path) |
| 453 | # Full indexes overload a 256MB image so reduce the number of rpms |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 454 | # in the feed by filtering to specific packages needed by the tests. |
| 455 | package_list = glob.glob(idx_path + "*/*.rpm") |
| 456 | |
| 457 | for pkg in package_list: |
| 458 | if not os.path.basename(pkg).startswith(("rpm", "run-postinsts", "busybox", "bash", "update-alternatives", "libc6", "curl", "musl")): |
| 459 | bb.utils.remove(pkg) |
| 460 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 461 | bb.utils.unlockfile(lf) |
| 462 | cmd = '%s --update -q %s' % (rpm_createrepo, idx_path) |
| 463 | |
| 464 | # Create repodata |
| 465 | result = create_index(cmd) |
| 466 | if result: |
| 467 | bb.fatal('%s' % ('\n'.join(result))) |
| 468 | |
| 469 | def package_extraction(d, test_suites): |
| 470 | from oeqa.utils.package_manager import find_packages_to_extract |
| 471 | from oeqa.utils.package_manager import extract_packages |
| 472 | |
| 473 | bb.utils.remove(d.getVar("TEST_NEEDED_PACKAGES_DIR"), recurse=True) |
| 474 | packages = find_packages_to_extract(test_suites) |
| 475 | if packages: |
| 476 | bb.utils.mkdirhier(d.getVar("TEST_INSTALL_TMP_DIR")) |
| 477 | bb.utils.mkdirhier(d.getVar("TEST_PACKAGED_DIR")) |
| 478 | bb.utils.mkdirhier(d.getVar("TEST_EXTRACTED_DIR")) |
| 479 | extract_packages(d, packages) |
| 480 | |
| 481 | testimage_main[vardepsexclude] += "BB_ORIGENV DATETIME" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 482 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 483 | python () { |
| 484 | if oe.types.boolean(d.getVar("TESTIMAGE_AUTO") or "False"): |
| 485 | bb.build.addtask("testimage", "do_build", "do_image_complete", d) |
| 486 | } |
| 487 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 488 | inherit testsdk |