def test_do_ismount_successes_ino(self): _os_lstat = os.lstat class MockStat(object): def __init__(self, mode, dev, ino): self.st_mode = mode self.st_dev = dev self.st_ino = ino def _mock_os_lstat(path): if path.endswith(".."): return _os_lstat(path) else: parent_path = os.path.join(path, "..") child = _os_lstat(path) parent = _os_lstat(parent_path) return MockStat(child.st_mode, parent.st_ino, child.st_dev) tmpdir = mkdtemp() try: with patch("os.lstat", _mock_os_lstat): try: fs.do_ismount(tmpdir) except GlusterFileSystemOSError: self.fail("Unexpected exception") else: pass finally: shutil.rmtree(tmpdir)
def test_do_ismount_path_error(self): def _mock_os_lstat(path): raise OSError(13, "foo") tmpdir = mkdtemp() try: with patch("os.lstat", _mock_os_lstat): try: fs.do_ismount(tmpdir) except GlusterFileSystemOSError: pass else: self.fail("Expected GlusterFileSystemOSError") finally: shutil.rmtree(tmpdir)
def test_do_ismount_path_is_symlink(self): tmpdir = mkdtemp() try: link = os.path.join(tmpdir, "tmp") os.symlink("/tmp", link) assert False == fs.do_ismount(link) finally: shutil.rmtree(tmpdir)
def test_do_ismount_parent_path_error(self): _os_lstat = os.lstat def _mock_os_lstat(path): if path.endswith(".."): raise OSError(13, "foo") else: return _os_lstat(path) tmpdir = mkdtemp() try: with patch("os.lstat", _mock_os_lstat): try: fs.do_ismount(tmpdir) except SwiftOnFileSystemOSError: pass else: self.fail("Expected SwiftOnFileSystemOSError") finally: shutil.rmtree(tmpdir)
def test_do_ismount_path_not_mount(self): tmpdir = mkdtemp() try: assert False == fs.do_ismount(tmpdir) finally: shutil.rmtree(tmpdir)
def test_do_ismount_path_does_not_exist(self): tmpdir = mkdtemp() try: assert False == fs.do_ismount(os.path.join(tmpdir, 'bar')) finally: shutil.rmtree(tmpdir)
def test_do_ismount_path_is_root(self): assert True == fs.do_ismount('/')
def mount(root, drive): """ Verify that the path to the device is a mount point and mounted. This allows us to fast fail on drives that have been unmounted because of issues, and also prevents us for accidentally filling up the root partition. This method effectively replaces the swift.common.constraints.check_mount method in behavior, adding the ability to auto-mount the volume, which is dubious (FIXME). :param root: base path where the devices are mounted :param drive: drive name to be checked :returns: True if it is a valid mounted device, False otherwise """ if not (urllib.quote_plus(drive) == drive): return False mount_point = _get_drive_mount_point_name(drive) full_mount_path = os.path.join(root, mount_point) if do_ismount(full_mount_path): # Don't bother checking volume if it is already a mount point. Allows # us to use local file systems for unit tests and some functional test # environments to isolate behaviors from GlusterFS itself. return True # FIXME: Possible thundering herd problem here el = _get_export_list() for export in el: if drive == export: break else: logging.error("No export found in %r matching drive, %s", el, drive) return False try: os.makedirs(full_mount_path) except OSError as err: if err.errno == errno.EEXIST: pass else: logging.exception("Could not create mount path hierarchy:" " %s" % full_mount_path) return False mnt_cmd = "mount -t glusterfs %s:%s %s" % (MOUNT_IP, export, full_mount_path) if _allow_mount_per_server: if os.system(mnt_cmd): logging.exception("Mount failed %s" % (mnt_cmd)) return True lck_file = os.path.join(RUN_DIR, "%s.lock" % mount_point) try: os.mkdir(RUN_DIR) except OSError as err: if err.errno == errno.EEXIST: pass else: logging.exception("Could not create RUN_DIR: %s" % full_mount_path) return False fd = os.open(lck_file, os.O_CREAT | os.O_RDWR) with os.fdopen(fd, "r+b") as f: try: fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError as ex: if ex.errno in (errno.EACCES, errno.EAGAIN): # This means that some other process is mounting the # filesystem, so wait for the mount process to complete return _busy_wait(full_mount_path) if os.system(mnt_cmd) or not _busy_wait(full_mount_path): logging.error("Mount failed %s", mnt_cmd) return False return True
def mount(root, drive): """ Verify that the path to the device is a mount point and mounted. This allows us to fast fail on drives that have been unmounted because of issues, and also prevents us for accidentally filling up the root partition. This method effectively replaces the swift.common.constraints.check_mount method in behavior, adding the ability to auto-mount the volume, which is dubious (FIXME). :param root: base path where the devices are mounted :param drive: drive name to be checked :returns: True if it is a valid mounted device, False otherwise """ if not (urllib.quote_plus(drive) == drive): return False mount_point = _get_drive_mount_point_name(drive) full_mount_path = os.path.join(root, mount_point) if do_ismount(full_mount_path): # Don't bother checking volume if it is already a mount point. Allows # us to use local file systems for unit tests and some functional test # environments to isolate behaviors from GlusterFS itself. return True # FIXME: Possible thundering herd problem here el = _get_export_list() for export in el: if drive == export: break else: logging.error('No export found in %r matching drive, %s', el, drive) return False try: os.makedirs(full_mount_path) except OSError as err: if err.errno == errno.EEXIST: pass else: logging.exception('Could not create mount path hierarchy:' ' %s' % full_mount_path) return False mnt_cmd = 'mount -t glusterfs %s:%s %s' % (MOUNT_IP, export, full_mount_path) if _allow_mount_per_server: if os.system(mnt_cmd): logging.exception('Mount failed %s' % (mnt_cmd)) return True lck_file = os.path.join(RUN_DIR, '%s.lock' % mount_point) try: os.mkdir(RUN_DIR) except OSError as err: if err.errno == errno.EEXIST: pass else: logging.exception('Could not create RUN_DIR: %s' % full_mount_path) return False fd = os.open(lck_file, os.O_CREAT | os.O_RDWR) with os.fdopen(fd, 'r+b') as f: try: fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError as ex: if ex.errno in (errno.EACCES, errno.EAGAIN): # This means that some other process is mounting the # filesystem, so wait for the mount process to complete return _busy_wait(full_mount_path) if os.system(mnt_cmd) or not _busy_wait(full_mount_path): logging.error('Mount failed %s', mnt_cmd) return False return True