xref: /openbmc/openbmc/poky/meta/classes-recipe/testimage.bbclass (revision c9537f57ab488bf5d90132917b0184e2527970a5)
1# Copyright (C) 2013 Intel Corporation
2#
3# SPDX-License-Identifier: MIT
4
5inherit metadata_scm
6inherit image-artifact-names
7
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:
12# - first add IMAGE_CLASSES += "testimage" in local.conf
13# - build a qemu core-image-sato
14# - then bitbake core-image-sato -c testimage. That will run a standard suite of tests.
15#
16# The tests can be run automatically each time an image is built if you set
17# TESTIMAGE_AUTO = "1"
18
19TESTIMAGE_AUTO ??= "0"
20
21# When any test fails, TESTIMAGE_FAILED_QA ARTIFACTS will be parsed and for
22# each entry in it, if artifact pointed by path description exists on target,
23# it will be retrieved onto host
24
25TESTIMAGE_FAILED_QA_ARTIFACTS = "\
26    ${localstatedir}/log \
27    ${localstatedir}/volatile/log \
28    ${sysconfdir}/version \
29    ${sysconfdir}/os-release \
30    ${nonarch_libdir}/os-release \
31"
32
33# If some ptests are run and fail, retrieve corresponding directories
34TESTIMAGE_FAILED_QA_ARTIFACTS += "${@bb.utils.contains('DISTRO_FEATURES', 'ptest', '${libdir}/*/ptest', '', d)}"
35
36# You can set (or append to) TEST_SUITES in local.conf to select the tests
37# which you want to run for your target.
38# The test names are the module names in meta/lib/oeqa/runtime/cases.
39# Each name in TEST_SUITES represents a required test for the image. (no skipping allowed)
40# 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).
41# Note that order in TEST_SUITES is relevant: tests are run in an order such that
42# tests mentioned in @skipUnlessPassed run before the tests that depend on them,
43# but without such dependencies, tests run in the order in which they are listed
44# in TEST_SUITES.
45#
46# A layer can add its own tests in lib/oeqa/runtime, provided it extends BBPATH as normal in its layer.conf.
47
48# 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.
49# Booting is handled by this class, and it's not a test in itself.
50# TEST_QEMUBOOT_TIMEOUT can be used to set the maximum time in seconds the launch code will wait for the login prompt.
51# TEST_OVERALL_TIMEOUT can be used to set the maximum time in seconds the tests will be allowed to run (defaults to no limit).
52# TEST_QEMUPARAMS can be used to pass extra parameters to qemu, e.g. "-m 1024" for setting the amount of ram to 1 GB.
53# TEST_RUNQEMUPARAMS can be used to pass extra parameters to runqemu, e.g. "gl" to enable OpenGL acceleration.
54# QEMU_USE_KVM can be set to "" to disable the use of kvm (by default it is enabled if target_arch == build_arch or both of them are x86 archs)
55
56# TESTIMAGE_BOOT_PATTERNS can be used to override certain patterns used to communicate with the target when booting,
57# if a pattern is not specifically present on this variable a default will be used when booting the target.
58# TESTIMAGE_BOOT_PATTERNS[<flag>] overrides the pattern used for that specific flag, where flag comes from a list of accepted flags
59# 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
60# if we wanted to log in as the hypothetical "webserver" user for example we could set the following:
61# TESTIMAGE_BOOT_PATTERNS = "send_login_user search_login_succeeded"
62# TESTIMAGE_BOOT_PATTERNS[send_login_user] = "webserver\n"
63# TESTIMAGE_BOOT_PATTERNS[search_login_succeeded] = "webserver@[a-zA-Z0-9\-]+:~#"
64# The accepted flags are the following: search_reached_prompt, send_login_user, search_login_succeeded, search_cmd_finished.
65# They are prefixed with either search/send, to differentiate if the pattern is meant to be sent or searched to/from the target terminal
66
67TEST_LOG_DIR ?= "${WORKDIR}/testimage"
68
69TEST_EXPORT_DIR ?= "${TMPDIR}/testimage/${PN}"
70TEST_INSTALL_TMP_DIR ?= "${WORKDIR}/testimage/install_tmp"
71TEST_NEEDED_PACKAGES_DIR ?= "${WORKDIR}/testimage/packages"
72TEST_EXTRACTED_DIR ?= "${TEST_NEEDED_PACKAGES_DIR}/extracted"
73TEST_PACKAGED_DIR ?= "${TEST_NEEDED_PACKAGES_DIR}/packaged"
74
75BASICTESTSUITE = "\
76    ping date df ssh scp python perl gi ptest parselogs \
77    logrotate connman systemd oe_syslog pam stap ldd xorg \
78    kernelmodule gcc buildcpio buildlzip buildgalculator \
79    dnf rpm opkg apt weston go rust"
80
81DEFAULT_TEST_SUITES = "${BASICTESTSUITE}"
82
83# musl doesn't support systemtap
84DEFAULT_TEST_SUITES:remove:libc-musl = "stap"
85
86# qemumips is quite slow and has reached the timeout limit several times on the YP build cluster,
87# mitigate this by removing build tests for qemumips machines.
88MIPSREMOVE ??= "buildcpio buildlzip buildgalculator"
89DEFAULT_TEST_SUITES:remove:qemumips = "${MIPSREMOVE}"
90DEFAULT_TEST_SUITES:remove:qemumips64 = "${MIPSREMOVE}"
91
92TEST_SUITES ?= "${DEFAULT_TEST_SUITES}"
93
94QEMU_USE_KVM ?= "1"
95TEST_QEMUBOOT_TIMEOUT ?= "1000"
96TEST_OVERALL_TIMEOUT ?= ""
97TEST_TARGET ?= "qemu"
98TEST_QEMUPARAMS ?= ""
99TEST_RUNQEMUPARAMS ?= ""
100
101TESTIMAGE_BOOT_PATTERNS ?= ""
102
103TESTIMAGEDEPENDS = ""
104TESTIMAGEDEPENDS:append:qemuall = " qemu-native:do_populate_sysroot qemu-helper-native:do_populate_sysroot qemu-helper-native:do_addto_recipe_sysroot"
105TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'dnf-native:do_populate_sysroot', '', d)}"
106TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'createrepo-c-native:do_populate_sysroot', '', d)}"
107TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'ipk', 'opkg-utils-native:do_populate_sysroot package-index:do_package_index', '', d)}"
108TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'deb', 'apt-native:do_populate_sysroot  package-index:do_package_index', '', d)}"
109
110TESTIMAGELOCK = "${TMPDIR}/testimage.lock"
111TESTIMAGELOCK:qemuall = ""
112
113TESTIMAGE_DUMP_DIR ?= "${LOG_DIR}/runtime-hostdump/"
114
115TESTIMAGE_UPDATE_VARS ?= "DL_DIR WORKDIR DEPLOY_DIR_IMAGE IMAGE_LINK_NAME IMAGE_NAME"
116
117testimage_dump_monitor () {
118    query-status
119    query-block
120    dump-guest-memory {"paging":false,"protocol":"file:%s.img"}
121}
122
123python do_testimage() {
124    testimage_main(d)
125}
126
127addtask testimage
128do_testimage[nostamp] = "1"
129do_testimage[network] = "1"
130do_testimage[depends] += "${TESTIMAGEDEPENDS}"
131do_testimage[lockfiles] += "${TESTIMAGELOCK}"
132
133def testimage_sanity(d):
134    if (d.getVar('TEST_TARGET') == 'simpleremote'
135        and (not d.getVar('TEST_TARGET_IP')
136             or not d.getVar('TEST_SERVER_IP'))):
137        bb.fatal('When TEST_TARGET is set to "simpleremote" '
138                 'TEST_TARGET_IP and TEST_SERVER_IP are needed too.')
139
140def get_testimage_configuration(d, test_type, machine):
141    import platform
142    from oeqa.utils.metadata import get_layers
143    configuration = {'TEST_TYPE': test_type,
144                    'MACHINE': machine,
145                    'DISTRO': d.getVar("DISTRO"),
146                    'IMAGE_BASENAME': d.getVar("IMAGE_BASENAME"),
147                    'IMAGE_PKGTYPE': d.getVar("IMAGE_PKGTYPE"),
148                    'STARTTIME': d.getVar("DATETIME"),
149                    'HOST_DISTRO': oe.lsb.distro_identifier().replace(' ', '-'),
150                    'LAYERS': get_layers(d.getVar("BBLAYERS"))}
151    return configuration
152get_testimage_configuration[vardepsexclude] = "DATETIME"
153
154def get_testimage_result_id(configuration):
155    return '%s_%s_%s_%s' % (configuration['TEST_TYPE'], configuration['IMAGE_BASENAME'], configuration['MACHINE'], configuration['STARTTIME'])
156
157def get_testimage_boot_patterns(d):
158    from collections import defaultdict
159    boot_patterns = defaultdict(str)
160    # Only accept certain values
161    accepted_patterns = ['search_reached_prompt', 'send_login_user', 'search_login_succeeded', 'search_cmd_finished']
162    # Not all patterns need to be overriden, e.g. perhaps we only want to change the user
163    boot_patterns_flags = d.getVarFlags('TESTIMAGE_BOOT_PATTERNS') or {}
164    if boot_patterns_flags:
165        patterns_set = [p for p in boot_patterns_flags.items() if p[0] in d.getVar('TESTIMAGE_BOOT_PATTERNS').split()]
166        for flag, flagval in patterns_set:
167                if flag not in accepted_patterns:
168                    bb.fatal('Testimage: The only accepted boot patterns are: search_reached_prompt,send_login_user, \
169                    search_login_succeeded,search_cmd_finished\n Make sure your TESTIMAGE_BOOT_PATTERNS=%s \
170                    contains an accepted flag.' % d.getVar('TESTIMAGE_BOOT_PATTERNS'))
171                    return
172                boot_patterns[flag] = flagval.encode().decode('unicode-escape')
173    return boot_patterns
174
175def testimage_main(d):
176    import os
177    import json
178    import signal
179    import logging
180    import shutil
181
182    from bb.utils import export_proxies
183    from oeqa.runtime.context import OERuntimeTestContext
184    from oeqa.runtime.context import OERuntimeTestContextExecutor
185    from oeqa.core.target.qemu import supported_fstypes
186    from oeqa.core.utils.test import getSuiteCases
187    from oeqa.utils import make_logger_bitbake_compatible
188    from oeqa.utils import get_json_result_dir
189    from oeqa.utils.postactions import run_failed_tests_post_actions
190
191    def sigterm_exception(signum, stackframe):
192        """
193        Catch SIGTERM from worker in order to stop qemu.
194        """
195        os.kill(os.getpid(), signal.SIGINT)
196
197    def handle_test_timeout(timeout):
198        bb.warn("Global test timeout reached (%s seconds), stopping the tests." %(timeout))
199        os.kill(os.getpid(), signal.SIGINT)
200
201    testimage_sanity(d)
202
203    if (d.getVar('IMAGE_PKGTYPE') == 'rpm'
204       and ('dnf' in d.getVar('TEST_SUITES') or 'auto' in d.getVar('TEST_SUITES'))):
205        create_rpm_index(d)
206
207    logger = make_logger_bitbake_compatible(logging.getLogger("BitBake"))
208    pn = d.getVar("PN")
209
210    bb.utils.mkdirhier(d.getVar("TEST_LOG_DIR"))
211
212    image_name = ("%s/%s" % (d.getVar('DEPLOY_DIR_IMAGE'),
213                             d.getVar('IMAGE_LINK_NAME') or d.getVar('IMAGE_NAME')))
214
215    tdname = "%s.testdata.json" % image_name
216    try:
217        with open(tdname, "r") as f:
218            td = json.load(f)
219    except FileNotFoundError as err:
220        bb.fatal('File %s not found (%s).\nHave you built the image with IMAGE_CLASSES += "testimage" in the conf/local.conf?' % (tdname, err))
221
222    # Some variables need to be updates (mostly paths) with the
223    # ones of the current environment because some tests require them.
224    for var in d.getVar('TESTIMAGE_UPDATE_VARS').split():
225        td[var] = d.getVar(var)
226    td['ORIGPATH'] = d.getVar("BB_ORIGENV").getVar("PATH")
227
228    image_manifest = "%s.manifest" % image_name
229    image_packages = OERuntimeTestContextExecutor.readPackagesManifest(image_manifest)
230
231    extract_dir = d.getVar("TEST_EXTRACTED_DIR")
232
233    # Get machine
234    machine = d.getVar("MACHINE")
235
236    # Get rootfs
237    fstypes = d.getVar('IMAGE_FSTYPES').split()
238    if d.getVar("TEST_TARGET") == "qemu":
239        fstypes = [fs for fs in fstypes if fs in supported_fstypes]
240        if not fstypes:
241            bb.fatal('Unsupported image type built. Add a compatible image to '
242                     'IMAGE_FSTYPES. Supported types: %s' %
243                     ', '.join(supported_fstypes))
244    elif d.getVar("TEST_TARGET") == "serial":
245        bb.fatal('Serial target is currently only supported in testexport.')
246    qfstype = fstypes[0]
247    qdeffstype = d.getVar("QB_DEFAULT_FSTYPE")
248    if qdeffstype:
249        qfstype = qdeffstype
250    rootfs = '%s.%s' % (image_name, qfstype)
251
252    # Get tmpdir (not really used, just for compatibility)
253    tmpdir = d.getVar("TMPDIR")
254
255    # Get deploy_dir_image (not really used, just for compatibility)
256    dir_image = d.getVar("DEPLOY_DIR_IMAGE")
257
258    # Get bootlog
259    bootlog = os.path.join(d.getVar("TEST_LOG_DIR"),
260                           'qemu_boot_log.%s' % d.getVar('DATETIME'))
261
262    # Get display
263    display = d.getVar("BB_ORIGENV").getVar("DISPLAY")
264
265    # Get kernel
266    kernel_name = ('%s-%s.bin' % (d.getVar("KERNEL_IMAGETYPE"), machine))
267    kernel = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), kernel_name)
268
269    # Get boottime
270    boottime = int(d.getVar("TEST_QEMUBOOT_TIMEOUT"))
271
272    # Get use_kvm
273    kvm = oe.types.qemu_use_kvm(d.getVar('QEMU_USE_KVM'), d.getVar('TARGET_ARCH'))
274
275    # Get OVMF
276    ovmf = d.getVar("QEMU_USE_OVMF")
277
278    slirp = False
279    if bb.utils.contains('TEST_RUNQEMUPARAMS', 'slirp', True, False, d):
280        slirp = True
281
282    # TODO: We use the current implementation of qemu runner because of
283    # time constrains, qemu runner really needs a refactor too.
284    target_kwargs = { 'machine'     : machine,
285                      'rootfs'      : rootfs,
286                      'tmpdir'      : tmpdir,
287                      'dir_image'   : dir_image,
288                      'display'     : display,
289                      'kernel'      : kernel,
290                      'boottime'    : boottime,
291                      'bootlog'     : bootlog,
292                      'kvm'         : kvm,
293                      'slirp'       : slirp,
294                      'dump_dir'    : d.getVar("TESTIMAGE_DUMP_DIR"),
295                      'serial_ports': len(d.getVar("SERIAL_CONSOLES").split()),
296                      'ovmf'        : ovmf,
297                      'tmpfsdir'    : d.getVar("RUNQEMU_TMPFS_DIR"),
298                    }
299
300    if d.getVar("TESTIMAGE_BOOT_PATTERNS"):
301        target_kwargs['boot_patterns'] = get_testimage_boot_patterns(d)
302
303    # hardware controlled targets might need further access
304    target_kwargs['powercontrol_cmd'] = d.getVar("TEST_POWERCONTROL_CMD") or None
305    target_kwargs['powercontrol_extra_args'] = d.getVar("TEST_POWERCONTROL_EXTRA_ARGS") or ""
306    target_kwargs['serialcontrol_cmd'] = d.getVar("TEST_SERIALCONTROL_CMD") or None
307    target_kwargs['serialcontrol_extra_args'] = d.getVar("TEST_SERIALCONTROL_EXTRA_ARGS") or ""
308    target_kwargs['testimage_dump_monitor'] = d.getVar("testimage_dump_monitor") or ""
309
310    def export_ssh_agent(d):
311        import os
312
313        variables = ['SSH_AGENT_PID', 'SSH_AUTH_SOCK']
314        for v in variables:
315            if v not in os.environ.keys():
316                val = d.getVar(v)
317                if val is not None:
318                    os.environ[v] = val
319
320    export_ssh_agent(d)
321
322    # runtime use network for download projects for build
323    export_proxies(d)
324
325    if slirp:
326        # Default to 127.0.0.1 and let the runner identify the port forwarding
327        # (as OEQemuTarget does), but allow overriding.
328        target_ip = d.getVar("TEST_TARGET_IP") or "127.0.0.1"
329        # Default to 10.0.2.2 as this is the IP that the guest has with the
330        # default qemu slirp networking configuration, but allow overriding.
331        server_ip = d.getVar("TEST_SERVER_IP") or "10.0.2.2"
332    else:
333        target_ip = d.getVar("TEST_TARGET_IP")
334        server_ip = d.getVar("TEST_SERVER_IP")
335
336    # the robot dance
337    target = OERuntimeTestContextExecutor.getTarget(
338        d.getVar("TEST_TARGET"), logger, target_ip,
339        server_ip, **target_kwargs)
340
341    # test context
342    tc = OERuntimeTestContext(td, logger, target, image_packages, extract_dir)
343
344    # Load tests before starting the target
345    test_paths = get_runtime_paths(d)
346    test_modules = d.getVar('TEST_SUITES').split()
347    if not test_modules:
348        bb.fatal('Empty test suite, please verify TEST_SUITES variable')
349
350    tc.loadTests(test_paths, modules=test_modules)
351
352    suitecases = getSuiteCases(tc.suites)
353    if not suitecases:
354        bb.fatal('Empty test suite, please verify TEST_SUITES variable')
355    else:
356        bb.debug(2, 'test suites:\n\t%s' % '\n\t'.join([str(c) for c in suitecases]))
357
358    package_extraction(d, tc.suites)
359
360    results = None
361    complete = False
362    orig_sigterm_handler = signal.signal(signal.SIGTERM, sigterm_exception)
363    try:
364        # We need to check if runqemu ends unexpectedly
365        # or if the worker send us a SIGTERM
366        tc.target.start(params=d.getVar("TEST_QEMUPARAMS"), runqemuparams=d.getVar("TEST_RUNQEMUPARAMS"))
367        import threading
368        try:
369            threading.Timer(int(d.getVar("TEST_OVERALL_TIMEOUT")), handle_test_timeout, (int(d.getVar("TEST_OVERALL_TIMEOUT")),)).start()
370        except ValueError:
371            pass
372        results = tc.runTests()
373        complete = True
374        if results.hasAnyFailingTest():
375            run_failed_tests_post_actions(d, tc)
376    except (KeyboardInterrupt, BlockingIOError) as err:
377        if isinstance(err, KeyboardInterrupt):
378            bb.error('testimage interrupted, shutting down...')
379        else:
380            bb.error('runqemu failed, shutting down...')
381        if results:
382            results.stop()
383    finally:
384        signal.signal(signal.SIGTERM, orig_sigterm_handler)
385        tc.target.stop()
386
387    # Show results (if we have them)
388    if results:
389        configuration = get_testimage_configuration(d, 'runtime', machine)
390        results.logDetails(get_json_result_dir(d),
391                        configuration,
392                        get_testimage_result_id(configuration),
393                        dump_streams=d.getVar('TESTREPORT_FULLLOGS'))
394        results.logSummary(pn)
395
396    # Copy additional logs to tmp/log/oeqa so it's easier to find them
397    targetdir = os.path.join(get_json_result_dir(d), d.getVar("PN"))
398    os.makedirs(targetdir, exist_ok=True)
399    os.symlink(bootlog, os.path.join(targetdir, os.path.basename(bootlog)))
400    os.symlink(d.getVar("BB_LOGFILE"), os.path.join(targetdir, os.path.basename(d.getVar("BB_LOGFILE") + "." + d.getVar('DATETIME'))))
401
402    if not results or not complete:
403        bb.error('%s - FAILED - tests were interrupted during execution, check the logs in %s' % (pn, d.getVar("LOG_DIR")), forcelog=True)
404    if results and not results.wasSuccessful():
405        bb.error('%s - FAILED - also check the logs in %s' % (pn, d.getVar("LOG_DIR")), forcelog=True)
406
407def get_runtime_paths(d):
408    """
409    Returns a list of paths where runtime test must reside.
410
411    Runtime tests are expected in <LAYER_DIR>/lib/oeqa/runtime/cases/
412    """
413    paths = []
414
415    for layer in d.getVar('BBLAYERS').split():
416        path = os.path.join(layer, 'lib/oeqa/runtime/cases')
417        if os.path.isdir(path):
418            paths.append(path)
419    return paths
420
421def create_index(arg):
422    import subprocess
423
424    index_cmd = arg
425    try:
426        bb.note("Executing '%s' ..." % index_cmd)
427        result = subprocess.check_output(index_cmd,
428                                        stderr=subprocess.STDOUT,
429                                        shell=True)
430        result = result.decode('utf-8')
431    except subprocess.CalledProcessError as e:
432        return("Index creation command '%s' failed with return code "
433               '%d:\n%s' % (e.cmd, e.returncode, e.output.decode("utf-8")))
434    if result:
435        bb.note(result)
436    return None
437
438def create_rpm_index(d):
439    import glob
440    # Index RPMs
441    rpm_createrepo = bb.utils.which(os.getenv('PATH'), "createrepo_c")
442    index_cmds = []
443    archs = (d.getVar('ALL_MULTILIB_PACKAGE_ARCHS') or '').replace('-', '_')
444
445    for arch in archs.split():
446        rpm_dir = os.path.join(d.getVar('DEPLOY_DIR_RPM'), arch)
447        idx_path = os.path.join(d.getVar('WORKDIR'), 'oe-testimage-repo', arch)
448
449        if not os.path.isdir(rpm_dir):
450            continue
451
452        lockfilename = os.path.join(d.getVar('DEPLOY_DIR_RPM'), 'rpm.lock')
453        lf = bb.utils.lockfile(lockfilename, False)
454        oe.path.copyhardlinktree(rpm_dir, idx_path)
455        # Full indexes overload a 256MB image so reduce the number of rpms
456        # in the feed by filtering to specific packages needed by the tests.
457        package_list = glob.glob(idx_path + "*/*.rpm")
458
459        for pkg in package_list:
460            if not os.path.basename(pkg).startswith(("dnf-test-", "busybox", "update-alternatives", "libc6", "musl")):
461                bb.utils.remove(pkg)
462
463        bb.utils.unlockfile(lf)
464        cmd = '%s --update -q %s' % (rpm_createrepo, idx_path)
465
466        # Create repodata
467        result = create_index(cmd)
468        if result:
469            bb.fatal('%s' % ('\n'.join(result)))
470
471def package_extraction(d, test_suites):
472    from oeqa.utils.package_manager import find_packages_to_extract
473    from oeqa.utils.package_manager import extract_packages
474
475    bb.utils.remove(d.getVar("TEST_NEEDED_PACKAGES_DIR"), recurse=True)
476    packages = find_packages_to_extract(test_suites)
477    if packages:
478        bb.utils.mkdirhier(d.getVar("TEST_INSTALL_TMP_DIR"))
479        bb.utils.mkdirhier(d.getVar("TEST_PACKAGED_DIR"))
480        bb.utils.mkdirhier(d.getVar("TEST_EXTRACTED_DIR"))
481        extract_packages(d, packages)
482
483testimage_main[vardepsexclude] += "BB_ORIGENV DATETIME"
484
485python () {
486    if oe.types.boolean(d.getVar("TESTIMAGE_AUTO") or "False"):
487        bb.build.addtask("testimage", "do_build", "do_image_complete", d)
488}
489