diff options
Diffstat (limited to 'meta/classes/testimage.bbclass')
| -rw-r--r-- | meta/classes/testimage.bbclass | 232 |
1 files changed, 232 insertions, 0 deletions
diff --git a/meta/classes/testimage.bbclass b/meta/classes/testimage.bbclass new file mode 100644 index 0000000000..691c7f6785 --- /dev/null +++ b/meta/classes/testimage.bbclass | |||
| @@ -0,0 +1,232 @@ | |||
| 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 important (it's the order tests run) and it influences tests dependencies. | ||
| 20 | # A layer can add its own tests in lib/oeqa/runtime, provided it extends BBPATH as normal in its layer.conf. | ||
| 21 | |||
| 22 | # TEST_LOG_DIR contains a ssh log (what command is running, output and return codes) and a qemu boot log till login | ||
| 23 | # Booting is handled by this class, and it's not a test in itself. | ||
| 24 | # TEST_QEMUBOOT_TIMEOUT can be used to set the maximum time in seconds the launch code will wait for the login prompt. | ||
| 25 | |||
| 26 | TEST_LOG_DIR ?= "${WORKDIR}/testimage" | ||
| 27 | |||
| 28 | TEST_EXPORT_DIR ?= "${TMPDIR}/testimage/${PN}" | ||
| 29 | TEST_EXPORT_ONLY ?= "0" | ||
| 30 | |||
| 31 | DEFAULT_TEST_SUITES = "ping auto" | ||
| 32 | DEFAULT_TEST_SUITES_pn-core-image-minimal = "ping" | ||
| 33 | DEFAULT_TEST_SUITES_pn-core-image-sato = "ping ssh df connman syslog xorg scp vnc date rpm smart dmesg python" | ||
| 34 | DEFAULT_TEST_SUITES_pn-core-image-sato-sdk = "ping ssh df connman syslog xorg scp vnc date perl ldd gcc rpm smart kernelmodule dmesg python" | ||
| 35 | TEST_SUITES ?= "${DEFAULT_TEST_SUITES}" | ||
| 36 | |||
| 37 | TEST_QEMUBOOT_TIMEOUT ?= "1000" | ||
| 38 | TEST_TARGET ?= "qemu" | ||
| 39 | TEST_TARGET_IP ?= "" | ||
| 40 | TEST_SERVER_IP ?= "" | ||
| 41 | |||
| 42 | TESTIMAGEDEPENDS = "" | ||
| 43 | TESTIMAGEDEPENDS_qemuall = "qemu-native:do_populate_sysroot qemu-helper-native:do_populate_sysroot" | ||
| 44 | |||
| 45 | TESTIMAGELOCK = "${TMPDIR}/testimage.lock" | ||
| 46 | TESTIMAGELOCK_qemuall = "" | ||
| 47 | |||
| 48 | python do_testimage() { | ||
| 49 | testimage_main(d) | ||
| 50 | } | ||
| 51 | addtask testimage | ||
| 52 | do_testimage[nostamp] = "1" | ||
| 53 | do_testimage[depends] += "${TESTIMAGEDEPENDS}" | ||
| 54 | do_testimage[lockfiles] += "${TESTIMAGELOCK}" | ||
| 55 | |||
| 56 | |||
| 57 | def get_tests_list(d): | ||
| 58 | testsuites = d.getVar("TEST_SUITES", True).split() | ||
| 59 | bbpath = d.getVar("BBPATH", True).split(':') | ||
| 60 | |||
| 61 | # This relies on lib/ under each directory in BBPATH being added to sys.path | ||
| 62 | # (as done by default in base.bbclass) | ||
| 63 | testslist = [] | ||
| 64 | for testname in testsuites: | ||
| 65 | if testname != "auto": | ||
| 66 | found = False | ||
| 67 | for p in bbpath: | ||
| 68 | if os.path.exists(os.path.join(p, 'lib', 'oeqa', 'runtime', testname + '.py')): | ||
| 69 | testslist.append("oeqa.runtime." + testname) | ||
| 70 | found = True | ||
| 71 | break | ||
| 72 | if not found: | ||
| 73 | bb.error('Test %s specified in TEST_SUITES could not be found in lib/oeqa/runtime under BBPATH' % testname) | ||
| 74 | |||
| 75 | if "auto" in testsuites: | ||
| 76 | def add_auto_list(path): | ||
| 77 | if not os.path.exists(os.path.join(path, '__init__.py')): | ||
| 78 | bb.fatal('Tests directory %s exists but is missing __init__.py' % path) | ||
| 79 | files = sorted([f for f in os.listdir(path) if f.endswith('.py') and not f.startswith('_')]) | ||
| 80 | for f in files: | ||
| 81 | module = 'oeqa.runtime.' + f[:-3] | ||
| 82 | if module not in testslist: | ||
| 83 | testslist.append(module) | ||
| 84 | |||
| 85 | for p in bbpath: | ||
| 86 | testpath = os.path.join(p, 'lib', 'oeqa', 'runtime') | ||
| 87 | bb.debug(2, 'Searching for tests in %s' % testpath) | ||
| 88 | if os.path.exists(testpath): | ||
| 89 | add_auto_list(testpath) | ||
| 90 | |||
| 91 | return testslist | ||
| 92 | |||
| 93 | |||
| 94 | def exportTests(d,tc): | ||
| 95 | import json | ||
| 96 | import shutil | ||
| 97 | import pkgutil | ||
| 98 | |||
| 99 | exportpath = d.getVar("TEST_EXPORT_DIR", True) | ||
| 100 | |||
| 101 | savedata = {} | ||
| 102 | savedata["d"] = {} | ||
| 103 | savedata["target"] = {} | ||
| 104 | for key in tc.__dict__: | ||
| 105 | # special cases | ||
| 106 | if key != "d" and key != "target": | ||
| 107 | savedata[key] = getattr(tc, key) | ||
| 108 | savedata["target"]["ip"] = tc.target.ip or d.getVar("TEST_TARGET_IP", True) | ||
| 109 | savedata["target"]["server_ip"] = tc.target.server_ip or d.getVar("TEST_SERVER_IP", True) | ||
| 110 | |||
| 111 | keys = [ key for key in d.keys() if not key.startswith("_") and not key.startswith("BB") \ | ||
| 112 | and not key.startswith("B_pn") and not key.startswith("do_") and not d.getVarFlag(key, "func")] | ||
| 113 | for key in keys: | ||
| 114 | try: | ||
| 115 | savedata["d"][key] = d.getVar(key, True) | ||
| 116 | except bb.data_smart.ExpansionError: | ||
| 117 | # we don't care about those anyway | ||
| 118 | pass | ||
| 119 | |||
| 120 | with open(os.path.join(exportpath, "testdata.json"), "w") as f: | ||
| 121 | json.dump(savedata, f, skipkeys=True, indent=4, sort_keys=True) | ||
| 122 | |||
| 123 | # now start copying files | ||
| 124 | # we'll basically copy everything under meta/lib/oeqa, with these exceptions | ||
| 125 | # - oeqa/targetcontrol.py - not needed | ||
| 126 | # - oeqa/selftest - something else | ||
| 127 | # That means: | ||
| 128 | # - all tests from oeqa/runtime defined in TEST_SUITES (including from other layers) | ||
| 129 | # - the contents of oeqa/utils and oeqa/runtime/files | ||
| 130 | # - oeqa/oetest.py and oeqa/runexport.py (this will get copied to exportpath not exportpath/oeqa) | ||
| 131 | # - __init__.py files | ||
| 132 | bb.utils.mkdirhier(os.path.join(exportpath, "oeqa/runtime/files")) | ||
| 133 | bb.utils.mkdirhier(os.path.join(exportpath, "oeqa/utils")) | ||
| 134 | # copy test modules, this should cover tests in other layers too | ||
| 135 | for t in tc.testslist: | ||
| 136 | mod = pkgutil.get_loader(t) | ||
| 137 | shutil.copy2(mod.filename, os.path.join(exportpath, "oeqa/runtime")) | ||
| 138 | # copy __init__.py files | ||
| 139 | oeqadir = pkgutil.get_loader("oeqa").filename | ||
| 140 | shutil.copy2(os.path.join(oeqadir, "__init__.py"), os.path.join(exportpath, "oeqa")) | ||
| 141 | shutil.copy2(os.path.join(oeqadir, "runtime/__init__.py"), os.path.join(exportpath, "oeqa/runtime")) | ||
| 142 | # copy oeqa/oetest.py and oeqa/runexported.py | ||
| 143 | shutil.copy2(os.path.join(oeqadir, "oetest.py"), os.path.join(exportpath, "oeqa")) | ||
| 144 | shutil.copy2(os.path.join(oeqadir, "runexported.py"), exportpath) | ||
| 145 | # copy oeqa/utils/*.py | ||
| 146 | for root, dirs, files in os.walk(os.path.join(oeqadir, "utils")): | ||
| 147 | for f in files: | ||
| 148 | if f.endswith(".py"): | ||
| 149 | shutil.copy2(os.path.join(root, f), os.path.join(exportpath, "oeqa/utils")) | ||
| 150 | # copy oeqa/runtime/files/* | ||
| 151 | for root, dirs, files in os.walk(os.path.join(oeqadir, "runtime/files")): | ||
| 152 | for f in files: | ||
| 153 | shutil.copy2(os.path.join(root, f), os.path.join(exportpath, "oeqa/runtime/files")) | ||
| 154 | |||
| 155 | bb.plain("Exported tests to: %s" % exportpath) | ||
| 156 | |||
| 157 | |||
| 158 | def testimage_main(d): | ||
| 159 | import unittest | ||
| 160 | import os | ||
| 161 | import oeqa.runtime | ||
| 162 | import time | ||
| 163 | from oeqa.oetest import loadTests, runTests | ||
| 164 | from oeqa.targetcontrol import get_target_controller | ||
| 165 | |||
| 166 | pn = d.getVar("PN", True) | ||
| 167 | export = oe.utils.conditional("TEST_EXPORT_ONLY", "1", True, False, d) | ||
| 168 | bb.utils.mkdirhier(d.getVar("TEST_LOG_DIR", True)) | ||
| 169 | if export: | ||
| 170 | bb.utils.remove(d.getVar("TEST_EXPORT_DIR", True), recurse=True) | ||
| 171 | bb.utils.mkdirhier(d.getVar("TEST_EXPORT_DIR", True)) | ||
| 172 | |||
| 173 | # tests in TEST_SUITES become required tests | ||
| 174 | # they won't be skipped even if they aren't suitable for a image (like xorg for minimal) | ||
| 175 | # testslist is what we'll actually pass to the unittest loader | ||
| 176 | testslist = get_tests_list(d) | ||
| 177 | testsrequired = [t for t in d.getVar("TEST_SUITES", True).split() if t != "auto"] | ||
| 178 | |||
| 179 | # the robot dance | ||
| 180 | target = get_target_controller(d) | ||
| 181 | |||
| 182 | class TestContext(object): | ||
| 183 | def __init__(self): | ||
| 184 | self.d = d | ||
| 185 | self.testslist = testslist | ||
| 186 | self.testsrequired = testsrequired | ||
| 187 | self.filesdir = os.path.join(os.path.dirname(os.path.abspath(oeqa.runtime.__file__)),"files") | ||
| 188 | self.target = target | ||
| 189 | self.imagefeatures = d.getVar("IMAGE_FEATURES", True).split() | ||
| 190 | self.distrofeatures = d.getVar("DISTRO_FEATURES", True).split() | ||
| 191 | manifest = os.path.join(d.getVar("DEPLOY_DIR_IMAGE", True), d.getVar("IMAGE_LINK_NAME", True) + ".manifest") | ||
| 192 | try: | ||
| 193 | with open(manifest) as f: | ||
| 194 | self.pkgmanifest = f.read() | ||
| 195 | except IOError as e: | ||
| 196 | bb.fatal("No package manifest file found. Did you build the image?\n%s" % e) | ||
| 197 | |||
| 198 | # test context | ||
| 199 | tc = TestContext() | ||
| 200 | |||
| 201 | # this is a dummy load of tests | ||
| 202 | # we are doing that to find compile errors in the tests themselves | ||
| 203 | # before booting the image | ||
| 204 | try: | ||
| 205 | loadTests(tc) | ||
| 206 | except Exception as e: | ||
| 207 | import traceback | ||
| 208 | bb.fatal("Loading tests failed:\n%s" % traceback.format_exc()) | ||
| 209 | |||
| 210 | target.deploy() | ||
| 211 | |||
| 212 | try: | ||
| 213 | target.start() | ||
| 214 | if export: | ||
| 215 | exportTests(d,tc) | ||
| 216 | else: | ||
| 217 | starttime = time.time() | ||
| 218 | result = runTests(tc) | ||
| 219 | stoptime = time.time() | ||
| 220 | if result.wasSuccessful(): | ||
| 221 | bb.plain("%s - Ran %d test%s in %.3fs" % (pn, result.testsRun, result.testsRun != 1 and "s" or "", stoptime - starttime)) | ||
| 222 | msg = "%s - OK - All required tests passed" % pn | ||
| 223 | skipped = len(result.skipped) | ||
| 224 | if skipped: | ||
| 225 | msg += " (skipped=%d)" % skipped | ||
| 226 | bb.plain(msg) | ||
| 227 | else: | ||
| 228 | raise bb.build.FuncFailed("%s - FAILED - check the task log and the ssh log" % pn ) | ||
| 229 | finally: | ||
| 230 | target.stop() | ||
| 231 | |||
| 232 | testimage_main[vardepsexclude] =+ "BB_ORIGENV" | ||
