hub: add strict behaviors for get_archive_file and list_archive_files

This commit is contained in:
Yuming Zhu 2017-12-01 15:09:08 +00:00 committed by Mike McLean
parent 436465d66b
commit eedea9f0d5
2 changed files with 47 additions and 18 deletions

View file

@ -3958,7 +3958,6 @@ def list_archives(buildID=None, buildrootID=None, componentBuildrootID=None, hos
buildroot with that ID.
If hostID is not null it will restrict the list to archives built on the host with that ID.
If filename, size, and/or checksum are not null it will filter the results to entries matching the provided values.
Returns a list of maps containing the following keys:
id: unique id of the archive file (integer)
@ -4283,7 +4282,7 @@ def _get_tarball_list(archive_id, tarpath):
archive.close()
return result
def list_archive_files(archive_id, queryOpts=None):
def list_archive_files(archive_id, queryOpts=None, strict=False):
"""
Get information about the files contained in the archive with the given ID.
Returns a list of maps with with following keys:
@ -4291,6 +4290,8 @@ def list_archive_files(archive_id, queryOpts=None):
archive_id: id of the archive the file is contained in (integer)
name: name of the file (string)
size: uncompressed size of the file (integer)
if strict is True, it will raise GenericError when there is no files found for specified archive_id.
"""
archive_info = get_archive(archive_id, strict=True)
@ -4315,18 +4316,23 @@ def list_archive_files(archive_id, queryOpts=None):
archive_info.update(image_archive)
file_path = os.path.join(koji.pathinfo.imagebuild(build_info),
archive_info['filename'])
else:
return _applyQueryOpts([], queryOpts)
result = []
if archive_type['name'] in ('zip', 'jar'):
return _applyQueryOpts(_get_zipfile_list(archive_id, file_path), queryOpts)
result = _applyQueryOpts(_get_zipfile_list(archive_id, file_path), queryOpts)
elif archive_type['name'] == 'tar':
return _applyQueryOpts(_get_tarball_list(archive_id, file_path), queryOpts)
result = _applyQueryOpts(_get_tarball_list(archive_id, file_path), queryOpts)
else:
# XXX support other archive types
return _applyQueryOpts([], queryOpts)
result = _applyQueryOpts(result, queryOpts)
def get_archive_file(archive_id, filename):
if strict and not result:
raise koji.GenericError('Archive#%s doesn\'t contain any files' % archive_id)
return result
def get_archive_file(archive_id, filename, strict=False):
"""
Get information about a file with the given filename
contained in the archive with the given ID.
@ -4335,15 +4341,20 @@ def get_archive_file(archive_id, filename):
archive_id: id of the archive the file is contained in (integer)
name: name of the file (string)
size: uncompressed size of the file (integer)
if strict is True, it will raise GenericError when there is no files found for specified archive_id,
else returns None.
"""
files = list_archive_files(archive_id)
if not files:
raise koji.GenericError('Archive#%s doesn\'t contain any files' % archive_id)
files = list_archive_files(archive_id, strict=strict)
for file_info in files:
if file_info['name'] == filename:
return file_info
else:
raise koji.GenericError('No such file: %s in archive#%s' % (filename, archive_id))
if strict:
raise koji.GenericError('No such file: %s in archive#%s' % (filename, archive_id))
else:
return None
def list_task_output(taskID, stat=False, all_volumes=False):
"""List the files generated by the task with the given ID. This

View file

@ -21,23 +21,41 @@ class TestGetArchiveFile(unittest.TestCase):
list_archive_files.return_value = FILES
rv = kojihub.get_archive_file(1, 'archive1.zip')
list_archive_files.assert_called_with(1)
list_archive_files.assert_called_with(1, strict=False)
self.assertEqual(rv, FILES[0])
list_archive_files.reset_mock()
rv = kojihub.get_archive_file(1, 'archive1.zip', strict=True)
list_archive_files.assert_called_with(1, strict=True)
self.assertEqual(rv, FILES[0])
@mock.patch('kojihub.list_archive_files')
def test_empty_files(self, list_archive_files):
list_archive_files.return_value = EMPTY_FILES
rv = kojihub.get_archive_file(1, 'archive1.zip')
list_archive_files.assert_called_with(1, strict=False)
self.assertIsNone(rv)
list_archive_files.reset_mock()
list_archive_files.side_effect = koji.GenericError('error message')
with self.assertRaises(koji.GenericError) as cm:
kojihub.get_archive_file(1, 'archive1.zip')
list_archive_files.assert_called_with(1)
self.assertEqual(cm.exception.args[0], 'Archive#1 doesn\'t contain any files')
kojihub.get_archive_file(1, 'archive1.zip', strict=True)
list_archive_files.assert_called_with(1, strict=True)
self.assertEqual(cm.exception.args[0], 'error message')
@mock.patch('kojihub.list_archive_files')
def test_non_existing_file(self, list_archive_files):
list_archive_files.return_value = FILES
rv = kojihub.get_archive_file(1, 'archive3.xml')
list_archive_files.assert_called_with(1, strict=False)
self.assertEqual(rv, None)
list_archive_files.reset_mock()
with self.assertRaises(koji.GenericError) as cm:
kojihub.get_archive_file(1, 'archive3.xml')
list_archive_files.assert_called_with(1)
kojihub.get_archive_file(1, 'archive3.xml', strict=True)
list_archive_files.assert_called_with(1, strict=True)
self.assertEqual(cm.exception.args[0], 'No such file: archive3.xml in archive#1')