ff33a1
From f6f90805020d7c6ac46f17a13a00f319fc4351f6 Mon Sep 17 00:00:00 2001
20511f
From: Vojtech Trefny <vtrefny@redhat.com>
20511f
Date: Wed, 26 May 2021 12:15:54 +0200
20511f
Subject: [PATCH] Revert "More consistent lvm errors (API break)"
20511f
20511f
This reverts commit 49ec071c6d0673224a0774d613904387c52c7381.
20511f
---
ff33a1
 blivet/devices/lvm.py                     | 72 +++++++++++------------
ff33a1
 tests/unit_tests/devices_test/lvm_test.py | 14 ++---
20511f
 2 files changed, 43 insertions(+), 43 deletions(-)
20511f
20511f
diff --git a/blivet/devices/lvm.py b/blivet/devices/lvm.py
ff33a1
index 38e49e18..b8595d63 100644
20511f
--- a/blivet/devices/lvm.py
20511f
+++ b/blivet/devices/lvm.py
ff33a1
@@ -304,7 +304,7 @@ class LVMVolumeGroupDevice(ContainerDevice):
20511f
     def _add_log_vol(self, lv):
20511f
         """ Add an LV to this VG. """
20511f
         if lv in self._lvs:
20511f
-            raise errors.DeviceError("lv is already part of this vg")
20511f
+            raise ValueError("lv is already part of this vg")
20511f
 
20511f
         # verify we have the space, then add it
20511f
         # do not verify for growing vg (because of ks)
ff33a1
@@ -337,7 +337,7 @@ class LVMVolumeGroupDevice(ContainerDevice):
20511f
     def _remove_log_vol(self, lv):
20511f
         """ Remove an LV from this VG. """
20511f
         if lv not in self.lvs:
20511f
-            raise errors.DeviceError("specified lv is not part of this vg")
20511f
+            raise ValueError("specified lv is not part of this vg")
20511f
 
20511f
         self._lvs.remove(lv)
20511f
 
ff33a1
@@ -430,7 +430,7 @@ class LVMVolumeGroupDevice(ContainerDevice):
20511f
     @thpool_reserve.setter
20511f
     def thpool_reserve(self, value):
20511f
         if value is not None and not isinstance(value, ThPoolReserveSpec):
20511f
-            raise AttributeError("Invalid thpool_reserve given, must be of type ThPoolReserveSpec")
20511f
+            raise ValueError("Invalid thpool_reserve given, must be of type ThPoolReserveSpec")
20511f
         self._thpool_reserve = value
20511f
 
20511f
     @property
ff33a1
@@ -665,14 +665,14 @@ class LVMLogicalVolumeBase(DMDevice, RaidDevice):
ff33a1
             if seg_type not in [None, "linear", "thin", "thin-pool", "cache", "vdo-pool", "vdo", "cache-pool"] + lvm.raid_seg_types:
20511f
                 raise ValueError("Invalid or unsupported segment type: %s" % seg_type)
20511f
             if seg_type and seg_type in lvm.raid_seg_types and not pvs:
20511f
-                raise errors.DeviceError("List of PVs has to be given for every non-linear LV")
20511f
+                raise ValueError("List of PVs has to be given for every non-linear LV")
20511f
             elif (not seg_type or seg_type == "linear") and pvs:
20511f
                 if not all(isinstance(pv, LVPVSpec) for pv in pvs):
20511f
-                    raise errors.DeviceError("Invalid specification of PVs for a linear LV: either no or complete "
20511f
-                                             "specification (with all space split into PVs has to be given")
20511f
+                    raise ValueError("Invalid specification of PVs for a linear LV: either no or complete "
20511f
+                                     "specification (with all space split into PVs has to be given")
20511f
                 elif sum(spec.size for spec in pvs) != size:
20511f
-                    raise errors.DeviceError("Invalid specification of PVs for a linear LV: the sum of space "
20511f
-                                             "assigned to PVs is not equal to the size of the LV")
20511f
+                    raise ValueError("Invalid specification of PVs for a linear LV: the sum of space "
20511f
+                                     "assigned to PVs is not equal to the size of the LV")
20511f
 
20511f
         # When this device's format is set in the superclass constructor it will
20511f
         # try to access self.snapshots.
ff33a1
@@ -721,13 +721,13 @@ class LVMLogicalVolumeBase(DMDevice, RaidDevice):
20511f
         self._from_lvs = from_lvs
20511f
         if self._from_lvs:
20511f
             if exists:
20511f
-                raise errors.DeviceError("Only new LVs can be created from other LVs")
20511f
+                raise ValueError("Only new LVs can be created from other LVs")
20511f
             if size or maxsize or percent:
20511f
-                raise errors.DeviceError("Cannot specify size for a converted LV")
20511f
+                raise ValueError("Cannot specify size for a converted LV")
20511f
             if fmt:
20511f
-                raise errors.DeviceError("Cannot specify format for a converted LV")
20511f
+                raise ValueError("Cannot specify format for a converted LV")
20511f
             if any(lv.vg != self.vg for lv in self._from_lvs):
20511f
-                raise errors.DeviceError("Conversion of LVs only possible inside a VG")
20511f
+                raise ValueError("Conversion of LVs only possible inside a VG")
20511f
 
20511f
         self._cache = None
20511f
         if cache_request and not self.exists:
ff33a1
@@ -746,13 +746,13 @@ class LVMLogicalVolumeBase(DMDevice, RaidDevice):
20511f
             elif isinstance(pv_spec, StorageDevice):
20511f
                 self._pv_specs.append(LVPVSpec(pv_spec, Size(0)))
20511f
             else:
20511f
-                raise AttributeError("Invalid PV spec '%s' for the '%s' LV" % (pv_spec, self.name))
20511f
+                raise ValueError("Invalid PV spec '%s' for the '%s' LV" % (pv_spec, self.name))
20511f
         # Make sure any destination PVs are actually PVs in this VG
20511f
         if not set(spec.pv for spec in self._pv_specs).issubset(set(self.vg.parents)):
20511f
             missing = [r.name for r in
20511f
                        set(spec.pv for spec in self._pv_specs).difference(set(self.vg.parents))]
20511f
             msg = "invalid destination PV(s) %s for LV %s" % (missing, self.name)
20511f
-            raise errors.DeviceError(msg)
20511f
+            raise ValueError(msg)
20511f
         if self._pv_specs:
20511f
             self._assign_pv_space()
20511f
 
ff33a1
@@ -1130,7 +1130,7 @@ class LVMLogicalVolumeBase(DMDevice, RaidDevice):
20511f
         else:
20511f
             msg = "the specified internal LV '%s' doesn't belong to this LV ('%s')" % (int_lv.lv_name,
20511f
                                                                                        self.name)
20511f
-            raise errors.DeviceError(msg)
20511f
+            raise ValueError(msg)
20511f
 
20511f
     def populate_ksdata(self, data):
20511f
         super(LVMLogicalVolumeBase, self).populate_ksdata(data)
ff33a1
@@ -1229,7 +1229,7 @@ class LVMInternalLogicalVolumeMixin(object):
20511f
     def _init_check(self):
20511f
         # an internal LV should have no parents
20511f
         if self._parent_lv and self._parents:
20511f
-            raise errors.DeviceError("an internal LV should have no parents")
20511f
+            raise ValueError("an internal LV should have no parents")
20511f
 
20511f
     @property
20511f
     def is_internal_lv(self):
ff33a1
@@ -1289,7 +1289,7 @@ class LVMInternalLogicalVolumeMixin(object):
20511f
 
20511f
     @readonly.setter
20511f
     def readonly(self, value):  # pylint: disable=unused-argument
20511f
-        raise errors.DeviceError("Cannot make an internal LV read-write")
20511f
+        raise ValueError("Cannot make an internal LV read-write")
20511f
 
20511f
     @property
20511f
     def type(self):
ff33a1
@@ -1325,7 +1325,7 @@ class LVMInternalLogicalVolumeMixin(object):
20511f
     def _check_parents(self):
20511f
         # an internal LV should have no parents
20511f
         if self._parents:
20511f
-            raise errors.DeviceError("an internal LV should have no parents")
20511f
+            raise ValueError("an internal LV should have no parents")
20511f
 
20511f
     def _add_to_parents(self):
20511f
         # nothing to do here, an internal LV has no parents (in the DeviceTree's
ff33a1
@@ -1335,13 +1335,13 @@ class LVMInternalLogicalVolumeMixin(object):
20511f
     # internal LVs follow different rules limitting size
20511f
     def _set_size(self, newsize):
20511f
         if not isinstance(newsize, Size):
20511f
-            raise AttributeError("new size must of type Size")
20511f
+            raise ValueError("new size must of type Size")
20511f
 
20511f
         if not self.takes_extra_space:
20511f
             if newsize <= self.parent_lv.size:  # pylint: disable=no-member
20511f
                 self._size = newsize  # pylint: disable=attribute-defined-outside-init
20511f
             else:
20511f
-                raise errors.DeviceError("Internal LV cannot be bigger than its parent LV")
20511f
+                raise ValueError("Internal LV cannot be bigger than its parent LV")
20511f
         else:
20511f
             # same rules apply as for any other LV
20511f
             raise NotTypeSpecific()
ff33a1
@@ -1419,18 +1419,18 @@ class LVMSnapshotMixin(object):
20511f
             return
20511f
 
20511f
         if self.origin and not isinstance(self.origin, LVMLogicalVolumeDevice):
20511f
-            raise errors.DeviceError("lvm snapshot origin must be a logical volume")
20511f
+            raise ValueError("lvm snapshot origin must be a logical volume")
20511f
         if self.vorigin and not self.exists:
20511f
-            raise errors.DeviceError("only existing vorigin snapshots are supported")
20511f
+            raise ValueError("only existing vorigin snapshots are supported")
20511f
 
20511f
         if isinstance(self.origin, LVMLogicalVolumeDevice) and \
20511f
            isinstance(self.parents[0], LVMVolumeGroupDevice) and \
20511f
            self.origin.vg != self.parents[0]:
20511f
-            raise errors.DeviceError("lvm snapshot and origin must be in the same vg")
20511f
+            raise ValueError("lvm snapshot and origin must be in the same vg")
20511f
 
20511f
         if self.is_thin_lv:
20511f
             if self.origin and self.size and not self.exists:
20511f
-                raise errors.DeviceError("thin snapshot size is determined automatically")
20511f
+                raise ValueError("thin snapshot size is determined automatically")
20511f
 
20511f
     @property
20511f
     def is_snapshot_lv(self):
ff33a1
@@ -1606,7 +1606,7 @@ class LVMThinPoolMixin(object):
20511f
     def _check_from_lvs(self):
20511f
         if self._from_lvs:
20511f
             if len(self._from_lvs) != 2:
20511f
-                raise errors.DeviceError("two LVs required to create a thin pool")
20511f
+                raise ValueError("two LVs required to create a thin pool")
20511f
 
20511f
     def _convert_from_lvs(self):
20511f
         data_lv, metadata_lv = self._from_lvs
ff33a1
@@ -1652,7 +1652,7 @@ class LVMThinPoolMixin(object):
20511f
     def _add_log_vol(self, lv):
20511f
         """ Add an LV to this pool. """
20511f
         if lv in self._lvs:
20511f
-            raise errors.DeviceError("lv is already part of this vg")
20511f
+            raise ValueError("lv is already part of this vg")
20511f
 
20511f
         # TODO: add some checking to prevent overcommit for preexisting
20511f
         self.vg._add_log_vol(lv)
ff33a1
@@ -1663,7 +1663,7 @@ class LVMThinPoolMixin(object):
20511f
     def _remove_log_vol(self, lv):
20511f
         """ Remove an LV from this pool. """
20511f
         if lv not in self._lvs:
20511f
-            raise errors.DeviceError("specified lv is not part of this vg")
20511f
+            raise ValueError("specified lv is not part of this vg")
20511f
 
20511f
         self._lvs.remove(lv)
20511f
         self.vg._remove_log_vol(lv)
ff33a1
@@ -1772,14 +1772,14 @@ class LVMThinLogicalVolumeMixin(object):
20511f
         """Check that this device has parents as expected"""
20511f
         if isinstance(self.parents, (list, ParentList)):
20511f
             if len(self.parents) != 1:
20511f
-                raise errors.DeviceError("constructor requires a single thin-pool LV")
20511f
+                raise ValueError("constructor requires a single thin-pool LV")
20511f
 
20511f
             container = self.parents[0]
20511f
         else:
20511f
             container = self.parents
20511f
 
20511f
         if not container or not isinstance(container, LVMLogicalVolumeDevice) or not container.is_thin_pool:
20511f
-            raise errors.DeviceError("constructor requires a thin-pool LV")
20511f
+            raise ValueError("constructor requires a thin-pool LV")
20511f
 
20511f
     @property
20511f
     def is_thin_lv(self):
ff33a1
@@ -1816,7 +1816,7 @@ class LVMThinLogicalVolumeMixin(object):
20511f
 
20511f
     def _set_size(self, newsize):
20511f
         if not isinstance(newsize, Size):
20511f
-            raise AttributeError("new size must of type Size")
20511f
+            raise ValueError("new size must of type Size")
20511f
 
20511f
         newsize = self.vg.align(newsize)
20511f
         newsize = self.vg.align(util.numeric_type(newsize))
ff33a1
@@ -2499,7 +2499,7 @@ class LVMLogicalVolumeDevice(LVMLogicalVolumeBase, LVMInternalLogicalVolumeMixin
20511f
             container = self.parents
20511f
 
20511f
         if not isinstance(container, LVMVolumeGroupDevice):
20511f
-            raise AttributeError("constructor requires a LVMVolumeGroupDevice")
20511f
+            raise ValueError("constructor requires a LVMVolumeGroupDevice")
20511f
 
20511f
     @type_specific
20511f
     def _add_to_parents(self):
ff33a1
@@ -2510,12 +2510,12 @@ class LVMLogicalVolumeDevice(LVMLogicalVolumeBase, LVMInternalLogicalVolumeMixin
20511f
     @type_specific
20511f
     def _check_from_lvs(self):
20511f
         """Check the LVs to create this LV from"""
20511f
-        raise errors.DeviceError("Cannot create a new LV of type '%s' from other LVs" % self.seg_type)
20511f
+        raise ValueError("Cannot create a new LV of type '%s' from other LVs" % self.seg_type)
20511f
 
20511f
     @type_specific
20511f
     def _convert_from_lvs(self):
20511f
         """Convert the LVs to create this LV from into its internal LVs"""
20511f
-        raise errors.DeviceError("Cannot create a new LV of type '%s' from other LVs" % self.seg_type)
20511f
+        raise ValueError("Cannot create a new LV of type '%s' from other LVs" % self.seg_type)
20511f
 
20511f
     @property
20511f
     def external_dependencies(self):
ff33a1
@@ -2535,7 +2535,7 @@ class LVMLogicalVolumeDevice(LVMLogicalVolumeBase, LVMInternalLogicalVolumeMixin
20511f
     @type_specific
20511f
     def _set_size(self, newsize):
20511f
         if not isinstance(newsize, Size):
20511f
-            raise AttributeError("new size must be of type Size")
20511f
+            raise ValueError("new size must be of type Size")
20511f
 
20511f
         newsize = self.vg.align(newsize)
20511f
         log.debug("trying to set lv %s size to %s", self.name, newsize)
ff33a1
@@ -2544,7 +2544,7 @@ class LVMLogicalVolumeDevice(LVMLogicalVolumeBase, LVMInternalLogicalVolumeMixin
20511f
         # space for it. A similar reasoning applies to shrinking the LV.
20511f
         if not self.exists and newsize > self.size and newsize > self.vg.free_space + self.vg_space_used:
20511f
             log.error("failed to set size: %s short", newsize - (self.vg.free_space + self.vg_space_used))
20511f
-            raise errors.DeviceError("not enough free space in volume group")
20511f
+            raise ValueError("not enough free space in volume group")
20511f
 
20511f
         LVMLogicalVolumeBase._set_size(self, newsize)
20511f
 
ff33a1
@@ -2910,7 +2910,7 @@ class LVMCache(Cache):
20511f
                 spec.size = spec.pv.format.free
20511f
                 space_to_assign -= spec.pv.format.free
20511f
         if space_to_assign > 0:
20511f
-            raise errors.DeviceError("Not enough free space in the PVs for this cache: %s short" % space_to_assign)
20511f
+            raise ValueError("Not enough free space in the PVs for this cache: %s short" % space_to_assign)
20511f
 
20511f
     @property
20511f
     def size(self):
ff33a1
diff --git a/tests/unit_tests/devices_test/lvm_test.py b/tests/unit_tests/devices_test/lvm_test.py
ff33a1
index 47613fdc..995c2da4 100644
ff33a1
--- a/tests/unit_tests/devices_test/lvm_test.py
ff33a1
+++ b/tests/unit_tests/devices_test/lvm_test.py
ff33a1
@@ -32,10 +32,10 @@ class LVMDeviceTest(unittest.TestCase):
20511f
         lv = LVMLogicalVolumeDevice("testlv", parents=[vg],
20511f
                                     fmt=blivet.formats.get_format("xfs"))
20511f
 
20511f
-        with six.assertRaisesRegex(self, errors.DeviceError, "lvm snapshot origin must be a logical volume"):
20511f
+        with six.assertRaisesRegex(self, ValueError, "lvm snapshot origin must be a logical volume"):
20511f
             LVMLogicalVolumeDevice("snap1", parents=[vg], origin=pv)
20511f
 
20511f
-        with six.assertRaisesRegex(self, errors.DeviceError, "only existing vorigin snapshots are supported"):
20511f
+        with six.assertRaisesRegex(self, ValueError, "only existing vorigin snapshots are supported"):
20511f
             LVMLogicalVolumeDevice("snap1", parents=[vg], vorigin=True)
20511f
 
20511f
         lv.exists = True
ff33a1
@@ -60,7 +60,7 @@ class LVMDeviceTest(unittest.TestCase):
20511f
         pool = LVMLogicalVolumeDevice("pool1", parents=[vg], size=Size("500 MiB"), seg_type="thin-pool")
20511f
         thinlv = LVMLogicalVolumeDevice("thinlv", parents=[pool], size=Size("200 MiB"), seg_type="thin")
20511f
 
20511f
-        with six.assertRaisesRegex(self, errors.DeviceError, "lvm snapshot origin must be a logical volume"):
20511f
+        with six.assertRaisesRegex(self, ValueError, "lvm snapshot origin must be a logical volume"):
20511f
             LVMLogicalVolumeDevice("snap1", parents=[pool], origin=pv, seg_type="thin")
20511f
 
20511f
         # now make the constructor succeed so we can test some properties
ff33a1
@@ -310,21 +310,21 @@ class LVMDeviceTest(unittest.TestCase):
20511f
         vg = LVMVolumeGroupDevice("testvg", parents=[pv, pv2])
20511f
 
20511f
         # pvs have to be specified for non-linear LVs
20511f
-        with self.assertRaises(errors.DeviceError):
20511f
+        with self.assertRaises(ValueError):
20511f
             lv = LVMLogicalVolumeDevice("testlv", parents=[vg], size=Size("512 MiB"),
20511f
                                         fmt=blivet.formats.get_format("xfs"),
20511f
                                         exists=False, seg_type="raid1")
20511f
-        with self.assertRaises(errors.DeviceError):
20511f
+        with self.assertRaises(ValueError):
20511f
             lv = LVMLogicalVolumeDevice("testlv", parents=[vg], size=Size("512 MiB"),
20511f
                                         fmt=blivet.formats.get_format("xfs"),
20511f
                                         exists=False, seg_type="striped")
20511f
 
20511f
         # no or complete specification has to be given for linear LVs
20511f
-        with self.assertRaises(errors.DeviceError):
20511f
+        with self.assertRaises(ValueError):
20511f
             lv = LVMLogicalVolumeDevice("testlv", parents=[vg], size=Size("512 MiB"),
20511f
                                         fmt=blivet.formats.get_format("xfs"),
20511f
                                         exists=False, pvs=[pv])
20511f
-        with self.assertRaises(errors.DeviceError):
20511f
+        with self.assertRaises(ValueError):
20511f
             pv_spec = LVPVSpec(pv, Size("256 MiB"))
20511f
             pv_spec2 = LVPVSpec(pv2, Size("250 MiB"))
20511f
             lv = LVMLogicalVolumeDevice("testlv", parents=[vg], size=Size("512 MiB"),
20511f
-- 
ff33a1
2.38.1
20511f