test-runner: use type=os.path.abspath for argparse
James Prestwood
PATCH 1/2 test-runner: use type=os.path.abspath for argparse
This is a convenient type which automatically resolves the argument to an absolute path. Use this for any arguments expected to be paths.
--- tools/runner.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
tools/runner.py +6 -9
@@ -97,7 +97,8 @@ class RunnerCoreArgParse(ArgumentParser): self.add_argument('--start', '-s', help='Custom init process in virtual environment', dest='start', - default=None) + default=None, + type=os.path.abspath) self.add_argument('--verbose', '-v', metavar='<list>', type=str, help='Comma separated list of applications',
@@ -108,16 +109,17 @@ class RunnerCoreArgParse(ArgumentParser): help='Enable test-runner debugging', dest='dbg') self.add_argument('--log', '-l', - type=str, + type=os.path.abspath, help='Directory for log files') self.add_argument('--monitor', '-m', - type=str, + type=os.path.abspath, help='Enables iwmon output to file') self.add_argument('--sub-tests', '-S', metavar='<subtests>', type=str, nargs=1, help='List of subtests to run', default=None, dest='sub_tests') - self.add_argument('--result', '-e', type=str, + self.add_argument('--result', '-e', + type=os.path.abspath, help='Writes PASS/FAIL to results file') self.add_argument('--hw', '-w', type=str,
@@ -211,8 +213,6 @@ class Runner: args.start = os.path.abspath('tools/run-tests') else: raise Exception("Cannot locate run-tests binary") - else: - args.start = os.path.abspath(args.start) # If no runner is specified but we have a kernel image assume # if the kernel is executable its UML, otherwise qemu
@@ -242,9 +242,6 @@ class RunnerAbstract: def __init__(self, args): self.args = args - if self.args.log: - self.args.log = os.path.abspath(self.args.log) - def start(self): print("Starting %s" % self.name) os.execlpe(self.cmdline[0], *self.cmdline, self.env)
Signature · 1 line
2.34.1
James Prestwood
PATCH 2/2 test-runner: add logging, monitor, and results to UML
This commonizes some mounting code between QEMU and UML to allow exporting of files to the host environment. UML does this with a hostfs mount while QEMU still uses 9p.
The common code sanitizing the inputs has been put into _prepare_outfiles and _prepare_mounts was modified to take an 'extra' arugment containing additional mount points.
The results and monitor parent directories are now passed into the environment via arguments, and these are hidden from the help text (in addition to testhome)
--- tools/runner.py | 147 +++++++++++++++++++++++++++--------------------- 1 file changed, 83 insertions(+), 64 deletions(-)
tools/runner.py +83 -64
@@ -1,7 +1,6 @@ #!/usr/bin/python3 -from argparse import ArgumentParser -from argparse import Namespace +from argparse import ArgumentParser, Namespace, SUPPRESS from configparser import ConfigParser from collections import namedtuple from shutil import copy, copytree, which, rmtree
@@ -124,7 +123,9 @@ class RunnerCoreArgParse(ArgumentParser): self.add_argument('--hw', '-w', type=str, help='Use physical adapters for tests (passthrough)') - self.add_argument('--testhome') + self.add_argument('--testhome', help=SUPPRESS) + self.add_argument('--monitor_parent', help=SUPPRESS) + self.add_argument('--result_parent', help=SUPPRESS) # Prevent --autotest/--unittest from being used together auto_unit_group = self.add_mutually_exclusive_group()
@@ -287,8 +288,8 @@ class RunnerAbstract: os.sync() # For QEMU/UML runners - def _prepare_mounts(self): - for entry in mounts_common: + def _prepare_mounts(self, extra=[]): + for entry in mounts_common + extra: try: os.lstat(entry.target) except:
@@ -302,6 +303,52 @@ class RunnerAbstract: os.setsid() + # For QEMU/UML --log, --monitor, --result + def _prepare_outfiles(self): + gid = None + append_gid_uid = False + + if os.environ.get('SUDO_GID', None): + uid = int(os.environ['SUDO_UID']) + gid = int(os.environ['SUDO_GID']) + + if self.args.log: + if os.getuid() != 0: + print("--log can only be used as root user") + quit() + + append_gid_uid = True + + if not os.path.exists(self.args.log): + os.mkdir(self.args.log) + + if gid: + os.chown(self.args.log, uid, gid) + + if self.args.monitor: + if os.getuid() != 0: + print("--monitor can only be used as root user") + quit() + + append_gid_uid = True + + self.args.monitor_parent = os.path.abspath( + os.path.join(self.args.monitor, os.pardir)) + + if self.args.result: + if os.getuid() != 0: + print("--result can only be used as root user") + quit() + + append_gid_uid = True + + self.args.result_parent = os.path.abspath( + os.path.join(self.args.result, os.pardir)) + + if append_gid_uid: + self.args.SUDO_UID = uid + self.args.SUDO_GID = gid + def stop(self): exit()
@@ -314,8 +361,6 @@ class QemuRunner(RunnerAbstract): usb_adapters = None pci_adapters = None - gid = None - append_gid_uid = False super().__init__(args)
@@ -328,6 +373,8 @@ class QemuRunner(RunnerAbstract): if not args.kernel or not os.path.exists(args.kernel): raise Exception('Cannot locate kernel image %s' % args.kernel) + self._prepare_outfiles() + if args.hw: hw_conf = ConfigParser() hw_conf.read(args.hw)
@@ -342,50 +389,6 @@ class QemuRunner(RunnerAbstract): if hw_conf.has_section('PCIAdapters'): pci_adapters = [v for v in hw_conf['PCIAdapters'].values()] - if os.environ.get('SUDO_GID', None): - uid = int(os.environ['SUDO_UID']) - gid = int(os.environ['SUDO_GID']) - - if args.log: - if os.getuid() != 0: - print("--log can only be used as root user") - quit() - - append_gid_uid = True - - args.log = os.path.abspath(args.log) - if not os.path.exists(self.args.log): - os.mkdir(self.args.log) - - if gid: - os.chown(self.args.log, uid, gid) - - if args.monitor: - if os.getuid() != 0: - print("--monitor can only be used as root user") - quit() - - append_gid_uid = True - - args.monitor = os.path.abspath(args.monitor) - monitor_parent_dir = os.path.abspath(os.path.join(self.args.monitor, - os.pardir)) - - if args.result: - if os.getuid() != 0: - print("--result can only be used as root user") - quit() - - append_gid_uid = True - - args.result = os.path.abspath(args.result) - result_parent_dir = os.path.abspath(os.path.join(self.args.result, - os.pardir)) - - if append_gid_uid: - args.SUDO_UID = uid - args.SUDO_GID = gid - kern_log = "ignore_loglevel" if "kernel" in args.verbose else "quiet" qemu_cmdline = [
@@ -438,29 +441,31 @@ class QemuRunner(RunnerAbstract): # qemu_cmdline.extend([ '-virtfs', - 'local,path=%s,%s' % (args.log, mount_options('logdir')) + 'local,path=%s,%s' % (args.log, + mount_options('logdir')) ]) if args.monitor: qemu_cmdline.extend([ '-virtfs', - 'local,path=%s,%s' % (monitor_parent_dir, mount_options('mondir')) + 'local,path=%s,%s' % (self.args.monitor_parent, + mount_options('mondir')) ]) if args.result: qemu_cmdline.extend([ '-virtfs', - 'local,path=%s,%s' % (result_parent_dir, mount_options('resultdir')) + 'local,path=%s,%s' % (self.args.result_parent, + mount_options('resultdir')) ]) + self.cmdline = qemu_cmdline def prepare_environment(self): - mounts_common.extend([ - MountInfo('debugfs', '/sys/kernel/debug', '', 0) - ]) + mounts = [ MountInfo('debugfs', '/sys/kernel/debug', '', 0) ] - self._prepare_mounts() + self._prepare_mounts(extra=mounts) super().prepare_environment()
@@ -477,14 +482,12 @@ class QemuRunner(RunnerAbstract): rmtree(f) else: os.remove(f) - elif self.args.monitor: - parent = os.path.abspath(os.path.join(self.args.monitor, os.pardir)) - mount('mondir', parent, '9p', 0, + if self.args.monitor: + mount('mondir', self.args.monitor_parent, '9p', 0, 'trans=virtio,version=9p2000.L,msize=10240') if self.args.result: - parent = os.path.abspath(os.path.join(self.args.result, os.pardir)) - mount('resultdir', parent, '9p', 0, + mount('resultdir', self.args.result_parent, '9p', 0, 'trans=virtio,version=9p2000.L,msize=10240') def stop(self):
@@ -507,6 +510,8 @@ class UmlRunner(RunnerAbstract): if not which(args.kernel): raise Exception('Cannot locate UML binary %s' % args.kernel) + self._prepare_outfiles() + kern_log = "ignore_loglevel" if "kernel" in args.verbose else "quiet" cmd = [args.kernel, 'rootfstype=hostfs', 'ro', 'mem=256M', 'mac80211_hwsim.radios=0',
@@ -517,7 +522,21 @@ class UmlRunner(RunnerAbstract): self.cmdline = cmd def prepare_environment(self): - self._prepare_mounts() + mounts = [] + + if self.args.log: + mounts.append(MountInfo('hostfs', self.args.log, + self.args.log, 0)) + + if self.args.monitor: + mounts.append(MountInfo('hostfs', self.args.monitor_parent, + self.args.monitor_parent, 0)) + + if self.args.result: + mounts.append(MountInfo('hostfs', self.args.result_parent, + self.args.result_parent, 0)) + + self._prepare_mounts(extra=mounts) super().prepare_environment()
Signature · 1 line
2.34.1