blob: c1f533802e2ffc4962211548a923fd3098c76ed7 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002# Copyright (c) 2013-2014 Intel Corporation
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: MIT
5#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006
7# DESCRIPTION
8# This module is mainly used by scripts/oe-selftest and modules under meta/oeqa/selftest
9# It provides a class and methods for running commands on the host in a convienent way for tests.
10
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011import os
12import sys
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013import subprocess
14import threading
Brad Bishopd7bf8c12018-02-25 22:55:05 -050015import time
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016import logging
17from oeqa.utils import CommandError
18from oeqa.utils import ftools
19import re
20import contextlib
Patrick Williams8e7b46e2023-05-01 14:19:06 -050021import errno
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050022# Export test doesn't require bb
23try:
24 import bb
25except ImportError:
26 pass
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027
28class Command(object):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050029 def __init__(self, command, bg=False, timeout=None, data=None, output_log=None, **options):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030
31 self.defaultopts = {
32 "stdout": subprocess.PIPE,
33 "stderr": subprocess.STDOUT,
34 "stdin": None,
35 "shell": False,
36 "bufsize": -1,
37 }
38
39 self.cmd = command
40 self.bg = bg
41 self.timeout = timeout
42 self.data = data
43
44 self.options = dict(self.defaultopts)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060045 if isinstance(self.cmd, str):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046 self.options["shell"] = True
47 if self.data:
48 self.options['stdin'] = subprocess.PIPE
49 self.options.update(options)
50
51 self.status = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -050052 # We collect chunks of output before joining them at the end.
53 self._output_chunks = []
54 self._error_chunks = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 self.output = None
56 self.error = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -050057 self.threads = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058
Brad Bishopd7bf8c12018-02-25 22:55:05 -050059 self.output_log = output_log
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060 self.log = logging.getLogger("utils.commands")
61
62 def run(self):
63 self.process = subprocess.Popen(self.cmd, **self.options)
64
Brad Bishopd7bf8c12018-02-25 22:55:05 -050065 def readThread(output, stream, logfunc):
66 if logfunc:
67 for line in stream:
68 output.append(line)
69 logfunc(line.decode("utf-8", errors='replace').rstrip())
70 else:
71 output.append(stream.read())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072
Brad Bishopd7bf8c12018-02-25 22:55:05 -050073 def readStderrThread():
74 readThread(self._error_chunks, self.process.stderr, self.output_log.error if self.output_log else None)
75
76 def readStdoutThread():
77 readThread(self._output_chunks, self.process.stdout, self.output_log.info if self.output_log else None)
78
79 def writeThread():
80 try:
81 self.process.stdin.write(self.data)
82 self.process.stdin.close()
83 except OSError as ex:
84 # It's not an error when the command does not consume all
85 # of our data. subprocess.communicate() also ignores that.
Patrick Williams8e7b46e2023-05-01 14:19:06 -050086 if ex.errno != errno.EPIPE:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050087 raise
88
89 # We write in a separate thread because then we can read
90 # without worrying about deadlocks. The additional thread is
91 # expected to terminate by itself and we mark it as a daemon,
92 # so even it should happen to not terminate for whatever
93 # reason, the main process will still exit, which will then
94 # kill the write thread.
95 if self.data:
Andrew Geisslerd25ed322020-06-27 00:28:28 -050096 thread = threading.Thread(target=writeThread, daemon=True)
97 thread.start()
98 self.threads.append(thread)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050099 if self.process.stderr:
100 thread = threading.Thread(target=readStderrThread)
101 thread.start()
102 self.threads.append(thread)
103 if self.output_log:
104 self.output_log.info('Running: %s' % self.cmd)
105 thread = threading.Thread(target=readStdoutThread)
106 thread.start()
107 self.threads.append(thread)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108
109 self.log.debug("Running command '%s'" % self.cmd)
110
111 if not self.bg:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500112 if self.timeout is None:
113 for thread in self.threads:
114 thread.join()
115 else:
116 deadline = time.time() + self.timeout
117 for thread in self.threads:
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500118 timeout = deadline - time.time()
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119 if timeout < 0:
120 timeout = 0
121 thread.join(timeout)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122 self.stop()
123
124 def stop(self):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500125 for thread in self.threads:
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600126 if thread.is_alive():
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500127 self.process.terminate()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128 # let's give it more time to terminate gracefully before killing it
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500129 thread.join(5)
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600130 if thread.is_alive():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131 self.process.kill()
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132 thread.join()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500134 def finalize_output(data):
135 if not data:
136 data = ""
137 else:
138 data = b"".join(data)
139 data = data.decode("utf-8", errors='replace').rstrip()
140 return data
141
142 self.output = finalize_output(self._output_chunks)
143 self._output_chunks = None
144 # self.error used to be a byte string earlier, probably unintentionally.
145 # Now it is a normal string, just like self.output.
146 self.error = finalize_output(self._error_chunks)
147 self._error_chunks = None
148 # At this point we know that the process has closed stdout/stderr, so
149 # it is safe and necessary to wait for the actual process completion.
150 self.status = self.process.wait()
Brad Bishopf86d0552018-12-04 14:18:15 -0800151 self.process.stdout.close()
152 if self.process.stderr:
153 self.process.stderr.close()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500154
155 self.log.debug("Command '%s' returned %d as exit code." % (self.cmd, self.status))
156 # logging the complete output is insane
157 # bitbake -e output is really big
158 # and makes the log file useless
159 if self.status:
160 lout = "\n".join(self.output.splitlines()[-20:])
161 self.log.debug("Last 20 lines:\n%s" % lout)
162
163
164class Result(object):
165 pass
166
167
Andrew Geissler4c19ea12020-10-27 13:52:24 -0500168def runCmd(command, ignore_status=False, timeout=None, assert_error=True, sync=True,
Patrick Williams92b42cb2022-09-03 06:53:57 -0500169 native_sysroot=None, target_sys=None, limit_exc_output=0, output_log=None, **options):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500170 result = Result()
171
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500172 if native_sysroot:
Patrick Williams92b42cb2022-09-03 06:53:57 -0500173 new_env = dict(options.get('env', os.environ))
174 paths = new_env["PATH"].split(":")
175 paths = [
176 os.path.join(native_sysroot, "bin"),
177 os.path.join(native_sysroot, "sbin"),
178 os.path.join(native_sysroot, "usr", "bin"),
179 os.path.join(native_sysroot, "usr", "sbin"),
180 ] + paths
181 if target_sys:
182 paths = [os.path.join(native_sysroot, "usr", "bin", target_sys)] + paths
183 new_env["PATH"] = ":".join(paths)
184 options['env'] = new_env
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500185
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500186 cmd = Command(command, timeout=timeout, output_log=output_log, **options)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187 cmd.run()
188
Andrew Geissler4c19ea12020-10-27 13:52:24 -0500189 # tests can be heavy on IO and if bitbake can't write out its caches, we see timeouts.
190 # call sync around the tests to ensure the IO queue doesn't get too large, taking any IO
191 # hit here rather than in bitbake shutdown.
192 if sync:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600193 p = os.environ['PATH']
194 os.environ['PATH'] = "/usr/bin:/bin:/usr/sbin:/sbin:" + p
Andrew Geissler4c19ea12020-10-27 13:52:24 -0500195 os.system("sync")
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600196 os.environ['PATH'] = p
Andrew Geissler4c19ea12020-10-27 13:52:24 -0500197
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500198 result.command = command
199 result.status = cmd.status
200 result.output = cmd.output
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600201 result.error = cmd.error
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202 result.pid = cmd.process.pid
203
204 if result.status and not ignore_status:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500205 exc_output = result.output
206 if limit_exc_output > 0:
207 split = result.output.splitlines()
208 if len(split) > limit_exc_output:
209 exc_output = "\n... (last %d lines of output)\n" % limit_exc_output + \
210 '\n'.join(split[-limit_exc_output:])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500211 if assert_error:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500212 raise AssertionError("Command '%s' returned non-zero exit status %d:\n%s" % (command, result.status, exc_output))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500213 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500214 raise CommandError(result.status, command, exc_output)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500215
216 return result
217
218
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500219def bitbake(command, ignore_status=False, timeout=None, postconfig=None, output_log=None, **options):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500220
221 if postconfig:
222 postconfig_file = os.path.join(os.environ.get('BUILDDIR'), 'oeqa-post.conf')
223 ftools.write_file(postconfig_file, postconfig)
224 extra_args = "-R %s" % postconfig_file
225 else:
226 extra_args = ""
227
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600228 if isinstance(command, str):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500229 cmd = "bitbake " + extra_args + " " + command
230 else:
231 cmd = [ "bitbake" ] + [a for a in (command + extra_args.split(" ")) if a not in [""]]
232
233 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500234 return runCmd(cmd, ignore_status, timeout, output_log=output_log, **options)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500235 finally:
236 if postconfig:
237 os.remove(postconfig_file)
238
239
240def get_bb_env(target=None, postconfig=None):
241 if target:
242 return bitbake("-e %s" % target, postconfig=postconfig).output
243 else:
244 return bitbake("-e", postconfig=postconfig).output
245
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600246def get_bb_vars(variables=None, target=None, postconfig=None):
247 """Get values of multiple bitbake variables"""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248 bbenv = get_bb_env(target, postconfig=postconfig)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600249
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500250 if variables is not None:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400251 variables = list(variables)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500252 var_re = re.compile(r'^(export )?(?P<var>\w+(_.*)?)="(?P<value>.*)"$')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600253 unset_re = re.compile(r'^unset (?P<var>\w+)$')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500254 lastline = None
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600255 values = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500256 for line in bbenv.splitlines():
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600257 match = var_re.match(line)
258 val = None
259 if match:
260 val = match.group('value')
261 else:
262 match = unset_re.match(line)
263 if match:
264 # Handle [unexport] variables
265 if lastline.startswith('# "'):
266 val = lastline.split('"')[1]
267 if val:
268 var = match.group('var')
269 if variables is None:
270 values[var] = val
271 else:
272 if var in variables:
273 values[var] = val
274 variables.remove(var)
275 # Stop after all required variables have been found
276 if not variables:
277 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500278 lastline = line
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600279 if variables:
280 # Fill in missing values
281 for var in variables:
282 values[var] = None
283 return values
284
285def get_bb_var(var, target=None, postconfig=None):
286 return get_bb_vars([var], target, postconfig)[var]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500287
288def get_test_layer():
289 layers = get_bb_var("BBLAYERS").split()
290 testlayer = None
291 for l in layers:
292 if '~' in l:
293 l = os.path.expanduser(l)
294 if "/meta-selftest" in l and os.path.isdir(l):
295 testlayer = l
296 break
297 return testlayer
298
299def create_temp_layer(templayerdir, templayername, priority=999, recipepathspec='recipes-*/*'):
300 os.makedirs(os.path.join(templayerdir, 'conf'))
Andrew Geissler517393d2023-01-13 08:55:19 -0600301 corenames = get_bb_var('LAYERSERIES_CORENAMES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500302 with open(os.path.join(templayerdir, 'conf', 'layer.conf'), 'w') as f:
303 f.write('BBPATH .= ":${LAYERDIR}"\n')
304 f.write('BBFILES += "${LAYERDIR}/%s/*.bb \\' % recipepathspec)
305 f.write(' ${LAYERDIR}/%s/*.bbappend"\n' % recipepathspec)
306 f.write('BBFILE_COLLECTIONS += "%s"\n' % templayername)
307 f.write('BBFILE_PATTERN_%s = "^${LAYERDIR}/"\n' % templayername)
308 f.write('BBFILE_PRIORITY_%s = "%d"\n' % (templayername, priority))
309 f.write('BBFILE_PATTERN_IGNORE_EMPTY_%s = "1"\n' % templayername)
Andrew Geissler517393d2023-01-13 08:55:19 -0600310 f.write('LAYERSERIES_COMPAT_%s = "%s"\n' % (templayername, corenames))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500311
312@contextlib.contextmanager
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500313def runqemu(pn, ssh=True, runqemuparams='', image_fstype=None, launch_cmd=None, qemuparams=None, overrides={}, discard_writes=True):
314 """
315 launch_cmd means directly run the command, don't need set rootfs or env vars.
316 """
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500317
318 import bb.tinfoil
319 import bb.build
320
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500321 # Need a non-'BitBake' logger to capture the runner output
322 targetlogger = logging.getLogger('TargetRunner')
323 targetlogger.setLevel(logging.DEBUG)
324 handler = logging.StreamHandler(sys.stdout)
325 targetlogger.addHandler(handler)
326
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500327 tinfoil = bb.tinfoil.Tinfoil()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500328 tinfoil.prepare(config_only=False, quiet=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500329 try:
330 tinfoil.logger.setLevel(logging.WARNING)
331 import oeqa.targetcontrol
Andrew Geissler82c905d2020-04-13 13:39:40 -0500332 recipedata = tinfoil.parse_recipe(pn)
333 recipedata.setVar("TEST_LOG_DIR", "${WORKDIR}/testimage")
334 recipedata.setVar("TEST_QEMUBOOT_TIMEOUT", "1000")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500335 # Tell QemuTarget() whether need find rootfs/kernel or not
336 if launch_cmd:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500337 recipedata.setVar("FIND_ROOTFS", '0')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500338 else:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500339 recipedata.setVar("FIND_ROOTFS", '1')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500340
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500341 for key, value in overrides.items():
342 recipedata.setVar(key, value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500343
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500344 logdir = recipedata.getVar("TEST_LOG_DIR")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500345
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500346 qemu = oeqa.targetcontrol.QemuTarget(recipedata, targetlogger, image_fstype)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500347 finally:
348 # We need to shut down tinfoil early here in case we actually want
349 # to run tinfoil-using utilities with the running QEMU instance.
350 # Luckily QemuTarget doesn't need it after the constructor.
351 tinfoil.shutdown()
352
353 try:
354 qemu.deploy()
355 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500356 qemu.start(params=qemuparams, ssh=ssh, runqemuparams=runqemuparams, launch_cmd=launch_cmd, discard_writes=discard_writes)
Brad Bishop79641f22019-09-10 07:20:22 -0400357 except Exception as e:
Brad Bishop08902b02019-08-20 09:16:51 -0400358 msg = str(e) + '\nFailed to start QEMU - see the logs in %s' % logdir
Brad Bishopf86d0552018-12-04 14:18:15 -0800359 if os.path.exists(qemu.qemurunnerlog):
360 with open(qemu.qemurunnerlog, 'r') as f:
361 msg = msg + "Qemurunner log output from %s:\n%s" % (qemu.qemurunnerlog, f.read())
362 raise Exception(msg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500363
364 yield qemu
365
366 finally:
Brad Bishopf86d0552018-12-04 14:18:15 -0800367 targetlogger.removeHandler(handler)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500368 qemu.stop()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600369
370def updateEnv(env_file):
371 """
372 Source a file and update environment.
373 """
374
375 cmd = ". %s; env -0" % env_file
376 result = runCmd(cmd)
377
378 for line in result.output.split("\0"):
379 (key, _, value) = line.partition("=")
380 os.environ[key] = value