From 7817ba7ca3f68c85eb1028ae1698134b8c6441c9 Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Nov 17 2018 02:14:32 +0000 Subject: hub: [getRPMDeps] add strict behavior --- diff --git a/hub/kojihub.py b/hub/kojihub.py index 789f6b3..a1a1fbc 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -10044,7 +10044,7 @@ class RootExports(object): getRPM = staticmethod(get_rpm) - def getRPMDeps(self, rpmID, depType=None, queryOpts=None): + def getRPMDeps(self, rpmID, depType=None, queryOpts=None, strict=False): """Return dependency information about the RPM with the given ID. If depType is specified, restrict results to dependencies of the given type. Otherwise, return all dependency information. A list of maps will be returned, @@ -10054,17 +10054,27 @@ class RootExports(object): - flags - type - If there is no RPM with the given ID, or the RPM has no dependency information, - an empty list will be returned. + If there is no *internal* RPM with the given ID, or no RPM file found, + an empty list will be returned, unless strict is True in which case a + GenericError is raised. + If the RPM has no dependency information, an empty list will be returned. """ if queryOpts is None: queryOpts = {} - rpm_info = get_rpm(rpmID) - if not rpm_info or not rpm_info['build_id']: + rpm_info = get_rpm(rpmID, strict=strict) + if not rpm_info: + return _applyQueryOpts([], queryOpts) + if rpm_info and not rpm_info['build_id']: + if strict: + raise koji.GenericError("Can not get dependencies," + " because RPM: %s is not internal" % rpmID) return _applyQueryOpts([], queryOpts) build_info = get_build(rpm_info['build_id']) - rpm_path = os.path.join(koji.pathinfo.build(build_info), koji.pathinfo.rpm(rpm_info)) + rpm_path = os.path.join(koji.pathinfo.build(build_info), + koji.pathinfo.rpm(rpm_info)) if not os.path.exists(rpm_path): + if strict: + raise koji.GenericError("RPM file of %s doesn't exist" % rpmID) return _applyQueryOpts([], queryOpts) results = [] diff --git a/tests/test_hub/test_getRPMDeps.py b/tests/test_hub/test_getRPMDeps.py index 093de14..d7b9c28 100644 --- a/tests/test_hub/test_getRPMDeps.py +++ b/tests/test_hub/test_getRPMDeps.py @@ -10,6 +10,44 @@ import kojihub class TestGetRPMDeps(unittest.TestCase): + + @mock.patch('kojihub.get_rpm') + def test_getRPMDeps_no_rpminfo(self, get_rpm): + def mock_get_rpm(rpmID, strict=False): + if strict: + raise koji.GenericError('msg') + else: + return None + get_rpm.side_effect = mock_get_rpm + re = kojihub.RootExports().getRPMDeps(1) + self.assertEquals(re, []) + with self.assertRaises(koji.GenericError) as cm: + kojihub.RootExports().getRPMDeps(1, strict=True) + self.assertEquals(cm.exception.args[0], 'msg') + + @mock.patch('kojihub.get_rpm', return_value={'id': 1, 'build_id': None}) + def test_getRPMDeps_external_rpm(self, get_rpm): + re = kojihub.RootExports().getRPMDeps(1) + self.assertEquals(re, []) + with self.assertRaises(koji.GenericError) as cm: + kojihub.RootExports().getRPMDeps(1, strict=True) + self.assertEquals(cm.exception.args[0], + 'Can not get dependencies,' + ' because RPM: 1 is not internal') + + @mock.patch('kojihub.get_rpm', return_value={'id': 1, 'build_id': 1}) + @mock.patch('kojihub.get_build', return_value={'id': 1}) + @mock.patch('koji.pathinfo.build', return_value='fakebuildpath') + @mock.patch('koji.pathinfo.rpm', return_value='fakerpmrelpath') + @mock.patch('os.path.exists', return_value=False) + def test_getRPMDeps_no_rpmfile(self, ope, pr, pb, get_build, get_rpm): + re = kojihub.RootExports().getRPMDeps(1) + self.assertEquals(re, []) + with self.assertRaises(koji.GenericError) as cm: + kojihub.RootExports().getRPMDeps(1, strict=True) + self.assertEquals(cm.exception.args[0], + "RPM file of 1 doesn't exist") + @mock.patch('kojihub.get_rpm') @mock.patch('kojihub.get_build') @mock.patch('koji.pathinfo')