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.
This commit is contained in:
Mattias Walström
2024-12-19 10:14:15 +01:00
parent 8e38b34c55
commit a90294fe08
+10 -1
View File
@@ -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")