From 8e8bb889dece8436a7068d5628335676618350c3 Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: Apr 27 2020 09:42:37 +0000
Subject: Include subject in passed test case requirements


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

---

diff --git a/functional-tests/consumers/test_resultsdb.py b/functional-tests/consumers/test_resultsdb.py
index f74728e..ffd0804 100644
--- a/functional-tests/consumers/test_resultsdb.py
+++ b/functional-tests/consumers/test_resultsdb.py
@@ -55,6 +55,8 @@ def test_consume_new_result(
         'product_version': 'fedora-26',
         'satisfied_requirements': [
             {
+                'subject_type': 'koji_build',
+                'subject_identifier': nvr,
                 'result_id': result['id'],
                 'testcase': 'dist.rpmdeplint',
                 'type': 'test-result-passed',
@@ -126,6 +128,8 @@ def test_consume_new_result(
         'product_version': 'fedora-26',
         'satisfied_requirements': [
             {
+                'subject_type': 'koji_build',
+                'subject_identifier': nvr,
                 'result_id': result['id'],
                 'testcase': 'dist.rpmdeplint',
                 'type': 'test-result-passed',
@@ -242,6 +246,8 @@ def test_consume_compose_id_result(
         'summary': '1 of 2 required test results missing',
         'previous': old_decision,
         'satisfied_requirements': [{
+            'subject_type': 'compose',
+            'subject_identifier': compose_id,
             'result_id': result['id'],
             'testcase': 'compose.install_no_user',
             'type': 'test-result-passed'
@@ -310,6 +316,8 @@ def test_consume_legacy_result(
         'decision_context': 'bodhi_update_push_stable',
         'product_version': 'fedora-26',
         'satisfied_requirements': [{
+            'subject_type': 'koji_build',
+            'subject_identifier': nvr,
             'result_id': result['id'],
             'testcase': 'dist.rpmdeplint',
             'type': 'test-result-passed'
@@ -367,6 +375,8 @@ def test_consume_legacy_result(
         'decision_context': 'bodhi_update_push_testing',
         'product_version': 'fedora-26',
         'satisfied_requirements': [{
+            'subject_type': 'koji_build',
+            'subject_identifier': nvr,
             'result_id': result['id'],
             'testcase': 'dist.rpmdeplint',
             'type': 'test-result-passed'
@@ -569,6 +579,8 @@ def test_consume_new_result_container_image(
         'summary': 'All required tests passed',
         'previous': old_decision,
         'satisfied_requirements': [{
+            'subject_type': 'container-image',
+            'subject_identifier': nvr,
             'result_id': result['id'],
             'testcase': 'baseos-qe.baseos-ci.tier1.functional',
             'type': 'test-result-passed'
diff --git a/functional-tests/consumers/test_waiverdb.py b/functional-tests/consumers/test_waiverdb.py
index 32b7e4e..15e7ca1 100644
--- a/functional-tests/consumers/test_waiverdb.py
+++ b/functional-tests/consumers/test_waiverdb.py
@@ -77,11 +77,15 @@ def test_consume_new_waiver(
             'summary': '1 of 3 required tests failed',
             'satisfied_requirements': [
                 {
+                    'subject_type': 'koji_build',
+                    'subject_identifier': nvr,
                     'result_id': results[0]['id'],
                     'testcase': passing_tests[0],
                     'type': 'test-result-passed'
                 },
                 {
+                    'subject_type': 'koji_build',
+                    'subject_identifier': nvr,
                     'result_id': results[1]['id'],
                     'testcase': passing_tests[1],
                     'type': 'test-result-passed'
@@ -105,16 +109,22 @@ def test_consume_new_waiver(
         'subject_identifier': nvr,
         'satisfied_requirements': [
             {
+                'subject_type': 'koji_build',
+                'subject_identifier': nvr,
                 'result_id': result['id'],
                 'testcase': failing_test,
                 'type': 'test-result-passed'
             },
             {
+                'subject_type': 'koji_build',
+                'subject_identifier': nvr,
                 'result_id': results[0]['id'],
                 'testcase': passing_tests[0],
                 'type': 'test-result-passed'
             },
             {
+                'subject_type': 'koji_build',
+                'subject_identifier': nvr,
                 'result_id': results[1]['id'],
                 'testcase': passing_tests[1],
                 'type': 'test-result-passed'
diff --git a/functional-tests/test_api_v1.py b/functional-tests/test_api_v1.py
index 4299365..0513656 100644
--- a/functional-tests/test_api_v1.py
+++ b/functional-tests/test_api_v1.py
@@ -258,6 +258,8 @@ def test_make_a_decision_with_verbose_flag(requests_session, greenwave_server, t
             'result_id': result['id'],
             'testcase': result['testcase']['name'],
             'type': 'test-result-passed',
+            'subject_type': 'koji_build',
+            'subject_identifier': nvr,
         } for result in results
     ]
     assert res_data['satisfied_requirements'] == expected_satisfied_requirements
diff --git a/greenwave/policies.py b/greenwave/policies.py
index 48e5066..3ec2ee1 100644
--- a/greenwave/policies.py
+++ b/greenwave/policies.py
@@ -173,7 +173,10 @@ class TestResultFailed(RuleNotSatisfied):
         }
 
     def to_waived(self):
-        return TestResultPassed(self.test_case_name, self.result_id)
+        return TestResultPassed(
+            self.subject,
+            self.test_case_name,
+            self.result_id)
 
 
 class TestResultErrored(RuleNotSatisfied):
@@ -210,7 +213,10 @@ class TestResultErrored(RuleNotSatisfied):
         }
 
     def to_waived(self):
-        return TestResultPassed(self.test_case_name, self.result_id)
+        return TestResultPassed(
+            self.subject,
+            self.test_case_name,
+            self.result_id)
 
 
 class InvalidRemoteRuleYaml(RuleNotSatisfied):
@@ -263,7 +269,8 @@ class TestResultPassed(RuleSatisfied):
     A required test case passed (that is, its outcome in ResultsDB was
     ``PASSED`` or ``INFO``) or a corresponding waiver was found.
     """
-    def __init__(self, test_case_name, result_id):
+    def __init__(self, subject, test_case_name, result_id):
+        self.subject = subject
         self.test_case_name = test_case_name
         self.result_id = result_id
 
@@ -272,6 +279,8 @@ class TestResultPassed(RuleSatisfied):
             'type': 'test-result-passed',
             'testcase': self.test_case_name,
             'result_id': self.result_id,
+            'subject_type': self.subject.type,
+            'subject_identifier': self.subject.identifier,
         }
 
 
@@ -607,7 +616,7 @@ class PassingTestCaseRule(Rule):
             log.debug('Test result passed for the result_id %s and testcase %s,'
                       ' because the outcome is %s', result['id'], self.test_case_name,
                       result['outcome'])
-            return TestResultPassed(self.test_case_name, result['id'])
+            return TestResultPassed(subject, self.test_case_name, result['id'])
 
         if result['outcome'] in ('QUEUED', 'RUNNING'):
             log.debug('Test result MISSING for the %s and '
diff --git a/greenwave/tests/test_waive.py b/greenwave/tests/test_waive.py
index 054dbea..c2ae22b 100644
--- a/greenwave/tests/test_waive.py
+++ b/greenwave/tests/test_waive.py
@@ -39,6 +39,8 @@ def test_waive_failed_result():
     expected_json = dict(
         type='test-result-passed',
         testcase='test1',
+        subject_type='koji_build',
+        subject_identifier='nethack-1.2.3-1.rawhide',
         result_id=99,
     )
     assert 1 == len(waived)

