From ed6998a9825b6125aa9251627f2f1c2022773b5f Mon Sep 17 00:00:00 2001 From: Valerij Maljulin Date: Nov 05 2019 11:11:30 +0000 Subject: Proceed other rules when there's no source URL in Koji build Signed-off-by: Valerij Maljulin --- diff --git a/greenwave/policies.py b/greenwave/policies.py index 8c58aec..3c6aabc 100644 --- a/greenwave/policies.py +++ b/greenwave/policies.py @@ -395,8 +395,14 @@ class RemoteRule(Rule): if policy.subject_type not in ['koji_build', 'redhat-module', 'redhat-container-image']: return [] - pkg_namespace, pkg_name, rev = greenwave.resources.retrieve_scm_from_koji( - subject_identifier) + try: + pkg_namespace, pkg_name, rev = greenwave.resources.retrieve_scm_from_koji( + subject_identifier + ) + except greenwave.resources.NoSourceException as e: + log.error(e) + return None + # if the element is actually a container and not a pkg there will be a "-container" # string at the end of the "pkg_name" and it will not match with the one in the # gating.yaml URL diff --git a/greenwave/resources.py b/greenwave/resources.py index b5bf10d..97d2315 100644 --- a/greenwave/resources.py +++ b/greenwave/resources.py @@ -123,6 +123,10 @@ class WaiversRetriever(BaseRetriever): **request_args) +class NoSourceException(RuntimeError): + pass + + @cached def retrieve_scm_from_koji(nvr): """ Retrieve cached rev and namespace from koji using the nvr """ @@ -141,7 +145,7 @@ def retrieve_scm_from_koji_build(nvr, build, koji_url): source = build.get('source') if not source: - raise BadGateway( + raise NoSourceException( 'Failed to retrieve SCM URL from Koji build "{}" at "{}" ' '(expected SCM URL in "source" attribute)' .format(nvr, koji_url)) diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py index c9cc97d..7fb5e39 100644 --- a/greenwave/tests/test_policies.py +++ b/greenwave/tests/test_policies.py @@ -1022,3 +1022,50 @@ def test_remote_rule_policy_on_demand_policy(namespace): decision = policy.check('fedora-26', nvr, results) assert len(decision) == 1 assert isinstance(decision[0], TestResultFailed) + + +@pytest.mark.parametrize('two_rules', (True, False)) +def test_on_demand_policy_match(two_rules): + """ Testing the RemoteRule with the koji interaction when on_demand policy is given. + In this case we are just mocking koji """ + + nvr = 'httpd-2.4.el9000' + + serverside_json = { + 'product_version': 'fedora-30', + 'id': 'taskotron_release_critical_tasks_with_remoterule', + 'subject_type': 'koji_build', + 'subject_identifier': nvr, + 'rules': [ + { + 'type': 'RemoteRule' + } + ], + } + + if two_rules: + serverside_json['rules'].append({ + "type": "PassingTestCaseRule", + "test_case_name": "fake.testcase.tier0.validation" + }) + + app = create_app('greenwave.config.TestingConfig') + with app.app_context(): + with mock.patch('xmlrpc.client.ServerProxy') as koji_server: + koji_server_instance = mock.MagicMock() + koji_server_instance.getBuild.return_value = {'source': None} + koji_server.return_value = koji_server_instance + policy = OnDemandPolicy.create_from_json(serverside_json) + + rv = policy.matches(subject_identifier=nvr) + + koji_server_instance.getBuild.assert_called_once() + assert rv is two_rules + + results = DummyResultsRetriever( + nvr, 'fake.testcase.tier0.validation', 'PASSED', 'koji_build' + ) + decision = policy.check('fedora-30', nvr, results) + if two_rules: + assert len(decision) == 1 + assert isinstance(decision[0], RuleSatisfied) diff --git a/greenwave/tests/test_retrieve_gating_yaml.py b/greenwave/tests/test_retrieve_gating_yaml.py index 665dd1b..e3ff07b 100644 --- a/greenwave/tests/test_retrieve_gating_yaml.py +++ b/greenwave/tests/test_retrieve_gating_yaml.py @@ -9,7 +9,9 @@ from werkzeug.exceptions import BadGateway, NotFound import greenwave.app_factory from greenwave.resources import ( - retrieve_scm_from_koji_build, retrieve_yaml_remote_rule, retrieve_scm_from_koji) + retrieve_scm_from_koji_build, retrieve_yaml_remote_rule, retrieve_scm_from_koji, + NoSourceException +) KOJI_URL = 'https://koji.fedoraproject.org/kojihub' @@ -52,7 +54,7 @@ def test_retrieve_scm_from_build_with_missing_source(): 'nvr': nvr } expected_error = 'expected SCM URL in "source" attribute' - with pytest.raises(BadGateway, match=expected_error): + with pytest.raises(NoSourceException, match=expected_error): retrieve_scm_from_koji_build(nvr, build, KOJI_URL)