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