| 1 |
# SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
"""Helper functions for the `testsigs` unit test suite for `debsigs.""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import contextlib |
| 8 |
import dataclasses |
| 9 |
import functools |
| 10 |
import itertools |
| 11 |
import os |
| 12 |
import pathlib |
| 13 |
import subprocess # noqa: S404 |
| 14 |
import sys |
| 15 |
import tempfile |
| 16 |
import typing |
| 17 |
|
| 18 |
import feature_check |
| 19 |
import pytest |
| 20 |
import typedload.dataloader |
| 21 |
from feature_check import version as fver |
| 22 |
|
| 23 |
from testsigs import defs # noqa: TC001 # typedload needs this |
| 24 |
from testsigs.parse import debsigs as p_debsigs |
| 25 |
|
| 26 |
|
| 27 |
if sys.version_info >= (3, 11): |
| 28 |
import tomllib |
| 29 |
else: |
| 30 |
import tomli as tomllib |
| 31 |
|
| 32 |
|
| 33 |
if typing.TYPE_CHECKING: |
| 34 |
from collections.abc import Iterator |
| 35 |
from typing import Final |
| 36 |
|
| 37 |
|
| 38 |
@dataclasses.dataclass(frozen=True) |
| 39 |
class ConfigAggrBase: |
| 40 |
"""The aggregated test configuration stored by the `testsigs` runner.""" |
| 41 |
|
| 42 |
cfg: defs.Config |
| 43 |
"""The runtime configuration of the `testsigs` runner.""" |
| 44 |
|
| 45 |
keys: defs.KeyFiles |
| 46 |
"""The OpenPGP keys generated beforehand.""" |
| 47 |
|
| 48 |
pub_key: defs.PublicKey |
| 49 |
"""The OpenPGP key to sign with.""" |
| 50 |
|
| 51 |
changes_orig: defs.ChangesFile |
| 52 |
"""The path to the downloaded Debian package to sign.""" |
| 53 |
|
| 54 |
@contextlib.contextmanager |
| 55 |
def cfg_with_tempd(self) -> Iterator[defs.Config]: |
| 56 |
"""Create a temporary directory, replace `cfg.path.work` with that.""" |
| 57 |
with tempfile.TemporaryDirectory(prefix="unit.", dir=self.cfg.path.work) as tempd_obj: |
| 58 |
tempd: Final = pathlib.Path(tempd_obj) |
| 59 |
yield dataclasses.replace( |
| 60 |
self.cfg, |
| 61 |
path=dataclasses.replace(self.cfg.path, work=tempd), |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
@dataclasses.dataclass(frozen=True) |
| 66 |
class ConfigAggr(ConfigAggrBase): |
| 67 |
"""The aggregated test configuration along with some more things we may need.""" |
| 68 |
|
| 69 |
debsigs_features: dict[str, feature_check.Version] |
| 70 |
"""The parsed output of `debsigs --features`.""" |
| 71 |
|
| 72 |
def has_feature(self, feature: str, min_version: str) -> bool: |
| 73 |
"""Determine whether the specified feature is there at that version or later.""" |
| 74 |
found: Final = self.debsigs_features.get(feature) |
| 75 |
if found is None: |
| 76 |
return False |
| 77 |
|
| 78 |
return fver.version_compare(found, feature_check.parse_version(min_version)) >= 0 |
| 79 |
|
| 80 |
def has_all_features(self, features: dict[str, str]) -> bool: |
| 81 |
"""Determine whether all of the specified features are there at least at those versions.""" |
| 82 |
return all(itertools.starmap(self.has_feature, features.items())) |
| 83 |
|
| 84 |
|
| 85 |
@functools.lru_cache |
| 86 |
def get_loader() -> typedload.dataloader.Loader: |
| 87 |
"""Prepare a dataloader that understands deferred type annotations.""" |
| 88 |
return typedload.dataloader.Loader(pep563=True) |
| 89 |
|
| 90 |
|
| 91 |
@pytest.fixture |
| 92 |
def test_cfg() -> ConfigAggr: |
| 93 |
"""Load the aggregated test configuration.""" |
| 94 |
with pathlib.Path(os.environ["DEBSIGS_TEST_CONFIG_FILE"]).open(mode="rb") as cfgf: |
| 95 |
cfg_base: Final = get_loader().load(tomllib.load(cfgf), ConfigAggrBase) |
| 96 |
|
| 97 |
return ConfigAggr( |
| 98 |
cfg=cfg_base.cfg, |
| 99 |
keys=cfg_base.keys, |
| 100 |
pub_key=cfg_base.pub_key, |
| 101 |
changes_orig=cfg_base.changes_orig, |
| 102 |
debsigs_features=feature_check.obtain_features(str(cfg_base.cfg.prog.debsigs)), |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
def debsigs_list(cfg: defs.Config, pkg: pathlib.Path) -> list[defs.DebSig]: |
| 107 |
"""Run `debsigs --list`, return any signatures returned.""" |
| 108 |
raw: Final = subprocess.check_output( # noqa: S603 |
| 109 |
[cfg.prog.debsigs, "--list", "--", pkg], |
| 110 |
cwd=cfg.path.work, |
| 111 |
encoding="UTF-8", |
| 112 |
env=cfg.env, |
| 113 |
) |
| 114 |
return p_debsigs.parse_list(raw) |
| 115 |
|
| 116 |
|
| 117 |
def get_signing_subkey(pub_key: defs.PublicKey) -> defs.Subkey: |
| 118 |
"""Find a single subkey capable of signing.""" |
| 119 |
match [subkey for subkey in pub_key.subkeys if "s" in subkey.capabilities]: |
| 120 |
case [single]: |
| 121 |
return single |
| 122 |
|
| 123 |
case _: |
| 124 |
raise RuntimeError(repr(pub_key)) |