def _write_metadata(self, path, metadata, fd=None): meta_dir_path, meta_file_path = self._get_metadata_dir(path) # create metadata dir if it doesn't exist # TODO error handling if not os.path.exists(meta_dir_path): mkdirs(meta_dir_path) metastr = serialize_metadata(metadata) tmpfile = meta_file_path + '_' + uuid4().hex try: with open(tmpfile, 'wt') as f: f.write(metastr) except IOError as err: if err.errno in (errno.ENOSPC, errno.EDQUOT): do_log_rl("_write_metadata failed: %s : %s", err, meta_file_path) raise DiskFileNoSpace() else: raise FileConnectorFileSystemIOError( err.errno, '_write_metadata failed: %s : %s' % (err, meta_file_path)) try: do_rename(tmpfile, meta_file_path) except OSError as err: # TODO better error handling raise FileConnectorFileSystemOSError( err.errno, "%s, rename('%s', '%s')" % ( err.strerror, tmpfile, meta_file_path))
def test_mkdirs(self): try: subdir = os.path.join('/tmp', str(random.random())) path = os.path.join(subdir, str(random.random())) fs.mkdirs(path) assert os.path.exists(path) finally: shutil.rmtree(subdir)
def test_mkdirs_already_dir(self): tmpdir = mkdtemp() try: fs.mkdirs(tmpdir) except (FileConnectorFileSystemOSError, OSError): self.fail("Unexpected exception") else: pass finally: shutil.rmtree(tmpdir)
def test_mkdirs_existing_file_on_path(self): tmpdir = mkdtemp() fd, tmpfile = mkstemp(dir=tmpdir) try: fs.mkdirs(os.path.join(tmpfile, 'b')) except OSError: pass else: self.fail("Expected FileConnectorFileSystemOSError exception") finally: os.close(fd) shutil.rmtree(tmpdir)
def test_read_cont_metadata(self): path = os.path.join(self.td, 'cont') md_dir = os.path.join(self.td, 'cont', self.mp.meta_dir) md_file = os.path.join(self.td, 'cont', self.mp.meta_dir, self.mp.cont_meta) orig_d = {'bar': 'foo'} mkdirs(md_dir) with open(md_file, 'wt') as f: f.write(serialize_metadata(orig_d)) md = self.mp.read_metadata(path) self.assertEqual(orig_d, md)
def test_mkdirs_DiskFileNoSpace(self): with patch('os.makedirs', mock_os_mkdir_makedirs_enospc): try: fs.mkdirs("blah") except DiskFileNoSpace: pass else: self.fail("Expected DiskFileNoSpace exception") with patch('os.makedirs', mock_os_mkdir_makedirs_edquot): try: fs.mkdirs("blah") except DiskFileNoSpace: pass else: self.fail("Expected DiskFileNoSpace exception")