def test_createdir_file_exists_no_mode(): with namedTemporaryDir() as base: path = os.path.join(base, "file") with open(path, "w"): with pytest.raises(OSError) as e: fileUtils.createdir(path) assert e.value.errno == errno.ENOTDIR
def test_createdir_file_exists_with_mode(): with namedTemporaryDir() as base: path = os.path.join(base, "file") with open(path, "w"): mode = stat.S_IMODE(os.lstat(path).st_mode) with pytest.raises(OSError): fileUtils.createdir(path, mode=mode)
def connect(self): if self._mount.isMounted(): return self.validate() fileUtils.createdir(self._getLocalPath()) try: self._mount.mount(self.options, self._vfsType, cgroup=self.CGROUP) except MountError: t, v, tb = sys.exc_info() try: os.rmdir(self._getLocalPath()) except OSError as e: self.log.warn("Error removing mountpoint directory %r: %s", self._getLocalPath(), e) six.reraise(t, v, tb) else: try: fileSD.validateDirAccess( self.getMountObj().getRecord().fs_file) except se.StorageServerAccessPermissionError: t, v, tb = sys.exc_info() try: self.disconnect() except OSError: self.log.exception("Error disconnecting") six.reraise(t, v, tb)
def createImageLinks(self, srcImgPath, imgUUID): """ qcow chain is built by reading each qcow header and reading the path to the parent. When creating the qcow layer, we pass a relative path which allows us to build a directory with links to all volumes in the chain anywhere we want. This method creates a directory with the image uuid under /var/run/vdsm and creates sym links to all the volumes in the chain. srcImgPath: Dir where the image volumes are. """ sdRunDir = os.path.join(sc.P_VDSM_STORAGE, self.sdUUID) self.log.info("Creating domain run directory %r", sdRunDir) fileUtils.createdir(sdRunDir) imgRunDir = os.path.join(sdRunDir, imgUUID) self.log.info("Creating symlink from %s to %s", srcImgPath, imgRunDir) try: os.symlink(srcImgPath, imgRunDir) except OSError as e: if e.errno == errno.EEXIST: self.log.debug("img run dir already exists: %s", imgRunDir) else: self.log.error("Failed to create img run dir: %s", imgRunDir) raise return imgRunDir
def connect(self): if self._mount.isMounted(): return self.validate() self.log.info("Creating directory %r", self._getLocalPath()) fileUtils.createdir(self._getLocalPath()) try: self._mount.mount(self.options, self._vfsType, cgroup=self.CGROUP) except MountError: t, v, tb = sys.exc_info() try: os.rmdir(self._getLocalPath()) except OSError as e: self.log.warn("Error removing mountpoint directory %r: %s", self._getLocalPath(), e) six.reraise(t, v, tb) else: try: fileSD.validateDirAccess( self.getMountObj().getRecord().fs_file) except se.StorageServerAccessPermissionError: t, v, tb = sys.exc_info() try: self.disconnect() except OSError: self.log.exception("Error disconnecting") six.reraise(t, v, tb)
def test_create_dirs_with_mode(self): with namedTemporaryDir() as base: path = os.path.join(base, "a", "b") mode = 0o700 fileUtils.createdir(path, mode=mode) self.assertTrue(os.path.isdir(path)) actual_mode = stat.S_IMODE(os.lstat(path).st_mode) self.assertEqual(actual_mode, mode)
def test_createdir_with_mode(): with namedTemporaryDir() as base: path = os.path.join(base, "a", "b") mode = 0o700 fileUtils.createdir(path, mode=mode) assert os.path.isdir(path) actual_mode = stat.S_IMODE(os.lstat(path).st_mode) assert oct(actual_mode) == oct(mode)
def create(cls, sdUUID, domainName, domClass, remotePath, storageType, version, block_size=sc.BLOCK_SIZE_512, alignment=sc.ALIGNMENT_1M): """ Create new storage domain Arguments: sdUUID (UUID): Storage Domain UUID domainName (str): Storage domain name domClass (int): Data/Iso remotePath (str): /data storageType (int): LOCALFS_DOMAIN version (int): DOMAIN_VERSIONS block_size (int): Underlying storage block size. Supported value is BLOCK_SIZE_512 alignment (int): Sanlock alignment to use for this storage domain. Supported value is ALIGN_1M """ cls.log.info("sdUUID=%s domainName=%s remotePath=%s " "domClass=%s, block_size=%s, alignment=%s", sdUUID, domainName, remotePath, domClass, block_size, alignment) cls._validate_block_and_alignment(block_size, alignment, version) if not misc.isAscii(domainName) and not sd.supportsUnicode(version): raise se.UnicodeArgumentException() # Create local path mntPath = fileUtils.transformPath(remotePath) mntPoint = os.path.join(sc.REPO_MOUNT_DIR, mntPath) cls._preCreateValidation(sdUUID, mntPoint, remotePath, version) domainDir = os.path.join(mntPoint, sdUUID) cls._prepareMetadata(domainDir, sdUUID, domainName, domClass, remotePath, storageType, version, alignment, block_size) # create domain images folder imagesDir = os.path.join(domainDir, sd.DOMAIN_IMAGES) cls.log.info("Creating domain images directory %r", imagesDir) fileUtils.createdir(imagesDir) # create special imageUUID for ISO/Floppy volumes # Actually the local domain shouldn't be ISO, but # we can allow it for systems without NFS at all if domClass is sd.ISO_DOMAIN: isoDir = os.path.join(imagesDir, sd.ISO_IMAGE_UUID) cls.log.info("Creating ISO domain images directory %r", isoDir) fileUtils.createdir(isoDir) fsd = LocalFsStorageDomain(os.path.join(mntPoint, sdUUID)) fsd.initSPMlease() return fsd
def create(cls, sdUUID, domainName, domClass, remotePath, storageType, version, block_size=sc.BLOCK_SIZE_512, alignment=sc.ALIGNMENT_1M): """ Create new storage domain Arguments: sdUUID (UUID): Storage Domain UUID domainName (str): Storage domain name domClass (int): Data/Iso remotePath (str): /data storageType (int): LOCALFS_DOMAIN version (int): DOMAIN_VERSIONS block_size (int): Underlying storage block size. Supported value is BLOCK_SIZE_512 alignment (int): Sanlock alignment to use for this storage domain. Supported value is ALIGN_1M """ cls.log.info("sdUUID=%s domainName=%s remotePath=%s " "domClass=%s, block_size=%s, alignment=%s", sdUUID, domainName, remotePath, domClass, block_size, alignment) cls._validate_block_and_alignment(block_size, alignment) if not misc.isAscii(domainName) and not sd.supportsUnicode(version): raise se.UnicodeArgumentException() # Create local path mntPath = fileUtils.transformPath(remotePath) mntPoint = os.path.join(sc.REPO_MOUNT_DIR, mntPath) cls._preCreateValidation(sdUUID, mntPoint, remotePath, version) domainDir = os.path.join(mntPoint, sdUUID) cls._prepareMetadata(domainDir, sdUUID, domainName, domClass, remotePath, storageType, version, alignment, block_size) # create domain images folder imagesDir = os.path.join(domainDir, sd.DOMAIN_IMAGES) cls.log.info("Creating domain images directory %r", imagesDir) fileUtils.createdir(imagesDir) # create special imageUUID for ISO/Floppy volumes # Actually the local domain shouldn't be ISO, but # we can allow it for systems without NFS at all if domClass is sd.ISO_DOMAIN: isoDir = os.path.join(imagesDir, sd.ISO_IMAGE_UUID) cls.log.info("Creating ISO domain images directory %r", isoDir) fileUtils.createdir(isoDir) fsd = LocalFsStorageDomain(os.path.join(mntPoint, sdUUID)) fsd.initSPMlease() return fsd
def test_create_dirs_with_mode(self): with namedTemporaryDir() as base: path = os.path.join(base, "a", "b") mode = 0o700 fileUtils.createdir(path, mode=mode) self.assertTrue(os.path.isdir(path)) while path != base: pathmode = stat.S_IMODE(os.lstat(path).st_mode) self.assertEqual(pathmode, mode) path = os.path.dirname(path)
def test_createdir_directory_exists_same_mode(): with namedTemporaryDir() as tmpdir: path = os.path.join(tmpdir, "subdir") mode = 0o766 os.mkdir(path, mode=mode) # Folder exists, mode matches, operation should do nothing. fileUtils.createdir(path, mode=mode) assert os.path.isdir(path) expected_mode = mode & ~get_umask() actual_mode = stat.S_IMODE(os.lstat(path).st_mode) assert oct(actual_mode) == oct(expected_mode)
def test_createdir_raise_errors(): with namedTemporaryDir() as base: path = os.path.join(base, "a", "b") mode = 0o400 # TODO: remove when all platforms support python-3.7 if sys.version_info[:2] == (3, 6): with pytest.raises(OSError): fileUtils.createdir(path, mode=mode) else: # os.makedirs() behavior changed since python 3.7, # os.makedirs() will not respect the 'mode' parameter for # intermediate directories and will create them with the # default mode=0o777 fileUtils.createdir(path, mode=mode) actual_mode = stat.S_IMODE(os.lstat(path).st_mode) assert oct(actual_mode) == oct(mode & ~get_umask())
def test_createdir_with_mode_intermediate(): with namedTemporaryDir() as base: intermediate = os.path.join(base, "a") path = os.path.join(intermediate, "b") mode = 0o700 fileUtils.createdir(path, mode=mode) assert os.path.isdir(path) actual_mode = stat.S_IMODE(os.lstat(intermediate).st_mode) # TODO: remove when all platforms support python-3.7 if sys.version_info[:2] == (3, 6): assert actual_mode == mode else: # os.makedirs() behavior changed since python 3.7, # os.makedirs() will not respect the 'mode' parameter for # intermediate directories and will create them with the # default mode=0o777 assert oct(actual_mode) == oct(0o777 & ~get_umask())
def create(cls, sdUUID, domainName, domClass, remotePath, storageType, version): """ Create new storage domain. 'sdUUID' - Storage Domain UUID 'domainName' - storage domain name ("iso" or "data domain name") 'domClass' - Data/Iso 'remotePath' - /data2 'storageType' - NFS_DOMAIN, LOCALFS_DOMAIN, &etc. 'version' - DOMAIN_VERSIONS """ cls.log.info("sdUUID=%s domainName=%s remotePath=%s " "domClass=%s", sdUUID, domainName, remotePath, domClass) if not misc.isAscii(domainName) and not sd.supportsUnicode(version): raise se.UnicodeArgumentException() # Create local path mntPath = fileUtils.transformPath(remotePath) mntPoint = os.path.join(sc.REPO_MOUNT_DIR, mntPath) cls._preCreateValidation(sdUUID, mntPoint, remotePath, version) domainDir = os.path.join(mntPoint, sdUUID) cls._prepareMetadata(domainDir, sdUUID, domainName, domClass, remotePath, storageType, version) # create domain images folder imagesDir = os.path.join(domainDir, sd.DOMAIN_IMAGES) cls.log.info("Creating domain images directory %r", imagesDir) fileUtils.createdir(imagesDir) # create special imageUUID for ISO/Floppy volumes # Actually the local domain shouldn't be ISO, but # we can allow it for systems without NFS at all if domClass is sd.ISO_DOMAIN: isoDir = os.path.join(imagesDir, sd.ISO_IMAGE_UUID) cls.log.info("Creating ISO domain images directory %r", isoDir) fileUtils.createdir(isoDir) fsd = LocalFsStorageDomain(os.path.join(mntPoint, sdUUID)) fsd.initSPMlease() return fsd
def create(cls, sdUUID, domainName, domClass, remotePath, storageType, version): """ Create new storage domain. 'sdUUID' - Storage Domain UUID 'domainName' - storage domain name ("iso" or "data domain name") 'domClass' - Data/Iso 'remotePath' - /data2 'storageType' - NFS_DOMAIN, LOCALFS_DOMAIN, &etc. 'version' - DOMAIN_VERSIONS """ cls.log.info("sdUUID=%s domainName=%s remotePath=%s " "domClass=%s", sdUUID, domainName, remotePath, domClass) if not misc.isAscii(domainName) and not sd.supportsUnicode(version): raise se.UnicodeArgumentException() # Create local path mntPath = fileUtils.transformPath(remotePath) mntPoint = os.path.join(cls.storage_repository, sd.DOMAIN_MNT_POINT, mntPath) cls._preCreateValidation(sdUUID, mntPoint, remotePath, version) domainDir = os.path.join(mntPoint, sdUUID) cls._prepareMetadata(domainDir, sdUUID, domainName, domClass, remotePath, storageType, version) # create domain images folder imagesDir = os.path.join(domainDir, sd.DOMAIN_IMAGES) fileUtils.createdir(imagesDir) # create special imageUUID for ISO/Floppy volumes # Actually the local domain shouldn't be ISO, but # we can allow it for systems without NFS at all if domClass is sd.ISO_DOMAIN: isoDir = os.path.join(imagesDir, sd.ISO_IMAGE_UUID) fileUtils.createdir(isoDir) fsd = LocalFsStorageDomain(os.path.join(mntPoint, sdUUID)) fsd.initSPMlease() return fsd
def create(cls, sdUUID, domainName, domClass, remotePath, storageType, version, block_size=sc.BLOCK_SIZE_512, max_hosts=sc.HOSTS_4K_1M): """ Create new storage domain Arguments: sdUUID (UUID): Storage Domain UUID domainName (str): Storage domain name domClass (int): Data/Iso remotePath (str): /data storageType (int): LOCALFS_DOMAIN version (int): DOMAIN_VERSIONS block_size (int): Underlying storage block size. Supported value is BLOCK_SIZE_512 max_hosts (int): Maximum number of hosts accessing this domain, default to sc.HOSTS_4K_1M. """ cls._validate_block_size(block_size, version) cls.validate_version(version) if not misc.isAscii(domainName) and not sd.supportsUnicode(version): raise se.UnicodeArgumentException() # Create local path mntPath = fileUtils.transformPath(remotePath) mntPoint = os.path.join(sc.REPO_MOUNT_DIR, mntPath) cls._preCreateValidation(sdUUID, mntPoint, remotePath, version) storage_block_size = cls._detect_block_size(sdUUID, mntPoint) block_size = cls._validate_storage_block_size(block_size, storage_block_size) alignment = clusterlock.alignment(block_size, max_hosts) domainDir = os.path.join(mntPoint, sdUUID) cls._prepareMetadata(domainDir, sdUUID, domainName, domClass, remotePath, storageType, version, alignment, block_size) # create domain images folder imagesDir = os.path.join(domainDir, sd.DOMAIN_IMAGES) cls.log.info("Creating domain images directory %r", imagesDir) fileUtils.createdir(imagesDir) # create special imageUUID for ISO/Floppy volumes # Actually the local domain shouldn't be ISO, but # we can allow it for systems without NFS at all if domClass is sd.ISO_DOMAIN: isoDir = os.path.join(imagesDir, sd.ISO_IMAGE_UUID) cls.log.info("Creating ISO domain images directory %r", isoDir) fileUtils.createdir(isoDir) fsd = LocalFsStorageDomain(os.path.join(mntPoint, sdUUID)) fsd.initSPMlease() return fsd
def test_createdir_directory_exists_other_mode(): with namedTemporaryDir() as base: with pytest.raises(OSError) as e: fileUtils.createdir(base, mode=0o755) assert e.value.errno == errno.EPERM
def test_createdir_directory_exists_other_mode(): with namedTemporaryDir() as base: with pytest.raises(OSError): fileUtils.createdir(base, mode=0o755)
def test_createdir_no_mode(): with namedTemporaryDir() as base: path = os.path.join(base, "a", "b") assert not os.path.isdir(path) fileUtils.createdir(path) assert os.path.isdir(path)
def test_directory_exists_no_mode(self): with namedTemporaryDir() as base: fileUtils.createdir(base)
def test_create_dirs_no_mode(self): with namedTemporaryDir() as base: path = os.path.join(base, "a", "b") self.assertFalse(os.path.isdir(path)) fileUtils.createdir(path) self.assertTrue(os.path.isdir(path))