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 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 11 | # - add IMAGE_CLASSES += "testimage" in local.conf |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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}" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 33 | TEST_INSTALL_TMP_DIR ?= "${WORKDIR}/testimage/install_tmp" |
| 34 | TEST_NEEDED_PACKAGES_DIR ?= "${WORKDIR}/testimage/packages" |
| 35 | TEST_EXTRACTED_DIR ?= "${TEST_NEEDED_PACKAGES_DIR}/extracted" |
| 36 | TEST_PACKAGED_DIR ?= "${TEST_NEEDED_PACKAGES_DIR}/packaged" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | |
| 38 | RPMTESTSUITE = "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'smart rpm', '', d)}" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 39 | MINTESTSUITE = "ping" |
| 40 | NETTESTSUITE = "${MINTESTSUITE} ssh df date scp syslog" |
| 41 | DEVTESTSUITE = "gcc kernelmodule ldd" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 43 | DEFAULT_TEST_SUITES = "${MINTESTSUITE} auto" |
| 44 | DEFAULT_TEST_SUITES_pn-core-image-minimal = "${MINTESTSUITE}" |
| 45 | DEFAULT_TEST_SUITES_pn-core-image-minimal-dev = "${MINTESTSUITE}" |
| 46 | DEFAULT_TEST_SUITES_pn-core-image-full-cmdline = "${NETTESTSUITE} perl python logrotate" |
| 47 | DEFAULT_TEST_SUITES_pn-core-image-x11 = "${MINTESTSUITE}" |
| 48 | DEFAULT_TEST_SUITES_pn-core-image-lsb = "${NETTESTSUITE} pam parselogs ${RPMTESTSUITE}" |
| 49 | DEFAULT_TEST_SUITES_pn-core-image-sato = "${NETTESTSUITE} connman xorg parselogs ${RPMTESTSUITE} \ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | ${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'python', '', d)}" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 51 | DEFAULT_TEST_SUITES_pn-core-image-sato-sdk = "${NETTESTSUITE} connman xorg perl python \ |
| 52 | ${DEVTESTSUITE} parselogs ${RPMTESTSUITE}" |
| 53 | DEFAULT_TEST_SUITES_pn-core-image-lsb-dev = "${NETTESTSUITE} pam perl python parselogs ${RPMTESTSUITE}" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 54 | DEFAULT_TEST_SUITES_pn-core-image-lsb-sdk = "${NETTESTSUITE} buildcvs buildiptables buildgalculator \ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 55 | connman ${DEVTESTSUITE} pam perl python parselogs ${RPMTESTSUITE}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | DEFAULT_TEST_SUITES_pn-meta-toolchain = "auto" |
| 57 | |
| 58 | # aarch64 has no graphics |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 59 | DEFAULT_TEST_SUITES_remove_aarch64 = "xorg" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 61 | # qemumips is quite slow and has reached the timeout limit several times on the YP build cluster, |
| 62 | # mitigate this by removing build tests for qemumips machines. |
| 63 | MIPSREMOVE ??= "buildcvs buildiptables buildgalculator" |
| 64 | DEFAULT_TEST_SUITES_remove_qemumips = "${MIPSREMOVE}" |
| 65 | DEFAULT_TEST_SUITES_remove_qemumips64 = "${MIPSREMOVE}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | |
| 67 | TEST_SUITES ?= "${DEFAULT_TEST_SUITES}" |
| 68 | |
| 69 | TEST_QEMUBOOT_TIMEOUT ?= "1000" |
| 70 | TEST_TARGET ?= "qemu" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | |
| 72 | TESTIMAGEDEPENDS = "" |
| 73 | TESTIMAGEDEPENDS_qemuall = "qemu-native:do_populate_sysroot qemu-helper-native:do_populate_sysroot" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 74 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'cpio-native:do_populate_sysroot', '', d)}" |
| 75 | TESTIMAGEDEPENDS_qemuall += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'cpio-native:do_populate_sysroot', '', d)}" |
| 76 | TESTIMAGEDEPENDS_qemuall += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'createrepo-native:do_populate_sysroot', '', d)}" |
| 77 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'python-smartpm-native:do_populate_sysroot', '', d)}" |
| 78 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'ipk', 'opkg-utils-native:do_populate_sysroot', '', d)}" |
| 79 | TESTIMAGEDEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'deb', 'apt-native:do_populate_sysroot', '', d)}" |
| 80 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | |
| 82 | TESTIMAGELOCK = "${TMPDIR}/testimage.lock" |
| 83 | TESTIMAGELOCK_qemuall = "" |
| 84 | |
| 85 | TESTIMAGE_DUMP_DIR ?= "/tmp/oe-saved-tests/" |
| 86 | |
| 87 | testimage_dump_target () { |
| 88 | top -bn1 |
| 89 | ps |
| 90 | free |
| 91 | df |
| 92 | # The next command will export the default gateway IP |
| 93 | export DEFAULT_GATEWAY=$(ip route | awk '/default/ { print $3}') |
| 94 | ping -c3 $DEFAULT_GATEWAY |
| 95 | dmesg |
| 96 | netstat -an |
| 97 | ip address |
| 98 | # Next command will dump logs from /var/log/ |
| 99 | find /var/log/ -type f 2>/dev/null -exec echo "====================" \; -exec echo {} \; -exec echo "====================" \; -exec cat {} \; -exec echo "" \; |
| 100 | } |
| 101 | |
| 102 | testimage_dump_host () { |
| 103 | top -bn1 |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 104 | iostat -x -z -N -d -p ALL 20 2 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | ps -ef |
| 106 | free |
| 107 | df |
| 108 | memstat |
| 109 | dmesg |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 110 | ip -s link |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | netstat -an |
| 112 | } |
| 113 | |
| 114 | python do_testimage() { |
| 115 | testimage_main(d) |
| 116 | } |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 117 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 118 | addtask testimage |
| 119 | do_testimage[nostamp] = "1" |
| 120 | do_testimage[depends] += "${TESTIMAGEDEPENDS}" |
| 121 | do_testimage[lockfiles] += "${TESTIMAGELOCK}" |
| 122 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 123 | def testimage_main(d): |
| 124 | import unittest |
| 125 | import os |
| 126 | import oeqa.runtime |
| 127 | import time |
| 128 | import signal |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 129 | from oeqa.oetest import ImageTestContext |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 130 | from oeqa.targetcontrol import get_target_controller |
| 131 | from oeqa.utils.dump import get_host_dumper |
| 132 | |
| 133 | pn = d.getVar("PN", True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 134 | bb.utils.mkdirhier(d.getVar("TEST_LOG_DIR", True)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 135 | test_create_extract_dirs(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 136 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 137 | # we need the host dumper in test context |
| 138 | host_dumper = get_host_dumper(d) |
| 139 | |
| 140 | # the robot dance |
| 141 | target = get_target_controller(d) |
| 142 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 143 | # test context |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 144 | tc = ImageTestContext(d, target, host_dumper) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 145 | |
| 146 | # this is a dummy load of tests |
| 147 | # we are doing that to find compile errors in the tests themselves |
| 148 | # before booting the image |
| 149 | try: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 150 | tc.loadTests() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | except Exception as e: |
| 152 | import traceback |
| 153 | bb.fatal("Loading tests failed:\n%s" % traceback.format_exc()) |
| 154 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 155 | tc.extract_packages() |
| 156 | target.deploy() |
| 157 | try: |
| 158 | bootparams = None |
| 159 | if d.getVar('VIRTUAL-RUNTIME_init_manager', '') == 'systemd': |
| 160 | bootparams = 'systemd.log_level=debug systemd.log_target=console' |
| 161 | target.start(extra_bootparams=bootparams) |
| 162 | starttime = time.time() |
| 163 | result = tc.runTests() |
| 164 | stoptime = time.time() |
| 165 | if result.wasSuccessful(): |
| 166 | bb.plain("%s - Ran %d test%s in %.3fs" % (pn, result.testsRun, result.testsRun != 1 and "s" or "", stoptime - starttime)) |
| 167 | msg = "%s - OK - All required tests passed" % pn |
| 168 | skipped = len(result.skipped) |
| 169 | if skipped: |
| 170 | msg += " (skipped=%d)" % skipped |
| 171 | bb.plain(msg) |
| 172 | else: |
| 173 | bb.fatal("%s - FAILED - check the task log and the ssh log" % pn) |
| 174 | finally: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 175 | signal.signal(signal.SIGTERM, tc.origsigtermhandler) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 176 | target.stop() |
| 177 | |
| 178 | def test_create_extract_dirs(d): |
| 179 | install_path = d.getVar("TEST_INSTALL_TMP_DIR", True) |
| 180 | package_path = d.getVar("TEST_PACKAGED_DIR", True) |
| 181 | extracted_path = d.getVar("TEST_EXTRACTED_DIR", True) |
| 182 | bb.utils.mkdirhier(d.getVar("TEST_LOG_DIR", True)) |
| 183 | bb.utils.remove(package_path, recurse=True) |
| 184 | bb.utils.mkdirhier(install_path) |
| 185 | bb.utils.mkdirhier(package_path) |
| 186 | bb.utils.mkdirhier(extracted_path) |
| 187 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 188 | |
| 189 | testimage_main[vardepsexclude] =+ "BB_ORIGENV" |
| 190 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 191 | inherit testsdk |