From 15c115e71eb1698400b86af7cb1c2c671026ad09 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: May 24 2021 08:38:01 +0000 Subject: hub: policy test buildtag_inheritance Fixes: https://pagure.io/koji/issue/2870 --- diff --git a/hub/kojihub.py b/hub/kojihub.py index 69304d7..e7a6b5e 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -9410,7 +9410,8 @@ def policy_get_cgs(data): return cgs -def policy_get_build_tags(data): +def policy_get_build_tags(data, taginfo=False): + """If taginfo is set, return list of taginfos, else list of names only""" if 'build_tag' in data: return [get_tag(data['build_tag'], strict=True)['name']] elif 'build_tags' in data: @@ -9424,12 +9425,18 @@ def policy_get_build_tags(data): return [target['build_tag_name']] # otherwise look at buildroots - tags = set() + tags = {} for br_id in policy_get_brs(data): if br_id is None: - tags.add(None) + tags[None] = None else: - tags.add(get_buildroot(br_id, strict=True)['tag_name']) + tinfo = get_buildroot(br_id, strict=True) + tags[tinfo['tag_name']] = tinfo + + if taginfo: + tags = tags.values() + else: + tags = tags.keys() return tags @@ -9631,6 +9638,25 @@ class BuildTagTest(koji.policy.BaseSimpleTest): return False +class BuildTagInheritsFromTest(koji.policy.BaseSimpleTest): + """Check all parents of buildtag (without child tag)""" + name = 'buildtag_inherits_from' + + def run(self, data): + test_name, *args = self.str.split() + assert(test_name == self.name) + for tinfo in policy_get_build_tags(data, taginfo=True): + if tinfo is None: + # content generator buildroots might not have tag info + continue + + for tag in readFullInheritance(tinfo['tag_id']): + if multi_fnmatch(tag['name'], args): + return True + # otherwise... + return False + + class BuildTypeTest(koji.policy.BaseSimpleTest): """Check the build type(s) of the build""" diff --git a/tests/test_hub/test_policy_tests.py b/tests/test_hub/test_policy_tests.py index bb153d7..3574d5f 100644 --- a/tests/test_hub/test_policy_tests.py +++ b/tests/test_hub/test_policy_tests.py @@ -259,6 +259,64 @@ class TestHasTagTest(unittest.TestCase): self.assertFalse(obj.run(data)) +class TestBuildTagInheritsFromTest(unittest.TestCase): + + def setUp(self): + self.policy_get_build_tags = mock.patch('kojihub.policy_get_build_tags').start() + self.readFullInheritance = mock.patch('kojihub.readFullInheritance').start() + + def tearDown(self): + mock.patch.stopall() + + def test_wrong_test_name(self): + obj = kojihub.BuildTagInheritsFromTest('buildtag_inherits_from_type foo*') + data = {} + self.policy_get_build_tags.return_value = {None} + with self.assertRaises(AssertionError): + obj.run(data) + + def test_no_buildtag(self): + obj = kojihub.BuildTagInheritsFromTest('buildtag_inherits_from foo*') + data = {} + self.policy_get_build_tags.return_value = {None} + self.assertFalse(obj.run(data)) + self.policy_get_build_tags.assert_called_once_with(data, taginfo=True) + self.readFullInheritance.assert_not_called() + + def test_no_inheritance(self): + obj = kojihub.BuildTagInheritsFromTest('buildtag_inherits_from foo*') + data = {} + self.policy_get_build_tags.return_value = [{'tag_id': 123, 'tag_name': 'foox'}] + self.readFullInheritance.return_value = [] + self.assertFalse(obj.run(data)) + self.policy_get_build_tags.assert_called_once_with(data, taginfo=True) + self.readFullInheritance.assert_called_once_with(123) + + def test_inheritance_pass(self): + obj = kojihub.BuildTagInheritsFromTest('buildtag_inherits_from foo*') + data = {} + self.policy_get_build_tags.return_value = [{'tag_id': 123, 'tag_name': 'wrong'}] + self.readFullInheritance.return_value = [ + {'name': 'still-wrong'}, + {'name': 'foox'}, + ] + self.assertTrue(obj.run(data)) + self.policy_get_build_tags.assert_called_once_with(data, taginfo=True) + self.readFullInheritance.assert_called_once_with(123) + + def test_inheritance_fail(self): + obj = kojihub.BuildTagInheritsFromTest('buildtag_inherits_from foo*') + data = {} + self.policy_get_build_tags.return_value = [{'tag_id': 123, 'tag_name': 'wrong'}] + self.readFullInheritance.return_value = [ + {'name': 'still-wrong'}, + {'name': 'still-still-wrong'}, + ] + self.assertFalse(obj.run(data)) + self.policy_get_build_tags.assert_called_once_with(data, taginfo=True) + self.readFullInheritance.assert_called_once_with(123) + + class TestBuildTypeTest(unittest.TestCase): def setUp(self): self.get_build_type = mock.patch('kojihub.get_build_type').start()