blob: 458c3f40b04281d8152956200982c33db4f2151f [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001# Copyright (C) 2013 - 2016 Intel Corporation
2#
3# Released under the MIT license (see COPYING.MIT)
4
5# testsdk.bbclass enables testing for SDK and Extensible SDK
6#
Brad Bishop37a0e4d2017-12-04 01:01:44 -05007# To run SDK tests, run the commands:
8# $ bitbake <image-name> -c populate_sdk
9# $ bitbake <image-name> -c testsdk
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050010#
Brad Bishop37a0e4d2017-12-04 01:01:44 -050011# To run eSDK tests, run the commands:
12# $ bitbake <image-name> -c populate_sdk_ext
13# $ bitbake <image-name> -c testsdkext
14#
15# where "<image-name>" is an image like core-image-sato.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050016
Brad Bishopf86d0552018-12-04 14:18:15 -080017def get_sdk_configuration(d, test_type):
18 import platform
19 from oeqa.utils.metadata import get_layers
20 configuration = {'TEST_TYPE': test_type,
21 'MACHINE': d.getVar("MACHINE"),
22 'SDKMACHINE': d.getVar("SDKMACHINE"),
23 'IMAGE_BASENAME': d.getVar("IMAGE_BASENAME"),
24 'IMAGE_PKGTYPE': d.getVar("IMAGE_PKGTYPE"),
25 'STARTTIME': d.getVar("DATETIME"),
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080026 'HOST_DISTRO': oe.lsb.distro_identifier().replace(' ', '-'),
Brad Bishopf86d0552018-12-04 14:18:15 -080027 'LAYERS': get_layers(d.getVar("BBLAYERS"))}
28 return configuration
29get_sdk_configuration[vardepsexclude] = "DATETIME"
30
31def get_sdk_json_result_dir(d):
32 json_result_dir = os.path.join(d.getVar("LOG_DIR"), 'oeqa')
33 custom_json_result_dir = d.getVar("OEQA_JSON_RESULT_DIR")
34 if custom_json_result_dir:
35 json_result_dir = custom_json_result_dir
36 return json_result_dir
37
38def get_sdk_result_id(configuration):
39 return '%s_%s_%s_%s_%s' % (configuration['TEST_TYPE'], configuration['IMAGE_BASENAME'], configuration['SDKMACHINE'], configuration['MACHINE'], configuration['STARTTIME'])
40
Brad Bishop6e60e8b2018-02-01 10:27:11 -050041def testsdk_main(d):
42 import os
43 import subprocess
44 import json
45 import logging
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050046
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 from bb.utils import export_proxies
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048 from oeqa.sdk.context import OESDKTestContext, OESDKTestContextExecutor
49 from oeqa.utils import make_logger_bitbake_compatible
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050050
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 pn = d.getVar("PN")
52 logger = make_logger_bitbake_compatible(logging.getLogger("BitBake"))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050053
Brad Bishop6e60e8b2018-02-01 10:27:11 -050054 # sdk use network for download projects for build
55 export_proxies(d)
56
57 tcname = d.expand("${SDK_DEPLOY}/${TOOLCHAIN_OUTPUTNAME}.sh")
58 if not os.path.exists(tcname):
59 bb.fatal("The toolchain %s is not built. Build it before running the tests: 'bitbake <image> -c populate_sdk' ." % tcname)
60
61 tdname = d.expand("${SDK_DEPLOY}/${TOOLCHAIN_OUTPUTNAME}.testdata.json")
62 test_data = json.load(open(tdname, "r"))
63
64 target_pkg_manifest = OESDKTestContextExecutor._load_manifest(
65 d.expand("${SDK_DEPLOY}/${TOOLCHAIN_OUTPUTNAME}.target.manifest"))
66 host_pkg_manifest = OESDKTestContextExecutor._load_manifest(
67 d.expand("${SDK_DEPLOY}/${TOOLCHAIN_OUTPUTNAME}.host.manifest"))
68
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080069 processes = d.getVar("TESTIMAGE_NUMBER_THREADS") or d.getVar("BB_NUMBER_THREADS")
70 if processes:
71 try:
72 import testtools, subunit
73 except ImportError:
74 bb.warn("Failed to import testtools or subunit, the testcases will run serially")
75 processes = None
76
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077 sdk_dir = d.expand("${WORKDIR}/testimage-sdk/")
78 bb.utils.remove(sdk_dir, True)
79 bb.utils.mkdirhier(sdk_dir)
80 try:
81 subprocess.check_output("cd %s; %s <<EOF\n./\nY\nEOF" % (sdk_dir, tcname), shell=True)
82 except subprocess.CalledProcessError as e:
83 bb.fatal("Couldn't install the SDK:\n%s" % e.output.decode("utf-8"))
84
85 fail = False
86 sdk_envs = OESDKTestContextExecutor._get_sdk_environs(sdk_dir)
87 for s in sdk_envs:
88 sdk_env = sdk_envs[s]
89 bb.plain("SDK testing environment: %s" % s)
90 tc = OESDKTestContext(td=test_data, logger=logger, sdk_dir=sdk_dir,
91 sdk_env=sdk_env, target_pkg_manifest=target_pkg_manifest,
92 host_pkg_manifest=host_pkg_manifest)
93
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050094 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050095 tc.loadTests(OESDKTestContextExecutor.default_cases)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050096 except Exception as e:
97 import traceback
98 bb.fatal("Loading tests failed:\n%s" % traceback.format_exc())
99
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800100 if processes:
101 result = tc.runTests(processes=int(processes))
102 else:
103 result = tc.runTests()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500104
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 component = "%s %s" % (pn, OESDKTestContextExecutor.name)
106 context_msg = "%s:%s" % (os.path.basename(tcname), os.path.basename(sdk_env))
Brad Bishopf86d0552018-12-04 14:18:15 -0800107 configuration = get_sdk_configuration(d, 'sdk')
108 result.logDetails(get_sdk_json_result_dir(d),
109 configuration,
110 get_sdk_result_id(configuration))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111 result.logSummary(component, context_msg)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500112
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500113 if not result.wasSuccessful():
114 fail = True
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500115
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500116 if fail:
117 bb.fatal("%s - FAILED - check the task log and the commands log" % pn)
118
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500119testsdk_main[vardepsexclude] =+ "BB_ORIGENV"
120
121python do_testsdk() {
122 testsdk_main(d)
123}
124addtask testsdk
125do_testsdk[nostamp] = "1"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500126
127def testsdkext_main(d):
128 import os
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500129 import json
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500130 import subprocess
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131 import logging
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500132
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500133 from bb.utils import export_proxies
134 from oeqa.utils import avoid_paths_in_environ, make_logger_bitbake_compatible, subprocesstweak
135 from oeqa.sdkext.context import OESDKExtTestContext, OESDKExtTestContextExecutor
136
137 pn = d.getVar("PN")
138 logger = make_logger_bitbake_compatible(logging.getLogger("BitBake"))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500139
140 # extensible sdk use network
141 export_proxies(d)
142
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500143 subprocesstweak.errors_have_output()
144
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500145 # extensible sdk can be contaminated if native programs are
146 # in PATH, i.e. use perl-native instead of eSDK one.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500147 paths_to_avoid = [d.getVar('STAGING_DIR'),
148 d.getVar('BASE_WORKDIR')]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500149 os.environ['PATH'] = avoid_paths_in_environ(paths_to_avoid)
150
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500151 tcname = d.expand("${SDK_DEPLOY}/${TOOLCHAINEXT_OUTPUTNAME}.sh")
152 if not os.path.exists(tcname):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153 bb.fatal("The toolchain ext %s is not built. Build it before running the" \
154 " tests: 'bitbake <image> -c populate_sdk_ext' ." % tcname)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500155
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500156 tdname = d.expand("${SDK_DEPLOY}/${TOOLCHAINEXT_OUTPUTNAME}.testdata.json")
157 test_data = json.load(open(tdname, "r"))
158
159 target_pkg_manifest = OESDKExtTestContextExecutor._load_manifest(
160 d.expand("${SDK_DEPLOY}/${TOOLCHAINEXT_OUTPUTNAME}.target.manifest"))
161 host_pkg_manifest = OESDKExtTestContextExecutor._load_manifest(
162 d.expand("${SDK_DEPLOY}/${TOOLCHAINEXT_OUTPUTNAME}.host.manifest"))
163
164 sdk_dir = d.expand("${WORKDIR}/testsdkext/")
165 bb.utils.remove(sdk_dir, True)
166 bb.utils.mkdirhier(sdk_dir)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500167 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500168 subprocess.check_output("%s -y -d %s" % (tcname, sdk_dir), shell=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500169 except subprocess.CalledProcessError as e:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600170 msg = "Couldn't install the extensible SDK:\n%s" % e.output.decode("utf-8")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500171 logfn = os.path.join(sdk_dir, 'preparing_build_system.log')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600172 if os.path.exists(logfn):
173 msg += '\n\nContents of preparing_build_system.log:\n'
174 with open(logfn, 'r') as f:
175 for line in f:
176 msg += line
177 bb.fatal(msg)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500178
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500179 fail = False
180 sdk_envs = OESDKExtTestContextExecutor._get_sdk_environs(sdk_dir)
181 for s in sdk_envs:
182 bb.plain("Extensible SDK testing environment: %s" % s)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500183
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500184 sdk_env = sdk_envs[s]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500185
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500186 # Use our own SSTATE_DIR and DL_DIR so that updates to the eSDK come from our sstate cache
187 # and we don't spend hours downloading kernels for the kernel module test
188 # Abuse auto.conf since local.conf would be overwritten by the SDK
189 with open(os.path.join(sdk_dir, 'conf', 'auto.conf'), 'a+') as f:
190 f.write('SSTATE_MIRRORS += " \\n file://.* file://%s/PATH"\n' % test_data.get('SSTATE_DIR'))
191 f.write('SOURCE_MIRROR_URL = "file://%s"\n' % test_data.get('DL_DIR'))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400192 f.write('INHERIT += "own-mirrors"\n')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800193 f.write('PREMIRRORS_prepend = " git://git.yoctoproject.org/.* git://%s/git2/git.yoctoproject.org.BASENAME \\n "\n' % test_data.get('DL_DIR'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500194
195 # We need to do this in case we have a minimal SDK
Brad Bishop316dfdd2018-06-25 12:45:53 -0400196 subprocess.check_output(". %s > /dev/null; devtool sdk-install meta-extsdk-toolchain" % \
197 sdk_env, cwd=sdk_dir, shell=True, stderr=subprocess.STDOUT)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500198
199 tc = OESDKExtTestContext(td=test_data, logger=logger, sdk_dir=sdk_dir,
200 sdk_env=sdk_env, target_pkg_manifest=target_pkg_manifest,
201 host_pkg_manifest=host_pkg_manifest)
202
203 try:
204 tc.loadTests(OESDKExtTestContextExecutor.default_cases)
205 except Exception as e:
206 import traceback
207 bb.fatal("Loading tests failed:\n%s" % traceback.format_exc())
208
209 result = tc.runTests()
210
211 component = "%s %s" % (pn, OESDKExtTestContextExecutor.name)
212 context_msg = "%s:%s" % (os.path.basename(tcname), os.path.basename(sdk_env))
Brad Bishopf86d0552018-12-04 14:18:15 -0800213 configuration = get_sdk_configuration(d, 'sdkext')
214 result.logDetails(get_sdk_json_result_dir(d),
215 configuration,
216 get_sdk_result_id(configuration))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500217 result.logSummary(component, context_msg)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500218
219 if not result.wasSuccessful():
220 fail = True
221
222 if fail:
223 bb.fatal("%s - FAILED - check the task log and the commands log" % pn)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500224
225testsdkext_main[vardepsexclude] =+ "BB_ORIGENV"
226
227python do_testsdkext() {
228 testsdkext_main(d)
229}
230addtask testsdkext
231do_testsdkext[nostamp] = "1"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500232
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800233python () {
234 if oe.types.boolean(d.getVar("TESTIMAGE_AUTO") or "False"):
235 bb.build.addtask("testsdk", None, "do_populate_sdk", d)
236 bb.build.addtask("testsdkext", None, "do_populate_sdk_ext", d)
237}