Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""A sequence of tests of filesystem labeling.
* create the filesystem when passing an invalid label
* raise an exception when reading the filesystem
* raise an exception when relabeling the filesystem
"""
an_fs = self._fs_class(device=self.loop_devices[0], label=self._invalid_label)
if an_fs._readlabel.availability_errors or not an_fs.relabels():
self.skipTest("can not read or write label for filesystem %s" % an_fs.name)
self.assertIsNone(an_fs.create())
with self.assertRaises(FSReadLabelError):
an_fs.read_label()
an_fs.label = "an fs"
with self.assertRaises(FSError):
an_fs.write_label()
def test_set_invalid_uuid_later(self):
"""Create the filesystem and try to reassign an invalid UUID later."""
an_fs = self._fs_class(device=self.loop_devices[0])
if an_fs._writeuuid.availability_errors:
self.skipTest("can not write UUID for filesystem %s" % an_fs.name)
self.assertIsNone(an_fs.create())
an_fs.uuid = self._invalid_uuid
with six.assertRaisesRegex(self, FSError, "bad UUID format"):
an_fs.write_uuid()
self.assertEqual(an_fs.read_label(), an_fs._labelfs.default_label)
an_fs.label = "an_fs"
self.assertIsNone(an_fs.write_label())
self.assertEqual(an_fs.read_label(), an_fs.label)
an_fs.label = ""
self.assertIsNone(an_fs.write_label())
self.assertEqual(an_fs.read_label(), an_fs.label)
an_fs.label = None
with six.assertRaisesRegex(self, FSError, "default label"):
an_fs.write_label()
an_fs.label = "n" * 129
with six.assertRaisesRegex(self, FSError, "bad label format"):
an_fs.write_label()
def write_label(self, dry_run=False):
""" Create a label for this filesystem.
:raises: FSError
If self.label is None, this means accept the default, so raise
an FSError in this case.
Raises a FSError if the label can not be set.
"""
if not self._writelabel.available:
raise FSError("no application to set label for filesystem %s" % self.type)
if not dry_run:
if not self.exists:
raise FSError("filesystem has not been created")
if not os.path.exists(self.device):
raise FSError("device does not exist")
if self.label is None:
raise FSError("makes no sense to write a label when accepting default label")
if not self.label_format_ok(self.label):
raise FSError("bad label format for labelling application %s" % self._writelabel)
self._writelabel.do_task()
def do_task(self, mountpoint, options=None):
"""Create the format on the device and label if possible and desired.
:param str mountpoint: mountpoint that overrides self.mountpoint
:param options: mount options
:type options: str or None
"""
# pylint: disable=arguments-differ
error_msgs = self.availability_errors
if error_msgs:
raise FSError("\n".join(error_msgs))
try:
rc = util.mount(self.fs.device, mountpoint,
fstype=self.mount_type,
options=self.mount_options(options))
except OSError as e:
raise FSError("mount failed: %s" % e)
if rc:
raise FSError("mount failed: %s" % rc)
err = "makes no sense to write an UUID when not requested"
if not self.exists:
err = "filesystem has not been created"
if not self._writeuuid.available:
err = "no application to set UUID for filesystem %s" % self.type
if not self.uuid_format_ok(self.uuid):
err = "bad UUID format for application %s" % self._writeuuid
if not os.path.exists(self.device):
err = "device does not exist"
if err is not None:
raise FSError(err)
self._writeuuid.do_task()
def setLabelCommand(self, fs):
"""Get the command to label the filesystem.
:param FS fs: a filesystem object
:return: the command
:rtype: list of str
Raises an exception if fs.label is None.
"""
if fs.label is None:
raise errors.FSError("makes no sense to write a label when accepting default label")
return [self.name] + self._writeLabelArgs(fs)
def do_task(self):
error_msgs = self.availability_errors
if error_msgs:
raise FSError("\n".join(error_msgs))
resize_info = self._get_resize_info()
min_size = self._extract_min_size(resize_info)
if min_size is None:
raise FSError("Unable to discover minimum size of filesystem on %s" % self.fs.device)
return min_size
If self.label is None, this means accept the default, so raise
an FSError in this case.
Raises a FSError if the label can not be set.
"""
if not self._writelabel.available:
raise FSError("no application to set label for filesystem %s" % self.type)
if not dry_run:
if not self.exists:
raise FSError("filesystem has not been created")
if not os.path.exists(self.device):
raise FSError("device does not exist")
if self.label is None:
raise FSError("makes no sense to write a label when accepting default label")
if not self.label_format_ok(self.label):
raise FSError("bad label format for labelling application %s" % self._writelabel)
self._writelabel.do_task()
def extractLabel(self, labelstr):
"""Extract the label from an output string.
:param str labelstr: the string containing the label information
:return: the label
:rtype: str
Raises an FSError if the label can not be extracted.
"""
if not self.reads or self._label_regex is None:
raise errors.FSError("Unknown format for application %s" % self.name)
match = re.match(self._label_regex, labelstr)
if match is None:
raise errors.FSError("Unknown format for application %s" % self.name)
return match.group('label')