From bd123a3812fa4ba86f752d39b453a8b61da65862 Mon Sep 17 00:00:00 2001 From: Wojtek Porczyk Date: Tue, 19 Sep 2017 17:01:29 +0200 Subject: [PATCH] tests: fix some memory leaks This is work in progress. There are still more leaks. --- qubes/tests/app.py | 11 +++++++++++ qubes/tests/init.py | 10 ++++++++++ qubes/tests/storage_file.py | 6 ++++++ qubes/tests/storage_kernels.py | 4 ++++ qubes/tests/vm/adminvm.py | 5 +++++ qubes/tests/vm/appvm.py | 11 ++++++++++- qubes/tests/vm/dispvm.py | 9 +++++++++ qubes/tests/vm/mix/net.py | 13 +++++++++++++ qubes/tests/vm/qubesvm.py | 4 +++- qubes/vm/qubesvm.py | 2 ++ 10 files changed, 73 insertions(+), 2 deletions(-) diff --git a/qubes/tests/app.py b/qubes/tests/app.py index 3b38fb11..5ef51f8d 100644 --- a/qubes/tests/app.py +++ b/qubes/tests/app.py @@ -160,6 +160,17 @@ class TC_30_VMCollection(qubes.tests.QubesTestCase): self.testvm2 = qubes.tests.init.TestVM( None, None, qid=2, name='testvm2') + self.addCleanup(self.cleanup_vmcollection) + + def cleanup_vmcollection(self): + self.testvm1.close() + self.testvm2.close() + self.vms.close() + del self.testvm1 + del self.testvm2 + del self.vms + del self.app + def test_000_contains(self): self.vms._dict = {1: self.testvm1} diff --git a/qubes/tests/init.py b/qubes/tests/init.py index 777a925f..09d133bf 100644 --- a/qubes/tests/init.py +++ b/qubes/tests/init.py @@ -320,6 +320,16 @@ class TC_30_VMCollection(qubes.tests.QubesTestCase): self.testvm1 = TestVM(None, None, qid=1, name='testvm1') self.testvm2 = TestVM(None, None, qid=2, name='testvm2') + self.addCleanup(self.cleanup_testvm) + + def cleanup_testvm(self): + self.vms.close() + self.testvm1.close() + self.testvm2.close() + del self.testvm1 + del self.testvm2 + del self.vms + del self.app def test_000_contains(self): self.vms._dict = {1: self.testvm1} diff --git a/qubes/tests/storage_file.py b/qubes/tests/storage_file.py index 5d9a8f7d..1b79610e 100644 --- a/qubes/tests/storage_file.py +++ b/qubes/tests/storage_file.py @@ -75,6 +75,8 @@ class TC_00_FilePool(qubes.tests.QubesTestCase): def tearDown(self): self.app.cleanup() + self.app.close() + del self.app super(TC_00_FilePool, self).tearDown() def test000_default_pool_dir(self): @@ -120,6 +122,8 @@ class TC_01_FileVolumes(qubes.tests.QubesTestCase): """ Remove the file based storage pool after testing """ self.app.remove_pool("test-pool") self.app.cleanup() + self.app.close() + del self.app super(TC_01_FileVolumes, self).tearDown() shutil.rmtree(self.POOL_DIR, ignore_errors=True) @@ -328,6 +332,8 @@ class TC_03_FilePool(qubes.tests.QubesTestCase): """ Remove the file based storage pool after testing """ self.app.remove_pool("test-pool") self.app.cleanup() + self.app.close() + del self.app self.base_dir_patch3.stop() self.base_dir_patch2.stop() self.base_dir_patch.stop() diff --git a/qubes/tests/storage_kernels.py b/qubes/tests/storage_kernels.py index 9261b0c3..3e657c60 100644 --- a/qubes/tests/storage_kernels.py +++ b/qubes/tests/storage_kernels.py @@ -80,6 +80,8 @@ class TC_01_KernelVolumes(qubes.tests.QubesTestCase): """ Remove the file based storage pool after testing """ self.app.remove_pool("test-pool") self.app.cleanup() + self.app.close() + del self.app super(TC_01_KernelVolumes, self).tearDown() shutil.rmtree(self.POOL_DIR, ignore_errors=True) @@ -233,6 +235,8 @@ class TC_03_KernelPool(qubes.tests.QubesTestCase): """ Remove the file based storage pool after testing """ self.app.remove_pool("test-pool") self.app.cleanup() + self.app.close() + del self.app super(TC_03_KernelPool, self).tearDown() shutil.rmtree(self.POOL_DIR, ignore_errors=True) if os.path.exists('/tmp/qubes-test'): diff --git a/qubes/tests/vm/adminvm.py b/qubes/tests/vm/adminvm.py index 50d7494c..0560e130 100644 --- a/qubes/tests/vm/adminvm.py +++ b/qubes/tests/vm/adminvm.py @@ -39,11 +39,16 @@ class TC_00_AdminVM(qubes.tests.QubesTestCase): self.vm = qubes.vm.adminvm.AdminVM(self.app, xml=None) mock_qdb.assert_called_once_with('dom0') + self.addCleanup(self.cleanup_adminvm) except: # pylint: disable=bare-except if self.id().endswith('.test_000_init'): raise self.skipTest('setup failed') + def cleanup_adminvm(self): + self.vm.close() + del self.vm + def test_000_init(self): pass diff --git a/qubes/tests/vm/appvm.py b/qubes/tests/vm/appvm.py index ecfb9283..b4bceda9 100644 --- a/qubes/tests/vm/appvm.py +++ b/qubes/tests/vm/appvm.py @@ -67,12 +67,21 @@ class TC_90_AppVM(qubes.tests.vm.qubesvm.QubesVMTestsMixin, qid=1, name=qubes.tests.VMPREFIX + 'template') self.app.domains[self.template.name] = self.template self.app.domains[self.template] = self.template + self.addCleanup(self.cleanup_appvm) + + def cleanup_appvm(self): + self.template.close() + del self.template + self.app.domains.clear() + self.app.pools.clear() def get_vm(self, **kwargs): - return qubes.vm.appvm.AppVM(self.app, None, + vm = qubes.vm.appvm.AppVM(self.app, None, qid=2, name=qubes.tests.VMPREFIX + 'test', template=self.template, **kwargs) + self.addCleanup(vm.close) + return vm def test_000_init(self): self.get_vm() diff --git a/qubes/tests/vm/dispvm.py b/qubes/tests/vm/dispvm.py index c0bb1ed7..afc153ba 100644 --- a/qubes/tests/vm/dispvm.py +++ b/qubes/tests/vm/dispvm.py @@ -57,6 +57,15 @@ class TC_00_DispVM(qubes.tests.QubesTestCase): name='test-vm', template=self.template, label='red') self.app.domains[self.appvm.name] = self.appvm self.app.domains[self.appvm] = self.appvm + self.addCleanup(self.cleanup_dispvm) + + def cleanup_dispvm(self): + self.template.close() + self.appvm.close() + del self.template + del self.appvm + self.app.domains.clear() + self.app.pools.clear() @asyncio.coroutine def mock_coro(self, *args, **kwargs): diff --git a/qubes/tests/vm/mix/net.py b/qubes/tests/vm/mix/net.py index 9ee27e48..356f65a7 100644 --- a/qubes/tests/vm/mix/net.py +++ b/qubes/tests/vm/mix/net.py @@ -52,6 +52,18 @@ class TC_00_NetVMMixin( self.app.domains._dict[domain.qid] = domain self.app.default_netvm = self.netvm1 self.app.default_fw_netvm = self.netvm1 + self.addCleanup(self.cleanup_netvms) + + def cleanup_netvms(self): + self.netvm1.close() + self.netvm2.close() + self.nonetvm.close() + self.app.domains.close() + del self.netvm1 + del self.netvm2 + del self.nonetvm + del self.app.default_netvm + del self.app.default_fw_netvm @qubes.tests.skipUnlessDom0 @@ -81,6 +93,7 @@ class TC_00_NetVMMixin( def test_143_netvm_loopback(self): vm = self.get_vm() self.app.domains = {1: vm, vm: vm} + self.addCleanup(self.app.domains.clear) self.assertPropertyInvalidValue(vm, 'netvm', vm) def test_150_ip(self): diff --git a/qubes/tests/vm/qubesvm.py b/qubes/tests/vm/qubesvm.py index 08b37db9..fe508fba 100644 --- a/qubes/tests/vm/qubesvm.py +++ b/qubes/tests/vm/qubesvm.py @@ -122,9 +122,11 @@ class QubesVMTestsMixin(object): self.app.vmm.offline_mode = True def get_vm(self, **kwargs): - return qubes.vm.qubesvm.QubesVM(self.app, None, + vm = qubes.vm.qubesvm.QubesVM(self.app, None, qid=1, name=qubes.tests.VMPREFIX + 'test', **kwargs) + self.addCleanup(vm.close) + return vm def assertPropertyValue(self, vm, prop_name, set_value, expected_value, expected_xml_content=None): diff --git a/qubes/vm/qubesvm.py b/qubes/vm/qubesvm.py index 3588becb..1eaf97e0 100644 --- a/qubes/vm/qubesvm.py +++ b/qubes/vm/qubesvm.py @@ -710,6 +710,8 @@ class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM): if self._qdb_connection is not None: self._qdb_connection.close() self._qdb_connection = None + if self._libvirt_domain is not None: + self._libvirt_domain = None super().close() def __hash__(self):