From 783832addef27b4a196904f1119eaa4dc8387608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marta=20Marczykowska-G=C3=B3recka?= Date: Fri, 15 Nov 2019 17:45:09 +0100 Subject: [PATCH] Correct inconsistent behavior on unavailable usage data fixes QubesOS/qubes-issues#5463 --- qubes/storage/__init__.py | 11 +++++++---- qubes/tests/storage.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/qubes/storage/__init__.py b/qubes/storage/__init__.py index b8bd79d9..da518a99 100644 --- a/qubes/storage/__init__.py +++ b/qubes/storage/__init__.py @@ -801,10 +801,13 @@ class Pool: Contains data_usage for usage in bytes and data_size for pool size; other implementations may add more implementation-specific detail""" - return { - 'data_usage': self.usage, - 'data_size': self.size - } + result = {} + if self.usage is not None: + result['data_usage'] = self.usage + if self.size is not None: + result['data_size'] = self.size + + return result def _not_implemented(self, method_name): ''' Helper for emitting helpful `NotImplementedError` exceptions ''' diff --git a/qubes/tests/storage.py b/qubes/tests/storage.py index 6a49984b..fd58ae59 100644 --- a/qubes/tests/storage.py +++ b/qubes/tests/storage.py @@ -165,3 +165,32 @@ class TC_00_Pool(QubesTestCase): self.loop.run_until_complete(vm.create_on_disk(pool=pool)) with self.assertRaises(qubes.exc.QubesPoolInUseError): self.loop.run_until_complete(self.app.remove_pool(pool_name)) + + def test_006_pool_usage_qubespool(self): + qubes_pool = self.app.get_pool('varlibqubes') + + usage_details = qubes_pool.usage_details + + self.assertIsNotNone(usage_details['data_size'], + "Data size incorrectly reported as None") + self.assertIsNotNone(usage_details['data_usage'], + "Data usage incorrectly reported as None") + + def test_007_pool_kernels_usage(self): + pool_name = 'kernels_test_pools' + dir_path = '/tmp/{}'.format(pool_name) + pool = self.loop.run_until_complete( + self.app.add_pool(name=pool_name, + driver='linux-kernel', + dir_path=dir_path)) + self.assertTrue(self.assertPoolExists(pool_name)) + + qubes_pool = self.app.get_pool(pool_name) + + usage_details = qubes_pool.usage_details + + self.assertEqual(usage_details, + {}, + "Kernels pool should not report usage details.") + + self.loop.run_until_complete(self.app.remove_pool(pool_name))