Jelajahi Sumber

storage: use None for size/usage properties if unknown

Raising NotImplementedError in a _property_ is weird behaviour, better
suited for actions (methods). Use None instead.

QubesOS/qubes-issues#3241
Marek Marczykowski-Górecki 6 tahun lalu
induk
melakukan
03dc3e315e
3 mengubah file dengan 13 tambahan dan 17 penghapusan
  1. 7 8
      qubes/api/admin.py
  2. 4 4
      qubes/storage/__init__.py
  3. 2 5
      qubes/tests/api_admin.py

+ 7 - 8
qubes/api/admin.py

@@ -607,14 +607,13 @@ class QubesAdminAPI(qubes.api.AbstractQubesAPI):
         self.fire_event_for_permission(pool=pool)
 
         other_info = ''
-        try:
-            other_info += 'size={}\n'.format(pool.size)
-        except NotImplementedError:
-            pass
-        try:
-            other_info += 'usage={}\n'.format(pool.usage)
-        except NotImplementedError:
-            pass
+        pool_size = pool.size
+        if pool_size is not None:
+            other_info += 'size={}\n'.format(pool_size)
+
+        pool_usage = pool.usage
+        if pool_usage is not None:
+            other_info += 'usage={}\n'.format(pool_usage)
 
         try:
             included_in = pool.included_in(self.app)

+ 4 - 4
qubes/storage/__init__.py

@@ -828,13 +828,13 @@ class Pool(object):
 
     @property
     def size(self):
-        ''' Storage pool size in bytes '''
-        raise self._not_implemented("size")
+        ''' Storage pool size in bytes, or None if unknown '''
+        return None
 
     @property
     def usage(self):
-        ''' Space used in the pool, in bytes '''
-        raise self._not_implemented("usage")
+        ''' Space used in the pool in bytes, or None if unknown '''
+        return None
 
     def _not_implemented(self, method_name):
         ''' Helper for emitting helpful `NotImplementedError` exceptions '''

+ 2 - 5
qubes/tests/api_admin.py

@@ -567,13 +567,10 @@ class TC_00_VMs(AdminAPITestCase):
     def test_151_pool_info_unsupported_size(self):
         self.app.pools = {
             'pool1': unittest.mock.Mock(config={
-                'param1': 'value1', 'param2': 'value2'})
+                'param1': 'value1', 'param2': 'value2'},
+                size=None, usage=None),
         }
         self.app.pools['pool1'].included_in.return_value = None
-        type(self.app.pools['pool1']).size = unittest.mock.PropertyMock(
-            side_effect=NotImplementedError)
-        type(self.app.pools['pool1']).usage = unittest.mock.PropertyMock(
-            side_effect=NotImplementedError)
         value = self.call_mgmt_func(b'admin.pool.Info', b'dom0', b'pool1')
 
         self.assertEqual(value,