From be9a5427f98d7aa96ac687a610bb995d1e6d4e4a Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Apr 19 2018 11:42:41 +0000 Subject: Add support for wildcards to match multiple product versions Allows to specify product_versions like "fedora-*" in policies to match "fedora-27", "fedora-28" and any future release. --- diff --git a/conf/policies/fedora.yaml b/conf/policies/fedora.yaml index b69d05a..9f37374 100644 --- a/conf/policies/fedora.yaml +++ b/conf/policies/fedora.yaml @@ -25,7 +25,7 @@ rules: --- !Policy id: "taskotron_release_critical_tasks_for_testing" product_versions: - - fedora-26 + - fedora-* decision_context: bodhi_update_push_testing blacklist: [] rules: diff --git a/docs/policies.rst b/docs/policies.rst index 959b8ba..157b6f3 100644 --- a/docs/policies.rst +++ b/docs/policies.rst @@ -76,6 +76,8 @@ The document is a map (dictionary) with the following keys: `_ endpoint), although Greenwave does not enforce this. + You can match many product versions by using a wildcard like ``fedora-*``. + ``rules`` A list of rules which this policy enforces. Each item in the list is a YAML map, tagged with the rule type. diff --git a/functional-tests/consumers/test_resultsdb.py b/functional-tests/consumers/test_resultsdb.py index e0575f8..742d8cf 100644 --- a/functional-tests/consumers/test_resultsdb.py +++ b/functional-tests/consumers/test_resultsdb.py @@ -109,7 +109,7 @@ def test_consume_new_result( second_msg = { 'policies_satisfied': True, 'decision_context': 'bodhi_update_push_testing', - 'product_version': 'fedora-26', + 'product_version': 'fedora-*', 'unsatisfied_requirements': [], 'summary': 'all required tests passed', 'subject': [ @@ -519,7 +519,7 @@ def test_consume_legacy_result( second_msg = { 'policies_satisfied': True, 'decision_context': 'bodhi_update_push_testing', - 'product_version': 'fedora-26', + 'product_version': 'fedora-*', 'unsatisfied_requirements': [], 'summary': 'all required tests passed', 'subject': [ diff --git a/greenwave/policies.py b/greenwave/policies.py index cdac959..c810d18 100644 --- a/greenwave/policies.py +++ b/greenwave/policies.py @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0+ -import fnmatch +from fnmatch import fnmatch import yaml @@ -231,7 +231,7 @@ class PackageSpecificRule(Rule): nvr = item[self.nvr_key] pkg_name = nvr.rsplit('-', 2)[0] - if not any(fnmatch.fnmatch(pkg_name, repo) for repo in self.repos): + if not any(fnmatch(pkg_name, repo) for repo in self.repos): return RuleSatisfied() rule = PassingTestCaseRule() @@ -270,7 +270,7 @@ class Policy(yaml.YAMLObject): def applies_to(self, decision_context, product_version): return (decision_context == self.decision_context and - product_version in self.product_versions) + self._applies_to_product_version(product_version)) def check(self, item, results, waivers): # If an item is about a package and it is in the blacklist, return RuleSatisfied() @@ -294,3 +294,6 @@ class Policy(yaml.YAMLObject): 'rules': [rule.to_json() for rule in self.rules], 'blacklist': self.blacklist, } + + def _applies_to_product_version(self, product_version): + return any(fnmatch(product_version, version) for version in self.product_versions) diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py index 4dbec2d..614b6a3 100644 --- a/greenwave/tests/test_policies.py +++ b/greenwave/tests/test_policies.py @@ -359,3 +359,22 @@ def test_version_endpoint_jsonp(): assert output.status_code == 200 assert 'bac123' in output.data assert '"version": "%s"' % __version__ in output.data + + +def test_product_versions_pattern(tmpdir): + p = tmpdir.join('fedora.yaml') + p.write(""" +--- !Policy +id: dummy_policy +product_versions: + - fedora-* +decision_context: dummy_context +blacklist: [] +rules: [] + """) + policies = load_policies(tmpdir.strpath) + policy = policies[0] + + assert policy.applies_to('dummy_context', 'fedora-27') + assert policy.applies_to('dummy_context', 'fedora-28') + assert not policy.applies_to('dummy_context', 'epel-7')