From 7568236ccac412d362d214c1d814c7bf0920108d Mon Sep 17 00:00:00 2001 From: Giulia Naponiello Date: Feb 07 2020 12:20:25 +0000 Subject: Bug fix: failure to fetch gating.yaml New configuration variables were introduced, but `REMOTE_RULE_POLICIES` was not always present. Trying to get this config param, without a fallback will provoke this error: TypeError: 'NoneType' object does not support item assignment Changed the code to assume `{}` as default value. Ref: FACTORY-5877 Signed-off-by: Giulia Naponiello --- diff --git a/greenwave/config.py b/greenwave/config.py index 70242fb..207839c 100644 --- a/greenwave/config.py +++ b/greenwave/config.py @@ -28,16 +28,6 @@ class Config(object): # instead DIST_GIT_URL_TEMPLATE = \ 'https://src.fedoraproject.org/{pkg_namespace}{pkg_name}/raw/{rev}/f/gating.yaml' - REMOTE_RULE_POLICIES = { - 'brew-build-group': { - 'GIT_URL': 'git@gitlab.cee.redhat.com:devops/greenwave-policies/side-tags.git', - 'GIT_PATH_TEMPLATE': '{pkg_namespace}/{pkg_name}.yaml' - }, - '*': { - 'HTTP_URL_TEMPLATE': - 'https://src.fedoraproject.org/{pkg_namespace}{pkg_name}/raw/{rev}/f/gating.yaml' - } - } REMOTE_RULE_GIT_TIMEOUT = 30 REMOTE_RULE_GIT_MAX_RETRY = 3 KOJI_BASE_URL = 'https://koji.fedoraproject.org/kojihub' @@ -68,6 +58,16 @@ class DevelopmentConfig(Config): WAIVERDB_API_URL = 'http://localhost:5004/api/v1.0' GREENWAVE_API_URL = 'http://localhost:5005/api/v1.0' POLICIES_DIR = _local_conf_dir('policies') + REMOTE_RULE_POLICIES = { + 'brew-build-group': { + 'GIT_URL': 'git@gitlab.cee.redhat.com:devops/greenwave-policies/side-tags.git', + 'GIT_PATH_TEMPLATE': '{pkg_namespace}/{pkg_name}.yaml' + }, + '*': { + 'HTTP_URL_TEMPLATE': + 'https://src.fedoraproject.org/{pkg_namespace}{pkg_name}/raw/{rev}/f/gating.yaml' + } + } class TestingConfig(Config): @@ -76,3 +76,21 @@ class TestingConfig(Config): GREENWAVE_API_URL = 'http://localhost:5005/api/v1.0' KOJI_BASE_URL = 'http://localhost:5006/kojihub' POLICIES_DIR = _local_conf_dir('policies') + REMOTE_RULE_POLICIES = { + 'brew-build-group': { + 'GIT_URL': 'git@gitlab.cee.redhat.com:devops/greenwave-policies/side-tags.git', + 'GIT_PATH_TEMPLATE': '{pkg_namespace}/{pkg_name}.yaml' + }, + '*': { + 'HTTP_URL_TEMPLATE': + 'https://src.fedoraproject.org/{pkg_namespace}{pkg_name}/raw/{rev}/f/gating.yaml' + } + } + + +class FedoraTestingConfig(Config): + RESULTSDB_API_URL = 'http://localhost:5001/api/v2.0' + WAIVERDB_API_URL = 'http://localhost:5004/api/v1.0' + GREENWAVE_API_URL = 'http://localhost:5005/api/v1.0' + KOJI_BASE_URL = 'http://localhost:5006/kojihub' + POLICIES_DIR = _local_conf_dir('policies') diff --git a/greenwave/policies.py b/greenwave/policies.py index efac2ba..3285934 100644 --- a/greenwave/policies.py +++ b/greenwave/policies.py @@ -458,7 +458,7 @@ class RemoteRule(Rule): # remote rule file URL if pkg_namespace == 'containers': pkg_name = re.sub('-container$', '', pkg_name) - rr_policies_conf = current_app.config.get('REMOTE_RULE_POLICIES') + rr_policies_conf = current_app.config.get('REMOTE_RULE_POLICIES', {}) if not rr_policies_conf or '*' not in rr_policies_conf: rr_policies_conf['*'] = { 'HTTP_URL_TEMPLATE': current_app.config['DIST_GIT_URL_TEMPLATE'] diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py index 83032fb..69d996c 100644 --- a/greenwave/tests/test_policies.py +++ b/greenwave/tests/test_policies.py @@ -339,6 +339,52 @@ def test_remote_rule_policy(tmpdir, namespace): assert isinstance(decision[0], TestResultFailed) +def test_remote_rule_policy_with_no_remote_rule_policies_param_defined(tmpdir): + """ Testing the RemoteRule with the koji interaction. + But this time let's assume that REMOTE_RULE_POLICIES is not defined. """ + + subject = create_test_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 + 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} + """) + + p = tmpdir.join('gating.yaml') + p.write(serverside_fragment) + app = create_app('greenwave.config.FedoraTestingConfig') + + with app.app_context(): + 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] + + # Ensure that presence of a result is success. + results = DummyResultsRetriever(subject, 'dist.upgradepath') + decision = policy.check('fedora-26', subject, results) + assert len(decision) == 1 + assert isinstance(decision[0], RuleSatisfied) + + @pytest.mark.parametrize('namespace', ["modules", ""]) def test_remote_rule_policy_redhat_module(tmpdir, namespace): """ Testing the RemoteRule with the koji interaction.