From a90294fe0845fb0df6b68bec3bac8d49aab3273c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Walstr=C3=B6m?= Date: Thu, 19 Dec 2024 08:39:05 +0100 Subject: [PATCH] test: infamy: Add optional test cleanup If a test need to clean up itself disregarding test status, it can add an optional test-cleanup phase. This is useful if the test in some circomstances leave the device in a bad state. This can be used to restore the unit to its ordinary state. --- test/infamy/tap.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/infamy/tap.py b/test/infamy/tap.py index 42a2ed61..5c805a02 100644 --- a/test/infamy/tap.py +++ b/test/infamy/tap.py @@ -13,8 +13,17 @@ class Test: if self.out == sys.stdout: sys.stdout = self.commenter + self.test_cleanup=[] self.steps = 0 + def push_test_cleanup(self, fn): + self.test_cleanup.append(fn) + + def cleanup(self): + infamy.netns.IsolatedMacVlans.Cleanup() + for test_cleanup in reversed(self.test_cleanup): + test_cleanup() + def __enter__(self): now = datetime.datetime.now().strftime("%F %T") self.out.write(f"# Starting ({now})\n") @@ -26,7 +35,7 @@ class Test: self.out.write(f"# Exiting ({now})\n") self.out.flush() - infamy.netns.IsolatedMacVlans.Cleanup() + self.cleanup() if not e: self._not_ok("Missing explicit test result\n")