From 68298497c98e0f86cf3c9d02cd85f1c05022f944 Mon Sep 17 00:00:00 2001
From: Giulia Naponiello <gnaponie@redhat.com>
Date: Oct 24 2019 13:08:29 +0000
Subject: Add support for redhat-container-image


This change allow to gate container images based on test results on
redhat-container-image artifact. It introduces support also for it
for remote policies in gating.yaml files.

JIRA: FACTORY-5232

Signed-off-by: Giulia Naponiello <gnaponie@redhat.com>

---

diff --git a/greenwave/policies.py b/greenwave/policies.py
index cdb0ddf..8c58aec 100644
--- a/greenwave/policies.py
+++ b/greenwave/policies.py
@@ -392,7 +392,7 @@ class RemoteRule(Rule):
     }
 
     def _get_sub_policies(self, policy, subject_identifier):
-        if policy.subject_type not in ['koji_build', 'redhat-module']:
+        if policy.subject_type not in ['koji_build', 'redhat-module', 'redhat-container-image']:
             return []
 
         pkg_namespace, pkg_name, rev = greenwave.resources.retrieve_scm_from_koji(
@@ -736,7 +736,8 @@ class RemotePolicy(Policy):
     safe_yaml_attributes = {
         'id': SafeYAMLString(optional=True),
         'product_versions': SafeYAMLList(str),
-        'subject_type': SafeYAMLChoice('koji_build', 'redhat-module', optional=True),
+        'subject_type': SafeYAMLChoice(
+            'koji_build', 'redhat-module', 'redhat-container-image', optional=True),
         'decision_context': SafeYAMLString(),
         'rules': SafeYAMLList(Rule),
         'blacklist': SafeYAMLList(str, optional=True),
diff --git a/greenwave/product_versions.py b/greenwave/product_versions.py
index f83dec6..6c73ad7 100644
--- a/greenwave/product_versions.py
+++ b/greenwave/product_versions.py
@@ -71,7 +71,7 @@ def subject_product_version(
     if subject_type == "compose":
         return _guess_product_version(subject_identifier)
 
-    if subject_type == "redhat-module":
+    if subject_type in ("redhat-module", "redhat-container-image"):
         return "rhel-8"
 
     if koji_proxy:
diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py
index 4759d18..c9cc97d 100644
--- a/greenwave/tests/test_policies.py
+++ b/greenwave/tests/test_policies.py
@@ -394,6 +394,68 @@ def test_remote_rule_policy_redhat_module(tmpdir, namespace):
                 assert isinstance(decision[0], TestResultFailed)
 
 
+def test_remote_rule_policy_redhat_container_image(tmpdir):
+    """ Testing the RemoteRule with the koji interaction.
+    In this case we are just mocking koji """
+
+    nvr = '389-ds-1.4-820181127205924.9edba152'
+
+    serverside_fragment = dedent("""
+        --- !Policy
+        id: "taskotron_release_critical_tasks_with_remoterule"
+        product_versions:
+          - rhel-8
+        decision_context: osci_compose_gate
+        subject_type: redhat-container-image
+        rules:
+          - !RemoteRule {}
+        """)
+
+    remote_fragment = dedent("""
+        --- !Policy
+        product_versions:
+          - rhel-8
+        decision_context: osci_compose_gate
+        subject_type: redhat-container-image
+        rules:
+          - !PassingTestCaseRule {test_case_name: baseos-ci.redhat-container-image.tier0.functional}
+
+        """)
+
+    p = tmpdir.join('gating.yaml')
+    p.write(serverside_fragment)
+    app = create_app('greenwave.config.TestingConfig')
+    with app.app_context():
+        with mock.patch('greenwave.resources.retrieve_scm_from_koji') as scm:
+            scm.return_value = ('containers', '389-ds', 'c3c47a08a66451cb9686c49f040776ed35a0d1bb')
+            with mock.patch('greenwave.resources.retrieve_yaml_remote_rule') as f:
+                f.return_value = remote_fragment
+                policies = load_policies(tmpdir.strpath)
+                policy = policies[0]
+
+                # Ensure that presence of a result is success.
+                results = DummyResultsRetriever(
+                    nvr, 'baseos-ci.redhat-container-image.tier0.functional',
+                    subject_type='redhat-container-image')
+                decision = policy.check('rhel-8', nvr, results)
+                assert len(decision) == 1
+                assert isinstance(decision[0], RuleSatisfied)
+
+                # Ensure that absence of a result is failure.
+                results = DummyResultsRetriever(subject_type='redhat-container-image')
+                decision = policy.check('rhel-8', nvr, results)
+                assert len(decision) == 1
+                assert isinstance(decision[0], TestResultMissing)
+
+                # And that a result with a failure, is a failure.
+                results = DummyResultsRetriever(
+                    nvr, 'baseos-ci.redhat-container-image.tier0.functional', 'FAILED',
+                    subject_type='redhat-container-image')
+                decision = policy.check('rhel-8', nvr, results)
+                assert len(decision) == 1
+                assert isinstance(decision[0], TestResultFailed)
+
+
 def test_remote_rule_policy_optional_id(tmpdir):
     nvr = 'nethack-1.2.3-1.el9000'
 
@@ -882,7 +944,8 @@ def test_policy_with_subject_type_component_version(tmpdir):
     assert isinstance(decision[0], RuleSatisfied)
 
 
-def test_policy_with_subject_type_redhat_module(tmpdir):
+@pytest.mark.parametrize('subject_type', ["redhat-module", "redhat-container-image"])
+def test_policy_with_subject_type_redhat_module(tmpdir, subject_type):
     nsvc = 'httpd:2.4:20181018085700:9edba152'
     p = tmpdir.join('fedora.yaml')
     p.write(dedent("""
@@ -891,15 +954,15 @@ def test_policy_with_subject_type_redhat_module(tmpdir):
         product_versions:
         - fedora-29
         decision_context: decision_context_test_redhat_module
-        subject_type: redhat-module
+        subject_type: %s
         blacklist: []
         rules:
           - !PassingTestCaseRule {test_case_name: test_for_redhat_module_type}
-        """))
+        """ % subject_type))
     policies = load_policies(tmpdir.strpath)
     policy = policies[0]
     results = DummyResultsRetriever(nsvc, 'test_for_redhat_module_type', 'PASSED',
-                                    'redhat-module')
+                                    subject_type)
     decision = policy.check('fedora-29', nsvc, results)
     assert len(decision) == 1
     assert isinstance(decision[0], RuleSatisfied)

