Skip to contentSkip to conversation

test-runner: isolate Process/Namespace into utils

PATCH In review James Prestwood 2 participants 11 messages Apr 6, 2022

lore
Share & tools
lore

James Prestwood

author
#0 lore raw

PATCH 1/10 test-runner: isolate Process/Namespace into utils

Way too many classes have a dependency on the TestContext class, in most cases only for is_verbose. This patch removes the dependency from Process and Namespace classes.

For Process, the test arguments can be parsed in the class itself which will allow for this class to be completely isolated into its own file.

The Namespace class was already relatively isolated. Both were moved into utils.py which makes 'run-tests' quite a bit nicer to look at and more fitting to its name.

---
 tools/run-tests | 470 +-----------------------------------------------
 tools/utils.py  | 455 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 464 insertions(+), 461 deletions(-)
 create mode 100644 tools/utils.py
tools/run-tests +9 -461
index 0534a12f..4291250a 100755
@@ -2,7 +2,6 @@
 
 import os
 import shutil
-import fcntl
 import sys
 import subprocess
 import atexit
@@ -14,17 +13,16 @@ import multiprocessing
 import re
 import traceback
 
-from runner import Runner
-
 from configparser import ConfigParser
 from prettytable import PrettyTable
 from termcolor import colored
 from glob import glob
 from collections import namedtuple
-from time import sleep
 import dbus.mainloop.glib
 from gi.repository import GLib
-from weakref import WeakValueDictionary
+
+from runner import Runner
+from utils import Process, Namespace
 
 config = None
 intf_id = 0
@@ -81,241 +79,6 @@ def find_binary(list):
 			return path
 	return None
 
-# Partial DBus config. The remainder (<listen>) will be filled in for each
-# namespace that is created so each individual dbus-daemon has its own socket
-# and address.
-dbus_config = '''
-<!DOCTYPE busconfig PUBLIC \
-"-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN" \
-"http://www.freedesktop.org/standards/dbus/1.0/\
-busconfig.dtd\">
-<busconfig>
-<type>system</type>
-<limit name=\"reply_timeout\">2147483647</limit>
-<auth>ANONYMOUS</auth>
-<allow_anonymous/>
-<policy context=\"default\">
-<allow user=\"*\"/>
-<allow own=\"*\"/>
-<allow send_type=\"method_call\"/>
-<allow send_type=\"signal\"/>
-<allow send_type=\"method_return\"/>
-<allow send_type=\"error\"/>
-<allow receive_type=\"method_call\"/>
-<allow receive_type=\"signal\"/>
-<allow receive_type=\"method_return\"/>
-<allow receive_type=\"error\"/>
-<allow send_destination=\"*\" eavesdrop=\"true\"/>
-<allow eavesdrop=\"true\"/>
-</policy>
-'''
-
-class Process(subprocess.Popen):
-	processes = WeakValueDictionary()
-	ctx = None
-
-	def __new__(cls, *args, **kwargs):
-		obj = super().__new__(cls)
-		cls.processes[id(obj)] = obj
-		return obj
-
-	def __init__(self, args, namespace=None, outfile=None, env=None, check=False, cleanup=None):
-		self.write_fds = []
-		self.io_watch = None
-		self.cleanup = cleanup
-		self.verbose = False
-		self.out = ''
-		self.hup = False
-		self.killed = False
-		self.namespace = namespace
-
-		logfile = args[0]
-
-		if not self.ctx:
-			global config
-			self.ctx = config.ctx
-
-		if self.ctx.is_verbose(args[0], log=False):
-			self.verbose = True
-
-		if namespace:
-			args = ['ip', 'netns', 'exec', namespace] + args
-			logfile += '-%s' % namespace
-
-		if outfile:
-			# outfile is only used by iwmon, in which case we don't want
-			# to append to an existing file.
-			self._append_outfile(outfile, append=False)
-
-		if self.ctx.args.log:
-			testdir = os.getcwd()
-
-			# Special case any processes started prior to a test
-			# (i.e. from testhome). Put these in the root log directory
-			if testdir == self.ctx.args.testhome:
-				testdir = '.'
-			else:
-				testdir = os.path.basename(testdir)
-
-			logfile = '%s/%s/%s' % (self.ctx.args.log, testdir, logfile)
-			self._append_outfile(logfile)
-
-		super().__init__(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
-					env=env, cwd=os.getcwd())
-
-		# Set as non-blocking so read() in the IO callback doesn't block forever
-		fl = fcntl.fcntl(self.stdout, fcntl.F_GETFL)
-		fcntl.fcntl(self.stdout, fcntl.F_SETFL, fl | os.O_NONBLOCK)
-
-		self.io_watch = GLib.io_add_watch(self.stdout, GLib.IO_IN |
-						GLib.IO_HUP | GLib.IO_ERR, self.process_io)
-
-		print("Starting process {}".format(self.args))
-
-		if check:
-			self.wait(10)
-			self.killed = True
-			if self.returncode != 0:
-				raise subprocess.CalledProcessError(returncode=self.returncode,
-									cmd=args)
-
-	@classmethod
-	def get_all(cls):
-		return cls.processes.values()
-
-	@classmethod
-	def kill_all(cls):
-		for p in cls.processes.values():
-			if p.args[0] == 'dmesg':
-				continue
-
-			p.kill()
-
-	@staticmethod
-	def _write_io(instance, data, stdout=True):
-		for f in instance.write_fds:
-			f.write(data)
-
-			# Write out a separator so multiple process calls per
-			# test are easer to read.
-			if instance.hup:
-				f.write("Terminated: {}\n\n".format(instance.args))
-
-			f.flush()
-
-		if instance.verbose and stdout:
-			sys.__stdout__.write(data)
-			sys.__stdout__.flush()
-
-	@classmethod
-	def write_separators(cls, sep):
-		for proc in cls.processes.values():
-			if proc.killed:
-				continue
-
-			cls._write_io(proc, sep, stdout=False)
-
-	def process_io(self, source, condition):
-		if condition & GLib.IO_HUP:
-			self.hup = True
-
-		data = source.read()
-
-		if not data:
-			return True
-
-		data = data.decode('utf-8')
-
-		# Save data away in case the caller needs it (e.g. list_sta)
-		self.out += data
-
-		self._write_io(self, data)
-
-		return True
-
-	def _append_outfile(self, file, append=True):
-		gid = int(os.environ.get('SUDO_GID', os.getgid()))
-		uid = int(os.environ.get('SUDO_UID', os.getuid()))
-		dir = os.path.dirname(file)
-
-		if not path_exists(dir):
-			os.mkdir(dir)
-			os.chown(dir, uid, gid)
-
-		file = os.path.join(dir,file)
-
-		# If the out file exists, append. Useful for processes like
-		# hostapd_cli where it is called multiple times independently.
-		if os.path.isfile(file) and append:
-			mode = 'a'
-		else:
-			mode = 'w'
-
-		try:
-			f = open(file, mode)
-		except Exception as e:
-			traceback.print_exc()
-			sys.exit(0)
-
-		os.fchown(f.fileno(), uid, gid)
-
-		self.write_fds.append(f)
-
-	def wait_for_socket(self, socket, wait):
-		Namespace.non_block_wait(os.path.exists, wait, socket)
-
-	# Wait for both process termination and HUP signal
-	def __wait(self, timeout):
-		try:
-			super().wait(timeout)
-			if not self.hup:
-				return False
-
-			return True
-		except:
-			return False
-
-	# Override wait() so it can do so non-blocking
-	def wait(self, timeout=10):
-		Namespace.non_block_wait(self.__wait, timeout, 1)
-		self._cleanup()
-
-	def _cleanup(self):
-		if self.cleanup:
-			self.cleanup()
-
-		self.write_fds = []
-
-		if self.io_watch:
-			GLib.source_remove(self.io_watch)
-			self.io_watch = None
-
-		self.cleanup = None
-		self.killed = True
-
-	# Override kill()
-	def kill(self, force=False):
-		if self.killed:
-			return
-
-		print("Killing process {}".format(self.args))
-
-		if force:
-			super().kill()
-		else:
-			self.terminate()
-
-		try:
-			self.wait(timeout=15)
-		except:
-			dbg("Process %s did not complete in 15 seconds!" % self.name)
-			super().kill()
-
-		self._cleanup()
-
-	def __str__(self):
-		return str(self.args) + '\n'
-
 class Interface:
 	def __init__(self, name, config):
 		self.name = name
@@ -447,9 +210,7 @@ class Hostapd:
 		A set of running hostapd instances. This is really just a single
 		process since hostapd can be started with multiple config files.
 	'''
-	def __init__(self, ctx, radios, configs, radius):
-		self.ctx = ctx
-
+	def __init__(self, radios, configs, radius):
 		if len(configs) != len(radios):
 			raise Exception("Config (%d) and radio (%d) list length not equal" % \
 						(len(configs), len(radios)))
@@ -485,7 +246,7 @@ class Hostapd:
 		if radius:
 			args.append(radius)
 
-		if ctx.is_verbose('hostapd'):
+		if Process.is_verbose('hostapd'):
 			args.append('-d')
 
 		self.process = Process(args)
@@ -545,226 +306,13 @@ class Hostapd:
 
 		# Hostapd may have already been stopped
 		if self.process:
-			self.ctx.stop_process(self.process)
-
-		self.ctx = None
+			self.process.kill()
 
 		# Hostapd creates simdb sockets for EAP-SIM/AKA tests but does not
 		# clean them up.
 		for f in glob("/tmp/eap_sim_db*"):
 			os.remove(f)
 
-dbus_count = 0
-
-class Namespace:
-	def __init__(self, args, name, radios):
-		self.dbus_address = None
-		self.name = name
-		self.radios = radios
-		self.args = args
-
-		Process(['ip', 'netns', 'add', name]).wait()
-		for r in radios:
-			Process(['iw', 'phy', r.name, 'set', 'netns', 'name', name]).wait()
-
-		self.start_dbus()
-
-	def reset(self):
-		self._bus = None
-
-		for r in self.radios:
-			r._radio = None
-
-		self.radios = []
-
-		Process.kill_all()
-
-	def __del__(self):
-		if self.name:
-			print("Removing namespace %s" % self.name)
-
-			Process(['ip', 'netns', 'del', self.name]).wait()
-
-	def get_bus(self):
-		return self._bus
-
-	def start_process(self, args, env=None, **kwargs):
-		if not env:
-			env = os.environ.copy()
-
-		if hasattr(self, "dbus_address"):
-			# In case this process needs DBus...
-			env['DBUS_SYSTEM_BUS_ADDRESS'] = self.dbus_address
-
-		return Process(args, namespace=self.name, env=env, **kwargs)
-
-	def stop_process(self, p, force=False):
-		p.kill(force)
-
-	def is_process_running(self, process):
-		for p in Process.get_all():
-			if p.namespace == self.name and p.args[0] == process:
-				return True
-		return False
-
-	def _cleanup_dbus(self):
-		try:
-			os.remove(self.dbus_address.split('=')[1])
-		except:
-			pass
-
-		os.remove(self.dbus_cfg)
-
-	def start_dbus(self):
-		global dbus_count
-
-		self.dbus_address = 'unix:path=/tmp/dbus%d' % dbus_count
-		self.dbus_cfg = '/tmp/dbus%d.conf' % dbus_count
-		dbus_count += 1
-
-		with open(self.dbus_cfg, 'w+') as f:
-			f.write(dbus_config)
-			f.write('<listen>%s</listen>\n' % self.dbus_address)
-			f.write('</busconfig>\n')
-
-		p = self.start_process(['dbus-daemon', '--config-file=%s' % self.dbus_cfg],
-					cleanup=self._cleanup_dbus)
-
-		p.wait_for_socket(self.dbus_address.split('=')[1], 5)
-
-		self._bus = dbus.bus.BusConnection(address_or_type=self.dbus_address)
-
-	def start_iwd(self, config_dir = '/tmp', storage_dir = '/tmp/iwd'):
-		args = []
-		iwd_radios = ','.join([r.name for r in self.radios if r.use == 'iwd'])
-
-		if self.args.valgrind:
-			args.extend(['valgrind', '--leak-check=full', '--track-origins=yes',
-					'--show-leak-kinds=all',
-					'--log-file=/tmp/valgrind.log.%p'])
-
-		args.extend(['iwd', '-E'])
-
-		if iwd_radios != '':
-			args.extend(['-p', iwd_radios])
-
-		if self.is_verbose(args[0]):
-			args.append('-d')
-
-		env = os.environ.copy()
-
-		env['CONFIGURATION_DIRECTORY'] = config_dir
-		env['STATE_DIRECTORY'] = storage_dir
-
-		if self.is_verbose('iwd-dhcp'):
-			env['IWD_DHCP_DEBUG'] = '1'
-
-		if self.is_verbose('iwd-tls'):
-			env['IWD_TLS_DEBUG'] = '1'
-
-		if self.is_verbose('iwd-acd'):
-			env['IWD_ACD_DEBUG'] = '1'
-
-		return self.start_process(args, env=env)
-
-	def is_verbose(self, process, log=True):
-		process = os.path.basename(process)
-
-		if self.args is None:
-			return False
-
-		# every process is verbose when logging is enabled
-		if log and self.args.log:
-			return True
-
-		if process in self.args.verbose:
-			return True
-
-		# Special case here to enable verbose output with valgrind running
-		if process == 'valgrind' and 'iwd' in self.args.verbose:
-			return True
-
-		# Handle any glob matches
-		for item in self.args.verbose:
-			if process in glob(item):
-				return True
-
-		return False
-
-	@staticmethod
-	def non_block_wait(func, timeout, *args, exception=True):
-		'''
-			Convenience function for waiting in a non blocking
-			manor using GLibs context iteration i.e. does not block
-			the main loop while waiting.
-
-			'func' will be called at least once and repeatedly until
-			either it returns success, throws an exception, or the
-			'timeout' expires.
-
-			'timeout' is the ultimate timeout in seconds
-
-			'*args' will be passed to 'func'
-
-			If 'exception' is an Exception type it will be raised.
-			If 'exception' is True a generic TimeoutError will be raised.
-			Any other value will not result in an exception.
-		'''
-		# Simple class for signaling the wait timeout
-		class Bool:
-			def __init__(self, value):
-				self.value = value
-
-		def wait_timeout_cb(done):
-			done.value = True
-			return False
-
-		mainloop = GLib.MainLoop()
-		done = Bool(False)
-
-		timeout = GLib.timeout_add_seconds(timeout, wait_timeout_cb, done)
-		context = mainloop.get_context()
-
-		while True:
-			try:
-				ret = func(*args)
-				if ret:
-					if not done.value:
-						GLib.source_remove(timeout)
-					return ret
-			except Exception as e:
-				if not done.value:
-					GLib.source_remove(timeout)
-				raise e
-
-			if done.value == True:
-				if isinstance(exception, Exception):
-					raise exception
-				elif type(exception) == bool and exception:
-					raise TimeoutError("Timeout on non_block_wait")
-				else:
-					return
-
-			context.iteration(may_block=True)
-
-	def __str__(self):
-		ret = 'Namespace: %s\n' % self.name
-		ret += 'Processes:\n'
-		for p in Process.get_all():
-			ret += '\t%s' % str(p)
-
-		ret += 'Radios:\n'
-		if len(self.radios) > 0:
-			for r in self.radios:
-				ret += '\t%s\n' % str(r)
-		else:
-			ret += '\tNo Radios\n'
-
-		ret += 'DBus Address: %s\n' % self.dbus_address
-		ret += '===================================================\n\n'
-
-		return ret
-
 class BarChart():
 	def __init__(self, height=10, max_width=80):
 		self._height = height
@@ -828,7 +376,7 @@ class TestContext(Namespace):
 		self._mem_chart = BarChart()
 
 	def start_dbus_monitor(self):
-		if not self.is_verbose('dbus-monitor'):
+		if not Process.is_verbose('dbus-monitor'):
 			return
 
 		self.start_process(['dbus-monitor', '--address', self.dbus_address])
@@ -933,7 +481,7 @@ class TestContext(Namespace):
 
 		radius_config = settings.get('radius_server', None)
 
-		self.hostapd = Hostapd(self, hapd_radios, hapd_configs, radius_config)
+		self.hostapd = Hostapd(hapd_radios, hapd_configs, radius_config)
 		self.hostapd.attach_cli()
 
 	def get_frequencies(self):
@@ -1001,7 +549,7 @@ class TestContext(Namespace):
 		time.sleep(3)
 
 		ofono_args = ['ofonod', '-n', '--plugin=atmodem,phonesim']
-		if self.is_verbose('ofonod'):
+		if Process.is_verbose('ofonod'):
 			ofono_args.append('-d')
 
 		self.start_process(ofono_args)
tools/utils.py new file +455 -0

455 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/10 test-runner: remove path_exists/find_binary

These are already implemented in the shutil module

---
 tools/run-tests | 24 +-----------------------
 1 file changed, 1 insertion(+), 23 deletions(-)
tools/run-tests +1 -23
index 4291250a..a95a7494 100755
@@ -57,28 +57,6 @@ def exit_vm():
 
 	runner.stop()
 
-def path_exists(path):
-	'''
-		Searches PATH as well as absolute paths.
-	'''
-	if shutil.which(path):
-		return True
-	try:
-		os.stat(path)
-	except:
-		return False
-	return True
-
-def find_binary(list):
-	'''
-		Returns a binary from 'list' if its found in PATH or on a
-		valid absolute path.
-	'''
-	for path in list:
-		if path_exists(path):
-			return path
-	return None
-
 class Interface:
 	def __init__(self, name, config):
 		self.name = name
@@ -529,7 +507,7 @@ class TestContext(Namespace):
 			os.environ['IWD_SIM_KEYS'] = sim_keys
 			return
 
-		if not find_binary(['ofonod']) or not find_binary(['phonesim']):
+		if not shutil.which('ofonod') or not shutil.which(['phonesim']):
 			print("Ofono or Phonesim not found, skipping test")
 			return
 
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 3/10 test-runner: simplify start_iwd handling

In reply to James Prestwood

Replace two separate if blocks to handle the default value with fallback=True.

---
 tools/run-tests | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
tools/run-tests +1 -8
index a95a7494..5f405e67 100755
@@ -886,15 +886,8 @@ def pre_test(ctx, test, copied):
 	ctx.start_wpas_interfaces()
 	ctx.start_ofono()
 
-	if ctx.hw_config.has_option('SETUP', 'start_iwd'):
-		start = ctx.hw_config.getboolean('SETUP', 'start_iwd')
-	else:
-		start = True
-
-	if start:
+	if ctx.hw_config.getboolean('SETUP', 'start_iwd', fallback=True):
 		ctx.start_iwd()
-	else:
-		print("Not starting IWD from test-runner")
 
 	print(ctx)
 
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 4/10 test-runner: remove unused class members

In reply to James Prestwood
---
 tools/run-tests | 5 -----
 1 file changed, 5 deletions(-)
tools/run-tests +0 -5
index 5f405e67..92ba324a 100755
@@ -343,12 +343,8 @@ class TestContext(Namespace):
 		self.hw_config = None
 		self.hostapd = None
 		self.wpas_interfaces = None
-		self.cur_radio_id = 0
-		self.cur_iface_id = 0
 		self.radios = []
-		self.loopback_started = False
 		self.results = {}
-		self.mainloop = GLib.MainLoop()
 		self.namespaces = []
 		self._last_mem_available = 0
 		self._mem_chart = BarChart()
@@ -387,7 +383,6 @@ class TestContext(Namespace):
 				rad_config = self.hw_config[name]
 
 			self.radios.append(VirtualRadio(name, rad_config))
-			self.cur_radio_id += 1
 
 	def discover_radios(self):
 		import pyroute2
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 5/10 test-runner: move BarChart into utils.py

In reply to James Prestwood
---
 tools/run-tests | 43 +------------------------------------------
 tools/utils.py  | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 42 deletions(-)
tools/run-tests +1 -42
index 92ba324a..c4013258 100755
@@ -22,7 +22,7 @@ import dbus.mainloop.glib
 from gi.repository import GLib
 
 from runner import Runner
-from utils import Process, Namespace
+from utils import Process, Namespace, BarChart
 
 config = None
 intf_id = 0
@@ -291,47 +291,6 @@ class Hostapd:
 		for f in glob("/tmp/eap_sim_db*"):
 			os.remove(f)
 
-class BarChart():
-	def __init__(self, height=10, max_width=80):
-		self._height = height
-		self._max_width = max_width
-		self._values = []
-		self._max_value = 0
-		self._min_value = 0
-
-	def add_value(self, value):
-		if len(self._values) == 0:
-			self._max_value = int(1.01 * value)
-			self._min_value = int(0.99 * value)
-		elif value > self._max_value:
-			self._max_value = int(1.01 * value)
-		elif value < self._min_value:
-			self._min_value = int(0.99 * value)
-
-		self._values.append(value)
-
-	def _value_to_stars(self, value):
-		# Need to scale value (range of min_value -> max_value) to
-		# a range of 0 -> height
-		#
-		# Scaled = ((value - min_value) / ( max_value - min_value)) * (Height - 0) + 0
-
-		return int(((value - self._min_value) /
-			(self._max_value - self._min_value)) * self._height)
-
-	def __str__(self):
-		# Need to map value from range 0 - self._height
-		ret = ''
-
-		for i, value in enumerate(self._values):
-			stars = self._value_to_stars(value)
-			ret += '[%3u] ' % i + '%-10s' % ('*' * stars) + '\t\t\t%d\n' % value
-
-		ret += '\n'
-
-		return ret
-
-
 class TestContext(Namespace):
 	'''
 		Contains all information for a given set of tests being run
tools/utils.py +40 -0
index 4c71e934..c2478ff2 100644
@@ -453,3 +453,43 @@ class Namespace:
 		ret += '===================================================\n\n'
 
 		return ret
+
+class BarChart():
+	def __init__(self, height=10, max_width=80):
+		self._height = height
+		self._max_width = max_width
+		self._values = []
+		self._max_value = 0
+		self._min_value = 0
+
+	def add_value(self, value):
+		if len(self._values) == 0:
+			self._max_value = int(1.01 * value)
+			self._min_value = int(0.99 * value)
+		elif value > self._max_value:
+			self._max_value = int(1.01 * value)
+		elif value < self._min_value:
+			self._min_value = int(0.99 * value)
+
+		self._values.append(value)
+
+	def _value_to_stars(self, value):
+		# Need to scale value (range of min_value -> max_value) to
+		# a range of 0 -> height
+		#
+		# Scaled = ((value - min_value) / ( max_value - min_value)) * (Height - 0) + 0
+
+		return int(((value - self._min_value) /
+			(self._max_value - self._min_value)) * self._height)
+
+	def __str__(self):
+		# Need to map value from range 0 - self._height
+		ret = ''
+
+		for i, value in enumerate(self._values):
+			stars = self._value_to_stars(value)
+			ret += '[%3u] ' % i + '%-10s' % ('*' * stars) + '\t\t\t%d\n' % value
+
+		ret += '\n'
+
+		return ret
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH 6/10 test-runner: write separators for transient processes

In reply to James Prestwood

Many processes are not long running (e.g. hostapd_cli, ip, iw, etc) and the separators written to log files don't show up for these which makes debugging difficult. This is even true for IWD/Hostapd for tests with start_iwd=0.

After writing separators for long running processes write them out for any additional log files too.

---
 tools/run-tests |  5 ++++-
 tools/utils.py  | 23 ++++++++++++++++++++++-
 2 files changed, 26 insertions(+), 2 deletions(-)
tools/run-tests +4 -1
index c4013258..78665de1 100755
@@ -724,7 +724,10 @@ def start_test(ctx, subtests, rqueue):
 
 					sys.__stdout__.flush()
 
-					Process.write_separators("\n====== %s:%s ======\n\n" % (file, func))
+					name = os.path.basename(os.getcwd())
+
+					Process.write_separators(name, "\n====== %s:%s:%s ======\n\n" %
+									(name, file, func))
 
 					if not skip:
 						# Run test (setUp/tearDown run automatically)
tools/utils.py +22 -1
index c2478ff2..be4c8638 100644
@@ -131,12 +131,33 @@ class Process(subprocess.Popen):
 			sys.__stdout__.flush()
 
 	@classmethod
-	def write_separators(cls, sep):
+	def write_separators(cls, test, sep):
+		#
+		# There are either log running processes (cls.processes) or
+		# processes that have terminated already but a log file exists
+		# on disk. We still want the separators to show for both cases
+		# so after writing separators for running processes, also
+		# write them in any additional log files.
+		#
+		nowrite = []
+
 		for proc in cls.processes.values():
 			if proc.killed:
 				continue
 
 			cls._write_io(proc, sep, stdout=False)
+			nowrite.append(proc.args[0])
+
+		if cls.testargs.log:
+			logfiles = os.listdir('%s/%s' % (cls.testargs.log, test))
+
+			extra = list(set(logfiles) - set(nowrite))
+
+			for log in extra:
+				logfile = '%s/%s/%s' % (cls.testargs.log, test, log)
+				with open(logfile, 'a') as f:
+					f.write(sep)
+				f.close()
 
 	def process_io(self, source, condition):
 		if condition & GLib.IO_HUP:
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH 7/10 test-runner: Unify QEMU/UML logging code

In reply to James Prestwood

The MountInfo tuple was changed to explicitly take a source string. This is redundant for UML and system mounts since the fstype/source are the same, but it allows QEMU to specify the '9p' fstype and use MountInfo rather than calling mount() explicitly.

This also moves logging cleanup into _prepare_mounts so both UML and QEMU can use it.

---
 tools/runner.py | 73 +++++++++++++++++++++++++------------------------
 1 file changed, 38 insertions(+), 35 deletions(-)
tools/runner.py +38 -35
index d303af7e..1c1f6eed 100644
@@ -24,19 +24,19 @@ MS_STRICTATIME = 1 << 24
 STDIN_FILENO = 0
 TIOCSTTY = 0x540E
 
-MountInfo = namedtuple('MountInfo', 'fstype target options flags')
+MountInfo = namedtuple('MountInfo', 'fstype source target options flags')
 DevInfo = namedtuple('DevInfo', 'target linkpath')
 
 mounts_common = [
-	MountInfo('sysfs', '/sys', '', MS_NOSUID|MS_NOEXEC|MS_NODEV),
-	MountInfo('proc', '/proc', '', MS_NOSUID|MS_NOEXEC|MS_NODEV),
-	MountInfo('devpts', '/dev/pts', 'mode=0620', MS_NOSUID|MS_NOEXEC),
-	MountInfo('tmpfs', '/dev/shm', 'mode=1777',
+	MountInfo('sysfs', 'sysfs', '/sys', '', MS_NOSUID|MS_NOEXEC|MS_NODEV),
+	MountInfo('proc', 'proc', '/proc', '', MS_NOSUID|MS_NOEXEC|MS_NODEV),
+	MountInfo('devpts', 'devpts', '/dev/pts', 'mode=0620', MS_NOSUID|MS_NOEXEC),
+	MountInfo('tmpfs', 'tmpfs', '/dev/shm', 'mode=1777',
 					MS_NOSUID|MS_NODEV|MS_STRICTATIME),
-	MountInfo('tmpfs', '/run', 'mode=0755',
+	MountInfo('tmpfs', 'tmpfs', '/run', 'mode=0755',
 					MS_NOSUID|MS_NODEV|MS_STRICTATIME),
-	MountInfo('tmpfs', '/tmp', '', 0),
-	MountInfo('tmpfs', '/usr/share/dbus-1', 'mode=0755',
+	MountInfo('tmpfs', 'tmpfs', '/tmp', '', 0),
+	MountInfo('tmpfs', 'tmpfs', '/usr/share/dbus-1', 'mode=0755',
 					MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME),
 ]
 
@@ -279,6 +279,16 @@ class RunnerAbstract:
 		copytree(self.args.testhome + '/autotests/misc/secrets', '/tmp/secrets')
 		copy(self.args.testhome + '/autotests/misc/phonesim/phonesim.conf', '/tmp')
 
+		# Clear out any log files from other test runs
+		if self.args.log:
+			for f in [os.path.join(self.args.log, file) for file in os.listdir(self.args.log)]:
+				print("removing %s" % f)
+
+				if os.path.isdir(f):
+					rmtree(f)
+				else:
+					os.remove(f)
+
 	def cleanup_environment(self):
 		rmtree('/tmp/iwd')
 		rmtree('/tmp/certs')
@@ -295,7 +305,7 @@ class RunnerAbstract:
 			except:
 				os.mkdir(entry.target, 755)
 
-			mount(entry.fstype, entry.target, entry.fstype, entry.flags,
+			mount(entry.source, entry.target, entry.fstype, entry.flags,
 				entry.options)
 
 		for entry in dev_table:
@@ -463,32 +473,25 @@ class QemuRunner(RunnerAbstract):
 		self.cmdline = qemu_cmdline
 
 	def prepare_environment(self):
-		mounts = [ MountInfo('debugfs', '/sys/kernel/debug', '', 0) ]
-
-		self._prepare_mounts(extra=mounts)
-
-		super().prepare_environment()
-
-		fcntl.ioctl(STDIN_FILENO, TIOCSTTY, 1)
+		mounts = [ MountInfo('debugfs', 'debugfs', '/sys/kernel/debug', '', 0) ]
 
 		if self.args.log:
-			mount('logdir', self.args.log, '9p', 0,
-					'trans=virtio,version=9p2000.L,msize=10240')
-			# Clear out any log files from other test runs
-			for f in glob('%s/*' % self.args.log):
-				print("removing %s" % f)
+			mounts.append(MountInfo('9p', 'logdir', self.args.log,
+					'trans=virtio,version=9p2000.L,msize=10240', 0))
 
-				if os.path.isdir(f):
-					rmtree(f)
-				else:
-					os.remove(f)
 		if self.args.monitor:
-			mount('mondir', self.args.monitor_parent, '9p', 0,
-					'trans=virtio,version=9p2000.L,msize=10240')
+			mounts.append(MountInfo('9p', 'mondir', self.args.monitor_parent,
+					'trans=virtio,version=9p2000.L,msize=10240', 0))
 
 		if self.args.result:
-			mount('resultdir', self.args.result_parent, '9p', 0,
-					'trans=virtio,version=9p2000.L,msize=10240')
+			mounts.append(MountInfo('9p', 'resultdir', self.args.result_parent,
+					'trans=virtio,version=9p2000.L,msize=10240', 0))
+
+		self._prepare_mounts(extra=mounts)
+
+		super().prepare_environment()
+
+		fcntl.ioctl(STDIN_FILENO, TIOCSTTY, 1)
 
 	def stop(self):
 		RB_AUTOBOOT = 0x01234567
@@ -525,16 +528,16 @@ class UmlRunner(RunnerAbstract):
 		mounts = []
 
 		if self.args.log:
-			mounts.append(MountInfo('hostfs', self.args.log,
-							self.args.log, 0))
+			mounts.append(MountInfo('hostfs', '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))
+			mounts.append(MountInfo('hostfs', '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))
+			mounts.append(MountInfo('hostfs', 'hostfs', self.args.result_parent,
+						self.args.result_parent, 0))
 
 		self._prepare_mounts(extra=mounts)
 
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH 8/10 test-runner: fix ctrl-c for UML

In reply to James Prestwood

The TIOCSTTY ioctl was not shared between UML and QEMU which prevented any console input from making it into UML. This fixes that, and now ctrl-c can be used to stop UML test execution.

---
 tools/runner.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/runner.py +2 -2
index 1c1f6eed..1f087d6c 100644
@@ -289,6 +289,8 @@ class RunnerAbstract:
 				else:
 					os.remove(f)
 
+		fcntl.ioctl(STDIN_FILENO, TIOCSTTY, 1)
+
 	def cleanup_environment(self):
 		rmtree('/tmp/iwd')
 		rmtree('/tmp/certs')
@@ -491,8 +493,6 @@ class QemuRunner(RunnerAbstract):
 
 		super().prepare_environment()
 
-		fcntl.ioctl(STDIN_FILENO, TIOCSTTY, 1)
-
 	def stop(self):
 		RB_AUTOBOOT = 0x01234567
 		#
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH 9/10 auto-t: cleanup the few uses of is_verbose

In reply to James Prestwood

These were used outside of run-tests in a couple places. Replace these with Process.is_verbose()

---
 autotests/testP2P/connection_test.py | 4 +++-
 autotests/util/wpas.py               | 5 ++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
autotests/testP2P/connection_test.py +3 -1
index d1681164..f22f0682 100644
@@ -11,6 +11,8 @@ import testutil
 from config import ctx
 from wpas import Wpas
 
+from utils import Process
+
 class Test(unittest.TestCase):
     leases_path = '/tmp/dhcp.lease'
 
@@ -110,7 +112,7 @@ class Test(unittest.TestCase):
             self.assertEqual(wpas.p2p_group['go_ip_addr'], '192.168.1.1')
 
             extra_params = []
-            if ctx.is_verbose('dhclient'):
+            if Process.is_verbose('dhclient'):
                 extra_params.append('-v')
 
             dhcp = ctx.start_process(['dhclient', '-d', '--no-pid', '-cf', '/dev/null', '-lf', self.leases_path,
autotests/util/wpas.py +4 -1
index 995bdad1..67b32d5f 100644
@@ -3,8 +3,11 @@ import os
 import socket
 from gi.repository import GLib
 from config import ctx
+
 import binascii
 
+from utils import Process
+
 ctrl_count = 0
 
 class Wpas:
@@ -31,7 +34,7 @@ class Wpas:
         self.io_watch = None
 
         cmd = ['wpa_supplicant', '-i', self.interface.name, '-c', self.config_path]
-        if ctx.is_verbose('wpa_supplicant-dbg'):
+        if Process.is_verbose('wpa_supplicant-dbg'):
             cmd += ['-d']
 
         self.wpa_supplicant = ctx.start_process(cmd)
Signature · 1 line
2.34.1
author
#9 lore raw

PATCH 10/10 test-runner: allow decode failure when writing IO

In reply to James Prestwood

If this fails, e.g. an invalid utf-8 character it should not cause the test to fail.

---
 tools/utils.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/utils.py +4 -1
index be4c8638..55db4227 100644
@@ -168,7 +168,10 @@ class Process(subprocess.Popen):
 		if not data:
 			return True
 
-		data = data.decode('utf-8')
+		try:
+			data = data.decode('utf-8')
+		except:
+			return True
 
 		# Save data away in case the caller needs it (e.g. list_sta)
 		self.out += data
Signature · 1 line
2.34.1
#10 lore raw
In reply to James Prestwood

Hi James,

Quoted 7 lines · James Prestwood

Way too many classes have a dependency on the TestContext class, in most cases only for is_verbose. This patch removes the dependency from Process and Namespace classes.

For Process, the test arguments can be parsed in the class itself which will allow for this class to be completely isolated into its own file.

Quoted 9 lines

The Namespace class was already relatively isolated. Both were moved into utils.py which makes 'run-tests' quite a bit nicer to look at and more fitting to its name.

---
  tools/run-tests | 470 +-----------------------------------------------
  tools/utils.py  | 455 ++++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 464 insertions(+), 461 deletions(-)

create mode 100644 tools/utils.py

All applied, thanks.

Regards,
-Denis