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