#70 add sanity checks when loading the polices
Merged 6 years ago by ralph. Opened 6 years ago by mjia.
mjia/greenwave issue60  into  master

@@ -2,9 +2,11 @@ 

  # SPDX-License-Identifier: GPL-2.0+

  

  import json

+ import pytest

  

  from greenwave.app_factory import create_app

  from greenwave.policies import summarize_answers, RuleSatisfied, TestResultMissing, TestResultFailed

+ from greenwave.utils import load_policies

  

  

  def test_summarize_answers():
@@ -153,3 +155,35 @@ 

      assert output.status_code == 404

      assert output.data == '{\n  "message": "Cannot find any applicable '\

          'policies for fedora-26 and bodhi_update_push"\n}\n'

+ 

+ 

+ def test_misconfigured_policies(tmpdir):

+     p = tmpdir.join('fedora.yaml')

+     p.write("""

+ ---

+ id: "taskotron_release_critical_tasks"

+ product_versions:

+   - fedora-26

+ decision_context: bodhi_update_push_stable

+ rules:

+   - !PassingTestCaseRule {test_case_name: dist.abicheck}

+         """)

+     with pytest.raises(RuntimeError) as excinfo:

+         load_policies(tmpdir.strpath)

+     assert 'Policies are not configured properly' in str(excinfo.value)

+ 

+ 

+ def test_misconfigured_policy_rules(tmpdir):

+     p = tmpdir.join('fedora.yaml')

+     p.write("""

+ --- !Policy

+ id: "taskotron_release_critical_tasks"

+ product_versions:

+   - fedora-26

+ decision_context: bodhi_update_push_stable

+ rules:

+   - {test_case_name: dist.abicheck}

+         """)

+     with pytest.raises(RuntimeError) as excinfo:

+         load_policies(tmpdir.strpath)

+     assert 'Policies are not configured properly' in str(excinfo.value)

file modified
+9
@@ -6,6 +6,7 @@ 

  from flask import jsonify, current_app

  from flask.config import Config

  from werkzeug.exceptions import HTTPException

+ from greenwave.policies import Policy, Rule

  

  

  def json_error(error):
@@ -72,6 +73,14 @@ 

      policies = []

      for policy_pathname in policy_pathnames:

          policies.extend(yaml.safe_load_all(open(policy_pathname, 'r')))

+     for policy in policies:

+         if not isinstance(policy, Policy):

+             raise RuntimeError('Policies are not configured properly as policy %s '

+                                'is not an instance of Policy' % policy)

+         for rule in policy.rules:

+             if not isinstance(rule, Rule):

+                 raise RuntimeError('Policies are not configured properly as rule %s '

+                                    'is not an instance of Rule' % rule)

      return policies

  

  

no initial comment

rebased

6 years ago

:+1:

Could we cover this with unit tests, perhaps? By feeding in some bad YAML?

Good idea, I've added the unit tests.

rebased

6 years ago

If you use the pytest tmpdir fixture, it will clean up for you (and does the other neat stuff like keeping old ones with rotation).

https://docs.pytest.org/en/latest/tmpdir.html

Thanks, completely forgot that we have this built in py.test. I've updated the patch.

rebased

6 years ago

Pull-Request has been merged by ralph

6 years ago