From dce981ae76d348fb05f9aa5fde7c15af851e7c34 Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: May 05 2020 15:38:12 +0000
Subject: [PATCH 1/2] Make waived requirements more consistent


JIRA: RHELWF-656
Signed-off-by: Lukas Holecek <hluk@email.cz>

---

diff --git a/functional-tests/consumers/test_waiverdb.py b/functional-tests/consumers/test_waiverdb.py
index 15e7ca1..d89fc46 100644
--- a/functional-tests/consumers/test_waiverdb.py
+++ b/functional-tests/consumers/test_waiverdb.py
@@ -113,7 +113,8 @@ def test_consume_new_waiver(
                 'subject_identifier': nvr,
                 'result_id': result['id'],
                 'testcase': failing_test,
-                'type': 'test-result-passed'
+                'type': 'test-result-failed-waived',
+                'scenario': None
             },
             {
                 'subject_type': 'koji_build',
diff --git a/greenwave/policies.py b/greenwave/policies.py
index 3ec2ee1..726e52f 100644
--- a/greenwave/policies.py
+++ b/greenwave/policies.py
@@ -123,29 +123,32 @@ class TestResultMissing(RuleNotSatisfied):
         }
 
     def to_waived(self):
-        return TestResultMissingWaived(
-            self.subject,
-            self.test_case_name,
-            self.scenario)
+        return TestResultWaived(self)
 
 
-class TestResultMissingWaived(RuleSatisfied):
+class TestResultWaived(RuleSatisfied):
     """
-    Same as TestResultMissing but the result was waived.
+    A waived unsatisfied rule.
+
+    Contains same data as unsatisfied rule except the type has "-waived"
+    suffix. Also, the deprecated "item" field is dropped.
     """
-    def __init__(self, subject, test_case_name, scenario):
-        self.subject = subject
-        self.test_case_name = test_case_name
-        self.scenario = scenario
+    def __init__(self, unsatisfied_rule):
+        self.unsatisfied_rule = unsatisfied_rule
 
     def to_json(self):
-        return {
-            'type': 'test-result-missing-waived',
-            'testcase': self.test_case_name,
-            'subject_type': self.subject.type,
-            'subject_identifier': self.subject.identifier,
-            'scenario': self.scenario,
-        }
+        satisfied_rule = self.unsatisfied_rule.to_json()
+        satisfied_rule['type'] += '-waived'
+
+        item = satisfied_rule.get('item')
+        if isinstance(item, dict) and 'item' in item and 'type' in item:
+            if 'subject_identifier' not in satisfied_rule:
+                satisfied_rule['subject_identifier'] = item['item']
+            if 'subject_type' not in satisfied_rule:
+                satisfied_rule['subject_type'] = item['type']
+            del satisfied_rule['item']
+
+        return satisfied_rule
 
 
 class TestResultFailed(RuleNotSatisfied):
@@ -173,10 +176,7 @@ class TestResultFailed(RuleNotSatisfied):
         }
 
     def to_waived(self):
-        return TestResultPassed(
-            self.subject,
-            self.test_case_name,
-            self.result_id)
+        return TestResultWaived(self)
 
 
 class TestResultErrored(RuleNotSatisfied):
@@ -213,10 +213,7 @@ class TestResultErrored(RuleNotSatisfied):
         }
 
     def to_waived(self):
-        return TestResultPassed(
-            self.subject,
-            self.test_case_name,
-            self.result_id)
+        return TestResultWaived(self)
 
 
 class InvalidRemoteRuleYaml(RuleNotSatisfied):
diff --git a/greenwave/tests/test_summary.py b/greenwave/tests/test_summary.py
index 202f5bd..79c20ac 100644
--- a/greenwave/tests/test_summary.py
+++ b/greenwave/tests/test_summary.py
@@ -5,7 +5,7 @@ from greenwave.policies import (
     TestResultErrored,
     TestResultFailed,
     TestResultMissing,
-    TestResultMissingWaived,
+    TestResultWaived,
     InvalidRemoteRuleYaml,
 )
 
@@ -21,8 +21,6 @@ testResultFailed = TestResultFailed(
     testSubject, 'test', None, 1)
 testResultMissing = TestResultMissing(
     testSubject, 'test', None)
-testResultMissingWaived = TestResultMissingWaived(
-    testSubject, 'test', None)
 testInvalidGatingYaml = InvalidRemoteRuleYaml(
     testSubject, 'test', 'Missing !Policy tag')
 
@@ -55,7 +53,7 @@ def test_summary_missing():
 
 def test_summary_missing_waived():
     answers = [
-        testResultMissingWaived,
+        TestResultWaived(testResultMissing),
     ]
     assert summarize_answers(answers) == 'All required tests passed'
 
@@ -86,7 +84,7 @@ def test_summary_one_passed_one_missing():
 def test_summary_one_passed_one_missing_waived():
     answers = [
         testResultPassed,
-        testResultMissingWaived,
+        TestResultWaived(testResultMissing),
     ]
     assert summarize_answers(answers) == 'All required tests passed'
 
diff --git a/greenwave/tests/test_waive.py b/greenwave/tests/test_waive.py
index c2ae22b..dc296e1 100644
--- a/greenwave/tests/test_waive.py
+++ b/greenwave/tests/test_waive.py
@@ -37,11 +37,12 @@ def test_waive_failed_result():
     ]
     waived = waive_answers(answers, waivers)
     expected_json = dict(
-        type='test-result-passed',
+        type='test-result-failed-waived',
         testcase='test1',
         subject_type='koji_build',
         subject_identifier='nethack-1.2.3-1.rawhide',
         result_id=99,
+        scenario='scenario1',
     )
     assert 1 == len(waived)
     assert expected_json == waived[0].to_json()

From 2d5fd012402b3c3a7eab73458a81496a00a5d5d5 Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: May 05 2020 15:41:12 +0000
Subject: [PATCH 2/2] docs: Update test-result-failed-waived requirement example


Signed-off-by: Lukas Holecek <hluk@email.cz>

---

diff --git a/docs/decision_requirements.rst b/docs/decision_requirements.rst
index a049cd1..ae812b2 100644
--- a/docs/decision_requirements.rst
+++ b/docs/decision_requirements.rst
@@ -130,11 +130,12 @@ Waived failed test result
 .. code-block:: json
 
     {
-        "type": "test-result-passed",
+        "type": "test-result-failed-waived",
         "testcase": "example.test.case",
         "subject_type": "koji-build",
         "subject_identifier": "nethack-1.2.3-1.rawhide",
-        "result_id": 1002
+        "result_id": 1002,
+        "scenario": null
     }
 
 Waived missing test result

