Testutil: add DNF5 helper function for depsolving

This will be used for testing the SBOM implementation with DNF5.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
This commit is contained in:
Tomáš Hozza 2024-11-07 13:27:16 +01:00 committed by Simon de Vlieger
parent 3bf6ca399f
commit 7993b78e7c
2 changed files with 61 additions and 0 deletions

50
osbuild/testutil/dnf5.py Normal file
View file

@ -0,0 +1,50 @@
import tempfile
from typing import List, Tuple
import libdnf5
from libdnf5.base import GoalProblem_NO_PROBLEM as NO_PROBLEM
def depsolve_pkgset(
repo_paths: List[str],
pkg_include: List[str]
) -> Tuple[libdnf5.base.Base, List[libdnf5.rpm.Package]]:
"""
Perform a dependency resolution on a set of local RPM repositories.
"""
with tempfile.TemporaryDirectory() as tempdir:
base = libdnf5.base.Base()
conf = base.get_config()
conf.config_file_path = "/dev/null"
conf.persistdir = f"{tempdir}{conf.persistdir}"
conf.cachedir = f"{tempdir}{conf.cachedir}"
conf.reposdir = ["/dev/null"]
conf.pluginconfpath = "/dev/null"
conf.varsdir = ["/dev/null"]
sack = base.get_repo_sack()
for idx, repo_path in enumerate(repo_paths):
repo = sack.create_repo(f"repo{idx}")
conf = repo.get_config()
conf.baseurl = f"file://{repo_path}"
base.setup()
sack.load_repos(libdnf5.repo.Repo.Type_AVAILABLE)
goal = libdnf5.base.Goal(base)
for pkg in pkg_include:
goal.add_install(pkg)
transaction = goal.resolve()
transaction_problems = transaction.get_problems()
if transaction_problems != NO_PROBLEM:
raise RuntimeError(f"transaction problems: {transaction.get_resolve_logs_as_strings()}")
pkgs = []
for tsi in transaction.get_transaction_packages():
pkgs.append(tsi.get_package())
# NB: return the base object as well, to workaround a bug in libdnf5:
# https://github.com/rpm-software-management/dnf5/issues/1748
return base, pkgs

View file

@ -0,0 +1,11 @@
import os.path
import pytest
testutil_dnf5 = pytest.importorskip("osbuild.testutil.dnf5")
def test_depsolve_pkgset():
_, pkgset = testutil_dnf5.depsolve_pkgset([os.path.abspath("./test/data/testrepos/baseos")], ["bash"])
assert len(pkgset) == 15
assert "bash" in [pkg.get_name() for pkg in pkgset]