[blivet:rhel7/master 07/10] Add a labelFormatOK method to the DeviceFormat's interface (#1038590)

mulhern amulhern at redhat.com
Thu Jan 9 18:54:57 UTC 2014


Related: rhbz#1038590

Need to expose this information to any client of a DeviceFormat since it's
really an operational question about the labeling application not about
the state the DeviceFormat is allowed to have.

In other words, have to allow for a legal DeviceFormat label which the labeling
application can not handle.

Signed-off-by: mulhern <amulhern at redhat.com>
---
 blivet/formats/__init__.py      | 14 ++++++++++++
 blivet/formats/fs.py            | 20 +++++++++--------
 blivet/formats/swap.py          |  5 +++++
 tests/formats_test/misc_test.py | 49 ++++++++++++-----------------------------
 4 files changed, 44 insertions(+), 44 deletions(-)

diff --git a/blivet/formats/__init__.py b/blivet/formats/__init__.py
index 1de00a6..2d7966d 100644
--- a/blivet/formats/__init__.py
+++ b/blivet/formats/__init__.py
@@ -222,6 +222,20 @@ class DeviceFormat(object):
         """Returns False by default since most formats are non-labeling."""
         return False
 
+    @classmethod
+    def labelFormatOK(cls, label):
+        """Checks whether the format of the label is OK for whatever
+           application is used by blivet to write a label for this format.
+           If there is no application that blivet uses to write a label,
+           then no format is acceptable, so must return False.
+
+           :param str label: The label to be checked
+
+           :rtype: bool
+           :return: True if the format of the label is OK, otherwise False
+        """
+        return cls.labeling()
+
     def _setOptions(self, options):
         self._options = options
 
diff --git a/blivet/formats/fs.py b/blivet/formats/fs.py
index 716f43b..bb78786 100644
--- a/blivet/formats/fs.py
+++ b/blivet/formats/fs.py
@@ -160,12 +160,18 @@ class FS(DeviceFormat):
         """
         return cls._labelfs is not None
 
+    @classmethod
+    def labelFormatOK(cls, label):
+        return cls._labelfs is not None and cls._labelfs.labelFormatOK(label)
+
     def _setLabel(self, label):
         """Sets the label for this filesystem.
 
            :param label: the label for this filesystem
            :type label: str or None
 
+           Raises an FSError if the label is "".
+
            Note that some filesystems do not possess a label, so this method
            always accept the value None for label.
 
@@ -176,12 +182,10 @@ class FS(DeviceFormat):
            the filesystem on the CD has a label, but there is no labeling
            application for the Iso9660FS format.
 
-           However, if a labeling application does exist, we require that
-           the label have the correct format for that application.
-
-           It is unlikely, but may be possible, that a filesystem may have
-           a label that the accepted labeling application will not accept.
-           In this case, this method will raise a FSError.
+           If a labeling application does exist, the label is not
+           required to have the correct format for that application.
+           The allowable format for the label may be more permissive than
+           the format allowed by the labeling application.
 
            This method is not intended to be overridden.
         """
@@ -189,10 +193,8 @@ class FS(DeviceFormat):
             self._label = None
         elif label == "":
             raise FSError("Empty filesystem label not permitted.")
-        elif self._labelfs == None or self._labelfs.labelFormatOK(label):
-            self._label = label
         else:
-            raise FSError("Filesystem label '%s' is incorrectly formatted for %s." % (label, self._labelfs.name))
+            self._label = label
 
     def _getLabel(self):
         """The label for this filesystem.
diff --git a/blivet/formats/swap.py b/blivet/formats/swap.py
index 5bfa9db..d7c948e 100644
--- a/blivet/formats/swap.py
+++ b/blivet/formats/swap.py
@@ -83,6 +83,11 @@ class SwapSpace(DeviceFormat):
         """Returns True as mkswap can write a label to the swap space."""
         return True
 
+    @classmethod
+    def labelFormatOK(cls, label):
+        """Returns True since no known restrictions on the label."""
+        return True
+
     def _setPriority(self, priority):
         if priority is None:
             self._priority = None
diff --git a/tests/formats_test/misc_test.py b/tests/formats_test/misc_test.py
index 195a45b..be3b98a 100755
--- a/tests/formats_test/misc_test.py
+++ b/tests/formats_test/misc_test.py
@@ -5,6 +5,7 @@ import unittest
 from devicelibs_test import baseclass
 from blivet.formats import device_formats
 import blivet.formats.fs as fs
+import blivet.formats.swas as swap
 
 class InitializationTestCase(unittest.TestCase):
     """Test FS object initialization."""
@@ -13,50 +14,28 @@ class InitializationTestCase(unittest.TestCase):
         """Initialize some filesystems with valid and invalid labels."""
 
         # Ext2FS has a maximum length of 16
-        self.assertRaisesRegexp(fs.FSError,
-           "Filesystem label.*incorrectly formatted",
-           fs.Ext2FS,
-           device="/dev", label="root___filesystem")
-
-        self.assertIsNotNone(fs.Ext2FS(label="root__filesystem"))
+        self.assertFalse(fs.Ext2FS.labelFormatOK("root___filesystem"))
+        self.assertTrue(fs.Ext2FS.labelFormatOK("root__filesystem"))
 
         # FATFS has a maximum length of 11
-        self.assertRaisesRegexp(fs.FSError,
-           "Filesystem label.*incorrectly formatted",
-           fs.FATFS,
-           device="/dev", label="rtfilesystem")
-
-        self.assertIsNotNone(fs.FATFS(label="rfilesystem"))
+        self.assertFalse(fs.FATFS.labelFormatOK("rtfilesystem"))
+        self.assertTrue(fs.FATFS.labelFormatOK("rfilesystem"))
 
         # JFS has a maximum length of 16
-        self.assertRaisesRegexp(fs.FSError,
-           "Filesystem label.*incorrectly formatted",
-           fs.JFS,
-           device="/dev", label="root___filesystem")
-
-        self.assertIsNotNone(fs.JFS(label="root__filesystem"))
+        self.assertFalse(fs.JFS.labelFormatOK("root___filesystem"))
+        self.assertTrue(fs.JFS.labelFormatOK("root__filesystem"))
 
         # ReiserFS has a maximum length of 16
-        self.assertRaisesRegexp(fs.FSError,
-           "Filesystem label.*incorrectly formatted",
-           fs.ReiserFS,
-           device="/dev", label="root___filesystem")
-
-        self.assertIsNotNone(fs.ReiserFS(label="root__filesystem"))
+        self.assertFalse(fs.ReiserFS.labelFormatOK("root___filesystem"))
+        self.assertTrue(fs.ReiserFS.labelFormatOK("root__filesystem"))
 
         #XFS has a maximum length 12 and does not allow spaces
-        self.assertRaisesRegexp(fs.FSError,
-           "Filesystem label.*incorrectly formatted",
-           fs.XFS,
-           device="/dev", label="root filesyst")
-        self.assertRaisesRegexp(fs.FSError,
-           "Filesystem label.*incorrectly formatted",
-           fs.XFS,
-           device="/dev", label="root file")
-
-        self.assertIsNotNone(fs.XFS(label="root_filesys"))
+        self.assertFalse(fs.XFS.labelFormatOK("root_filesyst"))
+        self.assertFalse(fs.XFS.labelFormatOK("root file"))
+        self.assertTrue(fs.XFS.labelFormatOK("root_filesys"))
 
-        # all devices are permitted to have a label of None
+        # all devices are permitted to be passed a label argument of None
+        # some will ignore it completely
         for k, v  in device_formats.items():
             self.assertIsNotNone(v(label=None))
 
-- 
1.8.3.1



More information about the anaconda-patches mailing list