From 638ad1bbf14b5c855a06f86cdfca3e4b52558a2d Mon Sep 17 00:00:00 2001
From: Dan Callaghan <dcallagh@redhat.com>
Date: Jun 20 2017 02:04:45 +0000
Subject: [PATCH 1/2] allow policies to apply to multiple product versions


---

diff --git a/greenwave/api_v1.py b/greenwave/api_v1.py
index 33a4175..9ea8d69 100644
--- a/greenwave/api_v1.py
+++ b/greenwave/api_v1.py
@@ -41,8 +41,7 @@ def make_decision():
     product_version = request.get_json()['product_version']
     decision_context = request.get_json()['decision_context']
     applicable_policies = [policy for policy in policies
-                           if policy.product_version == product_version and
-                           policy.decision_context == decision_context]
+                           if policy.applies_to(decision_context, product_version)]
     if not applicable_policies:
         raise NotFound('Cannot find any applicable policies for %s' % product_version)
     subjects = [item.strip() for item in request.get_json()['subject'] if item]
diff --git a/greenwave/policies.py b/greenwave/policies.py
index 5767524..8fdff19 100644
--- a/greenwave/policies.py
+++ b/greenwave/policies.py
@@ -150,12 +150,16 @@ class PassingTestCaseRule(Rule):
 
 class Policy(object):
 
-    def __init__(self, id, product_version, decision_context, rules):
+    def __init__(self, id, product_versions, decision_context, rules):
         self.id = id
-        self.product_version = product_version
+        self.product_versions = frozenset(product_versions)
         self.decision_context = decision_context
         self.rules = rules
 
+    def applies_to(self, decision_context, product_version):
+        return (decision_context == self.decision_context and
+                product_version in self.product_versions)
+
     def check(self, item, results, waivers):
         return [rule.check(item, results, waivers) for rule in self.rules]
 
@@ -167,7 +171,9 @@ policies = [
     # tests need to be passed.
     Policy(
         id='1',
-        product_version='rhel-7',
+        product_versions=[
+            'rhel-7',
+        ],
         decision_context='errata_newfile_to_qe',
         rules=[
             PassingTestCaseRule('dist.rpmdiff.analysis.abi_symbols'),

From bc3a02b5ef4a255848f4e4e6a4e53651b65516ad Mon Sep 17 00:00:00 2001
From: Dan Callaghan <dcallagh@redhat.com>
Date: Jun 22 2017 01:17:43 +0000
Subject: [PATCH 2/2] "Unrestricted" policy for cdk-2 and devstudio-2


---

diff --git a/functional-tests/test_api_v1.py b/functional-tests/test_api_v1.py
index c816692..c7fe377 100644
--- a/functional-tests/test_api_v1.py
+++ b/functional-tests/test_api_v1.py
@@ -238,3 +238,23 @@ def test_make_a_decison_on_no_results(requests_session, greenwave_server, testda
         } for name in all_rpmdiff_testcase_names
     ]
     assert res_data['unsatisfied_requirements'] == expected_unsatisfied_requirements
+
+
+def test_unrestricted_policy_is_always_satisfied(
+        requests_session, greenwave_server, testdatabuilder):
+    nvr = testdatabuilder.unique_nvr()
+    data = {
+        'decision_context': 'errata_newfile_to_qe',
+        'product_version': 'cdk-2',
+        'subject': [nvr]
+    }
+    r = requests_session.post(greenwave_server.url + 'api/v1.0/decision',
+                              headers={'Content-Type': 'application/json'},
+                              data=json.dumps(data))
+    assert r.status_code == 200
+    res_data = r.json()
+    assert res_data['policies_satisified'] is True
+    assert res_data['applicable_policies'] == ['errata-unrestricted']
+    expected_summary = 'no tests are required'
+    assert res_data['summary'] == expected_summary
+    assert res_data['unsatisfied_requirements'] == []
diff --git a/greenwave/policies.py b/greenwave/policies.py
index 8fdff19..290fe69 100644
--- a/greenwave/policies.py
+++ b/greenwave/policies.py
@@ -87,6 +87,8 @@ def summarize_answers(answers):
     Returns:
         str: Human-readable summary.
     """
+    if len(answers) == 0:
+        return 'no tests are required'
     if all(answer.is_satisfied for answer in answers):
         return 'all required tests passed'
     failure_count = len([answer for answer in answers if isinstance(answer, TestResultFailed)])
@@ -249,4 +251,14 @@ policies = [
             PassingTestCaseRule('dist.rpmdiff.comparison.xml_validity'),
         ],
     ),
+    # Errata Tool "Unrestricted" rule set
+    Policy(
+        id='errata-unrestricted',
+        decision_context='errata_newfile_to_qe',
+        product_versions=[
+            'cdk-2',
+            'devstudio-2',
+        ],
+        rules=[],
+    ),
 ]

