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 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 67 | # musl doesn't support systemtap |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 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 | 213cb26 | 2021-08-07 19:21:33 -0500 | [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 = "" |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [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" |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 95 | TESTIMAGELOCK:qemuall = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 128 | testimage_dump_monitor () { |
| 129 | query-status |
| 130 | query-block |
Andrew Geissler | 5f35090 | 2021-07-23 13:09:54 -0400 | [diff] [blame] | 131 | dump-guest-memory {"paging":false,"protocol":"file:%s.img"} |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 134 | python do_testimage() { |
| 135 | testimage_main(d) |
| 136 | } |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 137 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 138 | addtask testimage |
| 139 | do_testimage[nostamp] = "1" |
| 140 | do_testimage[depends] += "${TESTIMAGEDEPENDS}" |
| 141 | do_testimage[lockfiles] += "${TESTIMAGELOCK}" |
| 142 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 143 | def testimage_sanity(d): |
| 144 | if (d.getVar('TEST_TARGET') == 'simpleremote' |
| 145 | and (not d.getVar('TEST_TARGET_IP') |
| 146 | or not d.getVar('TEST_SERVER_IP'))): |
| 147 | bb.fatal('When TEST_TARGET is set to "simpleremote" ' |
| 148 | 'TEST_TARGET_IP and TEST_SERVER_IP are needed too.') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 149 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 150 | def get_testimage_configuration(d, test_type, machine): |
| 151 | import platform |
| 152 | from oeqa.utils.metadata import get_layers |
| 153 | configuration = {'TEST_TYPE': test_type, |
| 154 | 'MACHINE': machine, |
| 155 | 'DISTRO': d.getVar("DISTRO"), |
| 156 | 'IMAGE_BASENAME': d.getVar("IMAGE_BASENAME"), |
| 157 | 'IMAGE_PKGTYPE': d.getVar("IMAGE_PKGTYPE"), |
| 158 | 'STARTTIME': d.getVar("DATETIME"), |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 159 | 'HOST_DISTRO': oe.lsb.distro_identifier().replace(' ', '-'), |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 160 | 'LAYERS': get_layers(d.getVar("BBLAYERS"))} |
| 161 | return configuration |
| 162 | get_testimage_configuration[vardepsexclude] = "DATETIME" |
| 163 | |
| 164 | def get_testimage_json_result_dir(d): |
| 165 | json_result_dir = os.path.join(d.getVar("LOG_DIR"), 'oeqa') |
| 166 | custom_json_result_dir = d.getVar("OEQA_JSON_RESULT_DIR") |
| 167 | if custom_json_result_dir: |
| 168 | json_result_dir = custom_json_result_dir |
| 169 | return json_result_dir |
| 170 | |
| 171 | def get_testimage_result_id(configuration): |
| 172 | return '%s_%s_%s_%s' % (configuration['TEST_TYPE'], configuration['IMAGE_BASENAME'], configuration['MACHINE'], configuration['STARTTIME']) |
| 173 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 174 | def get_testimage_boot_patterns(d): |
| 175 | from collections import defaultdict |
| 176 | boot_patterns = defaultdict(str) |
| 177 | # Only accept certain values |
| 178 | accepted_patterns = ['search_reached_prompt', 'send_login_user', 'search_login_succeeded', 'search_cmd_finished'] |
| 179 | # Not all patterns need to be overriden, e.g. perhaps we only want to change the user |
| 180 | boot_patterns_flags = d.getVarFlags('TESTIMAGE_BOOT_PATTERNS') or {} |
| 181 | if boot_patterns_flags: |
| 182 | patterns_set = [p for p in boot_patterns_flags.items() if p[0] in d.getVar('TESTIMAGE_BOOT_PATTERNS').split()] |
| 183 | for flag, flagval in patterns_set: |
| 184 | if flag not in accepted_patterns: |
| 185 | bb.fatal('Testimage: The only accepted boot patterns are: search_reached_prompt,send_login_user, \ |
| 186 | search_login_succeeded,search_cmd_finished\n Make sure your TESTIMAGE_BOOT_PATTERNS=%s \ |
| 187 | contains an accepted flag.' % d.getVar('TESTIMAGE_BOOT_PATTERNS')) |
| 188 | return |
| 189 | # We know boot prompt is searched through in binary format, others might be expressions |
| 190 | if flag == 'search_reached_prompt': |
| 191 | boot_patterns[flag] = flagval.encode() |
| 192 | else: |
| 193 | boot_patterns[flag] = flagval.encode().decode('unicode-escape') |
| 194 | return boot_patterns |
| 195 | |
| 196 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 197 | def testimage_main(d): |
| 198 | import os |
| 199 | import json |
| 200 | import signal |
| 201 | import logging |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 202 | import shutil |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 203 | |
| 204 | from bb.utils import export_proxies |
| 205 | from oeqa.core.utils.misc import updateTestData |
| 206 | from oeqa.runtime.context import OERuntimeTestContext |
| 207 | from oeqa.runtime.context import OERuntimeTestContextExecutor |
| 208 | from oeqa.core.target.qemu import supported_fstypes |
| 209 | from oeqa.core.utils.test import getSuiteCases |
| 210 | from oeqa.utils import make_logger_bitbake_compatible |
| 211 | |
| 212 | def sigterm_exception(signum, stackframe): |
| 213 | """ |
| 214 | Catch SIGTERM from worker in order to stop qemu. |
| 215 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 216 | os.kill(os.getpid(), signal.SIGINT) |
| 217 | |
| 218 | def handle_test_timeout(timeout): |
| 219 | bb.warn("Global test timeout reached (%s seconds), stopping the tests." %(timeout)) |
| 220 | os.kill(os.getpid(), signal.SIGINT) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 221 | |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 222 | testimage_sanity(d) |
| 223 | |
| 224 | if (d.getVar('IMAGE_PKGTYPE') == 'rpm' |
| 225 | and ('dnf' in d.getVar('TEST_SUITES') or 'auto' in d.getVar('TEST_SUITES'))): |
| 226 | create_rpm_index(d) |
| 227 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 228 | logger = make_logger_bitbake_compatible(logging.getLogger("BitBake")) |
| 229 | pn = d.getVar("PN") |
| 230 | |
| 231 | bb.utils.mkdirhier(d.getVar("TEST_LOG_DIR")) |
| 232 | |
| 233 | image_name = ("%s/%s" % (d.getVar('DEPLOY_DIR_IMAGE'), |
| 234 | d.getVar('IMAGE_LINK_NAME'))) |
| 235 | |
| 236 | tdname = "%s.testdata.json" % image_name |
| 237 | try: |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 238 | with open(tdname, "r") as f: |
| 239 | td = json.load(f) |
| 240 | except FileNotFoundError as err: |
| 241 | bb.fatal('File %s not found (%s).\nHave you built the image with INHERIT += "testimage" in the conf/local.conf?' % (tdname, err)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 242 | |
| 243 | # Some variables need to be updates (mostly paths) with the |
| 244 | # ones of the current environment because some tests require them. |
| 245 | updateTestData(d, td, d.getVar('TESTIMAGE_UPDATE_VARS').split()) |
| 246 | |
| 247 | image_manifest = "%s.manifest" % image_name |
| 248 | image_packages = OERuntimeTestContextExecutor.readPackagesManifest(image_manifest) |
| 249 | |
| 250 | extract_dir = d.getVar("TEST_EXTRACTED_DIR") |
| 251 | |
| 252 | # Get machine |
| 253 | machine = d.getVar("MACHINE") |
| 254 | |
| 255 | # Get rootfs |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 256 | fstypes = d.getVar('IMAGE_FSTYPES').split() |
| 257 | if d.getVar("TEST_TARGET") == "qemu": |
| 258 | fstypes = [fs for fs in fstypes if fs in supported_fstypes] |
| 259 | if not fstypes: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 260 | bb.fatal('Unsupported image type built. Add a compatible image to ' |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 261 | 'IMAGE_FSTYPES. Supported types: %s' % |
| 262 | ', '.join(supported_fstypes)) |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 263 | qfstype = fstypes[0] |
| 264 | qdeffstype = d.getVar("QB_DEFAULT_FSTYPE") |
| 265 | if qdeffstype: |
| 266 | qfstype = qdeffstype |
| 267 | rootfs = '%s.%s' % (image_name, qfstype) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 268 | |
| 269 | # Get tmpdir (not really used, just for compatibility) |
| 270 | tmpdir = d.getVar("TMPDIR") |
| 271 | |
| 272 | # Get deploy_dir_image (not really used, just for compatibility) |
| 273 | dir_image = d.getVar("DEPLOY_DIR_IMAGE") |
| 274 | |
| 275 | # Get bootlog |
| 276 | bootlog = os.path.join(d.getVar("TEST_LOG_DIR"), |
| 277 | 'qemu_boot_log.%s' % d.getVar('DATETIME')) |
| 278 | |
| 279 | # Get display |
| 280 | display = d.getVar("BB_ORIGENV").getVar("DISPLAY") |
| 281 | |
| 282 | # Get kernel |
| 283 | kernel_name = ('%s-%s.bin' % (d.getVar("KERNEL_IMAGETYPE"), machine)) |
| 284 | kernel = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), kernel_name) |
| 285 | |
| 286 | # Get boottime |
| 287 | boottime = int(d.getVar("TEST_QEMUBOOT_TIMEOUT")) |
| 288 | |
| 289 | # Get use_kvm |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 290 | kvm = oe.types.qemu_use_kvm(d.getVar('QEMU_USE_KVM'), d.getVar('TARGET_ARCH')) |
| 291 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 292 | # Get OVMF |
| 293 | ovmf = d.getVar("QEMU_USE_OVMF") |
| 294 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 295 | slirp = False |
| 296 | if d.getVar("QEMU_USE_SLIRP"): |
| 297 | slirp = True |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 298 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 299 | # TODO: We use the current implementation of qemu runner because of |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 300 | # time constrains, qemu runner really needs a refactor too. |
| 301 | target_kwargs = { 'machine' : machine, |
| 302 | 'rootfs' : rootfs, |
| 303 | 'tmpdir' : tmpdir, |
| 304 | 'dir_image' : dir_image, |
| 305 | 'display' : display, |
| 306 | 'kernel' : kernel, |
| 307 | 'boottime' : boottime, |
| 308 | 'bootlog' : bootlog, |
| 309 | 'kvm' : kvm, |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 310 | 'slirp' : slirp, |
| 311 | 'dump_dir' : d.getVar("TESTIMAGE_DUMP_DIR"), |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 312 | 'serial_ports': len(d.getVar("SERIAL_CONSOLES").split()), |
| 313 | 'ovmf' : ovmf, |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 314 | 'tmpfsdir' : d.getVar("RUNQEMU_TMPFS_DIR"), |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 317 | if d.getVar("TESTIMAGE_BOOT_PATTERNS"): |
| 318 | target_kwargs['boot_patterns'] = get_testimage_boot_patterns(d) |
| 319 | |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 320 | # hardware controlled targets might need further access |
| 321 | target_kwargs['powercontrol_cmd'] = d.getVar("TEST_POWERCONTROL_CMD") or None |
| 322 | target_kwargs['powercontrol_extra_args'] = d.getVar("TEST_POWERCONTROL_EXTRA_ARGS") or "" |
| 323 | target_kwargs['serialcontrol_cmd'] = d.getVar("TEST_SERIALCONTROL_CMD") or None |
| 324 | target_kwargs['serialcontrol_extra_args'] = d.getVar("TEST_SERIALCONTROL_EXTRA_ARGS") or "" |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 325 | target_kwargs['testimage_dump_monitor'] = d.getVar("testimage_dump_monitor") or "" |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 326 | target_kwargs['testimage_dump_target'] = d.getVar("testimage_dump_target") or "" |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 327 | |
| 328 | def export_ssh_agent(d): |
| 329 | import os |
| 330 | |
| 331 | variables = ['SSH_AGENT_PID', 'SSH_AUTH_SOCK'] |
| 332 | for v in variables: |
| 333 | if v not in os.environ.keys(): |
| 334 | val = d.getVar(v) |
| 335 | if val is not None: |
| 336 | os.environ[v] = val |
| 337 | |
| 338 | export_ssh_agent(d) |
| 339 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 340 | # runtime use network for download projects for build |
| 341 | export_proxies(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 342 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 343 | # we need the host dumper in test context |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 344 | host_dumper = OERuntimeTestContextExecutor.getHostDumper( |
| 345 | d.getVar("testimage_dump_host"), |
| 346 | d.getVar("TESTIMAGE_DUMP_DIR")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 347 | |
| 348 | # the robot dance |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 349 | target = OERuntimeTestContextExecutor.getTarget( |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 350 | d.getVar("TEST_TARGET"), logger, d.getVar("TEST_TARGET_IP"), |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 351 | d.getVar("TEST_SERVER_IP"), **target_kwargs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 352 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 353 | # test context |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 354 | tc = OERuntimeTestContext(td, logger, target, host_dumper, |
| 355 | image_packages, extract_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 356 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 357 | # Load tests before starting the target |
| 358 | test_paths = get_runtime_paths(d) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 359 | test_modules = d.getVar('TEST_SUITES').split() |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 360 | if not test_modules: |
| 361 | bb.fatal('Empty test suite, please verify TEST_SUITES variable') |
| 362 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 363 | tc.loadTests(test_paths, modules=test_modules) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 364 | |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 365 | suitecases = getSuiteCases(tc.suites) |
| 366 | if not suitecases: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 367 | bb.fatal('Empty test suite, please verify TEST_SUITES variable') |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 368 | else: |
| 369 | 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] | 370 | |
| 371 | package_extraction(d, tc.suites) |
| 372 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 373 | results = None |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 374 | complete = False |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 375 | orig_sigterm_handler = signal.signal(signal.SIGTERM, sigterm_exception) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 376 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 377 | # We need to check if runqemu ends unexpectedly |
| 378 | # or if the worker send us a SIGTERM |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 379 | 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] | 380 | import threading |
| 381 | try: |
| 382 | threading.Timer(int(d.getVar("TEST_OVERALL_TIMEOUT")), handle_test_timeout, (int(d.getVar("TEST_OVERALL_TIMEOUT")),)).start() |
| 383 | except ValueError: |
| 384 | pass |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 385 | results = tc.runTests() |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 386 | complete = True |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 387 | except (KeyboardInterrupt, BlockingIOError) as err: |
| 388 | if isinstance(err, KeyboardInterrupt): |
| 389 | bb.error('testimage interrupted, shutting down...') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 390 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 391 | bb.error('runqemu failed, shutting down...') |
| 392 | if results: |
| 393 | results.stop() |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 394 | results = tc.results |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 395 | finally: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 396 | signal.signal(signal.SIGTERM, orig_sigterm_handler) |
| 397 | tc.target.stop() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 398 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 399 | # Show results (if we have them) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 400 | if results: |
| 401 | configuration = get_testimage_configuration(d, 'runtime', machine) |
| 402 | results.logDetails(get_testimage_json_result_dir(d), |
| 403 | configuration, |
| 404 | get_testimage_result_id(configuration), |
| 405 | dump_streams=d.getVar('TESTREPORT_FULLLOGS')) |
| 406 | results.logSummary(pn) |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 407 | |
| 408 | # Copy additional logs to tmp/log/oeqa so it's easier to find them |
| 409 | targetdir = os.path.join(get_testimage_json_result_dir(d), d.getVar("PN")) |
| 410 | os.makedirs(targetdir, exist_ok=True) |
| 411 | os.symlink(bootlog, os.path.join(targetdir, os.path.basename(bootlog))) |
| 412 | os.symlink(d.getVar("BB_LOGFILE"), os.path.join(targetdir, os.path.basename(d.getVar("BB_LOGFILE") + "." + d.getVar('DATETIME')))) |
| 413 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 414 | if not results or not complete: |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 415 | bb.fatal('%s - FAILED - tests were interrupted during execution, check the logs in %s' % (pn, d.getVar("LOG_DIR")), forcelog=True) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 416 | if not results.wasSuccessful(): |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 417 | bb.fatal('%s - FAILED - also check the logs in %s' % (pn, d.getVar("LOG_DIR")), forcelog=True) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 418 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 419 | def get_runtime_paths(d): |
| 420 | """ |
| 421 | Returns a list of paths where runtime test must reside. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 422 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 423 | Runtime tests are expected in <LAYER_DIR>/lib/oeqa/runtime/cases/ |
| 424 | """ |
| 425 | paths = [] |
| 426 | |
| 427 | for layer in d.getVar('BBLAYERS').split(): |
| 428 | path = os.path.join(layer, 'lib/oeqa/runtime/cases') |
| 429 | if os.path.isdir(path): |
| 430 | paths.append(path) |
| 431 | return paths |
| 432 | |
| 433 | def create_index(arg): |
| 434 | import subprocess |
| 435 | |
| 436 | index_cmd = arg |
| 437 | try: |
| 438 | bb.note("Executing '%s' ..." % index_cmd) |
| 439 | result = subprocess.check_output(index_cmd, |
| 440 | stderr=subprocess.STDOUT, |
| 441 | shell=True) |
| 442 | result = result.decode('utf-8') |
| 443 | except subprocess.CalledProcessError as e: |
| 444 | return("Index creation command '%s' failed with return code " |
| 445 | '%d:\n%s' % (e.cmd, e.returncode, e.output.decode("utf-8"))) |
| 446 | if result: |
| 447 | bb.note(result) |
| 448 | return None |
| 449 | |
| 450 | def create_rpm_index(d): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 451 | import glob |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 452 | # Index RPMs |
| 453 | rpm_createrepo = bb.utils.which(os.getenv('PATH'), "createrepo_c") |
| 454 | index_cmds = [] |
| 455 | archs = (d.getVar('ALL_MULTILIB_PACKAGE_ARCHS') or '').replace('-', '_') |
| 456 | |
| 457 | for arch in archs.split(): |
| 458 | rpm_dir = os.path.join(d.getVar('DEPLOY_DIR_RPM'), arch) |
| 459 | idx_path = os.path.join(d.getVar('WORKDIR'), 'oe-testimage-repo', arch) |
| 460 | |
| 461 | if not os.path.isdir(rpm_dir): |
| 462 | continue |
| 463 | |
| 464 | lockfilename = os.path.join(d.getVar('DEPLOY_DIR_RPM'), 'rpm.lock') |
| 465 | lf = bb.utils.lockfile(lockfilename, False) |
| 466 | oe.path.copyhardlinktree(rpm_dir, idx_path) |
| 467 | # Full indexes overload a 256MB image so reduce the number of rpms |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 468 | # in the feed by filtering to specific packages needed by the tests. |
| 469 | package_list = glob.glob(idx_path + "*/*.rpm") |
| 470 | |
| 471 | for pkg in package_list: |
| 472 | if not os.path.basename(pkg).startswith(("rpm", "run-postinsts", "busybox", "bash", "update-alternatives", "libc6", "curl", "musl")): |
| 473 | bb.utils.remove(pkg) |
| 474 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 475 | bb.utils.unlockfile(lf) |
| 476 | cmd = '%s --update -q %s' % (rpm_createrepo, idx_path) |
| 477 | |
| 478 | # Create repodata |
| 479 | result = create_index(cmd) |
| 480 | if result: |
| 481 | bb.fatal('%s' % ('\n'.join(result))) |
| 482 | |
| 483 | def package_extraction(d, test_suites): |
| 484 | from oeqa.utils.package_manager import find_packages_to_extract |
| 485 | from oeqa.utils.package_manager import extract_packages |
| 486 | |
| 487 | bb.utils.remove(d.getVar("TEST_NEEDED_PACKAGES_DIR"), recurse=True) |
| 488 | packages = find_packages_to_extract(test_suites) |
| 489 | if packages: |
| 490 | bb.utils.mkdirhier(d.getVar("TEST_INSTALL_TMP_DIR")) |
| 491 | bb.utils.mkdirhier(d.getVar("TEST_PACKAGED_DIR")) |
| 492 | bb.utils.mkdirhier(d.getVar("TEST_EXTRACTED_DIR")) |
| 493 | extract_packages(d, packages) |
| 494 | |
| 495 | testimage_main[vardepsexclude] += "BB_ORIGENV DATETIME" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 496 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 497 | python () { |
| 498 | if oe.types.boolean(d.getVar("TESTIMAGE_AUTO") or "False"): |
| 499 | bb.build.addtask("testimage", "do_build", "do_image_complete", d) |
| 500 | } |
| 501 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 502 | inherit testsdk |