def head(self, filename): try: return self.connection.head_object(self.container, filename) except swiftclient.exceptions.ClientException as exc: raise exceptions.StoreExceptions('Error while heading file ' '%s: %s' % (filename, str(exc)), status_code=exc.http_status)
def test_purge_failure(app, admin, user, jobstate_user_id, job_user_id, team_user_id): # create two files and archive them file_id1 = t_utils.post_file(user, jobstate_user_id, FileDesc('kikoolol', 'content')) user.delete('/api/v1/files/%s' % file_id1) file_id2 = t_utils.post_file(user, jobstate_user_id, FileDesc('kikoolol2', 'content2')) user.delete('/api/v1/files/%s' % file_id2) to_purge = admin.get('/api/v1/files/purge').data assert len(to_purge['files']) == 2 # purge will fail with mock.patch('dci.stores.filesystem.FileSystem.delete') as mock_delete: mock_delete.side_effect = dci_exc.StoreExceptions('error') purge_res = admin.post('/api/v1/files/purge') assert purge_res.status_code == 400 path1 = files_utils.build_file_path(team_user_id, job_user_id, file_id1) path2 = files_utils.build_file_path(team_user_id, job_user_id, file_id2) store = dci_config.get_store('files') store.get(path1) store.get(path2) to_purge = admin.get('/api/v1/files/purge').data assert len(to_purge['files']) == 2
def test_purge_failure(user, admin, job_user_id, jobstate_user_id, team_user_id): job = user.get('/api/v1/jobs/%s' % job_user_id).data['job'] # create a file file_id1 = t_utils.post_file(user, jobstate_user_id, FileDesc('kikoolol', 'content')) djob = admin.delete('/api/v1/jobs/%s' % job_user_id, headers={'If-match': job['etag']}) assert djob.status_code == 204 to_purge_jobs = admin.get('/api/v1/jobs/purge').data assert len(to_purge_jobs['jobs']) == 1 to_purge_files = admin.get('/api/v1/files/purge').data assert len(to_purge_files['files']) == 1 # purge will fail with mock.patch('dci.stores.filesystem.FileSystem.delete') as mock_delete: mock_delete.side_effect = dci_exc.StoreExceptions('error') purge_res = admin.post('/api/v1/jobs/purge') assert purge_res.status_code == 400 path1 = files_utils.build_file_path(team_user_id, job_user_id, file_id1) store = dci_config.get_store('files') # because the delete fail the backend didn't remove the files and the # files are still in the database store.get(path1) to_purge_files = admin.get('/api/v1/files/purge').data assert len(to_purge_files['files']) == 1 to_purge_jobs = admin.get('/api/v1/jobs/purge').data assert len(to_purge_jobs['jobs']) == 1
def test_purge_failure(admin, components_user_ids, topic_user_id): component_id = components_user_ids[0] url = '/api/v1/components/%s/files' % component_id c_file1 = admin.post(url, data='lol') assert c_file1.status_code == 201 c_files = admin.get('/api/v1/components/%s/files' % component_id) assert len(c_files.data['component_files']) == 1 d_component = admin.delete('/api/v1/components/%s' % component_id) assert d_component.status_code == 204 to_purge = admin.get('/api/v1/components/purge').data assert len(to_purge['components']) == 1 # purge will fail with mock.patch('dci.stores.filesystem.FileSystem.delete') as mock_delete: path1 = files_utils.build_file_path( topic_user_id, component_id, c_file1.data['component_file']['id']) # noqa mock_delete.side_effect = dci_exc.StoreExceptions('error') purge_res = admin.post('/api/v1/components/purge') assert purge_res.status_code == 400 store = dci_config.get_store('components') store.get(path1) to_purge = admin.get('/api/v1/components/purge').data assert len(to_purge['components']) == 1
def get(self, filename): try: return self.connection.get_object(self.container, filename, resp_chunk_size=65535) except swiftclient.exceptions.ClientException as exc: raise exceptions.StoreExceptions('Error while getting file ' '%s: %s' % (filename, str(exc)), status_code=exc.http_status)
def delete(self, filename): try: self.connection.delete_object(self.container, filename, headers={'X-Delete-After': 1}) except swiftclient.exceptions.ClientException as e: raise exceptions.StoreExceptions('Error while deleting file ' '%s: %s' % (filename, str(e)), status_code=e.http_status)
def delete(self, filename): try: os.remove(os.path.join(self._root_directory, filename)) except OSError as e: status_code = 400 if e.errno == errno.ENOENT: status_code = 404 raise exceptions.StoreExceptions('Error while deleting file ' '%s: %s' % (filename, str(e)), status_code=status_code)
def get(self, filename): file_path = os.path.join(self._root_directory, filename) try: return ([], open(file_path, 'r')) except IOError as e: status_code = 400 if e.errno == errno.ENOENT: status_code = 404 raise exceptions.StoreExceptions('Error while accessing file ' '%s: %s' % (filename, str(e)), status_code=status_code)
def head(self, filename): file_path = os.path.join(self._root_directory, filename) try: file_size = os.path.getsize(file_path) except IOError as e: status_code = 400 if e.errno == errno.ENOENT: status_code = 404 raise exceptions.StoreExceptions('Error while accessing file ' '%s: %s' % (filename, str(e)), status_code=status_code) md5 = files_utils.md5Checksum(file_path) return { 'content-length': file_size, 'etag': md5, 'content-type': 'application/octet-stream' }
def upload(self, file_path, iterable, pseudo_folder=None, create_container=True): try: self.connection.head_container(self.container) except swiftclient.exceptions.ClientException as exc: if exc.http_status == 404 and create_container: try: self.connection.put_container(self.container) except swiftclient.exceptions.ClientException as exc: raise exceptions.StoreExceptions( 'Error while creating file ' # noqa '%s: %s' % (file_path, str(exc)), # noqa status_code=exc.http_status) # noqa self.connection.put_object(self.container, file_path, iterable)
def delete(self, filename): try: self.connection.delete_object(self.container, filename) except swiftclient.exceptions.ClientException: raise exceptions.StoreExceptions('An error occured while ' 'deleting %s' % filename)