From 0c1f57d337634f193525565e9ea14ed353c73251 Mon Sep 17 00:00:00 2001 From: Valerij Maljulin Date: Dec 03 2019 12:59:02 +0000 Subject: product_versions in remote rule can now be optional This fixes #468 JIRA: FACTORY-4899 Signed-off-by: Valerij Maljulin --- diff --git a/docs/package-specific-policies.rst b/docs/package-specific-policies.rst index 6909de2..73e083e 100644 --- a/docs/package-specific-policies.rst +++ b/docs/package-specific-policies.rst @@ -38,9 +38,10 @@ The structure of the file is the same as the policies in Greenwave's configuration, with the only difference that the "id" key is optional. ``product_versions``, ``decision_context`` and ``subject_type`` in the -gating.yaml file should match with the defined values in the global +:file:`gating.yaml` file should match with the defined values in the global policy defined in the Greenwave conf that contains the ``RemoteRule`` -that will enable this check. +that will enable this check. If ``product_versions`` are not specified +in the remote :file:`gating.yaml`, values from the global policy will be used. The ``subject_type`` should always be defined and the permitted values are ``koji_build``, ``redhat-module`` and ``redhat-container-image``. diff --git a/greenwave/policies.py b/greenwave/policies.py index 7ed88b0..c4633c2 100644 --- a/greenwave/policies.py +++ b/greenwave/policies.py @@ -458,7 +458,7 @@ class RemoteRule(Rule): if isinstance(policy, OnDemandPolicy): return [ sub_policy for sub_policy in policies - if set(sub_policy.product_versions) == set(policy.product_versions) + if any(sub_policy.matches_product_version(pv) for pv in policy.product_versions) ] return [ @@ -758,7 +758,7 @@ class RemotePolicy(Policy): safe_yaml_attributes = { 'id': SafeYAMLString(optional=True), - 'product_versions': SafeYAMLList(str), + 'product_versions': SafeYAMLList(str, default=['*'], optional=True), 'subject_type': SafeYAMLString(optional=True, default='koji_build'), 'decision_context': SafeYAMLString(), 'rules': SafeYAMLList(Rule), diff --git a/greenwave/safe_yaml.py b/greenwave/safe_yaml.py index a1e7a55..429e4a9 100644 --- a/greenwave/safe_yaml.py +++ b/greenwave/safe_yaml.py @@ -100,7 +100,10 @@ class SafeYAMLList(SafeYAMLAttribute): """ YAML object attribute represeting a list of values. """ - def __init__(self, item_type, **kwargs): + def __init__(self, item_type, default=None, **kwargs): + if default is None: + default = [] + self.default = default super().__init__(**kwargs) self.item_type = item_type @@ -114,7 +117,7 @@ class SafeYAMLList(SafeYAMLAttribute): @property def default_value(self): - return [] + return self.default def to_json(self, value): return [self._item_to_json(item) for item in value] diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py index 396ac35..c8d3d09 100644 --- a/greenwave/tests/test_policies.py +++ b/greenwave/tests/test_policies.py @@ -493,12 +493,10 @@ def test_remote_rule_policy_optional_id(tmpdir): policy = policies[0] results = DummyResultsRetriever() - expected_details = "Policy 'untitled': Attribute 'product_versions' is required" decision = policy.check('fedora-26', subject, results) assert len(decision) == 1 - assert isinstance(decision[0], InvalidGatingYaml) + assert isinstance(decision[0], TestResultMissing) assert decision[0].is_satisfied is False - assert decision[0].details == expected_details def test_remote_rule_malformed_yaml(tmpdir): @@ -997,8 +995,6 @@ def test_remote_rule_policy_on_demand_policy(namespace): remote_fragment = dedent(""" --- !Policy id: "some-policy-from-a-random-packager" - product_versions: - - fedora-26 decision_context: bodhi_update_push_stable_with_remoterule rules: - !PassingTestCaseRule {test_case_name: dist.upgradepath}