From 00d6243beb852a37ec99361e9adedd80732386ac Mon Sep 17 00:00:00 2001
From: Dan Callaghan <dcallagh@redhat.com>
Date: Jun 15 2017 04:13:31 +0000
Subject: [PATCH 1/2] s/errta/errata/g


---

diff --git a/greenwave/policies.py b/greenwave/policies.py
index adbe294..8a2f3b7 100644
--- a/greenwave/policies.py
+++ b/greenwave/policies.py
@@ -17,7 +17,7 @@ policies = {
     # tests need to be passed.
     '1': {
         'product_version': 'rhel-7',
-        'decision_context': 'errta_newfile_to_qe',
+        'decision_context': 'errata_newfile_to_qe',
         'rules': [
             'dist.rpmdiff.comparison.xml_validity',
             'dist.rpmdiff.comparison.virus_scan',
diff --git a/greenwave/tests/test_api_v10.py b/greenwave/tests/test_api_v10.py
index 8210e13..090e32c 100644
--- a/greenwave/tests/test_api_v10.py
+++ b/greenwave/tests/test_api_v10.py
@@ -17,7 +17,7 @@ from mock import patch
 
 def test_cannot_make_decision_without_product_version(client):
     data = {
-        'decision_context': 'errta_newfile_to_qe',
+        'decision_context': 'errata_newfile_to_qe',
         'subject': ['foo-1.0.0-1.el7']
     }
     r = client.post('/api/v1.0/decision', data=json.dumps(data),
@@ -39,7 +39,7 @@ def test_cannot_make_decision_without_decision_context(client):
 
 def test_cannot_make_decision_without_subject(client):
     data = {
-        'decision_context': 'errta_newfile_to_qe',
+        'decision_context': 'errata_newfile_to_qe',
         'product_version': 'rhel-7',
     }
     r = client.post('/api/v1.0/decision', data=json.dumps(data),

From 1f61f74dfc9d02fc288041319ec9c040fb497613 Mon Sep 17 00:00:00 2001
From: Dan Callaghan <dcallagh@redhat.com>
Date: Jun 15 2017 04:43:36 +0000
Subject: [PATCH 2/2] tests: use real policies instead of mock dummy policies


This lets us refactor how the policies are represented, stored, and
evaluated without having to rewrite every test.

---

diff --git a/greenwave/tests/test_api_v10.py b/greenwave/tests/test_api_v10.py
index 090e32c..fbf6681 100644
--- a/greenwave/tests/test_api_v10.py
+++ b/greenwave/tests/test_api_v10.py
@@ -12,7 +12,16 @@
 import json
 import requests_mock
 from flask import current_app
-from mock import patch
+
+
+all_rpmdiff_testcase_names = [
+    # XXX this is not all of them
+    'dist.rpmdiff.comparison.xml_validity',
+    'dist.rpmdiff.comparison.virus_scan',
+    'dist.rpmdiff.comparison.upstream_source',
+    'dist.rpmdiff.comparison.symlinks',
+    'dist.rpmdiff.comparison.binary_stripping',
+]
 
 
 def test_cannot_make_decision_without_product_version(client):
@@ -74,46 +83,37 @@ def test_make_a_decison_on_passed_result(client):
                   "5d307e4f-1ade-4c41-9e67-e5a73d5cdd07"
               ],
               "href": "https://resultsdb.domain.local/api/v2.0/results/331284",
-              "id": 331284,
+              "id": id,
               "note": "",
               "outcome": "PASSED",
               "ref_url": "https://rpmdiff.domain.local/run/97683/26",
               "submit_time": "2017-05-19T04:41:13.957729",
               "testcase": {
-                "href": 'https://resultsdb.domain.local/api/v2.0/testcases/'
-                        'dist.rpmdiff.comparison.xml_validity',
-                "name": "dist.rpmdiff.comparison.xml_validity",
+                "href": 'https://resultsdb.domain.local/api/v2.0/testcases/' + name,
+                "name": name,
                 "ref_url": "https://docs.domain.local/display/HTD/rpmdiff-valid-file"
               }
-            }
+            } for id, name in enumerate(all_rpmdiff_testcase_names, 1)
           ]
         }
         m.register_uri('GET', '{}/results?item={}&testcases={}'.format(
             current_app.config['RESULTSDB_API_URL'],
             'foo-1.0.0-2.el7',
-            'dist.rpmdiff.comparison.xml_validity'
+            ','.join(all_rpmdiff_testcase_names)
         ), json=mocked_results)
-        dummy_policies = {
-            '1': {
-                'product_version': 'rhel-7',
-                'decision_context': 'dummpy_decision',
-                'rules': ['dist.rpmdiff.comparison.xml_validity']
-            }
+        data = {
+            'decision_context': 'errata_newfile_to_qe',
+            'product_version': 'rhel-7',
+            'subject': ['foo-1.0.0-2.el7']
         }
-        with patch.dict('greenwave.policies.policies', dummy_policies):
-            data = {
-                'decision_context': 'dummpy_decision',
-                'product_version': 'rhel-7',
-                'subject': ['foo-1.0.0-2.el7']
-            }
-            r = client.post('/api/v1.0/decision', data=json.dumps(data),
-                            content_type='application/json')
-            assert r.status_code == 200
-            res_data = json.loads(r.get_data(as_text=True))
-            assert res_data['policies_satisified'] is True
-            assert res_data['applicable_policies'] == ['1']
-            assert res_data['summary'] == 'foo-1.0.0-2.el7: policy 1 is satisfied as all required' \
-                ' tests are passing'
+        r = client.post('/api/v1.0/decision', data=json.dumps(data),
+                        content_type='application/json')
+        assert r.status_code == 200
+        res_data = json.loads(r.get_data(as_text=True))
+        assert res_data['policies_satisified'] is True
+        assert res_data['applicable_policies'] == ['1']
+        assert res_data['summary'] == 'foo-1.0.0-2.el7: policy 1 is satisfied as all required' \
+            ' tests are passing'
 
 
 def test_make_a_decison_on_failed_result_with_waiver(client):
@@ -130,24 +130,25 @@ def test_make_a_decison_on_failed_result_with_waiver(client):
                   "5d307e4f-1ade-4c41-9e67-e5a73d5cdd07"
               ],
               "href": "https://resultsdb.domain.local/api/v2.0/results/331284",
-              "id": 331284,
+              "id": id,
               "note": "",
-              "outcome": "FAILED",
+              "outcome": "PASSED",
               "ref_url": "https://rpmdiff.domain.local/run/97683/26",
               "submit_time": "2017-05-19T04:41:13.957729",
               "testcase": {
-                "href": 'https://resultsdb.domain.local/api/v2.0/testcases/'
-                        'dist.rpmdiff.comparison.xml_validity',
-                "name": "dist.rpmdiff.comparison.xml_validity",
+                "href": 'https://resultsdb.domain.local/api/v2.0/testcases/' + name,
+                "name": name,
                 "ref_url": "https://docs.domain.local/display/HTD/rpmdiff-valid-file"
               }
-            }
+            } for id, name in enumerate(all_rpmdiff_testcase_names, 1)
           ]
         }
+        mocked_results['data'][0]['id'] = 331284
+        mocked_results['data'][0]['outcome'] = 'FAILED'
         m.register_uri('GET', '{}/results?item={}&testcases={}'.format(
             current_app.config['RESULTSDB_API_URL'],
             'foo-1.0.0-2.el7',
-            'dist.rpmdiff.comparison.xml_validity'
+            ','.join(all_rpmdiff_testcase_names)
         ), json=mocked_results)
         mocked_waiver = {
           "data": [
@@ -167,27 +168,19 @@ def test_make_a_decison_on_failed_result_with_waiver(client):
             331284,
             'rhel-7'
         ), json=mocked_waiver)
-        dummy_policies = {
-            '1': {
-                'product_version': 'rhel-7',
-                'decision_context': 'dummpy_decision',
-                'rules': ['dist.rpmdiff.comparison.xml_validity']
-            }
+        data = {
+            'decision_context': 'errata_newfile_to_qe',
+            'product_version': 'rhel-7',
+            'subject': ['foo-1.0.0-2.el7']
         }
-        with patch.dict('greenwave.policies.policies', dummy_policies):
-            data = {
-                'decision_context': 'dummpy_decision',
-                'product_version': 'rhel-7',
-                'subject': ['foo-1.0.0-2.el7']
-            }
-            r = client.post('/api/v1.0/decision', data=json.dumps(data),
-                            content_type='application/json')
-            assert r.status_code == 200
-            res_data = json.loads(r.get_data(as_text=True))
-            assert res_data['policies_satisified'] is True
-            assert res_data['applicable_policies'] == ['1']
-            assert res_data['summary'] == 'foo-1.0.0-2.el7: policy 1 is satisfied as all required' \
-                ' tests are passing'
+        r = client.post('/api/v1.0/decision', data=json.dumps(data),
+                        content_type='application/json')
+        assert r.status_code == 200
+        res_data = json.loads(r.get_data(as_text=True))
+        assert res_data['policies_satisified'] is True
+        assert res_data['applicable_policies'] == ['1']
+        assert res_data['summary'] == 'foo-1.0.0-2.el7: policy 1 is satisfied as all required' \
+            ' tests are passing'
 
 
 def test_make_a_decison_on_failed_result(client):
@@ -221,48 +214,43 @@ def test_make_a_decison_on_failed_result(client):
         m.register_uri('GET', '{}/results?item={}&testcases={}'.format(
             current_app.config['RESULTSDB_API_URL'],
             'foo-1.0.0-2.el7',
-            'dist.rpmdiff.comparison.xml_validity,dist.rpmdiff.comparison.virus_scan'
+            ','.join(all_rpmdiff_testcase_names)
         ), json=mocked_results)
         m.register_uri('GET', '{}/waivers/?result_id={}&product_version={}'.format(
             current_app.config['WAIVERDB_API_URL'],
             331284,
             'rhel-7'
         ), json={"data": []})
-        dummy_policies = {
-            '1': {
-                'product_version': 'rhel-7',
-                'decision_context': 'dummpy_decision',
-                'rules': ['dist.rpmdiff.comparison.xml_validity',
-                          'dist.rpmdiff.comparison.virus_scan']
-            }
+        data = {
+            'decision_context': 'errata_newfile_to_qe',
+            'product_version': 'rhel-7',
+            'subject': ['foo-1.0.0-2.el7']
         }
-        with patch.dict('greenwave.policies.policies', dummy_policies):
-            data = {
-                'decision_context': 'dummpy_decision',
-                'product_version': 'rhel-7',
-                'subject': ['foo-1.0.0-2.el7']
-            }
-            r = client.post('/api/v1.0/decision', data=json.dumps(data),
-                            content_type='application/json')
-            assert r.status_code == 200
-            res_data = json.loads(r.get_data(as_text=True))
-            assert res_data['policies_satisified'] is False
-            assert res_data['applicable_policies'] == ['1']
-            assert res_data['summary'] == 'foo-1.0.0-2.el7: 2 of 2 required tests' \
-                ' failed, the policy 1 is not satisfied'
-            expected_unsatisfied_requirements = [
-                {
-                    'item': 'foo-1.0.0-2.el7',
-                    'result_id': 331284,
-                    'testcase': 'dist.rpmdiff.comparison.xml_validity',
-                    'type': 'test-result-failed'
-                },
-                {
-                 'item': 'foo-1.0.0-2.el7',
-                 'testcase': 'dist.rpmdiff.comparison.virus_scan',
-                 'type': 'test-result-missing'
-                }]
-            assert res_data['unsatisfied_requirements'] == expected_unsatisfied_requirements
+        r = client.post('/api/v1.0/decision', data=json.dumps(data),
+                        content_type='application/json')
+        assert r.status_code == 200
+        res_data = json.loads(r.get_data(as_text=True))
+        assert res_data['policies_satisified'] is False
+        assert res_data['applicable_policies'] == ['1']
+        # XXX actually 1 failed and 4 are missing, need to improve this summary
+        assert res_data['summary'] == 'foo-1.0.0-2.el7: 5 of 5 required tests' \
+            ' failed, the policy 1 is not satisfied'
+        expected_unsatisfied_requirements = [
+            {
+                'item': 'foo-1.0.0-2.el7',
+                'result_id': 331284,
+                'testcase': 'dist.rpmdiff.comparison.xml_validity',
+                'type': 'test-result-failed'
+            },
+        ] + [
+            {
+             'item': 'foo-1.0.0-2.el7',
+             'testcase': name,
+             'type': 'test-result-missing'
+            } for name in all_rpmdiff_testcase_names
+            if name != 'dist.rpmdiff.comparison.xml_validity'
+        ]
+        assert res_data['unsatisfied_requirements'] == expected_unsatisfied_requirements
 
 
 def test_make_a_decison_on_no_results(client):
@@ -270,32 +258,25 @@ def test_make_a_decison_on_no_results(client):
         m.register_uri('GET', '{}/results?item={}&testcases={}'.format(
             current_app.config['RESULTSDB_API_URL'],
             'foo-1.0.0-2.el7',
-            'dist.rpmdiff.comparison.xml_validity'
+            ','.join(all_rpmdiff_testcase_names)
         ), json={"data": []})
-        dummy_policies = {
-            '1': {
-                'product_version': 'rhel-7',
-                'decision_context': 'dummpy_decision',
-                'rules': ['dist.rpmdiff.comparison.xml_validity']
-            }
+        data = {
+            'decision_context': 'errata_newfile_to_qe',
+            'product_version': 'rhel-7',
+            'subject': ['foo-1.0.0-2.el7']
         }
-        with patch.dict('greenwave.policies.policies', dummy_policies):
-            data = {
-                'decision_context': 'dummpy_decision',
-                'product_version': 'rhel-7',
-                'subject': ['foo-1.0.0-2.el7']
-            }
-            r = client.post('/api/v1.0/decision', data=json.dumps(data),
-                            content_type='application/json')
-            assert r.status_code == 200
-            res_data = json.loads(r.get_data(as_text=True))
-            assert res_data['policies_satisified'] is False
-            assert res_data['applicable_policies'] == ['1']
-            assert res_data['summary'] == 'foo-1.0.0-2.el7: no test results found'
-            expected_unsatisfied_requirements = [
-                {
-                    'item': 'foo-1.0.0-2.el7',
-                    'testcase': 'dist.rpmdiff.comparison.xml_validity',
-                    'type': 'test-result-missing'
-                }]
-            assert res_data['unsatisfied_requirements'] == expected_unsatisfied_requirements
+        r = client.post('/api/v1.0/decision', data=json.dumps(data),
+                        content_type='application/json')
+        assert r.status_code == 200
+        res_data = json.loads(r.get_data(as_text=True))
+        assert res_data['policies_satisified'] is False
+        assert res_data['applicable_policies'] == ['1']
+        assert res_data['summary'] == 'foo-1.0.0-2.el7: no test results found'
+        expected_unsatisfied_requirements = [
+            {
+                'item': 'foo-1.0.0-2.el7',
+                'testcase': name,
+                'type': 'test-result-missing'
+            } for name in all_rpmdiff_testcase_names
+        ]
+        assert res_data['unsatisfied_requirements'] == expected_unsatisfied_requirements

