diff --git a/requirements-test.txt b/requirements-test.txt index f9a1547b2056f741148fe3c39b9ffdb3aec3d5db..ba4ed4b28cfdeb881a8a7f4d34daaf161192b394 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -5,6 +5,7 @@ urllib3 swh-core[testing] swh-scheduler[testing] >= 2.0.0 swh-storage[testing] >= 2.0.0 +swh-vault types-click types-python-dateutil types-pyyaml diff --git a/swh/loader/tests/__init__.py b/swh/loader/tests/__init__.py index 0046cfeef48797ef01d59602b021478fb26f64f4..119f3b1886a4f04514d085d75d4b28a666e492b9 100644 --- a/swh/loader/tests/__init__.py +++ b/swh/loader/tests/__init__.py @@ -7,13 +7,17 @@ from collections import defaultdict import os from pathlib import PosixPath import subprocess +import tempfile from typing import Dict, Iterable, List, Optional, Tuple, Union +from swh.loader.core.nar import Nar from swh.model.hashutil import hash_to_bytes from swh.model.model import ExtID, OriginVisitStatus, Snapshot, TargetType +from swh.model.swhids import ObjectType from swh.storage.algos.origin import origin_get_latest_visit_status from swh.storage.algos.snapshot import snapshot_get_all_branches from swh.storage.interface import StorageInterface +from swh.vault.to_disk import DirectoryBuilder def assert_last_visit_matches( @@ -283,6 +287,32 @@ def fetch_extids_from_checksums( extid = storage.extid_get_from_extid(id_type, ids, extid_version) extids.extend(extid) + for extid_ in extids: + if extid_.extid_type.startswith("nar-"): + # check NAR hashes of archived directory or content match the expected ones + target_swhid = extid_.target + with tempfile.TemporaryDirectory() as tmp_dir: + nar = Nar(hash_names=list(checksums.keys())) + if target_swhid.object_type == ObjectType.DIRECTORY: + dir_builder = DirectoryBuilder( + storage=storage, + root=tmp_dir.encode(), + dir_id=target_swhid.object_id, + ) + dir_builder.build() + path_to_hash = tmp_dir + else: + path_to_hash = os.path.join(tmp_dir, "content") + content_bytes = storage.content_get_data( + {"sha1_git": target_swhid.object_id} + ) + assert content_bytes is not None + with open(path_to_hash, "wb") as content: + content.write(content_bytes) + + nar.serialize(PosixPath(path_to_hash)) + assert nar.hexdigest() == checksums + return extids