1# 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 36addtask testexport 37do_testexport[nostamp] = "1" 38do_testexport[depends] += "${TEST_EXPORT_DEPENDS} ${TESTIMAGEDEPENDS}" 39do_testexport[lockfiles] += "${TEST_EXPORT_LOCK}" 40 41python do_testexport() { 42 testexport_main(d) 43} 44 45def testexport_main(d): 46 import json 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 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): 78 import shutil 79 import oe.path 80 81 from oeqa.utils.package_manager import _get_json_file 82 from oeqa.core.utils.test import getSuiteCasesFiles 83 84 export_path = d.getVar('TEST_EXPORT_DIR') 85 corebase_path = d.getVar('COREBASE') 86 87 # Clean everything before starting 88 oe.path.remove(export_path) 89 bb.utils.mkdirhier(os.path.join(export_path, 'lib', 'oeqa')) 90 91 # 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'), ] 102 103 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) 110 111 # 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) 116 test_modules = d.getVar('TEST_SUITES').split() 117 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')) 133 134 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 139 # Create tar file for common parts of testexport 140 testexport_create_tarball(d, "testexport.tar.gz", d.getVar("TEST_EXPORT_DIR")) 141 142 # Copy packages needed for runtime testing 143 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") 147 oe.path.copytree(test_pkg_dir, export_pkg_dir) 148 # Create tar file for packages needed by the DUT 149 testexport_create_tarball(d, "testexport_packages_%s.tar.gz" % d.getVar("MACHINE"), export_pkg_dir) 150 151 # Copy SDK 152 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") 155 tarball_path = os.path.join(sdk_deploy, tarball_name) 156 export_sdk_dir = os.path.join(d.getVar("TEST_EXPORT_DIR"), 157 d.getVar("TEST_EXPORT_SDK_DIR")) 158 bb.utils.mkdirhier(export_sdk_dir) 159 shutil.copy2(tarball_path, export_sdk_dir) 160 161 # Create tar file for the sdk 162 testexport_create_tarball(d, "testexport_sdk_%s.tar.gz" % d.getVar("SDK_ARCH"), export_sdk_dir) 163 164 bb.plain("Exported tests to: %s" % export_path) 165 166def testexport_create_tarball(d, tar_name, src_dir): 167 168 import tarfile 169 170 tar_path = os.path.join(d.getVar("TEST_EXPORT_DIR"), tar_name) 171 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 182inherit testimage 183