diff --git a/swh/deposit/loader/checker.py b/swh/deposit/loader/checker.py index 5c01027c..bb054529 100644 --- a/swh/deposit/loader/checker.py +++ b/swh/deposit/loader/checker.py @@ -1,48 +1,51 @@ -# Copyright (C) 2017-2019 The Software Heritage developers +# Copyright (C) 2017-2020 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 import logging from typing import Mapping from swh.core.config import SWHConfig from swh.deposit.client import PrivateApiDepositClient logger = logging.getLogger(__name__) class DepositChecker(SWHConfig): """Deposit checker implementation. Trigger deposit's checks through the private api. """ CONFIG_BASE_FILENAME = "deposit/checker" DEFAULT_CONFIG = { "deposit": ("dict", {"url": "http://localhost:5006/1/private/", "auth": {},}) } def __init__(self, config=None): super().__init__() if config is None: self.config = self.parse_config_file() else: self.config = config self.client = PrivateApiDepositClient(config=self.config["deposit"]) def check(self, collection: str, deposit_id: str) -> Mapping[str, str]: status = None deposit_check_url = f"/{collection}/{deposit_id}/check/" + logger.debug("deposit-check-url: %s", deposit_check_url) try: r = self.client.check(deposit_check_url) + logger.debug("Check result: %s", r) status = "eventful" if r == "verified" else "failed" except Exception: - logger.exception("Failure during check on '%s'" % (deposit_check_url,)) + logger.exception("Failure during check on '%s'", deposit_check_url) status = "failed" + logger.debug("Check status: %s", status) return {"status": status} diff --git a/swh/deposit/tests/loader/test_tasks.py b/swh/deposit/tests/loader/test_tasks.py index e4453d61..c62fd45a 100644 --- a/swh/deposit/tests/loader/test_tasks.py +++ b/swh/deposit/tests/loader/test_tasks.py @@ -1,24 +1,69 @@ # Copyright (C) 2018-2020 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 import pytest -from unittest.mock import patch + +@pytest.mark.db +def test_deposit_check_eventful(mocker, swh_config, swh_app, celery_session_worker): + """Successful check should make the check succeed + + """ + client = mocker.patch("swh.deposit.loader.checker.PrivateApiDepositClient.check") + client.return_value = "verified" + + collection = "collection" + deposit_id = 42 + res = swh_app.send_task( + "swh.deposit.loader.tasks.ChecksDepositTsk", args=[collection, deposit_id] + ) + assert res + res.wait() + assert res.successful() + + assert res.result == {"status": "eventful"} + client.assert_called_once_with(f"/{collection}/{deposit_id}/check/") + + +@pytest.mark.db +def test_deposit_check_failure(mocker, swh_config, swh_app, celery_session_worker): + """Unverified check status should make the check fail + + """ + client = mocker.patch("swh.deposit.loader.checker.PrivateApiDepositClient.check") + client.return_value = "not-verified" # will make the status "failed" + + collection = "collec" + deposit_id = 666 + res = swh_app.send_task( + "swh.deposit.loader.tasks.ChecksDepositTsk", args=[collection, deposit_id] + ) + assert res + res.wait() + assert res.successful() + + assert res.result == {"status": "failed"} + client.assert_called_once_with(f"/{collection}/{deposit_id}/check/") @pytest.mark.db -def test_deposit_check(mocker, swh_config, swh_app, celery_session_worker): - checker = mocker.patch("swh.deposit.loader.checker.DepositChecker.check") - checker.return_value = {"status": "uneventful"} +def test_deposit_check_3(mocker, swh_config, swh_app, celery_session_worker): + """Unexpected failures should fail the check + + """ + client = mocker.patch("swh.deposit.loader.checker.PrivateApiDepositClient.check") + client.side_effect = ValueError("unexpected failure will make it fail") + collection = "another-collection" + deposit_id = 999 res = swh_app.send_task( - "swh.deposit.loader.tasks.ChecksDepositTsk", args=["collection", 42] + "swh.deposit.loader.tasks.ChecksDepositTsk", args=[collection, deposit_id] ) assert res res.wait() assert res.successful() - assert res.result == {"status": "uneventful"} - checker.assert_called_once_with("collection", 42) + assert res.result == {"status": "failed"} + client.assert_called_once_with(f"/{collection}/{deposit_id}/check/")