diff options
Diffstat (limited to 'meta/classes/testimage.bbclass')
-rw-r--r-- | meta/classes/testimage.bbclass | 323 |
1 files changed, 323 insertions, 0 deletions
diff --git a/meta/classes/testimage.bbclass b/meta/classes/testimage.bbclass new file mode 100644 index 0000000000..683173854d --- /dev/null +++ b/meta/classes/testimage.bbclass | |||
@@ -0,0 +1,323 @@ | |||
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 command ssh log and may contain infromation about what command is running, output and return codes and for qemu a 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 parselogs" | ||
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 parselogs" | ||
35 | DEFAULT_TEST_SUITES_pn-meta-toolchain = "auto" | ||
36 | TEST_SUITES ?= "${DEFAULT_TEST_SUITES}" | ||
37 | |||
38 | TEST_QEMUBOOT_TIMEOUT ?= "1000" | ||
39 | TEST_TARGET ?= "qemu" | ||
40 | TEST_TARGET_IP ?= "" | ||
41 | TEST_SERVER_IP ?= "" | ||
42 | |||
43 | TESTIMAGEDEPENDS = "" | ||
44 | TESTIMAGEDEPENDS_qemuall = "qemu-native:do_populate_sysroot qemu-helper-native:do_populate_sysroot" | ||
45 | |||
46 | TESTIMAGELOCK = "${TMPDIR}/testimage.lock" | ||
47 | TESTIMAGELOCK_qemuall = "" | ||
48 | |||
49 | python do_testimage() { | ||
50 | testimage_main(d) | ||
51 | } | ||
52 | addtask testimage | ||
53 | do_testimage[nostamp] = "1" | ||
54 | do_testimage[depends] += "${TESTIMAGEDEPENDS}" | ||
55 | do_testimage[lockfiles] += "${TESTIMAGELOCK}" | ||
56 | |||
57 | python do_testsdk() { | ||
58 | testsdk_main(d) | ||
59 | } | ||
60 | addtask testsdk | ||
61 | do_testsdk[nostamp] = "1" | ||
62 | do_testsdk[depends] += "${TESTIMAGEDEPENDS}" | ||
63 | do_testsdk[lockfiles] += "${TESTIMAGELOCK}" | ||
64 | |||
65 | def get_tests_list(d, type="runtime"): | ||
66 | testsuites = d.getVar("TEST_SUITES", True).split() | ||
67 | bbpath = d.getVar("BBPATH", True).split(':') | ||
68 | |||
69 | # This relies on lib/ under each directory in BBPATH being added to sys.path | ||
70 | # (as done by default in base.bbclass) | ||
71 | testslist = [] | ||
72 | for testname in testsuites: | ||
73 | if testname != "auto": | ||
74 | found = False | ||
75 | for p in bbpath: | ||
76 | if os.path.exists(os.path.join(p, 'lib', 'oeqa', type, testname + '.py')): | ||
77 | testslist.append("oeqa." + type + "." + testname) | ||
78 | found = True | ||
79 | break | ||
80 | if not found: | ||
81 | bb.fatal('Test %s specified in TEST_SUITES could not be found in lib/oeqa/runtime under BBPATH' % testname) | ||
82 | |||
83 | if "auto" in testsuites: | ||
84 | def add_auto_list(path): | ||
85 | if not os.path.exists(os.path.join(path, '__init__.py')): | ||
86 | bb.fatal('Tests directory %s exists but is missing __init__.py' % path) | ||
87 | files = sorted([f for f in os.listdir(path) if f.endswith('.py') and not f.startswith('_')]) | ||
88 | for f in files: | ||
89 | module = 'oeqa.' + type + '.' + f[:-3] | ||
90 | if module not in testslist: | ||
91 | testslist.append(module) | ||
92 | |||
93 | for p in bbpath: | ||
94 | testpath = os.path.join(p, 'lib', 'oeqa', type) | ||
95 | bb.debug(2, 'Searching for tests in %s' % testpath) | ||
96 | if os.path.exists(testpath): | ||
97 | add_auto_list(testpath) | ||
98 | |||
99 | return testslist | ||
100 | |||
101 | |||
102 | def exportTests(d,tc): | ||
103 | import json | ||
104 | import shutil | ||
105 | import pkgutil | ||
106 | |||
107 | exportpath = d.getVar("TEST_EXPORT_DIR", True) | ||
108 | |||
109 | savedata = {} | ||
110 | savedata["d"] = {} | ||
111 | savedata["target"] = {} | ||
112 | for key in tc.__dict__: | ||
113 | # special cases | ||
114 | if key != "d" and key != "target": | ||
115 | savedata[key] = getattr(tc, key) | ||
116 | savedata["target"]["ip"] = tc.target.ip or d.getVar("TEST_TARGET_IP", True) | ||
117 | savedata["target"]["server_ip"] = tc.target.server_ip or d.getVar("TEST_SERVER_IP", True) | ||
118 | |||
119 | keys = [ key for key in d.keys() if not key.startswith("_") and not key.startswith("BB") \ | ||
120 | and not key.startswith("B_pn") and not key.startswith("do_") and not d.getVarFlag(key, "func")] | ||
121 | for key in keys: | ||
122 | try: | ||
123 | savedata["d"][key] = d.getVar(key, True) | ||
124 | except bb.data_smart.ExpansionError: | ||
125 | # we don't care about those anyway | ||
126 | pass | ||
127 | |||
128 | with open(os.path.join(exportpath, "testdata.json"), "w") as f: | ||
129 | json.dump(savedata, f, skipkeys=True, indent=4, sort_keys=True) | ||
130 | |||
131 | # now start copying files | ||
132 | # we'll basically copy everything under meta/lib/oeqa, with these exceptions | ||
133 | # - oeqa/targetcontrol.py - not needed | ||
134 | # - oeqa/selftest - something else | ||
135 | # That means: | ||
136 | # - all tests from oeqa/runtime defined in TEST_SUITES (including from other layers) | ||
137 | # - the contents of oeqa/utils and oeqa/runtime/files | ||
138 | # - oeqa/oetest.py and oeqa/runexport.py (this will get copied to exportpath not exportpath/oeqa) | ||
139 | # - __init__.py files | ||
140 | bb.utils.mkdirhier(os.path.join(exportpath, "oeqa/runtime/files")) | ||
141 | bb.utils.mkdirhier(os.path.join(exportpath, "oeqa/utils")) | ||
142 | # copy test modules, this should cover tests in other layers too | ||
143 | for t in tc.testslist: | ||
144 | mod = pkgutil.get_loader(t) | ||
145 | shutil.copy2(mod.filename, os.path.join(exportpath, "oeqa/runtime")) | ||
146 | # copy __init__.py files | ||
147 | oeqadir = pkgutil.get_loader("oeqa").filename | ||
148 | shutil.copy2(os.path.join(oeqadir, "__init__.py"), os.path.join(exportpath, "oeqa")) | ||
149 | shutil.copy2(os.path.join(oeqadir, "runtime/__init__.py"), os.path.join(exportpath, "oeqa/runtime")) | ||
150 | # copy oeqa/oetest.py and oeqa/runexported.py | ||
151 | shutil.copy2(os.path.join(oeqadir, "oetest.py"), os.path.join(exportpath, "oeqa")) | ||
152 | shutil.copy2(os.path.join(oeqadir, "runexported.py"), exportpath) | ||
153 | # copy oeqa/utils/*.py | ||
154 | for root, dirs, files in os.walk(os.path.join(oeqadir, "utils")): | ||
155 | for f in files: | ||
156 | if f.endswith(".py"): | ||
157 | shutil.copy2(os.path.join(root, f), os.path.join(exportpath, "oeqa/utils")) | ||
158 | # copy oeqa/runtime/files/* | ||
159 | for root, dirs, files in os.walk(os.path.join(oeqadir, "runtime/files")): | ||
160 | for f in files: | ||
161 | shutil.copy2(os.path.join(root, f), os.path.join(exportpath, "oeqa/runtime/files")) | ||
162 | |||
163 | bb.plain("Exported tests to: %s" % exportpath) | ||
164 | |||
165 | |||
166 | def testimage_main(d): | ||
167 | import unittest | ||
168 | import os | ||
169 | import oeqa.runtime | ||
170 | import time | ||
171 | from oeqa.oetest import loadTests, runTests | ||
172 | from oeqa.targetcontrol import get_target_controller | ||
173 | |||
174 | pn = d.getVar("PN", True) | ||
175 | export = oe.utils.conditional("TEST_EXPORT_ONLY", "1", True, False, d) | ||
176 | bb.utils.mkdirhier(d.getVar("TEST_LOG_DIR", True)) | ||
177 | if export: | ||
178 | bb.utils.remove(d.getVar("TEST_EXPORT_DIR", True), recurse=True) | ||
179 | bb.utils.mkdirhier(d.getVar("TEST_EXPORT_DIR", True)) | ||
180 | |||
181 | # tests in TEST_SUITES become required tests | ||
182 | # they won't be skipped even if they aren't suitable for a image (like xorg for minimal) | ||
183 | # testslist is what we'll actually pass to the unittest loader | ||
184 | testslist = get_tests_list(d) | ||
185 | testsrequired = [t for t in d.getVar("TEST_SUITES", True).split() if t != "auto"] | ||
186 | |||
187 | # the robot dance | ||
188 | target = get_target_controller(d) | ||
189 | |||
190 | class TestContext(object): | ||
191 | def __init__(self): | ||
192 | self.d = d | ||
193 | self.testslist = testslist | ||
194 | self.testsrequired = testsrequired | ||
195 | self.filesdir = os.path.join(os.path.dirname(os.path.abspath(oeqa.runtime.__file__)),"files") | ||
196 | self.target = target | ||
197 | self.imagefeatures = d.getVar("IMAGE_FEATURES", True).split() | ||
198 | self.distrofeatures = d.getVar("DISTRO_FEATURES", True).split() | ||
199 | manifest = os.path.join(d.getVar("DEPLOY_DIR_IMAGE", True), d.getVar("IMAGE_LINK_NAME", True) + ".manifest") | ||
200 | try: | ||
201 | with open(manifest) as f: | ||
202 | self.pkgmanifest = f.read() | ||
203 | except IOError as e: | ||
204 | bb.fatal("No package manifest file found. Did you build the image?\n%s" % e) | ||
205 | |||
206 | # test context | ||
207 | tc = TestContext() | ||
208 | |||
209 | # this is a dummy load of tests | ||
210 | # we are doing that to find compile errors in the tests themselves | ||
211 | # before booting the image | ||
212 | try: | ||
213 | loadTests(tc) | ||
214 | except Exception as e: | ||
215 | import traceback | ||
216 | bb.fatal("Loading tests failed:\n%s" % traceback.format_exc()) | ||
217 | |||
218 | target.deploy() | ||
219 | |||
220 | target.start() | ||
221 | try: | ||
222 | if export: | ||
223 | exportTests(d,tc) | ||
224 | else: | ||
225 | starttime = time.time() | ||
226 | result = runTests(tc) | ||
227 | stoptime = time.time() | ||
228 | if result.wasSuccessful(): | ||
229 | bb.plain("%s - Ran %d test%s in %.3fs" % (pn, result.testsRun, result.testsRun != 1 and "s" or "", stoptime - starttime)) | ||
230 | msg = "%s - OK - All required tests passed" % pn | ||
231 | skipped = len(result.skipped) | ||
232 | if skipped: | ||
233 | msg += " (skipped=%d)" % skipped | ||
234 | bb.plain(msg) | ||
235 | else: | ||
236 | raise bb.build.FuncFailed("%s - FAILED - check the task log and the ssh log" % pn ) | ||
237 | finally: | ||
238 | target.stop() | ||
239 | |||
240 | testimage_main[vardepsexclude] =+ "BB_ORIGENV" | ||
241 | |||
242 | |||
243 | def testsdk_main(d): | ||
244 | import unittest | ||
245 | import os | ||
246 | import glob | ||
247 | import oeqa.runtime | ||
248 | import oeqa.sdk | ||
249 | import time | ||
250 | import subprocess | ||
251 | from oeqa.oetest import loadTests, runTests | ||
252 | |||
253 | pn = d.getVar("PN", True) | ||
254 | bb.utils.mkdirhier(d.getVar("TEST_LOG_DIR", True)) | ||
255 | |||
256 | # tests in TEST_SUITES become required tests | ||
257 | # they won't be skipped even if they aren't suitable. | ||
258 | # testslist is what we'll actually pass to the unittest loader | ||
259 | testslist = get_tests_list(d, "sdk") | ||
260 | testsrequired = [t for t in d.getVar("TEST_SUITES", True).split() if t != "auto"] | ||
261 | |||
262 | sdktestdir = d.expand("${WORKDIR}/testimage-sdk/") | ||
263 | bb.utils.remove(sdktestdir, True) | ||
264 | bb.utils.mkdirhier(sdktestdir) | ||
265 | |||
266 | tcname = d.expand("${SDK_DEPLOY}/${TOOLCHAIN_OUTPUTNAME}.sh") | ||
267 | if not os.path.exists(tcname): | ||
268 | bb.fatal("The toolchain is not built. Build it before running the tests: 'bitbake meta-toolchain' .") | ||
269 | subprocess.call("cd %s; %s <<EOF\n./tc\nY\nEOF" % (sdktestdir, tcname), shell=True) | ||
270 | |||
271 | targets = glob.glob(d.expand(sdktestdir + "/tc/sysroots/*${TARGET_VENDOR}-linux*")) | ||
272 | if len(targets) > 1: | ||
273 | bb.fatal("Error, multiple targets within the SDK found and we don't know which to test? %s" % str(targets)) | ||
274 | sdkenv = sdktestdir + "/tc/environment-setup-" + os.path.basename(targets[0]) | ||
275 | |||
276 | class TestContext(object): | ||
277 | def __init__(self): | ||
278 | self.d = d | ||
279 | self.testslist = testslist | ||
280 | self.testsrequired = testsrequired | ||
281 | self.filesdir = os.path.join(os.path.dirname(os.path.abspath(oeqa.runtime.__file__)),"files") | ||
282 | self.sdktestdir = sdktestdir | ||
283 | self.sdkenv = sdkenv | ||
284 | self.imagefeatures = d.getVar("IMAGE_FEATURES", True).split() | ||
285 | self.distrofeatures = d.getVar("DISTRO_FEATURES", True).split() | ||
286 | manifest = os.path.join(d.getVar("SDK_MANIFEST", True)) | ||
287 | try: | ||
288 | with open(manifest) as f: | ||
289 | self.pkgmanifest = f.read() | ||
290 | except IOError as e: | ||
291 | bb.fatal("No package manifest file found. Did you build the sdk image?\n%s" % e) | ||
292 | |||
293 | # test context | ||
294 | tc = TestContext() | ||
295 | |||
296 | # this is a dummy load of tests | ||
297 | # we are doing that to find compile errors in the tests themselves | ||
298 | # before booting the image | ||
299 | try: | ||
300 | loadTests(tc, "sdk") | ||
301 | except Exception as e: | ||
302 | import traceback | ||
303 | bb.fatal("Loading tests failed:\n%s" % traceback.format_exc()) | ||
304 | |||
305 | try: | ||
306 | starttime = time.time() | ||
307 | result = runTests(tc, "sdk") | ||
308 | stoptime = time.time() | ||
309 | if result.wasSuccessful(): | ||
310 | bb.plain("%s - Ran %d test%s in %.3fs" % (pn, result.testsRun, result.testsRun != 1 and "s" or "", stoptime - starttime)) | ||
311 | msg = "%s - OK - All required tests passed" % pn | ||
312 | skipped = len(result.skipped) | ||
313 | if skipped: | ||
314 | msg += " (skipped=%d)" % skipped | ||
315 | bb.plain(msg) | ||
316 | else: | ||
317 | raise bb.build.FuncFailed("%s - FAILED - check the task log and the commands log" % pn ) | ||
318 | finally: | ||
319 | pass | ||
320 | bb.utils.remove(sdktestdir, True) | ||
321 | |||
322 | testsdk_main[vardepsexclude] =+ "BB_ORIGENV" | ||
323 | |||