def test_rename_file(self): with FileIO(self.test_rename_from_file, 'w') as testfile: testfile.write(b'test content for file which will be renamed') fs = FileServicer() # test rename of not existing file rename_request = fmsg.RenameRequest() rename_request.old = self.test_rename_to_file rename_request.new = self.test_rename_from_file rename_response = fs.Rename(rename_request, DummyContext()) self.assertEqual(rename_response.code, fmsg.ReturnStatus.StatusType.Value('OS_ERROR'), 'rename of not existing file returns a wrong result error: %d, expected: %d' % (rename_response.code, fmsg.ReturnStatus.StatusType.Value('OS_ERROR'))) # test rename rename_request = fmsg.RenameRequest() rename_request.old = self.test_rename_from_file rename_request.new = self.test_rename_to_file rename_response = fs.Rename(rename_request, DummyContext()) if not os.path.exists(self.test_rename_to_file): self.fail('After `rename` the target file does not exists')
def rename(self, old, new): ''' Rename path. :param str old: existing path :param str new: new path :raise OSError: :raise IOError: :raise Exception: ''' response = self.fm_stub.Rename(fmsg.RenameRequest(old=old, new=new), timeout=settings.GRPC_TIMEOUT) if response.code == OK: pass elif response.code == OS_ERROR: raise OSError(response.error_code, response.error_msg, response.error_file) elif response.code in [IO_ERROR]: raise IOError(response.error_code, response.error_msg, response.error_file) elif response.code == ERROR: raise Exception("%s, path: %s" % (response.error_msg, response.error_file))