From 8129a37028ae2cbdf1510be0293d31a56d3fd138 Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: May 18 2021 19:59:28 +0000 Subject: Allow policies to specify product versions to match as regexes This is to allow a policy that applies to fedora-34, fedora-35 and fedora-101, but not to fedora-rawhide. Currently for the openQA update testing policy we have to bodge this by listing fedora-3*, fedora-4*, fedora-5*...but that causes greenwave to publish a lot of unnecessary messages, and fixing that would be messy. Signed-off-by: Adam Williamson --- diff --git a/greenwave/policies.py b/greenwave/policies.py index dba3ea2..e4091aa 100644 --- a/greenwave/policies.py +++ b/greenwave/policies.py @@ -808,7 +808,15 @@ class Policy(SafeYAMLObject): return answers def matches_product_version(self, product_version): - return any(fnmatch(product_version, version) for version in self.product_versions) + for version in self.product_versions: + if version.startswith("/") and version.endswith("/"): + version = version.strip("/") + if re.search(version, product_version): + return True + else: + if fnmatch(product_version, version): + return True + return False @property def safe_yaml_label(self): diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py index 52d1287..e1a5383 100644 --- a/greenwave/tests/test_policies.py +++ b/greenwave/tests/test_policies.py @@ -296,6 +296,33 @@ def test_product_versions_pattern(product_version, applies, tmpdir): subject_type='bodhi_update') +@pytest.mark.parametrize(('product_version', 'applies'), [ + ('fedora-35', True), + ('fedora-101', True), + ('epel-7', False), + ('fedora-rawhide', False), +]) +def test_product_versions_pattern(product_version, applies, tmpdir): + p = tmpdir.join('fedora.yaml') + p.write(dedent(""" + --- !Policy + id: dummy_policy + product_versions: + - /^fedora-\d+$/ + decision_context: dummy_context + subject_type: bodhi_update + rules: + - !PassingTestCaseRule {test_case_name: test} + """)) + policies = load_policies(tmpdir.strpath) + policy = policies[0] + + assert applies == policy.matches( + decision_context='dummy_context', + product_version=product_version, + subject_type='bodhi_update') + + @pytest.mark.parametrize('namespace', ["rpms", ""]) def test_remote_rule_policy(tmpdir, namespace): """ Testing the RemoteRule with the koji interaction.