Files
OBLITERATUS/tests/test_distributed_identity_boundaries.py

77 lines
3.2 KiB
Python

"""Identity enumeration stays local, bounded and bound to the validated wheel."""
from pathlib import Path
from types import SimpleNamespace
import pytest
import obliteratus.distributed.model_identity as identity
from obliteratus.distributed.contracts import ContractError
from tests import test_distributed_model_identity as fixtures
installed_environment = fixtures.installed_environment
def test_installed_identity_rejects_nonfilesystem_provider(installed_environment, monkeypatch):
wheel = fixtures._wheel(installed_environment)
provider = SimpleNamespace(metadata=wheel.metadata, version=wheel.version,
read_text=wheel.read_text, _path=wheel._path)
monkeypatch.setattr(identity.importlib.metadata, "distributions", lambda **kwargs: iter([provider]))
with pytest.raises(ContractError, match="metadata must be on the local filesystem"):
identity.installed_package_set_digest()
def test_checkout_metadata_cannot_escape_between_symlink_check_and_resolution(tmp_path, monkeypatch):
source, outside = tmp_path / "checkout", tmp_path / "outside"
module = source / "obliteratus/distributed/model_identity.py"
module.parent.mkdir(parents=True)
module.touch()
outside.mkdir()
egg = source / "obliteratus.egg-info"
original = Path.is_symlink
replaced = []
def swap(path):
observed = original(path)
if path == egg and not replaced:
egg.symlink_to(outside, target_is_directory=True)
replaced.append(True)
return observed
monkeypatch.setattr(identity, "__file__", str(module))
monkeypatch.setattr(Path, "is_symlink", swap)
with pytest.raises(ContractError, match="resolves outside the checkout root"):
identity._qualification_checkout_egg_info()
assert replaced == [True]
assert list(outside.iterdir()) == []
def test_live_distribution_count_is_bounded_before_consuming_the_remainder(installed_environment, monkeypatch):
fixtures._wheel(installed_environment)
dependency = fixtures._distribution("dependency")
discover = identity.importlib.metadata.distributions
consumed = []
def distributions(**kwargs):
if kwargs:
yield from discover(**kwargs)
return
for index in range(identity.MAX_DISTRIBUTIONS + 2):
consumed.append(index)
yield dependency
monkeypatch.setattr(identity.importlib.metadata, "distributions", distributions)
with pytest.raises(ContractError, match="count bound"):
list(identity._package_distributions_with_installed_obliteratus())
assert consumed == list(range(identity.MAX_DISTRIBUTIONS + 1))
def test_validated_installed_wheel_is_retained_when_global_discovery_omits_it(installed_environment, monkeypatch):
wheel = fixtures._wheel(installed_environment)
dependency = fixtures._wheel(installed_environment, "dependency")
expected = identity.installed_package_set_digest([wheel, dependency])
discover = identity.importlib.metadata.distributions
monkeypatch.setattr(identity.importlib.metadata, "distributions",
lambda **kwargs: discover(**kwargs) if kwargs else iter([dependency]))
assert identity.installed_package_set_digest() == expected