Skip to content

Commit

Permalink
Raise an exception if upload report indicates failure
Browse files Browse the repository at this point in the history
  • Loading branch information
goosemania committed Apr 25, 2016
1 parent 2d3fd07 commit 44802e5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
7 changes: 5 additions & 2 deletions common/pulp/common/error_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
"PLP0032", _("Task %(task_id)s encountered one or more failures during execution."),
['task_id'])
PLP0034 = Error("PLP0034", _("The distributor %(distributor_id)s indicated a failed response when "
"publishing repository %(repository_id)s."),
['distributor_id', 'repository_id'])
"publishing repository %(repo_id)s."),
['distributor_id', 'repo_id'])
PLP0037 = Error(
"PLP0037",
_("Content import of %(path)s failed - must be an existing file."),
Expand All @@ -117,6 +117,9 @@
"not downloaded."), [])
PLP0046 = Error("PLP0046", _("The repository group cannot be exported because these repos have "
"units that are not downloaded: %(repos)s"), ['repos'])
PLP0047 = Error("PLP0047", _("The importer %(importer_id)s indicated a failed response when "
"uploading %(unit_type)s unit to repository %(repo_id)s."),
['importer_id', 'unit_type', 'repo_id'])

# Create a section for general validation errors (PLP1000 - PLP2999)
# Validation problems should be reported with a general PLP1000 error with a more specific
Expand Down
2 changes: 1 addition & 1 deletion server/pulp/server/controllers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ def _do_publish(repo_obj, dist_id, dist_inst, transfer_repo, conduit, call_confi
and not publish_report.success_flag:
_logger.info(publish_report.summary)
raise pulp_exceptions.PulpCodedException(
error_code=error_codes.PLP0034, repository_id=repo_obj.repo_id,
error_code=error_codes.PLP0034, repo_id=repo_obj.repo_id,
distributor_id=dist_id, summary=publish_report.summary
)

Expand Down
22 changes: 19 additions & 3 deletions server/pulp/server/managers/content/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

from celery import task

from pulp.common import error_codes
from pulp.plugins.conduits.upload import UploadConduit
from pulp.plugins.config import PluginCallConfiguration
from pulp.plugins.loader import api as plugin_api, exceptions as plugin_exceptions
from pulp.server import config as pulp_config
from pulp.server.async.tasks import Task
from pulp.server.db import model
from pulp.server.exceptions import (PulpDataException, MissingResource, PulpExecutionException,
PulpException)
PulpException, PulpCodedException)
from pulp.server.controllers import repository as repo_controller


Expand Down Expand Up @@ -180,8 +181,16 @@ def import_uploaded_unit(repo_id, unit_type_id, unit_key, unit_metadata, upload_
:type unit_metadata: dict
:param upload_id: upload being imported
:type upload_id: str
:return: A SyncReport indicating the success or failure of the upload
:rtype: pulp.plugins.model.SyncReport
:return: A dictionary describing the success or failure of the upload. It must
contain the following keys:
'success_flag': bool. Indicates whether the upload was successful
'summary': json-serializable object, providing summary
'details': json-serializable object, providing details
:rtype: dict
:raises MissingResource: if upload request was for the non-existent repository
:raises PulpCodedException: if import was unsuccessful and it was handled by the importer
:raises PulpException: if import was unsuccessful and it was not handled by the importer
:raises PulpExecutionException: if an unexpected error occured during the upload
"""
# If it doesn't raise an exception, it's good to go
ContentUploadManager.is_valid_upload(repo_id, unit_type_id)
Expand All @@ -207,6 +216,13 @@ def import_uploaded_unit(repo_id, unit_type_id, unit_key, unit_metadata, upload_
try:
result = importer_instance.upload_unit(transfer_repo, unit_type_id, unit_key,
unit_metadata, file_path, conduit, call_config)
if not result['success_flag']:
raise PulpCodedException(
error_code=error_codes.PLP0047, repo_id=transfer_repo.id,
importer_id=repo_importer['importer_type_id'],
unit_type=unit_type_id, summary=result['summary'], details=result['details']
)

repo_controller.rebuild_content_unit_counts(repo_obj)
return result

Expand Down
2 changes: 1 addition & 1 deletion server/test/unit/server/controllers/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ def test_failed_publish(self, m_dist_qs, m_repo_pub_result, mock_now,
e = assertion.exception
self.assertEqual(e.error_code, error_codes.PLP0034)
self.assertEqual(e.error_data['distributor_id'], 'dist')
self.assertEqual(e.error_data['repository_id'], fake_repo.repo_id)
self.assertEqual(e.error_data['repo_id'], fake_repo.repo_id)
self.assertEqual(e.error_data['summary'], fake_report.summary)


Expand Down
16 changes: 14 additions & 2 deletions server/test/unit/server/managers/content/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pulp.server.controllers import importer as importer_controller
from pulp.server.db import model
from pulp.server.exceptions import (MissingResource, PulpDataException, PulpExecutionException,
InvalidValue)
InvalidValue, PulpCodedException)
from pulp.server.managers.content.upload import ContentUploadManager
import pulp.server.managers.factory as manager_factory

Expand Down Expand Up @@ -169,7 +169,7 @@ def test_import_uploaded_unit(self, mock_repo_qs, mock_rebuild):
metadata = {'k1': 'v1'}

mock_repo = mock_repo_qs.get_repo_or_missing_resource.return_value
importer_return_report = object()
importer_return_report = {'success_flag': True, 'summary': '', 'details': {}}
mock_plugins.MOCK_IMPORTER.upload_unit.return_value = importer_return_report

upload_id = self.upload_manager.initialize_upload()
Expand Down Expand Up @@ -223,6 +223,18 @@ def test_import_uploaded_unit_importer_error_reraise_pulp_exception(self, mock_r
self.assertRaises(InvalidValue, self.upload_manager.import_uploaded_unit, 'repo-u',
'mock-type', {}, {}, upload_id)

@mock.patch('pulp.server.controllers.importer.model.Repository.objects')
def test_import_uploaded_unit_success_flag_false(self, mock_repo_qs):
"""Test that exception is raised if upload report indicates failure."""
importer_controller.set_importer('repo-u', 'mock-importer', {})
importer_return_report = {'success_flag': False, 'summary': '', 'details': {}}
mock_plugins.MOCK_IMPORTER.upload_unit.side_effect = None
mock_plugins.MOCK_IMPORTER.upload_unit.return_value = importer_return_report
upload_id = self.upload_manager.initialize_upload()
with self.assertRaises(PulpCodedException) as cm:
self.upload_manager.import_uploaded_unit('repo-u', 'mock-type', {}, {}, upload_id)
self.assertEqual('PLP0047', cm.exception.error_code.code)

def test_upload_dir_auto_created(self):
# Setup

Expand Down

0 comments on commit 44802e5

Please sign in to comment.