def test_is_compatible_file_system_ext4(self, mock_get_fs_type): #pylint: disable=unused-argument """Check non-zfs file systems pass compatiblilty checks""" with tempfile.NamedTemporaryFile("w+", prefix="testfile_", delete=True, dir=".", suffix=".img") as fobj: self.assertTrue(BmapHelpers.is_compatible_file_system(fobj.name))
def test_is_compatible_file_system_zfs_invalid(self, mock_get_fs_type): #pylint: disable=unused-argument """Check compatiblilty check fails when zfs param is set incorrectly""" with tempfile.NamedTemporaryFile("w+", prefix="testfile_", delete=True, dir=".", suffix=".img") as fobj: fobj.write("0") fobj.flush() mockobj = mock.patch.object(BmapHelpers, "ZFS_COMPAT_PARAM_PATH", fobj.name) with mockobj: self.assertFalse(BmapHelpers.is_compatible_file_system(fobj.name))
def __init__(self, image): """ Initialize a class instance. The 'image' argument is full path to the file or file object to operate on. """ self._f_image_needs_close = False if hasattr(image, "fileno"): self._f_image = image self._image_path = image.name else: self._image_path = image self._open_image_file() try: self.image_size = os.fstat(self._f_image.fileno()).st_size except IOError as err: raise Error("cannot get information about file '%s': %s" % (self._f_image.name, err)) try: self.block_size = BmapHelpers.get_block_size(self._f_image) except IOError as err: raise Error("cannot get block size for '%s': %s" % (self._image_path, err)) self.blocks_cnt = (self.image_size + self.block_size - 1) // self.block_size try: self._f_image.flush() except IOError as err: raise Error("cannot flush image file '%s': %s" % (self._image_path, err)) try: os.fsync(self._f_image.fileno()), except OSError as err: raise Error("cannot synchronize image file '%s': %s " % (self._image_path, err.strerror)) if not BmapHelpers.is_compatible_file_system(self._image_path): fstype = BmapHelpers.get_file_system_type(self._image_path) raise Error( "image file on incompatible file system '%s': '%s': see docs for fix" % (self._image_path, fstype)) _log.debug("opened image \"%s\"" % self._image_path) _log.debug("block size %d, blocks count %d, image size %d" % (self.block_size, self.blocks_cnt, self.image_size))