blob: 59cbaefbf9fcec01f411b9517b84ea770fd8ad4b [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001# Copyright (C) 2016 Intel Corporation
2#
3# Released under the MIT license (see COPYING.MIT)
4#
5#
6# testexport.bbclass allows to execute runtime test outside OE environment.
7# Most of the tests are commands run on target image over ssh.
8# To use it add testexport to global inherit and call your target image with -c testexport
9# You can try it out like this:
10# - First build an image. i.e. core-image-sato
11# - Add INHERIT += "testexport" in local.conf
12# - Then bitbake core-image-sato -c testexport. That will generate the directory structure
13# to execute the runtime tests using runexported.py.
14#
15# For more information on TEST_SUITES check testimage class.
16
17TEST_LOG_DIR ?= "${WORKDIR}/testexport"
18TEST_EXPORT_DIR ?= "${TMPDIR}/testexport/${PN}"
19TEST_EXPORT_PACKAGED_DIR ?= "packages/packaged"
20TEST_EXPORT_EXTRACTED_DIR ?= "packages/extracted"
21
22TEST_TARGET ?= "simpleremote"
23TEST_TARGET_IP ?= ""
24TEST_SERVER_IP ?= ""
25
26TEST_EXPORT_SDK_PACKAGES ?= ""
27TEST_EXPORT_SDK_ENABLED ?= "0"
28TEST_EXPORT_SDK_NAME ?= "testexport-tools-nativesdk"
29TEST_EXPORT_SDK_DIR ?= "sdk"
30
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
64 host_dumper = OERuntimeTestContextExecutor.getHostDumper(
65 d.getVar("testimage_dump_host"), d.getVar("TESTIMAGE_DUMP_DIR"))
66
67 image_manifest = "%s.manifest" % image_name
68 image_packages = OERuntimeTestContextExecutor.readPackagesManifest(image_manifest)
69
70 extract_dir = d.getVar("TEST_EXTRACTED_DIR")
71
72 tc = OERuntimeTestContext(td, logger, target, host_dumper,
73 image_packages, extract_dir)
74
75 copy_needed_files(d, tc)
76
77def copy_needed_files(d, tc):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060078 import shutil
Patrick Williamsc0f7c042017-02-23 20:41:17 -060079 import oe.path
80
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081 from oeqa.utils.package_manager import _get_json_file
82 from oeqa.core.utils.test import getSuiteCasesFiles
Patrick Williamsc0f7c042017-02-23 20:41:17 -060083
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084 export_path = d.getVar('TEST_EXPORT_DIR')
85 corebase_path = d.getVar('COREBASE')
Patrick Williamsc0f7c042017-02-23 20:41:17 -060086
Brad Bishop6e60e8b2018-02-01 10:27:11 -050087 # Clean everything before starting
88 oe.path.remove(export_path)
89 bb.utils.mkdirhier(os.path.join(export_path, 'lib', 'oeqa'))
Patrick Williamsc0f7c042017-02-23 20:41:17 -060090
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 # The source of files to copy are relative to 'COREBASE' directory
92 # The destination is relative to 'TEST_EXPORT_DIR'
93 # Because we are squashing the libraries, we need to remove
94 # the layer/script directory
95 files_to_copy = [ os.path.join('meta', 'lib', 'oeqa', 'core'),
96 os.path.join('meta', 'lib', 'oeqa', 'runtime'),
97 os.path.join('meta', 'lib', 'oeqa', 'files'),
98 os.path.join('meta', 'lib', 'oeqa', 'utils'),
99 os.path.join('scripts', 'oe-test'),
100 os.path.join('scripts', 'lib', 'argparse_oe.py'),
101 os.path.join('scripts', 'lib', 'scriptutils.py'), ]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600102
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103 for f in files_to_copy:
104 src = os.path.join(corebase_path, f)
105 dst = os.path.join(export_path, f.split('/', 1)[-1])
106 if os.path.isdir(src):
107 oe.path.copytree(src, dst)
108 else:
109 shutil.copy2(src, dst)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600110
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500111 # Remove cases and just copy the ones specified
112 cases_path = os.path.join(export_path, 'lib', 'oeqa', 'runtime', 'cases')
113 oe.path.remove(cases_path)
114 bb.utils.mkdirhier(cases_path)
115 test_paths = get_runtime_paths(d)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500116 test_modules = d.getVar('TEST_SUITES').split()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 tc.loadTests(test_paths, modules=test_modules)
118 for f in getSuiteCasesFiles(tc.suites):
119 shutil.copy2(f, cases_path)
120 json_file = _get_json_file(f)
121 if json_file:
122 shutil.copy2(json_file, cases_path)
123
124 # Copy test data
125 image_name = ("%s/%s" % (d.getVar('DEPLOY_DIR_IMAGE'),
126 d.getVar('IMAGE_LINK_NAME')))
127 image_manifest = "%s.manifest" % image_name
128 tdname = "%s.testdata.json" % image_name
129 test_data_path = os.path.join(export_path, 'data')
130 bb.utils.mkdirhier(test_data_path)
131 shutil.copy2(image_manifest, os.path.join(test_data_path, 'manifest'))
132 shutil.copy2(tdname, os.path.join(test_data_path, 'testdata.json'))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600133
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800134 for subdir, dirs, files in os.walk(export_path):
135 for dir in dirs:
136 if dir == '__pycache__':
137 shutil.rmtree(os.path.join(subdir, dir))
138
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600139 # Create tar file for common parts of testexport
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500140 create_tarball(d, "testexport.tar.gz", d.getVar("TEST_EXPORT_DIR"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600141
142 # Copy packages needed for runtime testing
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500143 package_extraction(d, tc.suites)
144 test_pkg_dir = d.getVar("TEST_NEEDED_PACKAGES_DIR")
145 if os.path.isdir(test_pkg_dir) and os.listdir(test_pkg_dir):
146 export_pkg_dir = os.path.join(d.getVar("TEST_EXPORT_DIR"), "packages")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600147 oe.path.copytree(test_pkg_dir, export_pkg_dir)
148 # Create tar file for packages needed by the DUT
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500149 create_tarball(d, "testexport_packages_%s.tar.gz" % d.getVar("MACHINE"), export_pkg_dir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600150
151 # Copy SDK
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500152 if d.getVar("TEST_EXPORT_SDK_ENABLED") == "1":
153 sdk_deploy = d.getVar("SDK_DEPLOY")
154 tarball_name = "%s.sh" % d.getVar("TEST_EXPORT_SDK_NAME")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600155 tarball_path = os.path.join(sdk_deploy, tarball_name)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500156 export_sdk_dir = os.path.join(d.getVar("TEST_EXPORT_DIR"),
157 d.getVar("TEST_EXPORT_SDK_DIR"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600158 bb.utils.mkdirhier(export_sdk_dir)
159 shutil.copy2(tarball_path, export_sdk_dir)
160
161 # Create tar file for the sdk
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500162 create_tarball(d, "testexport_sdk_%s.tar.gz" % d.getVar("SDK_ARCH"), export_sdk_dir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600163
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164 bb.plain("Exported tests to: %s" % export_path)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600165
166def create_tarball(d, tar_name, src_dir):
167
168 import tarfile
169
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500170 tar_path = os.path.join(d.getVar("TEST_EXPORT_DIR"), tar_name)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600171 current_dir = os.getcwd()
172 src_dir = src_dir.rstrip('/')
173 dir_name = os.path.dirname(src_dir)
174 base_name = os.path.basename(src_dir)
175
176 os.chdir(dir_name)
177 tar = tarfile.open(tar_path, "w:gz")
178 tar.add(base_name)
179 tar.close()
180 os.chdir(current_dir)
181
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600182inherit testimage