diff --git a/requirements-swh.txt b/requirements-swh.txt --- a/requirements-swh.txt +++ b/requirements-swh.txt @@ -1,3 +1,3 @@ swh.core[http] >= 0.3 -swh.deposit +swh.deposit >= 0.3 swh.storage >= 0.0.162 diff --git a/swh/icinga_plugins/deposit.py b/swh/icinga_plugins/deposit.py --- a/swh/icinga_plugins/deposit.py +++ b/swh/icinga_plugins/deposit.py @@ -1,4 +1,4 @@ -# Copyright (C) 2019 The Software Heritage developers +# Copyright (C) 2019-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 @@ -6,6 +6,7 @@ import datetime import sys import time +from typing import Any, Dict, Optional from swh.deposit.client import PublicApiDepositClient @@ -23,6 +24,7 @@ self._archive_path = obj["archive"] self._metadata_path = obj["metadata"] self._collection = obj["collection"] + self._slug: Optional[str] = None self._client = PublicApiDepositClient( { @@ -32,16 +34,36 @@ ) def upload_deposit(self): + slug = "check-deposit-%s" % datetime.datetime.now().isoformat() result = self._client.deposit_create( archive=self._archive_path, metadata=self._metadata_path, collection=self._collection, in_progress=False, - slug="check-deposit-%s" % datetime.datetime.now().isoformat(), + slug=slug, ) + self._slug = slug self._deposit_id = result["deposit_id"] return result + def update_deposit_with_metadata(self) -> Dict[str, Any]: + """Trigger a metadata update on the deposit once it's completed. + + """ + deposit = self.get_deposit_status() + assert deposit["deposit_status"] == "done" + swhid = deposit["deposit_swh_id"] + assert deposit["deposit_id"] == self._deposit_id + + # We can reuse the initial metadata file we already sent + return self._client.deposit_update( + self._collection, + self._deposit_id, + self._slug, + metadata=self._metadata_path, + swhid=swhid, + ) + def get_deposit_status(self): return self._client.deposit_status( collection=self._collection, deposit_id=self._deposit_id @@ -117,12 +139,34 @@ ) return 2 - # Everything went fine, check total time wasn't too large and - # print result (status_code, status) = self.get_status(metrics["total_time"]) self.print_result( status, f'Deposit took {metrics["total_time"]:.2f}s and succeeded.', **metrics, ) + + result = self.update_deposit_with_metadata() + metrics["total_time"] = time.time() - start_time + metrics["update_time"] = ( + metrics["total_time"] + - metrics["upload_time"] + - metrics["validation_time"] + - metrics["load_time"] + ) + if "error" in result: + self.print_result( + "CRITICAL", + f'Deposit metadata update failed: {result["error"]} ', + **metrics, + ) + + (status_code, status) = self.get_status(metrics["total_time"]) + self.print_result( + status, + f'Deposit Metadata update took {metrics["update_time"]:.2f}s ' + "and succeeded.", + **metrics, + ) + return status_code diff --git a/swh/icinga_plugins/tests/test_deposit.py b/swh/icinga_plugins/tests/test_deposit.py --- a/swh/icinga_plugins/tests/test_deposit.py +++ b/swh/icinga_plugins/tests/test_deposit.py @@ -64,6 +64,7 @@ 42 {status} {status_detail} + {swhid} """ @@ -113,8 +114,29 @@ ): scenario = WebScenario() + status_xml = STATUS_TEMPLATE.format( + status="done", + status_detail="", + swhid="swh:1:dir:02ed6084fb0e8384ac58980e07548a547431cf74", + ) + + # Initial deposit + scenario.add_step( + "post", f"{BASE_URL}/testcol/", ENTRY_TEMPLATE.format(status="done") + ) + # Then metadata update + scenario.add_step( + "get", f"{BASE_URL}/testcol/42/status/", status_xml, + ) + # internal deposit client does call status, then update metadata then status api + scenario.add_step( + "get", f"{BASE_URL}/testcol/42/status/", status_xml, + ) scenario.add_step( - "post", BASE_URL + "/testcol/", ENTRY_TEMPLATE.format(status="done") + "put", f"{BASE_URL}/testcol/42/metadata/", status_xml, + ) + scenario.add_step( + "get", f"{BASE_URL}/testcol/42/status/", status_xml, ) scenario.install_mock(requests_mock) @@ -137,8 +159,14 @@ "| 'total_time' = 0.00s\n" "| 'upload_time' = 0.00s\n" "| 'validation_time' = 0.00s\n" + "DEPOSIT OK - Deposit Metadata update took 0.00s and succeeded.\n" + "| 'load_time' = 0.00s\n" + "| 'total_time' = 0.00s\n" + "| 'update_time' = 0.00s\n" + "| 'upload_time' = 0.00s\n" + "| 'validation_time' = 0.00s\n" ) - assert result.exit_code == 0, result.output + assert result.exit_code == 0, f"Unexpected output: {result.output}" def test_deposit_delays(