blob: e77bb1192204658cbacdfe6b0209ad8016346a4d [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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
30TEST_LOG_DIR ?= "${WORKDIR}/testimage"
31
32TEST_EXPORT_DIR ?= "${TMPDIR}/testimage/${PN}"
33TEST_EXPORT_ONLY ?= "0"
34
35RPMTESTSUITE = "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'smart rpm', '', d)}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050036MINTESTSUITE = "ping"
37NETTESTSUITE = "${MINTESTSUITE} ssh df date scp syslog"
38DEVTESTSUITE = "gcc kernelmodule ldd"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050040DEFAULT_TEST_SUITES = "${MINTESTSUITE} auto"
41DEFAULT_TEST_SUITES_pn-core-image-minimal = "${MINTESTSUITE}"
42DEFAULT_TEST_SUITES_pn-core-image-minimal-dev = "${MINTESTSUITE}"
43DEFAULT_TEST_SUITES_pn-core-image-full-cmdline = "${NETTESTSUITE} perl python logrotate"
44DEFAULT_TEST_SUITES_pn-core-image-x11 = "${MINTESTSUITE}"
45DEFAULT_TEST_SUITES_pn-core-image-lsb = "${NETTESTSUITE} pam parselogs ${RPMTESTSUITE}"
46DEFAULT_TEST_SUITES_pn-core-image-sato = "${NETTESTSUITE} connman xorg parselogs ${RPMTESTSUITE} \
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047 ${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'python', '', d)}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050048DEFAULT_TEST_SUITES_pn-core-image-sato-sdk = "${NETTESTSUITE} connman xorg perl python \
49 ${DEVTESTSUITE} parselogs ${RPMTESTSUITE}"
50DEFAULT_TEST_SUITES_pn-core-image-lsb-dev = "${NETTESTSUITE} pam perl python parselogs ${RPMTESTSUITE}"
51DEFAULT_TEST_SUITES_pn-core-image-lsb-sdk = "${NETTESTSUITE} buildcvs buildiptables buildsudoku \
52 connman ${DEVTESTSUITE} pam perl python parselogs ${RPMTESTSUITE}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053DEFAULT_TEST_SUITES_pn-meta-toolchain = "auto"
54
55# aarch64 has no graphics
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050056DEFAULT_TEST_SUITES_remove_aarch64 = "xorg"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057
58#qemumips is too slow for buildsudoku
59DEFAULT_TEST_SUITES_remove_qemumips = "buildsudoku"
60
61TEST_SUITES ?= "${DEFAULT_TEST_SUITES}"
62
63TEST_QEMUBOOT_TIMEOUT ?= "1000"
64TEST_TARGET ?= "qemu"
65TEST_TARGET_IP ?= ""
66TEST_SERVER_IP ?= ""
67
68TESTIMAGEDEPENDS = ""
69TESTIMAGEDEPENDS_qemuall = "qemu-native:do_populate_sysroot qemu-helper-native:do_populate_sysroot"
70
71TESTIMAGELOCK = "${TMPDIR}/testimage.lock"
72TESTIMAGELOCK_qemuall = ""
73
74TESTIMAGE_DUMP_DIR ?= "/tmp/oe-saved-tests/"
75
76testimage_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
91testimage_dump_host () {
92 top -bn1
Patrick Williamsf1e5d692016-03-30 15:21:19 -050093 iostat -x -z -N -d -p ALL 20 2
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 ps -ef
95 free
96 df
97 memstat
98 dmesg
Patrick Williamsf1e5d692016-03-30 15:21:19 -050099 ip -s link
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 netstat -an
101}
102
103python do_testimage() {
104 testimage_main(d)
105}
106addtask testimage
107do_testimage[nostamp] = "1"
108do_testimage[depends] += "${TESTIMAGEDEPENDS}"
109do_testimage[lockfiles] += "${TESTIMAGELOCK}"
110
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111def exportTests(d,tc):
112 import json
113 import shutil
114 import pkgutil
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500115 import re
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116
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 Williamsd8c66bc2016-06-20 12:57:21 -0500125 if key not in ['d', 'target', 'host_dumper', 'suite']:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 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 Williamsd8c66bc2016-06-20 12:57:21 -0500131 and not key.startswith("B_pn") and not key.startswith("do_") and not d.getVarFlag(key, "func", True)]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132 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 Williamsf1e5d692016-03-30 15:21:19 -0500142 json_file = os.path.join(exportpath, "testdata.json")
143 with open(json_file, "w") as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144 json.dump(savedata, f, skipkeys=True, indent=4, sort_keys=True)
145
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500146 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500154 # 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 Williamsd8c66bc2016-06-20 12:57:21 -0500166 bbpath = d.getVar("BBPATH", True).split(':')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500167 for t in tc.testslist:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500168 isfolder = False
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500169 if re.search("\w+\.\w+\.test_\S+", t):
170 t = '.'.join(t.split('.')[:3])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 mod = pkgutil.get_loader(t)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500172 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500183 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500202def testimage_main(d):
203 import unittest
204 import os
205 import oeqa.runtime
206 import time
207 import signal
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500208 from oeqa.oetest import ImageTestContext
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500209 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 Williamsc124f4f2015-09-15 14:41:29 -0500219 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500225 # test context
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500226 tc = ImageTestContext(d, target, host_dumper)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500227
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 Williamsd8c66bc2016-06-20 12:57:21 -0500232 tc.loadTests()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 except Exception as e:
234 import traceback
235 bb.fatal("Loading tests failed:\n%s" % traceback.format_exc())
236
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500237 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 Williamsc124f4f2015-09-15 14:41:29 -0500245 starttime = time.time()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500246 result = tc.runTests()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500247 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 Williamsf1e5d692016-03-30 15:21:19 -0500257 finally:
258 signal.signal(signal.SIGTERM, tc.origsigtermhandler)
259 target.stop()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260
261testimage_main[vardepsexclude] =+ "BB_ORIGENV"
262
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500263inherit testsdk