Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/debian/tests/test_debian.py
# Copyright (C) 2019-2021 The Software Heritage developers | # Copyright (C) 2019-2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import logging | import logging | ||||
from os import path | from os import path | ||||
import random | |||||
import pytest | import pytest | ||||
from swh.loader.package.debian.loader import ( | from swh.loader.package.debian.loader import ( | ||||
DebianLoader, | DebianLoader, | ||||
DebianPackageChangelog, | DebianPackageChangelog, | ||||
DebianPackageInfo, | DebianPackageInfo, | ||||
IntrinsicPackageMetadata, | IntrinsicPackageMetadata, | ||||
▲ Show 20 Lines • Show All 389 Lines • ▼ Show 20 Lines | expected_snapshot = Snapshot( | ||||
b"releases/buster/contrib/0.7.2-4": SnapshotBranch( | b"releases/buster/contrib/0.7.2-4": SnapshotBranch( | ||||
target_type=TargetType.REVISION, | target_type=TargetType.REVISION, | ||||
target=hash_to_bytes("8224139c274c984147ef4b09aa0e462c55a10bd3"), | target=hash_to_bytes("8224139c274c984147ef4b09aa0e462c55a10bd3"), | ||||
), | ), | ||||
}, | }, | ||||
) | ) | ||||
check_snapshot(expected_snapshot, swh_storage) | check_snapshot(expected_snapshot, swh_storage) | ||||
def test_debian_resolve_revision_from_artifacts_edge_cases(): | |||||
"""Solving revision with empty data will result in unknown revision | |||||
""" | |||||
loader = DebianLoader(None, None, None, None) | |||||
empty_artifact = { | |||||
"name": PACKAGE_FILES["name"], | |||||
"version": PACKAGE_FILES["version"], | |||||
} | |||||
for package_artifacts in [empty_artifact, PACKAGE_FILES]: | |||||
p_info = DebianPackageInfo.from_metadata(package_artifacts, url=URL) | |||||
actual_revision = loader.resolve_revision_from_artifacts({}, p_info) | |||||
assert actual_revision is None | |||||
for known_artifacts in [{}, PACKAGE_FILES]: | |||||
actual_revision = loader.resolve_revision_from_artifacts( | |||||
known_artifacts, DebianPackageInfo.from_metadata(empty_artifact, url=URL) | |||||
) | |||||
assert actual_revision is None | |||||
known_package_artifacts = { | |||||
b"(\x07\xf5\xb3\xf8Ch\xb4\x88\x9a\x9a\xe8'\xfe\x85\x85O\xfe\xcf\x07": { | |||||
"extrinsic": { | |||||
# empty | |||||
}, | |||||
# ... removed the unnecessary intermediary data | |||||
} | |||||
} | |||||
assert not loader.resolve_revision_from_artifacts( | |||||
known_package_artifacts, DebianPackageInfo.from_metadata(PACKAGE_FILES, url=URL) | |||||
) | |||||
def test_debian_resolve_revision_from_artifacts_edge_cases_hit_and_miss(): | |||||
"""Solving revision with inconsistent data will result in unknown revision | |||||
""" | |||||
loader = DebianLoader(None, None, None, None) | |||||
artifact_metadata = PACKAGE_FILES2 | |||||
p_info = DebianPackageInfo.from_metadata(artifact_metadata, url=URL) | |||||
expected_revision_id = ( | |||||
b"(\x08\xf5\xb3\xf8Ch\xb4\x88\x9a\x9a\xe8'\xff\x85\x85O\xfe\xcf\x07" # noqa | |||||
) | |||||
known_package_artifacts = { | |||||
expected_revision_id: { | |||||
"extrinsic": {"raw": PACKAGE_FILES,}, | |||||
# ... removed the unnecessary intermediary data | |||||
} | |||||
} | |||||
actual_revision = loader.resolve_revision_from_artifacts( | |||||
known_package_artifacts, p_info | |||||
) | |||||
assert actual_revision is None | |||||
def test_debian_resolve_revision_from_artifacts(): | |||||
"""Solving revision with consistent data will solve the revision | |||||
""" | |||||
loader = DebianLoader(None, None, None, None) | |||||
artifact_metadata = PACKAGE_FILES | |||||
p_info = DebianPackageInfo.from_metadata(artifact_metadata, url=URL) | |||||
expected_revision_id = ( | |||||
b"(\x07\xf5\xb3\xf8Ch\xb4\x88\x9a\x9a\xe8'\xfe\x85\x85O\xfe\xcf\x07" # noqa | |||||
) | |||||
files = artifact_metadata["files"] | |||||
# shuffling dict's keys | |||||
keys = list(files.keys()) | |||||
random.shuffle(keys) | |||||
package_files = { | |||||
"name": PACKAGE_FILES["name"], | |||||
"version": PACKAGE_FILES["version"], | |||||
"files": {k: files[k] for k in keys}, | |||||
} | |||||
known_package_artifacts = { | |||||
expected_revision_id: { | |||||
"extrinsic": {"raw": package_files,}, | |||||
# ... removed the unnecessary intermediary data | |||||
} | |||||
} | |||||
actual_revision = loader.resolve_revision_from_artifacts( | |||||
known_package_artifacts, p_info | |||||
) | |||||
assert actual_revision == expected_revision_id | |||||
def test_debian_resolve_revision_from_artifacts_corrupt_known_artifact(): | |||||
"""To many or not enough .dsc files in the known_artifacts dict""" | |||||
loader = DebianLoader(None, None, None, None) | |||||
artifact_metadata = PACKAGE_FILES | |||||
p_info = DebianPackageInfo.from_metadata(artifact_metadata, url=URL) | |||||
expected_revision_id = ( | |||||
b"(\x07\xf5\xb3\xf8Ch\xb4\x88\x9a\x9a\xe8'\xfe\x85\x85O\xfe\xcf\x07" | |||||
) | |||||
files = dict(artifact_metadata["files"]) | |||||
package_files = { | |||||
"name": PACKAGE_FILES["name"], | |||||
"version": PACKAGE_FILES["version"], | |||||
"files": files, | |||||
} | |||||
known_package_artifacts = { | |||||
expected_revision_id: { | |||||
"extrinsic": {"raw": package_files,}, | |||||
# ... removed the unnecessary intermediary data | |||||
} | |||||
} | |||||
# Too many .dsc | |||||
files["another.dsc"] = files["cicero_0.7.2-3.dsc"] | |||||
assert ( | |||||
loader.resolve_revision_from_artifacts(known_package_artifacts, p_info) is None | |||||
) | |||||
# Not enough .dsc | |||||
del files["another.dsc"] | |||||
del files["cicero_0.7.2-3.dsc"] | |||||
assert ( | |||||
loader.resolve_revision_from_artifacts(known_package_artifacts, p_info) is None | |||||
) | |||||
def test_debian_resolve_revision_from_artifacts_corrupt_new_artifact(): | |||||
loader = DebianLoader(None, None, None, None) | |||||
artifact_metadata = PACKAGE_FILES | |||||
files = PACKAGE_FILES["files"] | |||||
files = {**files, "another.dsc": files["cicero_0.7.2-3.dsc"]} | |||||
artifact_metadata = {**PACKAGE_FILES, "files": files} | |||||
# Too many .dsc | |||||
files["another.dsc"] = files["cicero_0.7.2-3.dsc"] | |||||
p_info = DebianPackageInfo.from_metadata(artifact_metadata, url=URL) | |||||
assert loader.resolve_revision_from_artifacts(PACKAGE_FILES, p_info) is None | |||||
# Not enough .dsc | |||||
del files["another.dsc"] | |||||
del files["cicero_0.7.2-3.dsc"] | |||||
p_info = DebianPackageInfo.from_metadata(artifact_metadata, url=URL) | |||||
assert loader.resolve_revision_from_artifacts(PACKAGE_FILES, p_info) is None |