#1061 hub: [getRPMDeps] add strict behavior
Merged 5 years ago by mikem. Opened 5 years ago by julian8628.
julian8628/koji issue/1052  into  master

file modified
+16 -6
@@ -10023,7 +10023,7 @@ 

  

      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,
@@ -10033,17 +10033,27 @@ 

          - 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 = []

@@ -10,6 +10,44 @@ 

  

  

  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')

fixes: #1052
It's not a direct fix but adding strict keyword here to avoid changing the behavior to make error occurs on web or other 3rd-party tools
When strict=True, if no rpminfo found in DB, or rpminfo['build_id'] is None( equals to external rpm currently), or no rpmfile exists on volumes, raise koji.GenericError
No dependencies found won't cause errors here. [] returns still.

Commit 67be6a1 fixes this pull-request

Pull-Request has been merged by mikem

5 years ago