diff --git a/swh/loader/package/loader.py b/swh/loader/package/loader.py --- a/swh/loader/package/loader.py +++ b/swh/loader/package/loader.py @@ -259,14 +259,20 @@ tmp_revisions = {} # type: Dict[str, List] snapshot = None + # Prepare origin and origin_visit + origin = {'url': self.url} try: - # Prepare origin and origin_visit - origin = {'url': self.url} self.storage.origin_add_one(origin) visit_id = self.storage.origin_visit_add( origin=self.url, date=self.visit_date, type=self.visit_type)['visit'] + except Exception as e: + logger.error( + 'Failure to create origin/origin_visit. Reason: %s', e) + return {'status': 'failed'} + + try: last_snapshot = self.last_snapshot() logger.debug('last snapshot: %s', last_snapshot) known_artifacts = self.known_artifacts(last_snapshot) @@ -387,9 +393,9 @@ self.storage.origin_visit_update( origin=self.url, visit_id=visit_id, status=status_visit, snapshot=snapshot and snapshot['id']) - result = { - 'status': status_load, - } # type: Dict[str, Any] + result = { + 'status': status_load, + } # type: Dict[str, Any] if snapshot: result['snapshot_id'] = hash_to_hex(snapshot['id']) return result diff --git a/swh/loader/package/tests/test_loader.py b/swh/loader/package/tests/test_loader.py new file mode 100644 --- /dev/null +++ b/swh/loader/package/tests/test_loader.py @@ -0,0 +1,35 @@ +# Copyright (C) 2019 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +from swh.loader.package.loader import PackageLoader + + +class FakeStorage: + def origin_add_one(self, origin): + raise ValueError('We refuse to add an origin') + + +class FakeStorage2: + def origin_add_one(self, origin): + return origin + + def origin_visit_add(self, origin, date, type): + raise ValueError('We refuse to add an origin visit') + + +def test_loader_origin_visit_failure(swh_config): + """Failure to add origin or origin visit should failed immediately + + """ + loader = PackageLoader('some-url') + loader.storage = FakeStorage() + + actual_load_status = loader.load() + assert actual_load_status == {'status': 'failed'} + + loader.storage = FakeStorage2() + + actual_load_status2 = loader.load() + assert actual_load_status2 == {'status': 'failed'}