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 | |
| 5 | |
| 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: |
| 10 | # - first build a qemu core-image-sato |
| 11 | # - add INHERIT += "testimage" in local.conf |
| 12 | # - then bitbake core-image-sato -c testimage. That will run a standard suite of tests. |
| 13 | |
| 14 | # You can set (or append to) TEST_SUITES in local.conf to select the tests |
| 15 | # which you want to run for your target. |
| 16 | # The test names are the module names in meta/lib/oeqa/runtime. |
| 17 | # Each name in TEST_SUITES represents a required test for the image. (no skipping allowed) |
| 18 | # 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). |
| 19 | # Note that order in TEST_SUITES is relevant: tests are run in an order such that |
| 20 | # tests mentioned in @skipUnlessPassed run before the tests that depend on them, |
| 21 | # but without such dependencies, tests run in the order in which they are listed |
| 22 | # in TEST_SUITES. |
| 23 | # |
| 24 | # A layer can add its own tests in lib/oeqa/runtime, provided it extends BBPATH as normal in its layer.conf. |
| 25 | |
| 26 | # 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. |
| 27 | # Booting is handled by this class, and it's not a test in itself. |
| 28 | # TEST_QEMUBOOT_TIMEOUT can be used to set the maximum time in seconds the launch code will wait for the login prompt. |
| 29 | |
| 30 | TEST_LOG_DIR ?= "${WORKDIR}/testimage" |
| 31 | |
| 32 | TEST_EXPORT_DIR ?= "${TMPDIR}/testimage/${PN}" |
| 33 | TEST_EXPORT_ONLY ?= "0" |
| 34 | |
| 35 | RPMTESTSUITE = "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'smart rpm', '', d)}" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 36 | MINTESTSUITE = "ping" |
| 37 | NETTESTSUITE = "${MINTESTSUITE} ssh df date scp syslog" |
| 38 | DEVTESTSUITE = "gcc kernelmodule ldd" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 40 | DEFAULT_TEST_SUITES = "${MINTESTSUITE} auto" |
| 41 | DEFAULT_TEST_SUITES_pn-core-image-minimal = "${MINTESTSUITE}" |
| 42 | DEFAULT_TEST_SUITES_pn-core-image-minimal-dev = "${MINTESTSUITE}" |
| 43 | DEFAULT_TEST_SUITES_pn-core-image-full-cmdline = "${NETTESTSUITE} perl python logrotate" |
| 44 | DEFAULT_TEST_SUITES_pn-core-image-x11 = "${MINTESTSUITE}" |
| 45 | DEFAULT_TEST_SUITES_pn-core-image-lsb = "${NETTESTSUITE} pam parselogs ${RPMTESTSUITE}" |
| 46 | DEFAULT_TEST_SUITES_pn-core-image-sato = "${NETTESTSUITE} connman xorg parselogs ${RPMTESTSUITE} \ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | ${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'python', '', d)}" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 48 | DEFAULT_TEST_SUITES_pn-core-image-sato-sdk = "${NETTESTSUITE} connman xorg perl python \ |
| 49 | ${DEVTESTSUITE} parselogs ${RPMTESTSUITE}" |
| 50 | DEFAULT_TEST_SUITES_pn-core-image-lsb-dev = "${NETTESTSUITE} pam perl python parselogs ${RPMTESTSUITE}" |
| 51 | DEFAULT_TEST_SUITES_pn-core-image-lsb-sdk = "${NETTESTSUITE} buildcvs buildiptables buildsudoku \ |
| 52 | connman ${DEVTESTSUITE} pam perl python parselogs ${RPMTESTSUITE}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | DEFAULT_TEST_SUITES_pn-meta-toolchain = "auto" |
| 54 | |
| 55 | # aarch64 has no graphics |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 56 | DEFAULT_TEST_SUITES_remove_aarch64 = "xorg" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | |
| 58 | #qemumips is too slow for buildsudoku |
| 59 | DEFAULT_TEST_SUITES_remove_qemumips = "buildsudoku" |
| 60 | |
| 61 | TEST_SUITES ?= "${DEFAULT_TEST_SUITES}" |
| 62 | |
| 63 | TEST_QEMUBOOT_TIMEOUT ?= "1000" |
| 64 | TEST_TARGET ?= "qemu" |
| 65 | TEST_TARGET_IP ?= "" |
| 66 | TEST_SERVER_IP ?= "" |
| 67 | |
| 68 | TESTIMAGEDEPENDS = "" |
| 69 | TESTIMAGEDEPENDS_qemuall = "qemu-native:do_populate_sysroot qemu-helper-native:do_populate_sysroot" |
| 70 | |
| 71 | TESTIMAGELOCK = "${TMPDIR}/testimage.lock" |
| 72 | TESTIMAGELOCK_qemuall = "" |
| 73 | |
| 74 | TESTIMAGE_DUMP_DIR ?= "/tmp/oe-saved-tests/" |
| 75 | |
| 76 | testimage_dump_target () { |
| 77 | top -bn1 |
| 78 | ps |
| 79 | free |
| 80 | df |
| 81 | # The next command will export the default gateway IP |
| 82 | export DEFAULT_GATEWAY=$(ip route | awk '/default/ { print $3}') |
| 83 | ping -c3 $DEFAULT_GATEWAY |
| 84 | dmesg |
| 85 | netstat -an |
| 86 | ip address |
| 87 | # Next command will dump logs from /var/log/ |
| 88 | find /var/log/ -type f 2>/dev/null -exec echo "====================" \; -exec echo {} \; -exec echo "====================" \; -exec cat {} \; -exec echo "" \; |
| 89 | } |
| 90 | |
| 91 | testimage_dump_host () { |
| 92 | top -bn1 |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 93 | iostat -x -z -N -d -p ALL 20 2 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | ps -ef |
| 95 | free |
| 96 | df |
| 97 | memstat |
| 98 | dmesg |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 99 | ip -s link |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | netstat -an |
| 101 | } |
| 102 | |
| 103 | python do_testimage() { |
| 104 | testimage_main(d) |
| 105 | } |
| 106 | addtask testimage |
| 107 | do_testimage[nostamp] = "1" |
| 108 | do_testimage[depends] += "${TESTIMAGEDEPENDS}" |
| 109 | do_testimage[lockfiles] += "${TESTIMAGELOCK}" |
| 110 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | def exportTests(d,tc): |
| 112 | import json |
| 113 | import shutil |
| 114 | import pkgutil |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 115 | import re |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | |
| 117 | exportpath = d.getVar("TEST_EXPORT_DIR", True) |
| 118 | |
| 119 | savedata = {} |
| 120 | savedata["d"] = {} |
| 121 | savedata["target"] = {} |
| 122 | savedata["host_dumper"] = {} |
| 123 | for key in tc.__dict__: |
| 124 | # special cases |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 125 | if key not in ['d', 'target', 'host_dumper', 'suite']: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 126 | savedata[key] = getattr(tc, key) |
| 127 | savedata["target"]["ip"] = tc.target.ip or d.getVar("TEST_TARGET_IP", True) |
| 128 | savedata["target"]["server_ip"] = tc.target.server_ip or d.getVar("TEST_SERVER_IP", True) |
| 129 | |
| 130 | keys = [ key for key in d.keys() if not key.startswith("_") and not key.startswith("BB") \ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 131 | and not key.startswith("B_pn") and not key.startswith("do_") and not d.getVarFlag(key, "func", True)] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 132 | for key in keys: |
| 133 | try: |
| 134 | savedata["d"][key] = d.getVar(key, True) |
| 135 | except bb.data_smart.ExpansionError: |
| 136 | # we don't care about those anyway |
| 137 | pass |
| 138 | |
| 139 | savedata["host_dumper"]["parent_dir"] = tc.host_dumper.parent_dir |
| 140 | savedata["host_dumper"]["cmds"] = tc.host_dumper.cmds |
| 141 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 142 | json_file = os.path.join(exportpath, "testdata.json") |
| 143 | with open(json_file, "w") as f: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 144 | json.dump(savedata, f, skipkeys=True, indent=4, sort_keys=True) |
| 145 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 146 | # Replace absolute path with relative in the file |
| 147 | exclude_path = os.path.join(d.getVar("COREBASE", True),'meta','lib','oeqa') |
| 148 | f1 = open(json_file,'r').read() |
| 149 | f2 = open(json_file,'w') |
| 150 | m = f1.replace(exclude_path,'oeqa') |
| 151 | f2.write(m) |
| 152 | f2.close() |
| 153 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 154 | # now start copying files |
| 155 | # we'll basically copy everything under meta/lib/oeqa, with these exceptions |
| 156 | # - oeqa/targetcontrol.py - not needed |
| 157 | # - oeqa/selftest - something else |
| 158 | # That means: |
| 159 | # - all tests from oeqa/runtime defined in TEST_SUITES (including from other layers) |
| 160 | # - the contents of oeqa/utils and oeqa/runtime/files |
| 161 | # - oeqa/oetest.py and oeqa/runexport.py (this will get copied to exportpath not exportpath/oeqa) |
| 162 | # - __init__.py files |
| 163 | bb.utils.mkdirhier(os.path.join(exportpath, "oeqa/runtime/files")) |
| 164 | bb.utils.mkdirhier(os.path.join(exportpath, "oeqa/utils")) |
| 165 | # copy test modules, this should cover tests in other layers too |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 166 | bbpath = d.getVar("BBPATH", True).split(':') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 167 | for t in tc.testslist: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 168 | isfolder = False |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 169 | if re.search("\w+\.\w+\.test_\S+", t): |
| 170 | t = '.'.join(t.split('.')[:3]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 171 | mod = pkgutil.get_loader(t) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 172 | # More depth than usual? |
| 173 | if (t.count('.') > 2): |
| 174 | for p in bbpath: |
| 175 | foldername = os.path.join(p, 'lib', os.sep.join(t.split('.')).rsplit(os.sep, 1)[0]) |
| 176 | if os.path.isdir(foldername): |
| 177 | isfolder = True |
| 178 | target_folder = os.path.join(exportpath, "oeqa", "runtime", os.path.basename(foldername)) |
| 179 | if not os.path.exists(target_folder): |
| 180 | shutil.copytree(foldername, target_folder) |
| 181 | if not isfolder: |
| 182 | shutil.copy2(mod.filename, os.path.join(exportpath, "oeqa/runtime")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 183 | # copy __init__.py files |
| 184 | oeqadir = pkgutil.get_loader("oeqa").filename |
| 185 | shutil.copy2(os.path.join(oeqadir, "__init__.py"), os.path.join(exportpath, "oeqa")) |
| 186 | shutil.copy2(os.path.join(oeqadir, "runtime/__init__.py"), os.path.join(exportpath, "oeqa/runtime")) |
| 187 | # copy oeqa/oetest.py and oeqa/runexported.py |
| 188 | shutil.copy2(os.path.join(oeqadir, "oetest.py"), os.path.join(exportpath, "oeqa")) |
| 189 | shutil.copy2(os.path.join(oeqadir, "runexported.py"), exportpath) |
| 190 | # copy oeqa/utils/*.py |
| 191 | for root, dirs, files in os.walk(os.path.join(oeqadir, "utils")): |
| 192 | for f in files: |
| 193 | if f.endswith(".py"): |
| 194 | shutil.copy2(os.path.join(root, f), os.path.join(exportpath, "oeqa/utils")) |
| 195 | # copy oeqa/runtime/files/* |
| 196 | for root, dirs, files in os.walk(os.path.join(oeqadir, "runtime/files")): |
| 197 | for f in files: |
| 198 | shutil.copy2(os.path.join(root, f), os.path.join(exportpath, "oeqa/runtime/files")) |
| 199 | |
| 200 | bb.plain("Exported tests to: %s" % exportpath) |
| 201 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 202 | def testimage_main(d): |
| 203 | import unittest |
| 204 | import os |
| 205 | import oeqa.runtime |
| 206 | import time |
| 207 | import signal |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 208 | from oeqa.oetest import ImageTestContext |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 209 | from oeqa.targetcontrol import get_target_controller |
| 210 | from oeqa.utils.dump import get_host_dumper |
| 211 | |
| 212 | pn = d.getVar("PN", True) |
| 213 | export = oe.utils.conditional("TEST_EXPORT_ONLY", "1", True, False, d) |
| 214 | bb.utils.mkdirhier(d.getVar("TEST_LOG_DIR", True)) |
| 215 | if export: |
| 216 | bb.utils.remove(d.getVar("TEST_EXPORT_DIR", True), recurse=True) |
| 217 | bb.utils.mkdirhier(d.getVar("TEST_EXPORT_DIR", True)) |
| 218 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 219 | # we need the host dumper in test context |
| 220 | host_dumper = get_host_dumper(d) |
| 221 | |
| 222 | # the robot dance |
| 223 | target = get_target_controller(d) |
| 224 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 225 | # test context |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 226 | tc = ImageTestContext(d, target, host_dumper) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 227 | |
| 228 | # this is a dummy load of tests |
| 229 | # we are doing that to find compile errors in the tests themselves |
| 230 | # before booting the image |
| 231 | try: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 232 | tc.loadTests() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 233 | except Exception as e: |
| 234 | import traceback |
| 235 | bb.fatal("Loading tests failed:\n%s" % traceback.format_exc()) |
| 236 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 237 | if export: |
| 238 | signal.signal(signal.SIGTERM, tc.origsigtermhandler) |
| 239 | tc.origsigtermhandler = None |
| 240 | exportTests(d,tc) |
| 241 | else: |
| 242 | target.deploy() |
| 243 | try: |
| 244 | target.start() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 245 | starttime = time.time() |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 246 | result = tc.runTests() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 247 | stoptime = time.time() |
| 248 | if result.wasSuccessful(): |
| 249 | bb.plain("%s - Ran %d test%s in %.3fs" % (pn, result.testsRun, result.testsRun != 1 and "s" or "", stoptime - starttime)) |
| 250 | msg = "%s - OK - All required tests passed" % pn |
| 251 | skipped = len(result.skipped) |
| 252 | if skipped: |
| 253 | msg += " (skipped=%d)" % skipped |
| 254 | bb.plain(msg) |
| 255 | else: |
| 256 | raise bb.build.FuncFailed("%s - FAILED - check the task log and the ssh log" % pn ) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 257 | finally: |
| 258 | signal.signal(signal.SIGTERM, tc.origsigtermhandler) |
| 259 | target.stop() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 260 | |
| 261 | testimage_main[vardepsexclude] =+ "BB_ORIGENV" |
| 262 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 263 | inherit testsdk |