def fake_mounts(mount_lines): """ This method gets a list of mount lines, fakes the /proc/mounts and /etc/mtab files using monkey patch with a temporary file, and cleans everything on the end of use. Usage example: with fake_mounts([mount_line_1, mount_line_2]): <do something with /proc/mounts or /etc/mtab> """ data = b"".join(line + b"\n" for line in mount_lines) with temporaryPath(data=data) as fake_mounts: with monkeypatch.MonkeyPatchScope([ (mount, '_PROC_MOUNTS_PATH', fake_mounts), ]): yield
def fake_repo(): """ Create a temporary repository and monkeypatch the system to use it instead of /rhev/data-center. """ with namedTemporaryDir() as repo: # /rhev/data-center/mnt mnt_dir = os.path.join(repo, sd.DOMAIN_MNT_POINT) os.mkdir(mnt_dir) # /rhev/data-center/mnt/blockSD mnt_blocksd_dir = os.path.join(mnt_dir, sd.BLOCKSD_DIR) os.mkdir(mnt_blocksd_dir) # /rhev/data-center/mnt/glusterSD mnt_glustersd_dir = os.path.join(mnt_dir, sd.GLUSTERSD_DIR) os.mkdir(mnt_glustersd_dir) with monkeypatch.MonkeyPatchScope([ (sd.StorageDomain, 'storage_repository', repo), ]): yield repo