From: Jan Pokorny <japokorn(a)redhat.com>
- created new singletons from lv_info and pv_info
- edited the code to use them
- updated test to reflect the interface changes
---
blivet/populator/helpers/lvm.py | 16 ++++---
blivet/populator/populator.py | 24 +++--------
blivet/staticdata/__init__.py | 1 +
blivet/staticdata/lvm_info.py | 67 ++++++++++++++++++++++++++++
setup.py | 2 +-
tests/populator_test.py | 96 ++++++++++++++++++++++-------------------
6 files changed, 136 insertions(+), 70 deletions(-)
create mode 100644 blivet/staticdata/__init__.py
create mode 100644 blivet/staticdata/lvm_info.py
diff --git a/blivet/populator/helpers/lvm.py b/blivet/populator/helpers/lvm.py
index 94f2012..b07c2e9 100644
--- a/blivet/populator/helpers/lvm.py
+++ b/blivet/populator/helpers/lvm.py
@@ -37,6 +37,9 @@
from .devicepopulator import DevicePopulator
from .formatpopulator import FormatPopulator
+from ...staticdata.lvm_info import lvs_info, pvs_info
+
+
import logging
log = logging.getLogger("blivet")
@@ -86,7 +89,8 @@ class LVMFormatPopulator(FormatPopulator):
def _get_kwargs(self):
kwargs = super()._get_kwargs()
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
+
name = udev.device_get_name(self.data)
if pv_info:
if pv_info.vg_name:
@@ -118,7 +122,7 @@ def _update_lvs(self):
return
vg_name = vg_device.name
- lv_info = dict((k, v) for (k, v) in iter(self._devicetree.lv_info.items())
+ lv_info = dict((k, v) for (k, v) in iter(lvs_info.cache.items())
if v.vg_name == vg_name)
# FIXME: This should account for added/removed LVs.
@@ -343,7 +347,7 @@ def create_internal_lv(lv):
log.warning("Failed to determine parent LV for an internal LV '%s'", lv.name)
def _add_vg_device(self):
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if pv_info:
vg_name = pv_info.vg_name
vg_uuid = pv_info.vg_uuid
@@ -398,7 +402,7 @@ def _handle_vg_rename(self):
if vg_device is None:
return
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if not pv_info or not pv_info.vg_name:
return
@@ -408,7 +412,7 @@ def _handle_vg_rename(self):
# TODO: update name registry
def _update_pv_format(self):
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if not pv_info:
return
@@ -420,7 +424,7 @@ def _update_pv_format(self):
def update(self):
self._devicetree.drop_lvm_cache()
self._update_pv_format()
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
vg_device = self._get_vg_device()
if vg_device is None:
# The VG device isn't in the tree. The PV might have just been
diff --git a/blivet/populator/populator.py b/blivet/populator/populator.py
index 816c226..84181f9 100644
--- a/blivet/populator/populator.py
+++ b/blivet/populator/populator.py
@@ -45,6 +45,8 @@
from ..threads import SynchronizedMeta
from .helpers import get_device_helper, get_format_helper
+from ..staticdata.lvm_info import lvs_info, pvs_info
+
import logging
log = logging.getLogger("blivet")
@@ -161,7 +163,7 @@ def _add_slave_devices(self, info):
slave_dev = self.get_device_by_name(slave_name)
if slave_dev is None:
if udev.device_is_dm_lvm(info):
- if slave_name not in self.lv_info:
+ if slave_name not in lvs_info.cache:
# we do not expect hidden lvs to be in the tree
continue
@@ -528,26 +530,10 @@ def _populate(self):
# inconsistencies are ignored or resolved.
self._handle_inconsistencies()
- @property
- def pv_info(self):
- if self._pvs_cache is None:
- pvs = blockdev.lvm.pvs()
- self._pvs_cache = dict((pv.pv_name, pv) for pv in pvs) # pylint: disable=attribute-defined-outside-init
-
- return self._pvs_cache
-
- @property
- def lv_info(self):
- if self._lvs_cache is None:
- lvs = blockdev.lvm.lvs()
- self._lvs_cache = dict(("%s-%s" % (lv.vg_name, lv.lv_name), lv) for lv in lvs) # pylint: disable=attribute-defined-outside-init
-
- return self._lvs_cache
-
def drop_lvm_cache(self):
""" Drop cached lvm information. """
- self._pvs_cache = None # pylint: disable=attribute-defined-outside-init
- self._lvs_cache = None # pylint: disable=attribute-defined-outside-init
+ lvs_info.drop_cache()
+ pvs_info.drop_cache()
def handle_nodev_filesystems(self):
for line in open("/proc/mounts").readlines():
diff --git a/blivet/staticdata/__init__.py b/blivet/staticdata/__init__.py
new file mode 100644
index 0000000..333035f
--- /dev/null
+++ b/blivet/staticdata/__init__.py
@@ -0,0 +1 @@
+from .lvm_info import lvs_info, pvs_info
diff --git a/blivet/staticdata/lvm_info.py b/blivet/staticdata/lvm_info.py
new file mode 100644
index 0000000..ed2e995
--- /dev/null
+++ b/blivet/staticdata/lvm_info.py
@@ -0,0 +1,67 @@
+# volume_info.py
+# Backend code for populating a DeviceTree.
+#
+# Copyright (C) 2009-2016 Red Hat, Inc.
+#
+# This copyrighted material is made available to anyone wishing to use,
+# modify, copy, or redistribute it subject to the terms and conditions of
+# the GNU Lesser General Public License v.2, or (at your option) any later
+# version. This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY expressed or implied, including the implied
+# warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+# the GNU Lesser General Public License for more details. You should have
+# received a copy of the GNU Lesser General Public License along with this
+# program; if not, write to the Free Software Foundation, Inc., 51 Franklin
+# Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat trademarks
+# that are incorporated in the source code or documentation are not subject
+# to the GNU Lesser General Public License and may only be used or
+# replicated with the express permission of Red Hat, Inc.
+#
+# Red Hat Author(s): Jan Pokorny <japokorn(a)redhat.com>
+#
+
+import gi
+gi.require_version("BlockDev", "1.0")
+
+from gi.repository import BlockDev as blockdev
+
+import logging
+log = logging.getLogger("blivet")
+
+
+class LVsInfo(object):
+ """ Class to be used as a singleton.
+ Maintains the LVs cache.
+ """
+
+ @property
+ def cache(self):
+ if self._lvs_cache is None:
+ lvs = blockdev.lvm.lvs()
+ self._lvs_cache = dict(("%s-%s" % (lv.vg_name, lv.lv_name), lv) for lv in lvs) # pylint: disable=attribute-defined-outside-init
+
+ return self._lvs_cache
+
+ def drop_cache(self):
+ self._lvs_cache = None # pylint: disable=attribute-defined-outside-init
+
+lvs_info = LVsInfo()
+
+
+class PVsInfo(object):
+ """ Class to be used as a singleton.
+ Maintains the PVs cache.
+ """
+
+ @property
+ def cache(self):
+ if self._pvs_cache is None:
+ pvs = blockdev.lvm.pvs()
+ self._pvs_cache = dict((pv.pv_name, pv) for pv in pvs) # pylint: disable=attribute-defined-outside-init
+
+ return self._pvs_cache
+
+ def drop_cache(self):
+ self._pvs_cache = None # pylint: disable=attribute-defined-outside-init
+
+pvs_info = PVsInfo()
diff --git a/setup.py b/setup.py
index 8481d51..dbbf9f0 100644
--- a/setup.py
+++ b/setup.py
@@ -93,4 +93,4 @@ def make_release_tree(self, base_dir, files):
author='David Lehman', author_email='dlehman(a)redhat.com',
url='http://fedoraproject.org/wiki/blivet',
data_files=data_files,
- packages=['blivet', 'blivet.devices', 'blivet.devicelibs', 'blivet.events', 'blivet.formats', 'blivet.populator', 'blivet.tasks', 'blivet.populator.helpers'])
+ packages=['blivet', 'blivet.devices', 'blivet.devicelibs', 'blivet.events', 'blivet.formats', 'blivet.populator', 'blivet.staticdata', 'blivet.tasks', 'blivet.populator.helpers'])
diff --git a/tests/populator_test.py b/tests/populator_test.py
index 8975b0f..fabe09c 100644
--- a/tests/populator_test.py
+++ b/tests/populator_test.py
@@ -1,7 +1,7 @@
import gi
import os
import unittest
-from unittest.mock import call, patch, sentinel, Mock
+from unittest.mock import call, patch, sentinel, Mock, PropertyMock
gi.require_version("BlockDev", "1.0")
from gi.repository import BlockDev as blockdev
@@ -908,17 +908,19 @@ def test_run(self, *args):
vg_device.lvs = []
get_device_by_uuid.return_value = vg_device
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- helper = self.helper_class(devicetree, data, device)
- self.assertFalse(device in vg_device.parents)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path: pv_info}
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ helper = self.helper_class(devicetree, data, device)
+ self.assertFalse(device in vg_device.parents)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
- self.assertEqual(get_device_by_uuid.call_count, 3)
- get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
- self.assertTrue(device in vg_device.parents)
+ self.assertEqual(get_device_by_uuid.call_count, 3)
+ get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
+ self.assertTrue(device in vg_device.parents)
get_device_by_uuid.reset_mock()
get_device_by_uuid.return_value = None
@@ -931,18 +933,20 @@ def test_run(self, *args):
pv_info.vg_free_count = 0
pv_info.vg_pv_count = 1
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- helper = self.helper_class(devicetree, data, device)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
-
- self.assertEqual(get_device_by_uuid.call_count, 2)
- get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
- vg_device = devicetree.get_device_by_name(pv_info.vg_name)
- self.assertTrue(vg_device is not None)
- devicetree._remove_device(vg_device)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path: pv_info}
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ helper = self.helper_class(devicetree, data, device)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+
+ self.assertEqual(get_device_by_uuid.call_count, 2)
+ get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
+ vg_device = devicetree.get_device_by_name(pv_info.vg_name)
+ self.assertTrue(vg_device is not None)
+ devicetree._remove_device(vg_device)
get_device_by_uuid.reset_mock()
@@ -976,27 +980,31 @@ def gdbu(uuid, **kwargs): # pylint: disable=unused-argument
return next((d for d in devicetree.devices if d.uuid == uuid), None)
get_device_by_uuid.side_effect = gdbu
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- self.assertEqual(devicetree.get_device_by_name(pv_info.vg_name, incomplete=True), None)
- helper = self.helper_class(devicetree, data, device)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
-
- self.assertEqual(get_device_by_uuid.call_count, 4,
- get_device_by_uuid.mock_calls) # two for vg and one for each lv
- get_device_by_uuid.assert_has_calls([call(pv_info.vg_uuid, incomplete=True),
- call(lv1.uuid),
- call(lv2.uuid)],
- any_order=True)
- vg_device = devicetree.get_device_by_name(pv_info.vg_name)
- self.assertTrue(vg_device is not None)
-
- lv1_device = devicetree.get_device_by_name(lv1_name)
- self.assertEqual(lv1_device.uuid, lv1.uuid)
- lv2_device = devicetree.get_device_by_name(lv2_name)
- self.assertEqual(lv2_device.uuid, lv2.uuid)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path: pv_info}
+ with patch("blivet.staticdata.lvm_info.LVsInfo.cache", new_callable=PropertyMock) as mock_lvs_cache:
+ mock_lvs_cache.return_value = lv_info
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ self.assertEqual(devicetree.get_device_by_name(pv_info.vg_name, incomplete=True), None)
+ helper = self.helper_class(devicetree, data, device)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+
+ self.assertEqual(get_device_by_uuid.call_count, 4,
+ get_device_by_uuid.mock_calls) # two for vg and one for each lv
+ get_device_by_uuid.assert_has_calls([call(pv_info.vg_uuid, incomplete=True),
+ call(lv1.uuid),
+ call(lv2.uuid)],
+ any_order=True)
+ vg_device = devicetree.get_device_by_name(pv_info.vg_name)
+ self.assertTrue(vg_device is not None)
+
+ lv1_device = devicetree.get_device_by_name(lv1_name)
+ self.assertEqual(lv1_device.uuid, lv1.uuid)
+ lv2_device = devicetree.get_device_by_name(lv2_name)
+ self.assertEqual(lv2_device.uuid, lv2.uuid)
class MDFormatPopulatorTestCase(FormatPopulatorTestCase):
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/cb6fff9588a1d5eb04b559c412a36c…
From: Jan Pokorny <japokorn(a)redhat.com>
- updated LVM tests to be compliant with PEP8
---
tests/populator_test.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tests/populator_test.py b/tests/populator_test.py
index fabe09c..443df5f 100644
--- a/tests/populator_test.py
+++ b/tests/populator_test.py
@@ -993,7 +993,7 @@ def gdbu(uuid, **kwargs): # pylint: disable=unused-argument
msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
self.assertEqual(get_device_by_uuid.call_count, 4,
- get_device_by_uuid.mock_calls) # two for vg and one for each lv
+ get_device_by_uuid.mock_calls) # two for vg and one for each lv
get_device_by_uuid.assert_has_calls([call(pv_info.vg_uuid, incomplete=True),
call(lv1.uuid),
call(lv2.uuid)],
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/9ef220ae421eec2696703080e08650…
From: Jan Pokorny <japokorn(a)redhat.com>
- created new singletons from lv_info and pv_info
- edited the code to use them
- updated test to reflect the interface changes
---
blivet/populator/helpers/lvm.py | 16 ++++---
blivet/populator/populator.py | 24 +++--------
blivet/staticdata/__init__.py | 1 +
blivet/staticdata/lvm_info.py | 67 ++++++++++++++++++++++++++++
setup.py | 2 +-
tests/populator_test.py | 96 ++++++++++++++++++++++-------------------
6 files changed, 136 insertions(+), 70 deletions(-)
create mode 100644 blivet/staticdata/__init__.py
create mode 100644 blivet/staticdata/lvm_info.py
diff --git a/blivet/populator/helpers/lvm.py b/blivet/populator/helpers/lvm.py
index 94f2012..b07c2e9 100644
--- a/blivet/populator/helpers/lvm.py
+++ b/blivet/populator/helpers/lvm.py
@@ -37,6 +37,9 @@
from .devicepopulator import DevicePopulator
from .formatpopulator import FormatPopulator
+from ...staticdata.lvm_info import lvs_info, pvs_info
+
+
import logging
log = logging.getLogger("blivet")
@@ -86,7 +89,8 @@ class LVMFormatPopulator(FormatPopulator):
def _get_kwargs(self):
kwargs = super()._get_kwargs()
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
+
name = udev.device_get_name(self.data)
if pv_info:
if pv_info.vg_name:
@@ -118,7 +122,7 @@ def _update_lvs(self):
return
vg_name = vg_device.name
- lv_info = dict((k, v) for (k, v) in iter(self._devicetree.lv_info.items())
+ lv_info = dict((k, v) for (k, v) in iter(lvs_info.cache.items())
if v.vg_name == vg_name)
# FIXME: This should account for added/removed LVs.
@@ -343,7 +347,7 @@ def create_internal_lv(lv):
log.warning("Failed to determine parent LV for an internal LV '%s'", lv.name)
def _add_vg_device(self):
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if pv_info:
vg_name = pv_info.vg_name
vg_uuid = pv_info.vg_uuid
@@ -398,7 +402,7 @@ def _handle_vg_rename(self):
if vg_device is None:
return
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if not pv_info or not pv_info.vg_name:
return
@@ -408,7 +412,7 @@ def _handle_vg_rename(self):
# TODO: update name registry
def _update_pv_format(self):
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if not pv_info:
return
@@ -420,7 +424,7 @@ def _update_pv_format(self):
def update(self):
self._devicetree.drop_lvm_cache()
self._update_pv_format()
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
vg_device = self._get_vg_device()
if vg_device is None:
# The VG device isn't in the tree. The PV might have just been
diff --git a/blivet/populator/populator.py b/blivet/populator/populator.py
index 816c226..84181f9 100644
--- a/blivet/populator/populator.py
+++ b/blivet/populator/populator.py
@@ -45,6 +45,8 @@
from ..threads import SynchronizedMeta
from .helpers import get_device_helper, get_format_helper
+from ..staticdata.lvm_info import lvs_info, pvs_info
+
import logging
log = logging.getLogger("blivet")
@@ -161,7 +163,7 @@ def _add_slave_devices(self, info):
slave_dev = self.get_device_by_name(slave_name)
if slave_dev is None:
if udev.device_is_dm_lvm(info):
- if slave_name not in self.lv_info:
+ if slave_name not in lvs_info.cache:
# we do not expect hidden lvs to be in the tree
continue
@@ -528,26 +530,10 @@ def _populate(self):
# inconsistencies are ignored or resolved.
self._handle_inconsistencies()
- @property
- def pv_info(self):
- if self._pvs_cache is None:
- pvs = blockdev.lvm.pvs()
- self._pvs_cache = dict((pv.pv_name, pv) for pv in pvs) # pylint: disable=attribute-defined-outside-init
-
- return self._pvs_cache
-
- @property
- def lv_info(self):
- if self._lvs_cache is None:
- lvs = blockdev.lvm.lvs()
- self._lvs_cache = dict(("%s-%s" % (lv.vg_name, lv.lv_name), lv) for lv in lvs) # pylint: disable=attribute-defined-outside-init
-
- return self._lvs_cache
-
def drop_lvm_cache(self):
""" Drop cached lvm information. """
- self._pvs_cache = None # pylint: disable=attribute-defined-outside-init
- self._lvs_cache = None # pylint: disable=attribute-defined-outside-init
+ lvs_info.drop_cache()
+ pvs_info.drop_cache()
def handle_nodev_filesystems(self):
for line in open("/proc/mounts").readlines():
diff --git a/blivet/staticdata/__init__.py b/blivet/staticdata/__init__.py
new file mode 100644
index 0000000..333035f
--- /dev/null
+++ b/blivet/staticdata/__init__.py
@@ -0,0 +1 @@
+from .lvm_info import lvs_info, pvs_info
diff --git a/blivet/staticdata/lvm_info.py b/blivet/staticdata/lvm_info.py
new file mode 100644
index 0000000..ed2e995
--- /dev/null
+++ b/blivet/staticdata/lvm_info.py
@@ -0,0 +1,67 @@
+# volume_info.py
+# Backend code for populating a DeviceTree.
+#
+# Copyright (C) 2009-2016 Red Hat, Inc.
+#
+# This copyrighted material is made available to anyone wishing to use,
+# modify, copy, or redistribute it subject to the terms and conditions of
+# the GNU Lesser General Public License v.2, or (at your option) any later
+# version. This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY expressed or implied, including the implied
+# warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+# the GNU Lesser General Public License for more details. You should have
+# received a copy of the GNU Lesser General Public License along with this
+# program; if not, write to the Free Software Foundation, Inc., 51 Franklin
+# Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat trademarks
+# that are incorporated in the source code or documentation are not subject
+# to the GNU Lesser General Public License and may only be used or
+# replicated with the express permission of Red Hat, Inc.
+#
+# Red Hat Author(s): Jan Pokorny <japokorn(a)redhat.com>
+#
+
+import gi
+gi.require_version("BlockDev", "1.0")
+
+from gi.repository import BlockDev as blockdev
+
+import logging
+log = logging.getLogger("blivet")
+
+
+class LVsInfo(object):
+ """ Class to be used as a singleton.
+ Maintains the LVs cache.
+ """
+
+ @property
+ def cache(self):
+ if self._lvs_cache is None:
+ lvs = blockdev.lvm.lvs()
+ self._lvs_cache = dict(("%s-%s" % (lv.vg_name, lv.lv_name), lv) for lv in lvs) # pylint: disable=attribute-defined-outside-init
+
+ return self._lvs_cache
+
+ def drop_cache(self):
+ self._lvs_cache = None # pylint: disable=attribute-defined-outside-init
+
+lvs_info = LVsInfo()
+
+
+class PVsInfo(object):
+ """ Class to be used as a singleton.
+ Maintains the PVs cache.
+ """
+
+ @property
+ def cache(self):
+ if self._pvs_cache is None:
+ pvs = blockdev.lvm.pvs()
+ self._pvs_cache = dict((pv.pv_name, pv) for pv in pvs) # pylint: disable=attribute-defined-outside-init
+
+ return self._pvs_cache
+
+ def drop_cache(self):
+ self._pvs_cache = None # pylint: disable=attribute-defined-outside-init
+
+pvs_info = PVsInfo()
diff --git a/setup.py b/setup.py
index ebba229..076a6b1 100644
--- a/setup.py
+++ b/setup.py
@@ -93,4 +93,4 @@ def make_release_tree(self, base_dir, files):
author='David Lehman', author_email='dlehman(a)redhat.com',
url='http://fedoraproject.org/wiki/blivet',
data_files=data_files,
- packages=['blivet', 'blivet.devices', 'blivet.devicelibs', 'blivet.events', 'blivet.formats', 'blivet.populator', 'blivet.tasks', 'blivet.populator.helpers'])
+ packages=['blivet', 'blivet.devices', 'blivet.devicelibs', 'blivet.events', 'blivet.formats', 'blivet.populator', 'blivet.staticdata', 'blivet.tasks', 'blivet.populator.helpers'])
diff --git a/tests/populator_test.py b/tests/populator_test.py
index 8975b0f..fabe09c 100644
--- a/tests/populator_test.py
+++ b/tests/populator_test.py
@@ -1,7 +1,7 @@
import gi
import os
import unittest
-from unittest.mock import call, patch, sentinel, Mock
+from unittest.mock import call, patch, sentinel, Mock, PropertyMock
gi.require_version("BlockDev", "1.0")
from gi.repository import BlockDev as blockdev
@@ -908,17 +908,19 @@ def test_run(self, *args):
vg_device.lvs = []
get_device_by_uuid.return_value = vg_device
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- helper = self.helper_class(devicetree, data, device)
- self.assertFalse(device in vg_device.parents)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path: pv_info}
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ helper = self.helper_class(devicetree, data, device)
+ self.assertFalse(device in vg_device.parents)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
- self.assertEqual(get_device_by_uuid.call_count, 3)
- get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
- self.assertTrue(device in vg_device.parents)
+ self.assertEqual(get_device_by_uuid.call_count, 3)
+ get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
+ self.assertTrue(device in vg_device.parents)
get_device_by_uuid.reset_mock()
get_device_by_uuid.return_value = None
@@ -931,18 +933,20 @@ def test_run(self, *args):
pv_info.vg_free_count = 0
pv_info.vg_pv_count = 1
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- helper = self.helper_class(devicetree, data, device)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
-
- self.assertEqual(get_device_by_uuid.call_count, 2)
- get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
- vg_device = devicetree.get_device_by_name(pv_info.vg_name)
- self.assertTrue(vg_device is not None)
- devicetree._remove_device(vg_device)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path: pv_info}
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ helper = self.helper_class(devicetree, data, device)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+
+ self.assertEqual(get_device_by_uuid.call_count, 2)
+ get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
+ vg_device = devicetree.get_device_by_name(pv_info.vg_name)
+ self.assertTrue(vg_device is not None)
+ devicetree._remove_device(vg_device)
get_device_by_uuid.reset_mock()
@@ -976,27 +980,31 @@ def gdbu(uuid, **kwargs): # pylint: disable=unused-argument
return next((d for d in devicetree.devices if d.uuid == uuid), None)
get_device_by_uuid.side_effect = gdbu
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- self.assertEqual(devicetree.get_device_by_name(pv_info.vg_name, incomplete=True), None)
- helper = self.helper_class(devicetree, data, device)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
-
- self.assertEqual(get_device_by_uuid.call_count, 4,
- get_device_by_uuid.mock_calls) # two for vg and one for each lv
- get_device_by_uuid.assert_has_calls([call(pv_info.vg_uuid, incomplete=True),
- call(lv1.uuid),
- call(lv2.uuid)],
- any_order=True)
- vg_device = devicetree.get_device_by_name(pv_info.vg_name)
- self.assertTrue(vg_device is not None)
-
- lv1_device = devicetree.get_device_by_name(lv1_name)
- self.assertEqual(lv1_device.uuid, lv1.uuid)
- lv2_device = devicetree.get_device_by_name(lv2_name)
- self.assertEqual(lv2_device.uuid, lv2.uuid)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path: pv_info}
+ with patch("blivet.staticdata.lvm_info.LVsInfo.cache", new_callable=PropertyMock) as mock_lvs_cache:
+ mock_lvs_cache.return_value = lv_info
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ self.assertEqual(devicetree.get_device_by_name(pv_info.vg_name, incomplete=True), None)
+ helper = self.helper_class(devicetree, data, device)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+
+ self.assertEqual(get_device_by_uuid.call_count, 4,
+ get_device_by_uuid.mock_calls) # two for vg and one for each lv
+ get_device_by_uuid.assert_has_calls([call(pv_info.vg_uuid, incomplete=True),
+ call(lv1.uuid),
+ call(lv2.uuid)],
+ any_order=True)
+ vg_device = devicetree.get_device_by_name(pv_info.vg_name)
+ self.assertTrue(vg_device is not None)
+
+ lv1_device = devicetree.get_device_by_name(lv1_name)
+ self.assertEqual(lv1_device.uuid, lv1.uuid)
+ lv2_device = devicetree.get_device_by_name(lv2_name)
+ self.assertEqual(lv2_device.uuid, lv2.uuid)
class MDFormatPopulatorTestCase(FormatPopulatorTestCase):
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/f598d8dfd3f9e0908364141ab49521…
From: Jan Pokorny <japokorn(a)redhat.com>
- created new singletons from lv_info and pv_info
- edited the code to use them
- updated test to reflect the interface changes
---
blivet/populator/helpers/lvm.py | 16 ++++---
blivet/populator/populator.py | 24 +++--------
blivet/staticdata/__init__.py | 1 +
blivet/staticdata/lvm_info.py | 67 ++++++++++++++++++++++++++++
setup.py | 2 +-
tests/populator_test.py | 96 ++++++++++++++++++++++-------------------
6 files changed, 136 insertions(+), 70 deletions(-)
create mode 100644 blivet/staticdata/__init__.py
create mode 100644 blivet/staticdata/lvm_info.py
diff --git a/blivet/populator/helpers/lvm.py b/blivet/populator/helpers/lvm.py
index 94f2012..b07c2e9 100644
--- a/blivet/populator/helpers/lvm.py
+++ b/blivet/populator/helpers/lvm.py
@@ -37,6 +37,9 @@
from .devicepopulator import DevicePopulator
from .formatpopulator import FormatPopulator
+from ...staticdata.lvm_info import lvs_info, pvs_info
+
+
import logging
log = logging.getLogger("blivet")
@@ -86,7 +89,8 @@ class LVMFormatPopulator(FormatPopulator):
def _get_kwargs(self):
kwargs = super()._get_kwargs()
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
+
name = udev.device_get_name(self.data)
if pv_info:
if pv_info.vg_name:
@@ -118,7 +122,7 @@ def _update_lvs(self):
return
vg_name = vg_device.name
- lv_info = dict((k, v) for (k, v) in iter(self._devicetree.lv_info.items())
+ lv_info = dict((k, v) for (k, v) in iter(lvs_info.cache.items())
if v.vg_name == vg_name)
# FIXME: This should account for added/removed LVs.
@@ -343,7 +347,7 @@ def create_internal_lv(lv):
log.warning("Failed to determine parent LV for an internal LV '%s'", lv.name)
def _add_vg_device(self):
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if pv_info:
vg_name = pv_info.vg_name
vg_uuid = pv_info.vg_uuid
@@ -398,7 +402,7 @@ def _handle_vg_rename(self):
if vg_device is None:
return
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if not pv_info or not pv_info.vg_name:
return
@@ -408,7 +412,7 @@ def _handle_vg_rename(self):
# TODO: update name registry
def _update_pv_format(self):
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if not pv_info:
return
@@ -420,7 +424,7 @@ def _update_pv_format(self):
def update(self):
self._devicetree.drop_lvm_cache()
self._update_pv_format()
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
vg_device = self._get_vg_device()
if vg_device is None:
# The VG device isn't in the tree. The PV might have just been
diff --git a/blivet/populator/populator.py b/blivet/populator/populator.py
index 816c226..84181f9 100644
--- a/blivet/populator/populator.py
+++ b/blivet/populator/populator.py
@@ -45,6 +45,8 @@
from ..threads import SynchronizedMeta
from .helpers import get_device_helper, get_format_helper
+from ..staticdata.lvm_info import lvs_info, pvs_info
+
import logging
log = logging.getLogger("blivet")
@@ -161,7 +163,7 @@ def _add_slave_devices(self, info):
slave_dev = self.get_device_by_name(slave_name)
if slave_dev is None:
if udev.device_is_dm_lvm(info):
- if slave_name not in self.lv_info:
+ if slave_name not in lvs_info.cache:
# we do not expect hidden lvs to be in the tree
continue
@@ -528,26 +530,10 @@ def _populate(self):
# inconsistencies are ignored or resolved.
self._handle_inconsistencies()
- @property
- def pv_info(self):
- if self._pvs_cache is None:
- pvs = blockdev.lvm.pvs()
- self._pvs_cache = dict((pv.pv_name, pv) for pv in pvs) # pylint: disable=attribute-defined-outside-init
-
- return self._pvs_cache
-
- @property
- def lv_info(self):
- if self._lvs_cache is None:
- lvs = blockdev.lvm.lvs()
- self._lvs_cache = dict(("%s-%s" % (lv.vg_name, lv.lv_name), lv) for lv in lvs) # pylint: disable=attribute-defined-outside-init
-
- return self._lvs_cache
-
def drop_lvm_cache(self):
""" Drop cached lvm information. """
- self._pvs_cache = None # pylint: disable=attribute-defined-outside-init
- self._lvs_cache = None # pylint: disable=attribute-defined-outside-init
+ lvs_info.drop_cache()
+ pvs_info.drop_cache()
def handle_nodev_filesystems(self):
for line in open("/proc/mounts").readlines():
diff --git a/blivet/staticdata/__init__.py b/blivet/staticdata/__init__.py
new file mode 100644
index 0000000..333035f
--- /dev/null
+++ b/blivet/staticdata/__init__.py
@@ -0,0 +1 @@
+from .lvm_info import lvs_info, pvs_info
diff --git a/blivet/staticdata/lvm_info.py b/blivet/staticdata/lvm_info.py
new file mode 100644
index 0000000..ed2e995
--- /dev/null
+++ b/blivet/staticdata/lvm_info.py
@@ -0,0 +1,67 @@
+# volume_info.py
+# Backend code for populating a DeviceTree.
+#
+# Copyright (C) 2009-2016 Red Hat, Inc.
+#
+# This copyrighted material is made available to anyone wishing to use,
+# modify, copy, or redistribute it subject to the terms and conditions of
+# the GNU Lesser General Public License v.2, or (at your option) any later
+# version. This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY expressed or implied, including the implied
+# warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+# the GNU Lesser General Public License for more details. You should have
+# received a copy of the GNU Lesser General Public License along with this
+# program; if not, write to the Free Software Foundation, Inc., 51 Franklin
+# Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat trademarks
+# that are incorporated in the source code or documentation are not subject
+# to the GNU Lesser General Public License and may only be used or
+# replicated with the express permission of Red Hat, Inc.
+#
+# Red Hat Author(s): Jan Pokorny <japokorn(a)redhat.com>
+#
+
+import gi
+gi.require_version("BlockDev", "1.0")
+
+from gi.repository import BlockDev as blockdev
+
+import logging
+log = logging.getLogger("blivet")
+
+
+class LVsInfo(object):
+ """ Class to be used as a singleton.
+ Maintains the LVs cache.
+ """
+
+ @property
+ def cache(self):
+ if self._lvs_cache is None:
+ lvs = blockdev.lvm.lvs()
+ self._lvs_cache = dict(("%s-%s" % (lv.vg_name, lv.lv_name), lv) for lv in lvs) # pylint: disable=attribute-defined-outside-init
+
+ return self._lvs_cache
+
+ def drop_cache(self):
+ self._lvs_cache = None # pylint: disable=attribute-defined-outside-init
+
+lvs_info = LVsInfo()
+
+
+class PVsInfo(object):
+ """ Class to be used as a singleton.
+ Maintains the PVs cache.
+ """
+
+ @property
+ def cache(self):
+ if self._pvs_cache is None:
+ pvs = blockdev.lvm.pvs()
+ self._pvs_cache = dict((pv.pv_name, pv) for pv in pvs) # pylint: disable=attribute-defined-outside-init
+
+ return self._pvs_cache
+
+ def drop_cache(self):
+ self._pvs_cache = None # pylint: disable=attribute-defined-outside-init
+
+pvs_info = PVsInfo()
diff --git a/setup.py b/setup.py
index ebba229..076a6b1 100644
--- a/setup.py
+++ b/setup.py
@@ -93,4 +93,4 @@ def make_release_tree(self, base_dir, files):
author='David Lehman', author_email='dlehman(a)redhat.com',
url='http://fedoraproject.org/wiki/blivet',
data_files=data_files,
- packages=['blivet', 'blivet.devices', 'blivet.devicelibs', 'blivet.events', 'blivet.formats', 'blivet.populator', 'blivet.tasks', 'blivet.populator.helpers'])
+ packages=['blivet', 'blivet.devices', 'blivet.devicelibs', 'blivet.events', 'blivet.formats', 'blivet.populator', 'blivet.staticdata', 'blivet.tasks', 'blivet.populator.helpers'])
diff --git a/tests/populator_test.py b/tests/populator_test.py
index 8975b0f..fabe09c 100644
--- a/tests/populator_test.py
+++ b/tests/populator_test.py
@@ -1,7 +1,7 @@
import gi
import os
import unittest
-from unittest.mock import call, patch, sentinel, Mock
+from unittest.mock import call, patch, sentinel, Mock, PropertyMock
gi.require_version("BlockDev", "1.0")
from gi.repository import BlockDev as blockdev
@@ -908,17 +908,19 @@ def test_run(self, *args):
vg_device.lvs = []
get_device_by_uuid.return_value = vg_device
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- helper = self.helper_class(devicetree, data, device)
- self.assertFalse(device in vg_device.parents)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path: pv_info}
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ helper = self.helper_class(devicetree, data, device)
+ self.assertFalse(device in vg_device.parents)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
- self.assertEqual(get_device_by_uuid.call_count, 3)
- get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
- self.assertTrue(device in vg_device.parents)
+ self.assertEqual(get_device_by_uuid.call_count, 3)
+ get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
+ self.assertTrue(device in vg_device.parents)
get_device_by_uuid.reset_mock()
get_device_by_uuid.return_value = None
@@ -931,18 +933,20 @@ def test_run(self, *args):
pv_info.vg_free_count = 0
pv_info.vg_pv_count = 1
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- helper = self.helper_class(devicetree, data, device)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
-
- self.assertEqual(get_device_by_uuid.call_count, 2)
- get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
- vg_device = devicetree.get_device_by_name(pv_info.vg_name)
- self.assertTrue(vg_device is not None)
- devicetree._remove_device(vg_device)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path: pv_info}
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ helper = self.helper_class(devicetree, data, device)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+
+ self.assertEqual(get_device_by_uuid.call_count, 2)
+ get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
+ vg_device = devicetree.get_device_by_name(pv_info.vg_name)
+ self.assertTrue(vg_device is not None)
+ devicetree._remove_device(vg_device)
get_device_by_uuid.reset_mock()
@@ -976,27 +980,31 @@ def gdbu(uuid, **kwargs): # pylint: disable=unused-argument
return next((d for d in devicetree.devices if d.uuid == uuid), None)
get_device_by_uuid.side_effect = gdbu
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- self.assertEqual(devicetree.get_device_by_name(pv_info.vg_name, incomplete=True), None)
- helper = self.helper_class(devicetree, data, device)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
-
- self.assertEqual(get_device_by_uuid.call_count, 4,
- get_device_by_uuid.mock_calls) # two for vg and one for each lv
- get_device_by_uuid.assert_has_calls([call(pv_info.vg_uuid, incomplete=True),
- call(lv1.uuid),
- call(lv2.uuid)],
- any_order=True)
- vg_device = devicetree.get_device_by_name(pv_info.vg_name)
- self.assertTrue(vg_device is not None)
-
- lv1_device = devicetree.get_device_by_name(lv1_name)
- self.assertEqual(lv1_device.uuid, lv1.uuid)
- lv2_device = devicetree.get_device_by_name(lv2_name)
- self.assertEqual(lv2_device.uuid, lv2.uuid)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path: pv_info}
+ with patch("blivet.staticdata.lvm_info.LVsInfo.cache", new_callable=PropertyMock) as mock_lvs_cache:
+ mock_lvs_cache.return_value = lv_info
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ self.assertEqual(devicetree.get_device_by_name(pv_info.vg_name, incomplete=True), None)
+ helper = self.helper_class(devicetree, data, device)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+
+ self.assertEqual(get_device_by_uuid.call_count, 4,
+ get_device_by_uuid.mock_calls) # two for vg and one for each lv
+ get_device_by_uuid.assert_has_calls([call(pv_info.vg_uuid, incomplete=True),
+ call(lv1.uuid),
+ call(lv2.uuid)],
+ any_order=True)
+ vg_device = devicetree.get_device_by_name(pv_info.vg_name)
+ self.assertTrue(vg_device is not None)
+
+ lv1_device = devicetree.get_device_by_name(lv1_name)
+ self.assertEqual(lv1_device.uuid, lv1.uuid)
+ lv2_device = devicetree.get_device_by_name(lv2_name)
+ self.assertEqual(lv2_device.uuid, lv2.uuid)
class MDFormatPopulatorTestCase(FormatPopulatorTestCase):
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/f598d8dfd3f9e0908364141ab49521…
From: Jan Pokorny <japokorn(a)redhat.com>
- created new singletons from lv_info and pv_info
- edited the code to use them
- updated test to reflect the interface changes
---
blivet/populator/helpers/lvm.py | 16 ++++---
blivet/populator/populator.py | 24 +++--------
blivet/staticdata/__init__.py | 1 +
blivet/staticdata/lvm_info.py | 67 ++++++++++++++++++++++++++++
setup.py | 2 +-
tests/populator_test.py | 96 ++++++++++++++++++++++-------------------
6 files changed, 136 insertions(+), 70 deletions(-)
create mode 100644 blivet/staticdata/__init__.py
create mode 100644 blivet/staticdata/lvm_info.py
diff --git a/blivet/populator/helpers/lvm.py b/blivet/populator/helpers/lvm.py
index 94f2012..b07c2e9 100644
--- a/blivet/populator/helpers/lvm.py
+++ b/blivet/populator/helpers/lvm.py
@@ -37,6 +37,9 @@
from .devicepopulator import DevicePopulator
from .formatpopulator import FormatPopulator
+from ...staticdata.lvm_info import lvs_info, pvs_info
+
+
import logging
log = logging.getLogger("blivet")
@@ -86,7 +89,8 @@ class LVMFormatPopulator(FormatPopulator):
def _get_kwargs(self):
kwargs = super()._get_kwargs()
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
+
name = udev.device_get_name(self.data)
if pv_info:
if pv_info.vg_name:
@@ -118,7 +122,7 @@ def _update_lvs(self):
return
vg_name = vg_device.name
- lv_info = dict((k, v) for (k, v) in iter(self._devicetree.lv_info.items())
+ lv_info = dict((k, v) for (k, v) in iter(lvs_info.cache.items())
if v.vg_name == vg_name)
# FIXME: This should account for added/removed LVs.
@@ -343,7 +347,7 @@ def create_internal_lv(lv):
log.warning("Failed to determine parent LV for an internal LV '%s'", lv.name)
def _add_vg_device(self):
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if pv_info:
vg_name = pv_info.vg_name
vg_uuid = pv_info.vg_uuid
@@ -398,7 +402,7 @@ def _handle_vg_rename(self):
if vg_device is None:
return
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if not pv_info or not pv_info.vg_name:
return
@@ -408,7 +412,7 @@ def _handle_vg_rename(self):
# TODO: update name registry
def _update_pv_format(self):
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
if not pv_info:
return
@@ -420,7 +424,7 @@ def _update_pv_format(self):
def update(self):
self._devicetree.drop_lvm_cache()
self._update_pv_format()
- pv_info = self._devicetree.pv_info.get(self.device.path, None)
+ pv_info = pvs_info.cache.get(self.device.path, None)
vg_device = self._get_vg_device()
if vg_device is None:
# The VG device isn't in the tree. The PV might have just been
diff --git a/blivet/populator/populator.py b/blivet/populator/populator.py
index 816c226..84181f9 100644
--- a/blivet/populator/populator.py
+++ b/blivet/populator/populator.py
@@ -45,6 +45,8 @@
from ..threads import SynchronizedMeta
from .helpers import get_device_helper, get_format_helper
+from ..staticdata.lvm_info import lvs_info, pvs_info
+
import logging
log = logging.getLogger("blivet")
@@ -161,7 +163,7 @@ def _add_slave_devices(self, info):
slave_dev = self.get_device_by_name(slave_name)
if slave_dev is None:
if udev.device_is_dm_lvm(info):
- if slave_name not in self.lv_info:
+ if slave_name not in lvs_info.cache:
# we do not expect hidden lvs to be in the tree
continue
@@ -528,26 +530,10 @@ def _populate(self):
# inconsistencies are ignored or resolved.
self._handle_inconsistencies()
- @property
- def pv_info(self):
- if self._pvs_cache is None:
- pvs = blockdev.lvm.pvs()
- self._pvs_cache = dict((pv.pv_name, pv) for pv in pvs) # pylint: disable=attribute-defined-outside-init
-
- return self._pvs_cache
-
- @property
- def lv_info(self):
- if self._lvs_cache is None:
- lvs = blockdev.lvm.lvs()
- self._lvs_cache = dict(("%s-%s" % (lv.vg_name, lv.lv_name), lv) for lv in lvs) # pylint: disable=attribute-defined-outside-init
-
- return self._lvs_cache
-
def drop_lvm_cache(self):
""" Drop cached lvm information. """
- self._pvs_cache = None # pylint: disable=attribute-defined-outside-init
- self._lvs_cache = None # pylint: disable=attribute-defined-outside-init
+ lvs_info.drop_cache()
+ pvs_info.drop_cache()
def handle_nodev_filesystems(self):
for line in open("/proc/mounts").readlines():
diff --git a/blivet/staticdata/__init__.py b/blivet/staticdata/__init__.py
new file mode 100644
index 0000000..333035f
--- /dev/null
+++ b/blivet/staticdata/__init__.py
@@ -0,0 +1 @@
+from .lvm_info import lvs_info, pvs_info
diff --git a/blivet/staticdata/lvm_info.py b/blivet/staticdata/lvm_info.py
new file mode 100644
index 0000000..ed2e995
--- /dev/null
+++ b/blivet/staticdata/lvm_info.py
@@ -0,0 +1,67 @@
+# volume_info.py
+# Backend code for populating a DeviceTree.
+#
+# Copyright (C) 2009-2016 Red Hat, Inc.
+#
+# This copyrighted material is made available to anyone wishing to use,
+# modify, copy, or redistribute it subject to the terms and conditions of
+# the GNU Lesser General Public License v.2, or (at your option) any later
+# version. This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY expressed or implied, including the implied
+# warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+# the GNU Lesser General Public License for more details. You should have
+# received a copy of the GNU Lesser General Public License along with this
+# program; if not, write to the Free Software Foundation, Inc., 51 Franklin
+# Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat trademarks
+# that are incorporated in the source code or documentation are not subject
+# to the GNU Lesser General Public License and may only be used or
+# replicated with the express permission of Red Hat, Inc.
+#
+# Red Hat Author(s): Jan Pokorny <japokorn(a)redhat.com>
+#
+
+import gi
+gi.require_version("BlockDev", "1.0")
+
+from gi.repository import BlockDev as blockdev
+
+import logging
+log = logging.getLogger("blivet")
+
+
+class LVsInfo(object):
+ """ Class to be used as a singleton.
+ Maintains the LVs cache.
+ """
+
+ @property
+ def cache(self):
+ if self._lvs_cache is None:
+ lvs = blockdev.lvm.lvs()
+ self._lvs_cache = dict(("%s-%s" % (lv.vg_name, lv.lv_name), lv) for lv in lvs) # pylint: disable=attribute-defined-outside-init
+
+ return self._lvs_cache
+
+ def drop_cache(self):
+ self._lvs_cache = None # pylint: disable=attribute-defined-outside-init
+
+lvs_info = LVsInfo()
+
+
+class PVsInfo(object):
+ """ Class to be used as a singleton.
+ Maintains the PVs cache.
+ """
+
+ @property
+ def cache(self):
+ if self._pvs_cache is None:
+ pvs = blockdev.lvm.pvs()
+ self._pvs_cache = dict((pv.pv_name, pv) for pv in pvs) # pylint: disable=attribute-defined-outside-init
+
+ return self._pvs_cache
+
+ def drop_cache(self):
+ self._pvs_cache = None # pylint: disable=attribute-defined-outside-init
+
+pvs_info = PVsInfo()
diff --git a/setup.py b/setup.py
index ebba229..076a6b1 100644
--- a/setup.py
+++ b/setup.py
@@ -93,4 +93,4 @@ def make_release_tree(self, base_dir, files):
author='David Lehman', author_email='dlehman(a)redhat.com',
url='http://fedoraproject.org/wiki/blivet',
data_files=data_files,
- packages=['blivet', 'blivet.devices', 'blivet.devicelibs', 'blivet.events', 'blivet.formats', 'blivet.populator', 'blivet.tasks', 'blivet.populator.helpers'])
+ packages=['blivet', 'blivet.devices', 'blivet.devicelibs', 'blivet.events', 'blivet.formats', 'blivet.populator', 'blivet.staticdata', 'blivet.tasks', 'blivet.populator.helpers'])
diff --git a/tests/populator_test.py b/tests/populator_test.py
index 8975b0f..0f11ffc 100644
--- a/tests/populator_test.py
+++ b/tests/populator_test.py
@@ -1,7 +1,7 @@
import gi
import os
import unittest
-from unittest.mock import call, patch, sentinel, Mock
+from unittest.mock import call, patch, sentinel, Mock, PropertyMock
gi.require_version("BlockDev", "1.0")
from gi.repository import BlockDev as blockdev
@@ -908,17 +908,19 @@ def test_run(self, *args):
vg_device.lvs = []
get_device_by_uuid.return_value = vg_device
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- helper = self.helper_class(devicetree, data, device)
- self.assertFalse(device in vg_device.parents)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path:pv_info}
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ helper = self.helper_class(devicetree, data, device)
+ self.assertFalse(device in vg_device.parents)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
- self.assertEqual(get_device_by_uuid.call_count, 3)
- get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
- self.assertTrue(device in vg_device.parents)
+ self.assertEqual(get_device_by_uuid.call_count, 3)
+ get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
+ self.assertTrue(device in vg_device.parents)
get_device_by_uuid.reset_mock()
get_device_by_uuid.return_value = None
@@ -931,18 +933,20 @@ def test_run(self, *args):
pv_info.vg_free_count = 0
pv_info.vg_pv_count = 1
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- helper = self.helper_class(devicetree, data, device)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
-
- self.assertEqual(get_device_by_uuid.call_count, 2)
- get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
- vg_device = devicetree.get_device_by_name(pv_info.vg_name)
- self.assertTrue(vg_device is not None)
- devicetree._remove_device(vg_device)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path:pv_info}
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ helper = self.helper_class(devicetree, data, device)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+
+ self.assertEqual(get_device_by_uuid.call_count, 2)
+ get_device_by_uuid.assert_called_with(pv_info.vg_uuid, incomplete=True)
+ vg_device = devicetree.get_device_by_name(pv_info.vg_name)
+ self.assertTrue(vg_device is not None)
+ devicetree._remove_device(vg_device)
get_device_by_uuid.reset_mock()
@@ -976,27 +980,31 @@ def gdbu(uuid, **kwargs): # pylint: disable=unused-argument
return next((d for d in devicetree.devices if d.uuid == uuid), None)
get_device_by_uuid.side_effect = gdbu
- with patch("blivet.udev.device_get_format", return_value=self.udev_type):
- self.assertEqual(devicetree.get_device_by_name(pv_info.vg_name, incomplete=True), None)
- helper = self.helper_class(devicetree, data, device)
- helper.run()
- self.assertEqual(device.format.type,
- self.blivet_type,
- msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
-
- self.assertEqual(get_device_by_uuid.call_count, 4,
- get_device_by_uuid.mock_calls) # two for vg and one for each lv
- get_device_by_uuid.assert_has_calls([call(pv_info.vg_uuid, incomplete=True),
- call(lv1.uuid),
- call(lv2.uuid)],
- any_order=True)
- vg_device = devicetree.get_device_by_name(pv_info.vg_name)
- self.assertTrue(vg_device is not None)
-
- lv1_device = devicetree.get_device_by_name(lv1_name)
- self.assertEqual(lv1_device.uuid, lv1.uuid)
- lv2_device = devicetree.get_device_by_name(lv2_name)
- self.assertEqual(lv2_device.uuid, lv2.uuid)
+ with patch("blivet.staticdata.lvm_info.PVsInfo.cache", new_callable=PropertyMock) as mock_pvs_cache:
+ mock_pvs_cache.return_value = {sentinel.pv_path:pv_info}
+ with patch("blivet.staticdata.lvm_info.LVsInfo.cache", new_callable=PropertyMock) as mock_lvs_cache:
+ mock_lvs_cache.return_value = lv_info
+ with patch("blivet.udev.device_get_format", return_value=self.udev_type):
+ self.assertEqual(devicetree.get_device_by_name(pv_info.vg_name, incomplete=True), None)
+ helper = self.helper_class(devicetree, data, device)
+ helper.run()
+ self.assertEqual(device.format.type,
+ self.blivet_type,
+ msg="Wrong format type after FormatPopulator.run on %s" % self.udev_type)
+
+ self.assertEqual(get_device_by_uuid.call_count, 4,
+ get_device_by_uuid.mock_calls) # two for vg and one for each lv
+ get_device_by_uuid.assert_has_calls([call(pv_info.vg_uuid, incomplete=True),
+ call(lv1.uuid),
+ call(lv2.uuid)],
+ any_order=True)
+ vg_device = devicetree.get_device_by_name(pv_info.vg_name)
+ self.assertTrue(vg_device is not None)
+
+ lv1_device = devicetree.get_device_by_name(lv1_name)
+ self.assertEqual(lv1_device.uuid, lv1.uuid)
+ lv2_device = devicetree.get_device_by_name(lv2_name)
+ self.assertEqual(lv2_device.uuid, lv2.uuid)
class MDFormatPopulatorTestCase(FormatPopulatorTestCase):
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/6b77b0d908580b2eca4b1ac7dd7299…