diff --git a/swh/deposit/loader/checker.py b/swh/deposit/loader/checker.py --- a/swh/deposit/loader/checker.py +++ b/swh/deposit/loader/checker.py @@ -1,4 +1,4 @@ -# 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 @@ -39,10 +39,13 @@ 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 --- a/swh/deposit/tests/loader/test_tasks.py +++ b/swh/deposit/tests/loader/test_tasks.py @@ -1,21 +1,69 @@ -# Copyright (C) 2018-2019 The Software Heritage developers +# 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 -from unittest.mock import patch +import pytest -@patch("swh.deposit.loader.checker.DepositChecker.check") -def test_deposit_check(checker, swh_config, swh_app, celery_session_worker): - checker.return_value = {"status": "uneventful"} +@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_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/")