Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # Copyright (c) 2013-2014 Intel Corporation |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | |
| 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 | |
| 11 | |
| 12 | |
| 13 | import os |
| 14 | import sys |
| 15 | import signal |
| 16 | import subprocess |
| 17 | import threading |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 18 | import time |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | import logging |
| 20 | from oeqa.utils import CommandError |
| 21 | from oeqa.utils import ftools |
| 22 | import re |
| 23 | import contextlib |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 24 | # Export test doesn't require bb |
| 25 | try: |
| 26 | import bb |
| 27 | except ImportError: |
| 28 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | |
| 30 | class Command(object): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 31 | def __init__(self, command, bg=False, timeout=None, data=None, output_log=None, **options): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | |
| 33 | self.defaultopts = { |
| 34 | "stdout": subprocess.PIPE, |
| 35 | "stderr": subprocess.STDOUT, |
| 36 | "stdin": None, |
| 37 | "shell": False, |
| 38 | "bufsize": -1, |
| 39 | } |
| 40 | |
| 41 | self.cmd = command |
| 42 | self.bg = bg |
| 43 | self.timeout = timeout |
| 44 | self.data = data |
| 45 | |
| 46 | self.options = dict(self.defaultopts) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 47 | if isinstance(self.cmd, str): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | self.options["shell"] = True |
| 49 | if self.data: |
| 50 | self.options['stdin'] = subprocess.PIPE |
| 51 | self.options.update(options) |
| 52 | |
| 53 | self.status = None |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 54 | # We collect chunks of output before joining them at the end. |
| 55 | self._output_chunks = [] |
| 56 | self._error_chunks = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | self.output = None |
| 58 | self.error = None |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 59 | self.threads = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 61 | self.output_log = output_log |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | self.log = logging.getLogger("utils.commands") |
| 63 | |
| 64 | def run(self): |
| 65 | self.process = subprocess.Popen(self.cmd, **self.options) |
| 66 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 67 | def readThread(output, stream, logfunc): |
| 68 | if logfunc: |
| 69 | for line in stream: |
| 70 | output.append(line) |
| 71 | logfunc(line.decode("utf-8", errors='replace').rstrip()) |
| 72 | else: |
| 73 | output.append(stream.read()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 75 | def readStderrThread(): |
| 76 | readThread(self._error_chunks, self.process.stderr, self.output_log.error if self.output_log else None) |
| 77 | |
| 78 | def readStdoutThread(): |
| 79 | readThread(self._output_chunks, self.process.stdout, self.output_log.info if self.output_log else None) |
| 80 | |
| 81 | def writeThread(): |
| 82 | try: |
| 83 | self.process.stdin.write(self.data) |
| 84 | self.process.stdin.close() |
| 85 | except OSError as ex: |
| 86 | # It's not an error when the command does not consume all |
| 87 | # of our data. subprocess.communicate() also ignores that. |
| 88 | if ex.errno != EPIPE: |
| 89 | raise |
| 90 | |
| 91 | # We write in a separate thread because then we can read |
| 92 | # without worrying about deadlocks. The additional thread is |
| 93 | # expected to terminate by itself and we mark it as a daemon, |
| 94 | # so even it should happen to not terminate for whatever |
| 95 | # reason, the main process will still exit, which will then |
| 96 | # kill the write thread. |
| 97 | if self.data: |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 98 | thread = threading.Thread(target=writeThread, daemon=True) |
| 99 | thread.start() |
| 100 | self.threads.append(thread) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 101 | if self.process.stderr: |
| 102 | thread = threading.Thread(target=readStderrThread) |
| 103 | thread.start() |
| 104 | self.threads.append(thread) |
| 105 | if self.output_log: |
| 106 | self.output_log.info('Running: %s' % self.cmd) |
| 107 | thread = threading.Thread(target=readStdoutThread) |
| 108 | thread.start() |
| 109 | self.threads.append(thread) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 110 | |
| 111 | self.log.debug("Running command '%s'" % self.cmd) |
| 112 | |
| 113 | if not self.bg: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 114 | if self.timeout is None: |
| 115 | for thread in self.threads: |
| 116 | thread.join() |
| 117 | else: |
| 118 | deadline = time.time() + self.timeout |
| 119 | for thread in self.threads: |
| 120 | timeout = deadline - time.time() |
| 121 | if timeout < 0: |
| 122 | timeout = 0 |
| 123 | thread.join(timeout) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | self.stop() |
| 125 | |
| 126 | def stop(self): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 127 | for thread in self.threads: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 128 | if thread.is_alive(): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 129 | self.process.terminate() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 130 | # let's give it more time to terminate gracefully before killing it |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 131 | thread.join(5) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 132 | if thread.is_alive(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 133 | self.process.kill() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 134 | thread.join() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 135 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 136 | def finalize_output(data): |
| 137 | if not data: |
| 138 | data = "" |
| 139 | else: |
| 140 | data = b"".join(data) |
| 141 | data = data.decode("utf-8", errors='replace').rstrip() |
| 142 | return data |
| 143 | |
| 144 | self.output = finalize_output(self._output_chunks) |
| 145 | self._output_chunks = None |
| 146 | # self.error used to be a byte string earlier, probably unintentionally. |
| 147 | # Now it is a normal string, just like self.output. |
| 148 | self.error = finalize_output(self._error_chunks) |
| 149 | self._error_chunks = None |
| 150 | # At this point we know that the process has closed stdout/stderr, so |
| 151 | # it is safe and necessary to wait for the actual process completion. |
| 152 | self.status = self.process.wait() |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 153 | self.process.stdout.close() |
| 154 | if self.process.stderr: |
| 155 | self.process.stderr.close() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 156 | |
| 157 | self.log.debug("Command '%s' returned %d as exit code." % (self.cmd, self.status)) |
| 158 | # logging the complete output is insane |
| 159 | # bitbake -e output is really big |
| 160 | # and makes the log file useless |
| 161 | if self.status: |
| 162 | lout = "\n".join(self.output.splitlines()[-20:]) |
| 163 | self.log.debug("Last 20 lines:\n%s" % lout) |
| 164 | |
| 165 | |
| 166 | class Result(object): |
| 167 | pass |
| 168 | |
| 169 | |
Andrew Geissler | 4c19ea1 | 2020-10-27 13:52:24 -0500 | [diff] [blame] | 170 | def runCmd(command, ignore_status=False, timeout=None, assert_error=True, sync=True, |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 171 | native_sysroot=None, limit_exc_output=0, output_log=None, **options): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 172 | result = Result() |
| 173 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 174 | if native_sysroot: |
| 175 | extra_paths = "%s/sbin:%s/usr/sbin:%s/usr/bin" % \ |
| 176 | (native_sysroot, native_sysroot, native_sysroot) |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 177 | extra_libpaths = "%s/lib:%s/usr/lib" % \ |
| 178 | (native_sysroot, native_sysroot) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 179 | nenv = dict(options.get('env', os.environ)) |
| 180 | nenv['PATH'] = extra_paths + ':' + nenv.get('PATH', '') |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 181 | nenv['LD_LIBRARY_PATH'] = extra_libpaths + ':' + nenv.get('LD_LIBRARY_PATH', '') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 182 | options['env'] = nenv |
| 183 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 184 | cmd = Command(command, timeout=timeout, output_log=output_log, **options) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 185 | cmd.run() |
| 186 | |
Andrew Geissler | 4c19ea1 | 2020-10-27 13:52:24 -0500 | [diff] [blame] | 187 | # tests can be heavy on IO and if bitbake can't write out its caches, we see timeouts. |
| 188 | # call sync around the tests to ensure the IO queue doesn't get too large, taking any IO |
| 189 | # hit here rather than in bitbake shutdown. |
| 190 | if sync: |
| 191 | os.system("sync") |
| 192 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 193 | result.command = command |
| 194 | result.status = cmd.status |
| 195 | result.output = cmd.output |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 196 | result.error = cmd.error |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 197 | result.pid = cmd.process.pid |
| 198 | |
| 199 | if result.status and not ignore_status: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 200 | exc_output = result.output |
| 201 | if limit_exc_output > 0: |
| 202 | split = result.output.splitlines() |
| 203 | if len(split) > limit_exc_output: |
| 204 | exc_output = "\n... (last %d lines of output)\n" % limit_exc_output + \ |
| 205 | '\n'.join(split[-limit_exc_output:]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 206 | if assert_error: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 207 | raise AssertionError("Command '%s' returned non-zero exit status %d:\n%s" % (command, result.status, exc_output)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 208 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 209 | raise CommandError(result.status, command, exc_output) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 210 | |
| 211 | return result |
| 212 | |
| 213 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 214 | def bitbake(command, ignore_status=False, timeout=None, postconfig=None, output_log=None, **options): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 215 | |
| 216 | if postconfig: |
| 217 | postconfig_file = os.path.join(os.environ.get('BUILDDIR'), 'oeqa-post.conf') |
| 218 | ftools.write_file(postconfig_file, postconfig) |
| 219 | extra_args = "-R %s" % postconfig_file |
| 220 | else: |
| 221 | extra_args = "" |
| 222 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 223 | if isinstance(command, str): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 224 | cmd = "bitbake " + extra_args + " " + command |
| 225 | else: |
| 226 | cmd = [ "bitbake" ] + [a for a in (command + extra_args.split(" ")) if a not in [""]] |
| 227 | |
| 228 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 229 | return runCmd(cmd, ignore_status, timeout, output_log=output_log, **options) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 230 | finally: |
| 231 | if postconfig: |
| 232 | os.remove(postconfig_file) |
| 233 | |
| 234 | |
| 235 | def get_bb_env(target=None, postconfig=None): |
| 236 | if target: |
| 237 | return bitbake("-e %s" % target, postconfig=postconfig).output |
| 238 | else: |
| 239 | return bitbake("-e", postconfig=postconfig).output |
| 240 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 241 | def get_bb_vars(variables=None, target=None, postconfig=None): |
| 242 | """Get values of multiple bitbake variables""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 243 | bbenv = get_bb_env(target, postconfig=postconfig) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 244 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 245 | if variables is not None: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 246 | variables = list(variables) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 247 | var_re = re.compile(r'^(export )?(?P<var>\w+(_.*)?)="(?P<value>.*)"$') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 248 | unset_re = re.compile(r'^unset (?P<var>\w+)$') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 249 | lastline = None |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 250 | values = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 251 | for line in bbenv.splitlines(): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 252 | match = var_re.match(line) |
| 253 | val = None |
| 254 | if match: |
| 255 | val = match.group('value') |
| 256 | else: |
| 257 | match = unset_re.match(line) |
| 258 | if match: |
| 259 | # Handle [unexport] variables |
| 260 | if lastline.startswith('# "'): |
| 261 | val = lastline.split('"')[1] |
| 262 | if val: |
| 263 | var = match.group('var') |
| 264 | if variables is None: |
| 265 | values[var] = val |
| 266 | else: |
| 267 | if var in variables: |
| 268 | values[var] = val |
| 269 | variables.remove(var) |
| 270 | # Stop after all required variables have been found |
| 271 | if not variables: |
| 272 | break |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 273 | lastline = line |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 274 | if variables: |
| 275 | # Fill in missing values |
| 276 | for var in variables: |
| 277 | values[var] = None |
| 278 | return values |
| 279 | |
| 280 | def get_bb_var(var, target=None, postconfig=None): |
| 281 | return get_bb_vars([var], target, postconfig)[var] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 282 | |
| 283 | def get_test_layer(): |
| 284 | layers = get_bb_var("BBLAYERS").split() |
| 285 | testlayer = None |
| 286 | for l in layers: |
| 287 | if '~' in l: |
| 288 | l = os.path.expanduser(l) |
| 289 | if "/meta-selftest" in l and os.path.isdir(l): |
| 290 | testlayer = l |
| 291 | break |
| 292 | return testlayer |
| 293 | |
| 294 | def create_temp_layer(templayerdir, templayername, priority=999, recipepathspec='recipes-*/*'): |
| 295 | os.makedirs(os.path.join(templayerdir, 'conf')) |
| 296 | with open(os.path.join(templayerdir, 'conf', 'layer.conf'), 'w') as f: |
| 297 | f.write('BBPATH .= ":${LAYERDIR}"\n') |
| 298 | f.write('BBFILES += "${LAYERDIR}/%s/*.bb \\' % recipepathspec) |
| 299 | f.write(' ${LAYERDIR}/%s/*.bbappend"\n' % recipepathspec) |
| 300 | f.write('BBFILE_COLLECTIONS += "%s"\n' % templayername) |
| 301 | f.write('BBFILE_PATTERN_%s = "^${LAYERDIR}/"\n' % templayername) |
| 302 | f.write('BBFILE_PRIORITY_%s = "%d"\n' % (templayername, priority)) |
| 303 | f.write('BBFILE_PATTERN_IGNORE_EMPTY_%s = "1"\n' % templayername) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 304 | f.write('LAYERSERIES_COMPAT_%s = "${LAYERSERIES_COMPAT_core}"\n' % templayername) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 305 | |
| 306 | @contextlib.contextmanager |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 307 | def runqemu(pn, ssh=True, runqemuparams='', image_fstype=None, launch_cmd=None, qemuparams=None, overrides={}, discard_writes=True): |
| 308 | """ |
| 309 | launch_cmd means directly run the command, don't need set rootfs or env vars. |
| 310 | """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 311 | |
| 312 | import bb.tinfoil |
| 313 | import bb.build |
| 314 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 315 | # Need a non-'BitBake' logger to capture the runner output |
| 316 | targetlogger = logging.getLogger('TargetRunner') |
| 317 | targetlogger.setLevel(logging.DEBUG) |
| 318 | handler = logging.StreamHandler(sys.stdout) |
| 319 | targetlogger.addHandler(handler) |
| 320 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 321 | tinfoil = bb.tinfoil.Tinfoil() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 322 | tinfoil.prepare(config_only=False, quiet=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 323 | try: |
| 324 | tinfoil.logger.setLevel(logging.WARNING) |
| 325 | import oeqa.targetcontrol |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 326 | recipedata = tinfoil.parse_recipe(pn) |
| 327 | recipedata.setVar("TEST_LOG_DIR", "${WORKDIR}/testimage") |
| 328 | recipedata.setVar("TEST_QEMUBOOT_TIMEOUT", "1000") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 329 | # Tell QemuTarget() whether need find rootfs/kernel or not |
| 330 | if launch_cmd: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 331 | recipedata.setVar("FIND_ROOTFS", '0') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 332 | else: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 333 | recipedata.setVar("FIND_ROOTFS", '1') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 334 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 335 | for key, value in overrides.items(): |
| 336 | recipedata.setVar(key, value) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 337 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 338 | logdir = recipedata.getVar("TEST_LOG_DIR") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 339 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 340 | qemu = oeqa.targetcontrol.QemuTarget(recipedata, targetlogger, image_fstype) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 341 | finally: |
| 342 | # We need to shut down tinfoil early here in case we actually want |
| 343 | # to run tinfoil-using utilities with the running QEMU instance. |
| 344 | # Luckily QemuTarget doesn't need it after the constructor. |
| 345 | tinfoil.shutdown() |
| 346 | |
| 347 | try: |
| 348 | qemu.deploy() |
| 349 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 350 | qemu.start(params=qemuparams, ssh=ssh, runqemuparams=runqemuparams, launch_cmd=launch_cmd, discard_writes=discard_writes) |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 351 | except Exception as e: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 352 | msg = str(e) + '\nFailed to start QEMU - see the logs in %s' % logdir |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 353 | if os.path.exists(qemu.qemurunnerlog): |
| 354 | with open(qemu.qemurunnerlog, 'r') as f: |
| 355 | msg = msg + "Qemurunner log output from %s:\n%s" % (qemu.qemurunnerlog, f.read()) |
| 356 | raise Exception(msg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 357 | |
| 358 | yield qemu |
| 359 | |
| 360 | finally: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 361 | targetlogger.removeHandler(handler) |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 362 | qemu.stop() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 363 | |
| 364 | def updateEnv(env_file): |
| 365 | """ |
| 366 | Source a file and update environment. |
| 367 | """ |
| 368 | |
| 369 | cmd = ". %s; env -0" % env_file |
| 370 | result = runCmd(cmd) |
| 371 | |
| 372 | for line in result.output.split("\0"): |
| 373 | (key, _, value) = line.partition("=") |
| 374 | os.environ[key] = value |