From a642cdfc8a5b6ae763ffda697902c15113b6a617 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Jan 16 2020 13:40:08 +0000 Subject: Add 'target' policy Fixes: https://pagure.io/koji/issue/1040 --- diff --git a/docs/source/defining_hub_policies.rst b/docs/source/defining_hub_policies.rst index 1a035b4..b419552 100644 --- a/docs/source/defining_hub_policies.rst +++ b/docs/source/defining_hub_policies.rst @@ -204,6 +204,9 @@ Available tests * for untag operations, tests the tag the build is being removed from * only applicable to the tag policy +``target`` + * matches against the build's target name. Accepts glob patterns. + ``hastag`` * checks the current tags for the build in question against the arguments. diff --git a/koji/policy.py b/koji/policy.py index 853016e..fa1f988 100644 --- a/koji/policy.py +++ b/koji/policy.py @@ -137,6 +137,18 @@ class MatchTest(BaseSimpleTest): return False +class TargetTest(MatchTest): + """Matches target in the data against glob patterns + + True if any of the expressions match, else False + + Syntax: + target pattern1 [pattern2 ...] + """ + name = 'target' + field = 'target' + + class CompareTest(BaseSimpleTest): """Simple numeric field comparison diff --git a/tests/test_lib/test_policy.py b/tests/test_lib/test_policy.py index d2177df..26cb105 100644 --- a/tests/test_lib/test_policy.py +++ b/tests/test_lib/test_policy.py @@ -69,6 +69,14 @@ class TestBasicTests(unittest.TestCase): self.assertTrue(obj.run({'thing': 'elseplus'})) self.assertFalse(obj.run({})) + def test_target_test(self): + obj = koji.policy.TargetTest('target valid') + self.assertTrue(obj.run({'target': 'valid'})) + self.assertFalse(obj.run({'target': 'else'})) + obj = koji.policy.TargetTest('target valid else*') + self.assertTrue(obj.run({'target': 'valid'})) + self.assertTrue(obj.run({'target': 'elseplus'})) + def test_compare_test(self): obj = koji.policy.CompareTest('compare thing > 2') self.assertFalse(obj.run({'thing': 1})) @@ -120,6 +128,7 @@ class TestDiscovery(unittest.TestCase): 'has': koji.policy.HasTest, 'match': koji.policy.MatchTest, 'none': koji.policy.NoneTest, + 'target': koji.policy.TargetTest, 'true': koji.policy.TrueTest, } self.assertDictEqual(expected, actual)