blob: 572f5d9e7680f9d830ae43d855b07920960d07e5 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001# Copyright (C) 2016 Intel Corporation
2#
Patrick Williams92b42cb2022-09-03 06:53:57 -05003# SPDX-License-Identifier: MIT
Patrick Williamsc0f7c042017-02-23 20:41:17 -06004#
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 Williams7784c422022-11-17 07:29:11 -060010# - Add IMAGE_CLASSES += "testexport" in local.conf
Patrick Williamsc0f7c042017-02-23 20:41:17 -060011# - 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 Williams7784c422022-11-17 07:29:11 -060016inherit testimage
17
Patrick Williamsc0f7c042017-02-23 20:41:17 -060018TEST_LOG_DIR ?= "${WORKDIR}/testexport"
19TEST_EXPORT_DIR ?= "${TMPDIR}/testexport/${PN}"
20TEST_EXPORT_PACKAGED_DIR ?= "packages/packaged"
21TEST_EXPORT_EXTRACTED_DIR ?= "packages/extracted"
22
23TEST_TARGET ?= "simpleremote"
24TEST_TARGET_IP ?= ""
25TEST_SERVER_IP ?= ""
26
Patrick Williams92b42cb2022-09-03 06:53:57 -050027require conf/testexport.conf
28
Patrick Williamsc0f7c042017-02-23 20:41:17 -060029TEST_EXPORT_SDK_ENABLED ?= "0"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060030
31TEST_EXPORT_DEPENDS = ""
32TEST_EXPORT_DEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'cpio-native:do_populate_sysroot', '', d)}"
33TEST_EXPORT_DEPENDS += "${@bb.utils.contains('TEST_EXPORT_SDK_ENABLED', '1', 'testexport-tarball:do_populate_sdk', '', d)}"
34TEST_EXPORT_LOCK = "${TMPDIR}/testimage.lock"
35
Patrick Williamsc0f7c042017-02-23 20:41:17 -060036addtask testexport
37do_testexport[nostamp] = "1"
38do_testexport[depends] += "${TEST_EXPORT_DEPENDS} ${TESTIMAGEDEPENDS}"
39do_testexport[lockfiles] += "${TEST_EXPORT_LOCK}"
40
Brad Bishop6e60e8b2018-02-01 10:27:11 -050041python do_testexport() {
42 testexport_main(d)
43}
44
45def testexport_main(d):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060046 import json
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 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 Bishop6e60e8b2018-02-01 10:27:11 -050064 image_manifest = "%s.manifest" % image_name
65 image_packages = OERuntimeTestContextExecutor.readPackagesManifest(image_manifest)
66
67 extract_dir = d.getVar("TEST_EXTRACTED_DIR")
68
Andrew Geissler8f840682023-07-21 09:09:43 -050069 tc = OERuntimeTestContext(td, logger, target, image_packages, extract_dir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050070
71 copy_needed_files(d, tc)
72
73def copy_needed_files(d, tc):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060074 import shutil
Patrick Williamsc0f7c042017-02-23 20:41:17 -060075 import oe.path
76
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077 from oeqa.utils.package_manager import _get_json_file
78 from oeqa.core.utils.test import getSuiteCasesFiles
Patrick Williamsc0f7c042017-02-23 20:41:17 -060079
Brad Bishop6e60e8b2018-02-01 10:27:11 -050080 export_path = d.getVar('TEST_EXPORT_DIR')
81 corebase_path = d.getVar('COREBASE')
Patrick Williamsc0f7c042017-02-23 20:41:17 -060082
Brad Bishop6e60e8b2018-02-01 10:27:11 -050083 # Clean everything before starting
84 oe.path.remove(export_path)
85 bb.utils.mkdirhier(os.path.join(export_path, 'lib', 'oeqa'))
Patrick Williamsc0f7c042017-02-23 20:41:17 -060086
Brad Bishop6e60e8b2018-02-01 10:27:11 -050087 # 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 Williamsc0f7c042017-02-23 20:41:17 -060098
Brad Bishop6e60e8b2018-02-01 10:27:11 -050099 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 Williamsc0f7c042017-02-23 20:41:17 -0600106
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500107 # 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 Bishopd7bf8c12018-02-25 22:55:05 -0500112 test_modules = d.getVar('TEST_SUITES').split()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500113 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 Williamsc0f7c042017-02-23 20:41:17 -0600129
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800130 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 Williamsc0f7c042017-02-23 20:41:17 -0600135 # Create tar file for common parts of testexport
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500136 testexport_create_tarball(d, "testexport.tar.gz", d.getVar("TEST_EXPORT_DIR"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600137
138 # Copy packages needed for runtime testing
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500139 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 Williamsc0f7c042017-02-23 20:41:17 -0600143 oe.path.copytree(test_pkg_dir, export_pkg_dir)
144 # Create tar file for packages needed by the DUT
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500145 testexport_create_tarball(d, "testexport_packages_%s.tar.gz" % d.getVar("MACHINE"), export_pkg_dir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600146
147 # Copy SDK
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500148 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 Williamsc0f7c042017-02-23 20:41:17 -0600151 tarball_path = os.path.join(sdk_deploy, tarball_name)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500152 export_sdk_dir = os.path.join(d.getVar("TEST_EXPORT_DIR"),
153 d.getVar("TEST_EXPORT_SDK_DIR"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600154 bb.utils.mkdirhier(export_sdk_dir)
155 shutil.copy2(tarball_path, export_sdk_dir)
156
157 # Create tar file for the sdk
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500158 testexport_create_tarball(d, "testexport_sdk_%s.tar.gz" % d.getVar("SDK_ARCH"), export_sdk_dir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600159
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500160 bb.plain("Exported tests to: %s" % export_path)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600161
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500162def testexport_create_tarball(d, tar_name, src_dir):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600163
164 import tarfile
165
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500166 tar_path = os.path.join(d.getVar("TEST_EXPORT_DIR"), tar_name)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600167 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)