From 0951f6cc27e92f79d512cf7c8f303e974c02eeba Mon Sep 17 00:00:00 2001 From: Valerij Maljulin Date: Jan 21 2021 15:29:19 +0000 Subject: New rule type EmptyRule and new result called NoTestsRequired This fixes #603 JIRA: RHELWF-2189 --- diff --git a/docs/policies.rst b/docs/policies.rst index 4ba36bb..391a01e 100644 --- a/docs/policies.rst +++ b/docs/policies.rst @@ -184,6 +184,14 @@ PassingTestCaseRule .. _remote-rule: +EmptyRule +--------- + + This rule type is similar to previous with the only exception, that it + is always evaluated positively and result is ``no-tests-required`` when + other options were matched. + + RemoteRule ---------- diff --git a/greenwave/policies.py b/greenwave/policies.py index 34a49e2..958ead2 100644 --- a/greenwave/policies.py +++ b/greenwave/policies.py @@ -285,6 +285,21 @@ class TestResultPassed(RuleSatisfied): } +class NoTestsRequired(RuleSatisfied): + """ + Returned for EmptyRule + """ + def __init__(self, subject): + self.subject = subject + + def to_json(self): + return { + 'type': 'no-tests-required', + 'subject_type': self.subject.type, + 'subject_identifier': self.subject.identifier, + } + + class BlacklistedInPolicy(RuleSatisfied): """ Package was blacklisted in policy. @@ -652,6 +667,33 @@ class PassingTestCaseRule(Rule): return TestResultFailed(subject, self.test_case_name, self.scenario, result['id']) +class EmptyRule(Rule): + """ + This rule is similar as PassingTestCaseRule, with the exception, that + no test results are being expected and evaluated. Result is passed, + every time decision_context and subject are matched. + """ + yaml_tag = '!EmptyRule' + + safe_yaml_attributes = {} + + def check( + self, + policy, + product_version, + subject, + results_retriever): + return [NoTestsRequired(subject)] + + def matches(self, policy, **attributes): + return True + + def to_json(self): + return { + 'rule': self.__class__.__name__ + } + + class ObsoleteRule(Rule): """ The base class for an obsolete rule. diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py index 363b680..6e9a3d5 100644 --- a/greenwave/tests/test_policies.py +++ b/greenwave/tests/test_policies.py @@ -20,7 +20,8 @@ from greenwave.policies import ( TestResultPassed, InvalidRemoteRuleYaml, MissingRemoteRuleYaml, - OnDemandPolicy + OnDemandPolicy, + NoTestsRequired ) from greenwave.resources import ResultsRetriever from greenwave.safe_yaml import SafeYAMLError @@ -778,6 +779,45 @@ def test_remote_rule_policy_optional_id(tmpdir): assert decision[0].is_satisfied is False +def test_remote_rule_policy_empty_rule(tmpdir): + subject = create_subject('koji_build', 'nethack-1.2.3-1.el9000') + + serverside_fragment = dedent(""" + --- !Policy + id: "taskotron_release_critical_tasks_with_remoterule" + product_versions: + - fedora-26 + decision_context: bodhi_update_push_stable_with_remoterule + subject_type: koji_build + rules: + - !RemoteRule {} + """) + + remote_fragment = dedent(""" + --- !Policy + decision_context: bodhi_update_push_stable_with_remoterule + rules: + - !EmptyRule {} + """) + + p = tmpdir.join('gating.yaml') + p.write(serverside_fragment) + with mock.patch('greenwave.resources.retrieve_scm_from_koji') as scm: + scm.return_value = ('rpms', 'nethack', 'c3c47a08a66451cb9686c49f040776ed35a0d1bb') + with mock.patch('greenwave.resources.retrieve_yaml_remote_rule') as f: + f.return_value = remote_fragment + policies = load_policies(tmpdir.strpath) + policy = policies[0] + + results = DummyResultsRetriever() + print(results) + decision = policy.check('fedora-26', subject, results) + print(decision) + assert len(decision) == 1 + assert isinstance(decision[0], NoTestsRequired) + assert decision[0].is_satisfied is True + + def test_remote_rule_malformed_yaml(tmpdir): """ Testing the RemoteRule with a malformed gating.yaml file """