blob: f7c5242dc5eef846cddb5f77df69286ed46ae2ed [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
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
16TEST_LOG_DIR ?= "${WORKDIR}/testexport"
17TEST_EXPORT_DIR ?= "${TMPDIR}/testexport/${PN}"
18TEST_EXPORT_PACKAGED_DIR ?= "packages/packaged"
19TEST_EXPORT_EXTRACTED_DIR ?= "packages/extracted"
20
21TEST_TARGET ?= "simpleremote"
22TEST_TARGET_IP ?= ""
23TEST_SERVER_IP ?= ""
24
Patrick Williams92b42cb2022-09-03 06:53:57 -050025require conf/testexport.conf
26
Patrick Williamsc0f7c042017-02-23 20:41:17 -060027TEST_EXPORT_SDK_ENABLED ?= "0"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060028
29TEST_EXPORT_DEPENDS = ""
30TEST_EXPORT_DEPENDS += "${@bb.utils.contains('IMAGE_PKGTYPE', 'rpm', 'cpio-native:do_populate_sysroot', '', d)}"
31TEST_EXPORT_DEPENDS += "${@bb.utils.contains('TEST_EXPORT_SDK_ENABLED', '1', 'testexport-tarball:do_populate_sdk', '', d)}"
32TEST_EXPORT_LOCK = "${TMPDIR}/testimage.lock"
33
Patrick Williamsc0f7c042017-02-23 20:41:17 -060034addtask testexport
35do_testexport[nostamp] = "1"
36do_testexport[depends] += "${TEST_EXPORT_DEPENDS} ${TESTIMAGEDEPENDS}"
37do_testexport[lockfiles] += "${TEST_EXPORT_LOCK}"
38
Brad Bishop6e60e8b2018-02-01 10:27:11 -050039python do_testexport() {
40 testexport_main(d)
41}
42
43def testexport_main(d):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060044 import json
Brad Bishop6e60e8b2018-02-01 10:27:11 -050045 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
75def copy_needed_files(d, tc):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060076 import shutil
Patrick Williamsc0f7c042017-02-23 20:41:17 -060077 import oe.path
78
Brad Bishop6e60e8b2018-02-01 10:27:11 -050079 from oeqa.utils.package_manager import _get_json_file
80 from oeqa.core.utils.test import getSuiteCasesFiles
Patrick Williamsc0f7c042017-02-23 20:41:17 -060081
Brad Bishop6e60e8b2018-02-01 10:27:11 -050082 export_path = d.getVar('TEST_EXPORT_DIR')
83 corebase_path = d.getVar('COREBASE')
Patrick Williamsc0f7c042017-02-23 20:41:17 -060084
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085 # Clean everything before starting
86 oe.path.remove(export_path)
87 bb.utils.mkdirhier(os.path.join(export_path, 'lib', 'oeqa'))
Patrick Williamsc0f7c042017-02-23 20:41:17 -060088
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 # 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 Williamsc0f7c042017-02-23 20:41:17 -0600100
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500101 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 Williamsc0f7c042017-02-23 20:41:17 -0600108
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 # 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 Bishopd7bf8c12018-02-25 22:55:05 -0500114 test_modules = d.getVar('TEST_SUITES').split()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115 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 Williamsc0f7c042017-02-23 20:41:17 -0600131
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800132 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 Williamsc0f7c042017-02-23 20:41:17 -0600137 # Create tar file for common parts of testexport
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500138 testexport_create_tarball(d, "testexport.tar.gz", d.getVar("TEST_EXPORT_DIR"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600139
140 # Copy packages needed for runtime testing
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500141 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 Williamsc0f7c042017-02-23 20:41:17 -0600145 oe.path.copytree(test_pkg_dir, export_pkg_dir)
146 # Create tar file for packages needed by the DUT
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500147 testexport_create_tarball(d, "testexport_packages_%s.tar.gz" % d.getVar("MACHINE"), export_pkg_dir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600148
149 # Copy SDK
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500150 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 Williamsc0f7c042017-02-23 20:41:17 -0600153 tarball_path = os.path.join(sdk_deploy, tarball_name)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500154 export_sdk_dir = os.path.join(d.getVar("TEST_EXPORT_DIR"),
155 d.getVar("TEST_EXPORT_SDK_DIR"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600156 bb.utils.mkdirhier(export_sdk_dir)
157 shutil.copy2(tarball_path, export_sdk_dir)
158
159 # Create tar file for the sdk
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500160 testexport_create_tarball(d, "testexport_sdk_%s.tar.gz" % d.getVar("SDK_ARCH"), export_sdk_dir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600161
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500162 bb.plain("Exported tests to: %s" % export_path)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600163
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500164def testexport_create_tarball(d, tar_name, src_dir):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600165
166 import tarfile
167
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500168 tar_path = os.path.join(d.getVar("TEST_EXPORT_DIR"), tar_name)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600169 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 Williams92b42cb2022-09-03 06:53:57 -0500180IMAGE_CLASSES += "testimage"