From a97ded8c247b1f55277a70babae4c5a084cd0a3d Mon Sep 17 00:00:00 2001 From: Andrew Schoen Date: Mon, 11 May 2015 10:26:22 -0500 Subject: [PATCH] Adds methods to extract info on a package from koji task results. Once you get the results of a koji task you can use these methods to look for and extract information about a given package. If the package is found it will return information about how to download and consume the rpm generated by the task. Signed-off-by: Andrew Schoen --- teuthology/config.py | 1 + teuthology/packaging.py | 82 +++++++++++++++++++++++++++++++ teuthology/test/test_packaging.py | 43 ++++++++++++++++ 3 files changed, 126 insertions(+) diff --git a/teuthology/config.py b/teuthology/config.py index 145ee97deb..6545389db2 100644 --- a/teuthology/config.py +++ b/teuthology/config.py @@ -141,6 +141,7 @@ class TeuthologyConfig(YamlConfig): 'watchdog_interval': 120, 'kojihub_url': 'http://koji.fedoraproject.org/kojihub', 'kojiroot_url': 'http://kojipkgs.fedoraproject.org/packages', + 'koji_task_url': 'https://kojipkgs.fedoraproject.org/work/', } def __init__(self, yaml_path=None): diff --git a/teuthology/packaging.py b/teuthology/packaging.py index 5aa29031ba..2b22822553 100644 --- a/teuthology/packaging.py +++ b/teuthology/packaging.py @@ -141,6 +141,88 @@ def get_koji_task_result(task_id, remote, ctx): return task_result +def get_koji_task_rpm_info(package, task_rpms): + """ + Extracts information about a given package from the provided + rpm results of a koji task. + + For example, if trying to retrieve the package 'kernel' from + the results of a task, the output would look like this: + + { + 'base_url': 'https://kojipkgs.fedoraproject.org/work/tasks/6745/9666745/', + 'rpm_name': 'kernel-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', + 'package_name': 'kernel', + 'version': '4.1.0-0.rc2.git2.1.fc23.x86_64', + } + + :param task_rpms: A list of rpms from a tasks reusults. + :param package: The name of the package to retrieve. + :returns: A python dict containing info about the package. + """ + result = dict() + result['package_name'] = package + found_pkg = _find_koji_task_result(package, task_rpms) + if not found_pkg: + raise RuntimeError("The package {pkg} was not found in: {rpms}".format( + pkg=package, + rpms=task_rpms, + )) + + path, rpm_name = found_pkg.rsplit("/", 1) + result['rpm_name'] = rpm_name + result['base_url'] = "{koji_task_url}/{path}/".format( + koji_task_url=config.koji_task_url, + path=path, + ) + # removes the package name from the beginning of rpm_name + version = rpm_name.split("{0}-".format(package), 1)[1] + # removes .rpm from the rpm_name + version = version.split(".rpm")[0] + result['version'] = version + return result + + +def _find_koji_task_result(package, rpm_list): + """ + Looks in the list of rpms from koji task results to see if + the package we are looking for is present. + + Returns the full list item, including the path, if found. + + If not found, returns None. + """ + for rpm in rpm_list: + if package == _get_koji_task_result_package_name(rpm): + return rpm + return None + + +def _get_koji_task_result_package_name(path): + """ + Strips the package name from a koji rpm result. + + This makes the assumption that rpm names are in the following + format: -...rpm + + For example, given a koji rpm result might look like: + + tasks/6745/9666745/kernel-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm + + This method would return "kernel". + """ + filename = path.split('/')[-1] + trimmed = [] + for part in filename.split('-'): + # assumes that when the next part is not a digit + # we're past the name and at the version + if part[0].isdigit(): + return '-'.join(trimmed) + trimmed.append(part) + + return '-'.join(trimmed) + + def get_koji_build_info(build_id, remote, ctx): """ Queries kojihub and retrieves information about diff --git a/teuthology/test/test_packaging.py b/teuthology/test/test_packaging.py index b20b33c395..938092b2ff 100644 --- a/teuthology/test/test_packaging.py +++ b/teuthology/test/test_packaging.py @@ -4,6 +4,27 @@ from mock import patch, Mock from teuthology import packaging +KOJI_TASK_RPMS_MATRIX = [ + ('tasks/6745/9666745/kernel-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel'), + ('tasks/6745/9666745/kernel-modules-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel-modules'), + ('tasks/6745/9666745/kernel-tools-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel-tools'), + ('tasks/6745/9666745/kernel-tools-libs-devel-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel-tools-libs-devel'), + ('tasks/6745/9666745/kernel-headers-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel-headers'), + ('tasks/6745/9666745/kernel-tools-debuginfo-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel-tools-debuginfo'), + ('tasks/6745/9666745/kernel-debuginfo-common-x86_64-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel-debuginfo-common-x86_64'), + ('tasks/6745/9666745/perf-debuginfo-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'perf-debuginfo'), + ('tasks/6745/9666745/kernel-modules-extra-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel-modules-extra'), + ('tasks/6745/9666745/kernel-tools-libs-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel-tools-libs'), + ('tasks/6745/9666745/kernel-core-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel-core'), + ('tasks/6745/9666745/kernel-debuginfo-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel-debuginfo'), + ('tasks/6745/9666745/python-perf-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'python-perf'), + ('tasks/6745/9666745/kernel-devel-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'kernel-devel'), + ('tasks/6745/9666745/python-perf-debuginfo-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'python-perf-debuginfo'), + ('tasks/6745/9666745/perf-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm', 'perf'), +] + +KOJI_TASK_RPMS = [rpm[0] for rpm in KOJI_TASK_RPMS_MATRIX] + class TestPackaging(object): @@ -178,6 +199,24 @@ class TestPackaging(object): with pytest.raises(RuntimeError): packaging.get_koji_task_result(1, m_remote, m_ctx) + @patch("teuthology.packaging.config") + def test_get_koji_task_rpm_info_success(self, m_config): + m_config.koji_task_url = "http://kojihub.com/work" + expected = dict( + base_url="http://kojihub.com/work/tasks/6745/9666745/", + version="4.1.0-0.rc2.git2.1.fc23.x86_64", + rpm_name="kernel-4.1.0-0.rc2.git2.1.fc23.x86_64.rpm", + package_name="kernel", + ) + result = packaging.get_koji_task_rpm_info('kernel', KOJI_TASK_RPMS) + assert expected == result + + @patch("teuthology.packaging.config") + def test_get_koji_task_rpm_info_fail(self, m_config): + m_config.koji_task_url = "http://kojihub.com/work" + with pytest.raises(RuntimeError): + packaging.get_koji_task_rpm_info('ceph', KOJI_TASK_RPMS) + def test_get_package_version_deb_found(self): remote = Mock() remote.os.package_type = "deb" @@ -236,3 +275,7 @@ class TestPackaging(object): remote.run.return_value = proc result = packaging.get_package_version(remote, "httpd") assert result is None + + @pytest.mark.parametrize("input, expected", KOJI_TASK_RPMS_MATRIX) + def test_get_koji_task_result_package_name(self, input, expected): + assert packaging._get_koji_task_result_package_name(input) == expected -- 2.39.5