From 67c7d63983e3d1b0639119eaf3141222a3b67fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Thu, 11 Jul 2024 16:55:25 +0200 Subject: [PATCH] testutil: add helper function for depsolving pkgset with DNF4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will be useful for testing SBOM implementations. Signed-off-by: Tomáš Hozza --- osbuild/testutil/dnf4.py | 36 ++++++++++++++++++++++++++++++++++ test/mod/test_testutil_dnf4.py | 11 +++++++++++ 2 files changed, 47 insertions(+) create mode 100644 osbuild/testutil/dnf4.py create mode 100644 test/mod/test_testutil_dnf4.py diff --git a/osbuild/testutil/dnf4.py b/osbuild/testutil/dnf4.py new file mode 100644 index 00000000..76edea22 --- /dev/null +++ b/osbuild/testutil/dnf4.py @@ -0,0 +1,36 @@ +import tempfile +from typing import List, Optional + +import dnf + + +def depsolve_pkgset( + repo_paths: List[str], + pkg_include: List[str], + pkg_exclude: Optional[List[str]] = None +) -> List[dnf.package.Package]: + """ + Perform a dependency resolution on a set of local RPM repositories. + """ + + with tempfile.TemporaryDirectory() as tempdir: + conf = dnf.conf.Conf() + 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"] + + base = dnf.Base(conf) + + for idx, repo_path in enumerate(repo_paths): + repo = dnf.repo.Repo(f"repo{idx}", conf) + repo.baseurl = f"file://{repo_path}" + base.repos.add(repo) + + base.fill_sack(load_system_repo=False) + + base.install_specs(pkg_include, pkg_exclude) + base.resolve() + return base.transaction.install_set diff --git a/test/mod/test_testutil_dnf4.py b/test/mod/test_testutil_dnf4.py new file mode 100644 index 00000000..e49eecf4 --- /dev/null +++ b/test/mod/test_testutil_dnf4.py @@ -0,0 +1,11 @@ +import os.path + +import pytest + +testutil_dnf4 = pytest.importorskip("osbuild.testutil.dnf4") + + +def test_depsolve_pkgset(): + pkgset = testutil_dnf4.depsolve_pkgset([os.path.abspath("./test/data/testrepos/baseos")], ["bash"]) + assert len(pkgset) == 15 + assert "bash" in [pkg.name for pkg in pkgset]