From a0b4445f3b8380e07d60ab393a955a470d46cf68 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 6 Jan 2025 16:31:39 +0100 Subject: [PATCH] sources: add unit tests for `org.osbuild.librepo` This commit adds unit a basic test for librepo and also updates the code to the latest requirements to provide "fetch_all". It also fixes a bug in the handling of org.osbuild.rhsm secrets. --- sources/org.osbuild.librepo | 4 +- sources/test/test_librepo.py | 96 ++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 sources/test/test_librepo.py diff --git a/sources/org.osbuild.librepo b/sources/org.osbuild.librepo index 4499339e..f1382909 100755 --- a/sources/org.osbuild.librepo +++ b/sources/org.osbuild.librepo @@ -189,7 +189,7 @@ class LibRepoSource(sources.SourceService): cbdata=path, endcb=self._endcb) - def download(self, items: Dict) -> None: + def fetch_all(self, items: Dict) -> None: """Use librepo to download the packages""" # Organize the packages by the mirror id packages = dict() @@ -230,7 +230,7 @@ class LibRepoSource(sources.SourceService): handle.fastestmirror = True # If this mirror has secrets, set them up on the librepo handle - if "secrets" in m: + if "secrets" in mirror: self._setup_rhsm(handle, mirror) download = [] diff --git a/sources/test/test_librepo.py b/sources/test/test_librepo.py new file mode 100644 index 00000000..a36cae3b --- /dev/null +++ b/sources/test/test_librepo.py @@ -0,0 +1,96 @@ +#!/usr/bin/python3 +from unittest.mock import patch + +import librepo + +SOURCES_NAME = "org.osbuild.librepo" + + +@patch("librepo.download_packages") +def test_librepo_download_mocked(mocked_download_pkgs, sources_service): + TEST_SOURCES = { + "sha256:1111111111111111111111111111111111111111111111111111111111111111": { + "path": "Packages/a/a", + "mirror": "mirror_id", + }, + "sha256:2111111111111111111111111111111111111111111111111111111111111111": { + "path": "Packages/b/b", + "mirror": "mirror_id2", + }, + } + sources_service.options = { + "mirrors": { + "mirror_id": { + "url": "http://example.com/mirrorlist", + "type": "mirrorlist", + }, + "mirror_id2": { + "url": "http://example.com/mirrorlist2", + "type": "mirrorlist", + } + } + } + sources_service.cache = "cachedir" + sources_service.fetch_all(TEST_SOURCES) + # we expect one download call per mirror + assert len(mocked_download_pkgs.call_args_list) == 2 + # extract the list of packages to download (all_calls->call()->args->args[0]) + download_pkgs = mocked_download_pkgs.call_args_list[0][0][0] + assert download_pkgs[0].checksum == "1111111111111111111111111111111111111111111111111111111111111111" + assert download_pkgs[0].checksum_type == librepo.SHA256 + assert download_pkgs[0].relative_url == "Packages/a/a" + assert download_pkgs[0].dest == "cachedir/sha256:1111111111111111111111111111111111111111111111111111111111111111" + assert download_pkgs[0].handle.mirrorlisturl == "http://example.com/mirrorlist" + # and now the second call + download_pkgs = mocked_download_pkgs.call_args_list[1][0][0] + assert download_pkgs[0].checksum == "2111111111111111111111111111111111111111111111111111111111111111" + assert download_pkgs[0].checksum_type == librepo.SHA256 + assert download_pkgs[0].relative_url == "Packages/b/b" + assert download_pkgs[0].dest == "cachedir/sha256:2111111111111111111111111111111111111111111111111111111111111111" + assert download_pkgs[0].handle.mirrorlisturl == "http://example.com/mirrorlist2" + + +class FakeSubscriptionManager: + def __init__(self): + self.get_secrets_calls = [] + + def get_secrets(self, url): + self.get_secrets_calls.append(url) + return { + "ssl_ca_cert": "rhsm-ca-cert", + "ssl_client_cert": "rhsm-client-cert", + "ssl_client_key": "rhsm-client-key", + } + + +@patch("librepo.download_packages") +def test_librepo_secrets(mocked_download_pkgs, sources_service): + TEST_SOURCES = { + "sha256:1111111111111111111111111111111111111111111111111111111111111111": { + "path": "Packages/a/a", + "mirror": "mirror_id", + } + } + sources_service.options = { + "mirrors": { + "mirror_id": { + "url": "http://example.com/mirrorlist", + "type": "mirrorlist", + "secrets": { + "name": "org.osbuild.rhsm", + } + } + } + } + sources_service.cache = "cachedir" + sources_service.subscriptions = FakeSubscriptionManager() + sources_service.fetch_all(TEST_SOURCES) + assert len(mocked_download_pkgs.call_args_list) == 1 + # extract the list of packages to download (all_calls->call()->args->args[0]) + download_pkgs = mocked_download_pkgs.call_args_list[0][0][0] + assert download_pkgs[0].checksum == "1111111111111111111111111111111111111111111111111111111111111111" + assert download_pkgs[0].handle.sslclientkey == "rhsm-client-key" + assert download_pkgs[0].handle.sslclientcert == "rhsm-client-cert" + assert download_pkgs[0].handle.sslcacert == "rhsm-ca-cert" + # double check that get_secrets() was called + assert sources_service.subscriptions.get_secrets_calls == ["http://example.com/mirrorlist"]