From 1d9e990a48934423809be4d0ae0f9c93bfb20ce5 Mon Sep 17 00:00:00 2001 From: Mikolaj Izdebski Date: Oct 23 2018 16:03:27 +0000 Subject: Allow hub policy to match build NVRs Fixes #1134 --- diff --git a/docs/source/defining_hub_policies.rst b/docs/source/defining_hub_policies.rst index dd2ddb9..b06b604 100644 --- a/docs/source/defining_hub_policies.rst +++ b/docs/source/defining_hub_policies.rst @@ -174,6 +174,9 @@ Available tests ``package`` * Matches its arguments against the package name. Accepts glob patterns. +``nvr`` + * Matches its arguments against the build NVR. Accepts glob patterns. + ``tag`` * matches its arguments against the tag name. Accepts glob patterns. * for move operations, the tag name tested is the destination tag (see diff --git a/hub/kojihub.py b/hub/kojihub.py index 54aa1b8..fa85c6e 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -8128,6 +8128,16 @@ def policy_get_pkg(data): #else raise koji.GenericError("policy requires package data") +def policy_get_build(data): + """Determine build from policy data + + returns dict as get_build + """ + if 'build' in data: + return get_build(data['build'], strict=True) + #else + raise koji.GenericError("policy requires build data") + def policy_get_brs(data): """Determine content generators from policy data""" @@ -8192,6 +8202,15 @@ class PackageTest(koji.policy.MatchTest): data[self.field] = policy_get_pkg(data)['name'] return super(PackageTest, self).run(data) +class NvrTest(koji.policy.MatchTest): + """Checks build NVR against glob patterns""" + name = 'nvr' + field = '_nvr' + def run(self, data): + #we need to find the build NVR from the base data + data[self.field] = policy_get_build(data)['nvr'] + return super(NvrTest, self).run(data) + class VolumeTest(koji.policy.MatchTest): """Checks storage volume against glob patterns""" name = 'volume' diff --git a/tests/test_hub/test_policy_tests.py b/tests/test_hub/test_policy_tests.py index ad9e5c7..1823ae6 100644 --- a/tests/test_hub/test_policy_tests.py +++ b/tests/test_hub/test_policy_tests.py @@ -24,6 +24,14 @@ class TestBasicTests(unittest.TestCase): policy_get_pkg.return_value = {'name': 'foobar'} self.assertTrue(obj.run({})) + @mock.patch('kojihub.policy_get_build') + def test_nvr_test(self, policy_get_build): + obj = kojihub.NvrTest('nvr *bar') + policy_get_build.return_value = {'nvr': 'mypackage-bar-xyzzy'} + self.assertFalse(obj.run({})) + policy_get_build.return_value = {'nvr': 'pkg-foo-bar'} + self.assertTrue(obj.run({})) + @mock.patch('kojihub.policy_get_pkg') def test_new_package_test(self, policy_get_pkg): obj = kojihub.NewPackageTest('is_new_package')