From ff2c95f78c3ffaa3fb057d15a49f415c1f1fed7c Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Jun 07 2019 10:04:38 +0000 Subject: New 'buildtype' test for policies Fixes: https://pagure.io/koji/issue/1225 --- diff --git a/docs/source/defining_hub_policies.rst b/docs/source/defining_hub_policies.rst index 0d174db..8006f18 100644 --- a/docs/source/defining_hub_policies.rst +++ b/docs/source/defining_hub_policies.rst @@ -209,6 +209,9 @@ Available tests * for the tag policies, determines the build tag from the build data, which will by null for imported builds +``buildtype`` + * checks the build type(s) against the arguments + ``skip_tag`` * checks to see if the --skip-tag option was used * only applicable to the build_from_* policies diff --git a/hub/kojihub.py b/hub/kojihub.py index 6a03c8a..a654414 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -8349,6 +8349,15 @@ def policy_get_build_tags(data): return tags +def policy_get_build_types(data): + if 'btypes' in data: + # btypes can be already populated by caller + return set(data['btypes']) + if 'build' in data: + binfo = get_build(data['build'], strict=True) + return set(get_build_type(binfo).keys()) + return set() + class NewPackageTest(koji.policy.BaseSimpleTest): """Checks to see if a package exists yet""" name = 'is_new_package' @@ -8504,6 +8513,18 @@ class BuildTagTest(koji.policy.BaseSimpleTest): return False +class BuildTypeTest(koji.policy.BaseSimpleTest): + """Check the build type(s) of the build""" + + name = 'buildtype' + def run(self, data): + args = self.str.split()[1:] + for btype in policy_get_build_types(data): + if multi_fnmatch(btype, args): + return True + return False + + class ImportedTest(koji.policy.BaseSimpleTest): """Check if any part of a build was imported diff --git a/tests/test_hub/test_policy_tests.py b/tests/test_hub/test_policy_tests.py index ad9e5c7..90ab850 100644 --- a/tests/test_hub/test_policy_tests.py +++ b/tests/test_hub/test_policy_tests.py @@ -245,3 +245,38 @@ class TestHasTagTest(unittest.TestCase): # check no match case self.list_tags.return_value = [] self.assertFalse(obj.run(data)) + + +class TestBuildTypeTest(unittest.TestCase): + def setUp(self): + self.get_build_type = mock.patch('kojihub.get_build_type').start() + self.get_build = mock.patch('kojihub.get_build').start() + + def tearDown(self): + mock.patch.stopall() + + def test_invalid(self): + binfo = {'id': 1, 'name': 'nvr-1-2'} + self.get_build.return_value = binfo + self.get_build_type.return_value = {'rpm': None} + obj = kojihub.BuildTypeTest('buildtype foo-*') + data = {'build': 'nvr-1-2'} + self.assertFalse(obj.run(data)) + self.get_build_type.assert_called_once_with(binfo) + + def test_valid(self): + binfo = {'id': 1, 'name': 'nvr-1-2'} + self.get_build.return_value = binfo + self.get_build_type.return_value = {'rpm': None} + obj = kojihub.BuildTypeTest('buildtype rpm') + data = {'build': 'nvr-1-2'} + self.assertTrue(obj.run(data)) + self.get_build_type.assert_called_once_with(binfo) + + def test_prepopulated(self): + #self.get_build.return_value = {'id': 1, 'name': 'nvr-1-2'} + self.get_build_type.return_value = {'rpm': None} + obj = kojihub.BuildTypeTest('buildtype rpm') + data = {'build': 123, 'btypes': set(['rpm'])} + self.assertTrue(obj.run(data)) + self.get_build_type.assert_not_called()