Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | # Copyright (C) 2016 Intel Corporation |
| 2 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 3 | # SPDX-License-Identifier: MIT |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 4 | # |
| 5 | # testexport.bbclass allows to execute runtime test outside OE environment. |
| 6 | # Most of the tests are commands run on target image over ssh. |
| 7 | # To use it add testexport to global inherit and call your target image with -c testexport |
| 8 | # You can try it out like this: |
| 9 | # - First build an image. i.e. core-image-sato |
| 10 | # - Add INHERIT += "testexport" in local.conf |
| 11 | # - Then bitbake core-image-sato -c testexport. That will generate the directory structure |
| 12 | # to execute the runtime tests using runexported.py. |
| 13 | # |
| 14 | # For more information on TEST_SUITES check testimage class. |
| 15 | |
| 16 | TEST_LOG_DIR ?= "${WORKDIR}/testexport" |
| 17 | TEST_EXPORT_DIR ?= "${TMPDIR}/testexport/${PN}" |
| 18 | TEST_EXPORT_PACKAGED_DIR ?= "packages/packaged" |
| 19 | TEST_EXPORT_EXTRACTED_DIR ?= "packages/extracted" |
| 20 | |
| 21 | TEST_TARGET ?= "simpleremote" |
| 22 | TEST_TARGET_IP ?= "" |
| 23 | TEST_SERVER_IP ?= "" |
| 24 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 25 | require conf/testexport.conf |
| 26 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 27 | TEST_EXPORT_SDK_ENABLED ?= "0" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 28 | |
| 29 | TEST_EXPORT_DEPENDS = "" |
| 30 | TEST_EXPORT_DEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'cpio-native:do_populate_sysroot', '', d)}" |
| 31 | TEST_EXPORT_DEPENDS += "${@bb.utils.contains('TEST_EXPORT_SDK_ENABLED', '1', 'testexport-tarball:do_populate_sdk', '', d)}" |
| 32 | TEST_EXPORT_LOCK = "${TMPDIR}/testimage.lock" |
| 33 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 34 | addtask testexport |
| 35 | do_testexport[nostamp] = "1" |
| 36 | do_testexport[depends] += "${TEST_EXPORT_DEPENDS} ${TESTIMAGEDEPENDS}" |
| 37 | do_testexport[lockfiles] += "${TEST_EXPORT_LOCK}" |
| 38 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 39 | python do_testexport() { |
| 40 | testexport_main(d) |
| 41 | } |
| 42 | |
| 43 | def testexport_main(d): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 44 | import json |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 45 | import logging |
| 46 | |
| 47 | from oeqa.runtime.context import OERuntimeTestContext |
| 48 | from oeqa.runtime.context import OERuntimeTestContextExecutor |
| 49 | |
| 50 | image_name = ("%s/%s" % (d.getVar('DEPLOY_DIR_IMAGE'), |
| 51 | d.getVar('IMAGE_LINK_NAME'))) |
| 52 | |
| 53 | tdname = "%s.testdata.json" % image_name |
| 54 | td = json.load(open(tdname, "r")) |
| 55 | |
| 56 | logger = logging.getLogger("BitBake") |
| 57 | |
| 58 | target = OERuntimeTestContextExecutor.getTarget( |
| 59 | d.getVar("TEST_TARGET"), None, d.getVar("TEST_TARGET_IP"), |
| 60 | d.getVar("TEST_SERVER_IP")) |
| 61 | |
| 62 | host_dumper = OERuntimeTestContextExecutor.getHostDumper( |
| 63 | d.getVar("testimage_dump_host"), d.getVar("TESTIMAGE_DUMP_DIR")) |
| 64 | |
| 65 | image_manifest = "%s.manifest" % image_name |
| 66 | image_packages = OERuntimeTestContextExecutor.readPackagesManifest(image_manifest) |
| 67 | |
| 68 | extract_dir = d.getVar("TEST_EXTRACTED_DIR") |
| 69 | |
| 70 | tc = OERuntimeTestContext(td, logger, target, host_dumper, |
| 71 | image_packages, extract_dir) |
| 72 | |
| 73 | copy_needed_files(d, tc) |
| 74 | |
| 75 | def copy_needed_files(d, tc): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 76 | import shutil |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 77 | import oe.path |
| 78 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 79 | from oeqa.utils.package_manager import _get_json_file |
| 80 | from oeqa.core.utils.test import getSuiteCasesFiles |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 81 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 82 | export_path = d.getVar('TEST_EXPORT_DIR') |
| 83 | corebase_path = d.getVar('COREBASE') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 84 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 85 | # Clean everything before starting |
| 86 | oe.path.remove(export_path) |
| 87 | bb.utils.mkdirhier(os.path.join(export_path, 'lib', 'oeqa')) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 88 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 89 | # The source of files to copy are relative to 'COREBASE' directory |
| 90 | # The destination is relative to 'TEST_EXPORT_DIR' |
| 91 | # Because we are squashing the libraries, we need to remove |
| 92 | # the layer/script directory |
| 93 | files_to_copy = [ os.path.join('meta', 'lib', 'oeqa', 'core'), |
| 94 | os.path.join('meta', 'lib', 'oeqa', 'runtime'), |
| 95 | os.path.join('meta', 'lib', 'oeqa', 'files'), |
| 96 | os.path.join('meta', 'lib', 'oeqa', 'utils'), |
| 97 | os.path.join('scripts', 'oe-test'), |
| 98 | os.path.join('scripts', 'lib', 'argparse_oe.py'), |
| 99 | os.path.join('scripts', 'lib', 'scriptutils.py'), ] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 100 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 101 | for f in files_to_copy: |
| 102 | src = os.path.join(corebase_path, f) |
| 103 | dst = os.path.join(export_path, f.split('/', 1)[-1]) |
| 104 | if os.path.isdir(src): |
| 105 | oe.path.copytree(src, dst) |
| 106 | else: |
| 107 | shutil.copy2(src, dst) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 108 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 109 | # Remove cases and just copy the ones specified |
| 110 | cases_path = os.path.join(export_path, 'lib', 'oeqa', 'runtime', 'cases') |
| 111 | oe.path.remove(cases_path) |
| 112 | bb.utils.mkdirhier(cases_path) |
| 113 | test_paths = get_runtime_paths(d) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 114 | test_modules = d.getVar('TEST_SUITES').split() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 115 | tc.loadTests(test_paths, modules=test_modules) |
| 116 | for f in getSuiteCasesFiles(tc.suites): |
| 117 | shutil.copy2(f, cases_path) |
| 118 | json_file = _get_json_file(f) |
| 119 | if json_file: |
| 120 | shutil.copy2(json_file, cases_path) |
| 121 | |
| 122 | # Copy test data |
| 123 | image_name = ("%s/%s" % (d.getVar('DEPLOY_DIR_IMAGE'), |
| 124 | d.getVar('IMAGE_LINK_NAME'))) |
| 125 | image_manifest = "%s.manifest" % image_name |
| 126 | tdname = "%s.testdata.json" % image_name |
| 127 | test_data_path = os.path.join(export_path, 'data') |
| 128 | bb.utils.mkdirhier(test_data_path) |
| 129 | shutil.copy2(image_manifest, os.path.join(test_data_path, 'manifest')) |
| 130 | shutil.copy2(tdname, os.path.join(test_data_path, 'testdata.json')) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 131 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 132 | for subdir, dirs, files in os.walk(export_path): |
| 133 | for dir in dirs: |
| 134 | if dir == '__pycache__': |
| 135 | shutil.rmtree(os.path.join(subdir, dir)) |
| 136 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 137 | # Create tar file for common parts of testexport |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 138 | testexport_create_tarball(d, "testexport.tar.gz", d.getVar("TEST_EXPORT_DIR")) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 139 | |
| 140 | # Copy packages needed for runtime testing |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 141 | package_extraction(d, tc.suites) |
| 142 | test_pkg_dir = d.getVar("TEST_NEEDED_PACKAGES_DIR") |
| 143 | if os.path.isdir(test_pkg_dir) and os.listdir(test_pkg_dir): |
| 144 | export_pkg_dir = os.path.join(d.getVar("TEST_EXPORT_DIR"), "packages") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 145 | oe.path.copytree(test_pkg_dir, export_pkg_dir) |
| 146 | # Create tar file for packages needed by the DUT |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 147 | testexport_create_tarball(d, "testexport_packages_%s.tar.gz" % d.getVar("MACHINE"), export_pkg_dir) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 148 | |
| 149 | # Copy SDK |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 150 | if d.getVar("TEST_EXPORT_SDK_ENABLED") == "1": |
| 151 | sdk_deploy = d.getVar("SDK_DEPLOY") |
| 152 | tarball_name = "%s.sh" % d.getVar("TEST_EXPORT_SDK_NAME") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 153 | tarball_path = os.path.join(sdk_deploy, tarball_name) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 154 | export_sdk_dir = os.path.join(d.getVar("TEST_EXPORT_DIR"), |
| 155 | d.getVar("TEST_EXPORT_SDK_DIR")) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 156 | bb.utils.mkdirhier(export_sdk_dir) |
| 157 | shutil.copy2(tarball_path, export_sdk_dir) |
| 158 | |
| 159 | # Create tar file for the sdk |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 160 | testexport_create_tarball(d, "testexport_sdk_%s.tar.gz" % d.getVar("SDK_ARCH"), export_sdk_dir) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 161 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 162 | bb.plain("Exported tests to: %s" % export_path) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 163 | |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 164 | def testexport_create_tarball(d, tar_name, src_dir): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 165 | |
| 166 | import tarfile |
| 167 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 168 | tar_path = os.path.join(d.getVar("TEST_EXPORT_DIR"), tar_name) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 169 | current_dir = os.getcwd() |
| 170 | src_dir = src_dir.rstrip('/') |
| 171 | dir_name = os.path.dirname(src_dir) |
| 172 | base_name = os.path.basename(src_dir) |
| 173 | |
| 174 | os.chdir(dir_name) |
| 175 | tar = tarfile.open(tar_path, "w:gz") |
| 176 | tar.add(base_name) |
| 177 | tar.close() |
| 178 | os.chdir(current_dir) |
| 179 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 180 | IMAGE_CLASSES += "testimage" |