hub: [getRPMFile] add strict behavior

This commit is contained in:
Yuming Zhu 2018-08-29 19:04:58 +08:00
parent d3ef006842
commit 473bc1460b
4 changed files with 120 additions and 4 deletions

View file

@ -74,6 +74,7 @@ from koji.util import multi_fnmatch
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 @@ class RootExports(object):
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 @@ class RootExports(object):
- 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 @@ class RootExports(object):
'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):

View file

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

View file

@ -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