From a89d3f0cae9a075677d928c8184539d7900f1ac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Thu, 9 Jan 2020 01:42:46 +0100 Subject: [PATCH] tests: allow extensions to cleanup objects references Extension objects are singletons and normally do not require any special cleanup. But in case of tests, we try to remove all the qubes objects between tests and the cache in usb extension makes it hard. Add a 'qubes-close' event that extensions can handle to remove extra references stored in extension objects themselves. --- qubes/app.py | 17 +++++++++++++++++ qubes/tests/vm/qubesvm.py | 3 +++ 2 files changed, 20 insertions(+) diff --git a/qubes/app.py b/qubes/app.py index 7e1576b7..c8e875b2 100644 --- a/qubes/app.py +++ b/qubes/app.py @@ -724,6 +724,17 @@ class Qubes(qubes.PropertyHolder): :param event: Event name (``'pool-delete'``) :param pool: Pool object + .. event:: qubes-close (subject, event) + + Fired when this Qubes() object instance is going to be closed + and destroyed. In practice it is called only during tests, to + cleanup objects from one test, before another. + It is _not_ called when qubesd daemon is stopped. + + :param subject: Event emitter + :param event: Event name (``'qubes-close'``) + + Methods and attributes: """ default_guivm = qubes.VMProperty( @@ -1085,6 +1096,9 @@ class Qubes(qubes.PropertyHolder): for frame in traceback.extract_stack(): self.log.debug('%s', frame) + # let all the extension cleanup things + self.fire_event('qubes-close') + super().close() if self._domain_event_callback_id is not None: @@ -1094,6 +1108,9 @@ class Qubes(qubes.PropertyHolder): # Only our Lord, The God Almighty, knows what references # are kept in extensions. + # NOTE: this doesn't really delete extension objects - Extension class + # saves reference to instance, and also various registered (class level) + # event handlers do that too del self._extensions for vm in self.domains: diff --git a/qubes/tests/vm/qubesvm.py b/qubes/tests/vm/qubesvm.py index 1c57695b..c83a4e46 100644 --- a/qubes/tests/vm/qubesvm.py +++ b/qubes/tests/vm/qubesvm.py @@ -273,6 +273,9 @@ class QubesVMTestsMixin(object): def tearDown(self): try: + # self.app is not a real events emiter, so make the call manually + for handler in qubes.Qubes.__handlers__.get('qubes-close'): + handler(self.app, 'qubes-close') self.app.domains.close() except AttributeError: pass