PR#2796: api: getVolume with strict

Merges #2796
https://pagure.io/koji/pull-request/2796

Fixes: #1592
https://pagure.io/koji/issue/1592
API getVolume should has strict option
This commit is contained in:
Tomas Kopecek 2021-04-19 15:11:48 +02:00
commit b206d9e77e
2 changed files with 36 additions and 1 deletions

View file

@ -10708,7 +10708,10 @@ class RootExports(object):
volume, or None if no match.
If strict is true, raises an error if no match.
"""
return lookup_name('volume', volume, strict=strict)
result = lookup_name('volume', volume)
if not result and strict:
raise koji.GenericError("No such volume: %s" % volume)
return result
def applyVolumePolicy(self, build, strict=False):
"""Apply the volume policy to a given build

View file

@ -0,0 +1,32 @@
import unittest
import mock
import koji
import kojihub
class TestGetVolume(unittest.TestCase):
def setUp(self):
self.exports = kojihub.RootExports()
self.lookup_name = mock.patch('kojihub.lookup_name').start()
def test_non_exist_volume_with_strict(self):
volume = ['test-volume']
self.lookup_name.return_value = None
with self.assertRaises(koji.GenericError) as cm:
self.exports.getVolume(volume, strict=True)
self.assertEqual("No such volume: %s" % volume, str(cm.exception))
def test_non_exist_volume_with_strict(self):
volume = ['test-volume']
self.lookup_name.return_value = None
result = self.exports.getVolume(volume)
self.assertEqual(None, result)
def test_valid_volume(self):
volume = ['test-volume']
volume_dict = {'id': 0, 'name': 'DEFAULT'}
self.lookup_name.return_value = volume_dict
result = self.exports.getVolume(volume)
self.assertEqual(volume_dict, result)