diff --git a/lisa/tests/base.py b/lisa/tests/base.py index 49faaeeee5b04d45b12df3985845ac438330b2e7..9bb51ffcdf0b609b6fd9995d10bc77b18d9296a1 100644 --- a/lisa/tests/base.py +++ b/lisa/tests/base.py @@ -44,6 +44,7 @@ from lisa.target import Target from lisa.utils import ( Serializable, memoized, ArtifactPath, non_recursive_property, update_wrapper_doc, ExekallTaggable, annotations_from_signature, + nullcontext, ) from lisa.datautils import df_filter_task_ids from lisa.trace import FtraceCollector, FtraceConf, DmesgCollector @@ -1233,7 +1234,7 @@ class RTATestBundle(FtraceTestBundle, DmesgTestBundle): return '/' + cg.name @classmethod - def run_rtapp(cls, target, res_dir, profile=None, ftrace_coll=None, cg_cfg=None): + def run_rtapp(cls, target, res_dir, profile=None, ftrace_coll=None, cg_cfg=None, wipe_run_dir=True): """ Run the given RTA profile on the target, and collect an ftrace trace. @@ -1258,6 +1259,10 @@ class RTATestBundle(FtraceTestBundle, DmesgTestBundle): :meth:`lisa.tests.base.RTATestBundle.get_cgroup_configuration` is called with ``target.plat_info``. :type cg_cfg: dict + + :param wipe_run_dir: Remove the run directory on the target after + execution of the workload. + :type wipe_run_dir: bool """ trace_path = ArtifactPath.join(res_dir, cls.TRACE_PATH) @@ -1272,6 +1277,7 @@ class RTATestBundle(FtraceTestBundle, DmesgTestBundle): profile, res_dir=res_dir) cgroup = cls._target_configure_cgroup(target, cg_cfg) as_root = cgroup is not None + wload_cm = wload if wipe_run_dir else nullcontext(wload) # Pre-hit the calibration information, in case this is a lazy value. # This avoids polluting the trace and the dmesg output with the @@ -1279,7 +1285,7 @@ class RTATestBundle(FtraceTestBundle, DmesgTestBundle): # anything useful, it's reasonable to do it here. target.plat_info['rtapp']['calib'] - with dmesg_coll, ftrace_coll, target.freeze_userspace(): + with wload_cm, dmesg_coll, ftrace_coll, target.freeze_userspace(): wload.run(cgroup=cgroup, as_root=as_root) ftrace_coll.get_trace(trace_path) diff --git a/lisa/wlgen/workload.py b/lisa/wlgen/workload.py index 307a2f60cea3d9726d404e47728c8a57290e4780..017066d58e47dd731637aba8a570713d6ed2554a 100644 --- a/lisa/wlgen/workload.py +++ b/lisa/wlgen/workload.py @@ -105,12 +105,25 @@ class Workload(Loggable): def wipe_run_dir(self): """ - Wipe all content from the ``run_dir`` target directory + Wipe all content from the ``run_dir`` target directory. + + .. note :: This function should only be called directly in interactive + sessions. For other purposes, use :class:`Workload` instances as a + context manager. """ logger = self.get_logger() logger.info("Wiping run_dir [%s]", self.run_dir) self.target.execute("rm -rf {}".format(quote(self.run_dir))) + def __enter__(self): + return self + + def __exit__(exc_type, exc_val, exc_tb): + """ + Wipe the run directory on the target. + """ + self.wipe_run_dir() + def run(self, cpus=None, cgroup=None, background=False, as_root=False, timeout=None): """ Execute the workload on the configured target. diff --git a/tests/test_events_checkers.py b/tests/test_events_checkers.py index bbbeb445311246f4ee3f05dd6b33df983d97c0c5..49483ef92489ea69e782d458563a0cbe672735e0 100644 --- a/tests/test_events_checkers.py +++ b/tests/test_events_checkers.py @@ -16,8 +16,8 @@ # from unittest import TestCase -from .utils import nullcontext +from lisa.utils import nullcontext from lisa.trace import TraceEventChecker, AndTraceEventChecker, OrTraceEventChecker, MissingTraceEventError """ A test suite for event checking infrastructure.""" diff --git a/tests/utils.py b/tests/utils.py index 79715eb88cafa3f49a6ae63acb990dcb73a1c64d..def19d5577ec5963d344ec5864488e70ce97966d 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -58,16 +58,3 @@ class StorageTestCase(TestCase): def tearDown(self): shutil.rmtree(self.res_dir) -def nullcontext(enter_result=None): - """ - Backport of Python 3.7 contextlib.nullcontext - """ - - class CM: - def __enter__(self): - return enter_result - - def __exit__(self, *args, **kwargs): - return - - return CM()