#1068 hub: [getRPMFile] add strict behavior
Merged 4 years ago by tkopecek. Opened 5 years ago by julian8628.
julian8628/koji issue/1053  into  master

file modified
+20 -4
@@ -74,6 +74,7 @@ 

  from koji.util import safer_move

  from koji.util import to_list

  from six.moves import range

+ 

  logger = logging.getLogger('koji.hub')

  

  
@@ -10627,7 +10628,7 @@ 

  

          return _applyQueryOpts(results, queryOpts)

  

-     def getRPMFile(self, rpmID, filename):

+     def getRPMFile(self, rpmID, filename, strict=False):

          """

          Get info about the file in the given RPM with the given filename.

          A map will be returned with the following keys:
@@ -10643,14 +10644,26 @@ 

          - mtime

          - mode

  

-         If no such file exists, an empty map will be returned.

+         If there is no *internal* RPM with the given ID, or no RPM file found,

+         an empty map will be returned, unless strict is True in which case a

+         GenericError is raised.

+         If no such file exists, an empty map will be returned, unless strict is

+         True in which case a GenericError is raised.

          """

-         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 {}

+         if rpm_info and not rpm_info['build_id']:

+             if strict:

+                 raise koji.GenericError("Can not get RPM file,"

+                                         " because RPM: %s is not internal" % rpmID)

              return {}

          build_info = get_build(rpm_info['build_id'])

          rpm_path = joinpath(koji.pathinfo.build(build_info), koji.pathinfo.rpm(rpm_info))

          if not os.path.exists(rpm_path):

+             if strict:

+                 raise koji.GenericError(

+                     "RPM package file of %s doesn't exist" % rpmID)

              return {}

  

          hdr = koji.get_rpm_header(rpm_path)
@@ -10668,6 +10681,9 @@ 

                          'user': fields['fileusername'][i], 'group': fields['filegroupname'][i],

                          'mtime': fields['filemtimes'][i], 'mode': fields['filemodes'][i]}

              i += 1

+         if strict:

+             raise koji.GenericError(

+                 "No file: %s found in RPM: %s" % (filename, rpmID))

          return {}

  

      def getRPMHeaders(self, rpmID=None, taskID=None, filepath=None, headers=None):

@@ -0,0 +1,80 @@ 

+ from __future__ import absolute_import

+ 

+ import os

+ 

+ import mock

+ 

+ try:

+     import unittest2 as unittest

+ except ImportError:

+     import unittest

+ import koji

+ import kojihub

+ 

+ 

+ class TestGetRPMFile(unittest.TestCase):

+ 

+     @mock.patch('kojihub.get_rpm')

+     def test_getRPMFile_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().getRPMFile(1, 'filename')

+         self.assertEquals(re, {})

+         with self.assertRaises(koji.GenericError) as cm:

+             kojihub.RootExports().getRPMFile(1, 'filename', strict=True)

+         self.assertEquals(cm.exception.args[0], 'msg')

+ 

+     @mock.patch('kojihub.get_rpm', return_value={'id': 1, 'build_id': None})

+     def test_getRPMFile_external_rpm(self, get_rpm):

+         re = kojihub.RootExports().getRPMFile(1, 'filename')

+         self.assertEquals(re, {})

+         with self.assertRaises(koji.GenericError) as cm:

+             kojihub.RootExports().getRPMFile(1, 'filename', strict=True)

+         self.assertEquals(cm.exception.args[0],

+                           'Can not get RPM file,'

+                           ' 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_getRPMFile_no_rpmfile(self, ope, pr, pb, get_build, get_rpm):

+         re = kojihub.RootExports().getRPMFile(1, 'filename')

+         self.assertEquals(re, {})

+         with self.assertRaises(koji.GenericError) as cm:

+             kojihub.RootExports().getRPMFile(1, 'filename', strict=True)

+         self.assertEquals(cm.exception.args[0],

+                           "RPM package file of 1 doesn't exist")

+ 

+     @mock.patch('kojihub.get_rpm', return_value={'id': 1, 'build_id': 1})

+     @mock.patch('kojihub.get_build')

+     @mock.patch('koji.pathinfo')

+     def test_getRPMFile(self, pi, build, rpm):

+         pi.build.return_value = os.path.join(os.path.dirname(__file__),

+                                              '../test_lib/data/rpms')

+         pi.rpm.return_value = 'test-files-1-1.fc27.noarch.rpm'

+         getRPMFile = kojihub.RootExports().getRPMFile

+         res = getRPMFile(1, '/fileA')

+         self.assertDictEqual(res, {'digest_algo': 'sha256',

+                                    'user': 'root',

+                                    'mtime': int(1535536271),

+                                    'digest': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',

+                                    'size': 0,

+                                    'group': 'root',

+                                    'name': '/fileA',

+                                    'rpm_id': 1,

+                                    'flags': 0,

+                                    'mode': int(0o100755),

+                                    'md5': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'})

+         res = getRPMFile(1, '/fileB')

+         self.assertEquals(res, {})

+         with self.assertRaises(koji.GenericError) as cm:

+             res = getRPMFile(1, '/fileB', strict=True)

+         self.assertEquals(cm.exception.args[0],

+                           'No file: /fileB found in RPM: 1')

empty or binary file added
@@ -0,0 +1,20 @@ 

+ Name:		test-files

+ Version:	1

+ Release:	1%{?dist}

+ Summary:	Testing files header fields

+ 

+ License:        none

+ 

+ %description

+ Testing files header fields

+ 

+ %install

+ rm -rf $RPM_BUILD_ROOT

+ install -d $RPM_BUILD_ROOT/foo/bar

+ install -pm 0755 fileA $RPM_BUILD_ROOT

+ install -pm 0600 foo/bar/fileB $RPM_BUILD_ROOT/foo/bar

+ 

+ %files

+ %defattr(-,root,root)

+ /fileA

+ /foo/bar/fileB

change this comparison for python3, should I also change the result as well?

@julian would fixEncoding be needed with #1070?

@julian would fixEncoding be needed with #1070?

no, it look good and for python3, byte values in result turn to string.

1 new commit added

  • using decoded result in PR#1070
5 years ago

2 new commits added

  • using decoded result in PR#1070
  • hub: [getRPMFile] add strict behavior
5 years ago

debug?

oh, removed this line. Thanks

tests/test_hub/test_getRPMFile.py:6:1: F401 'six.b' imported but unused

2 new commits added

  • using decoded result in PR#1070
  • hub: [getRPMFile] add strict behavior
5 years ago

tests/test_hub/test_getRPMFile.py:6:1: F401 'six.b' imported but unused

updated

Metadata Update from @jcupova:
- Pull-request tagged with: testing-done

5 years ago

I don't think the import changes are relevant here. Can you drop them and rebase? The master branch has a number of import changes on it already.

While it makes sense to apply cleanup as we work, it is also important to keep PRs on topic.

rebased onto 9995da32c4b7510e1e4eccc7eed4bbaf00d4fd1d

5 years ago

sure, updated

imports were re-organized automatically by my ide :smoking:

pretty please pagure-ci rebuild

4 years ago

rebased onto 473bc14

4 years ago

Commit 51a6832 fixes this pull-request

Pull-Request has been merged by tkopecek

4 years ago