From e1d55b94ba38558efe6d43c0a89ed6869f983a90 Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: Jul 09 2019 09:55:58 +0000
Subject: [PATCH 1/2] Tests: Add synthetic test for Koji XMLRPC


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

---

diff --git a/greenwave/consumers/resultsdb.py b/greenwave/consumers/resultsdb.py
index 65138eb..92fb1b8 100644
--- a/greenwave/consumers/resultsdb.py
+++ b/greenwave/consumers/resultsdb.py
@@ -66,7 +66,7 @@ def _guess_product_version(toparse, koji_build=False):
     return None
 
 
-def _subject_product_version(subject_identifier, subject_type, koji_base_url=None):
+def _subject_product_version(subject_identifier, subject_type, koji_proxy=None):
     if subject_type == 'koji_build':
         try:
             short_prod_version = subject_identifier.split('.')[-1]
@@ -80,12 +80,11 @@ def _subject_product_version(subject_identifier, subject_type, koji_base_url=Non
     if subject_type == "redhat-module":
         return "rhel-8"
 
-    if koji_base_url:
-        proxy = xmlrpc.client.ServerProxy(koji_base_url)
+    if koji_proxy:
         try:
-            build = proxy.getBuild(subject_identifier)
+            build = koji_proxy.getBuild(subject_identifier)
             if build:
-                target = proxy.getTaskRequest(build['task_id'])[1]
+                target = koji_proxy.getTaskRequest(build['task_id'])[1]
                 return _guess_product_version(target, koji_build=True)
         except KeyError:
             pass
@@ -156,9 +155,14 @@ class ResultsDBHandler(fedmsg.consumers.FedmsgConsumer):
 
         self.flask_app = greenwave.app_factory.create_app(config)
         self.greenwave_api_url = self.flask_app.config['GREENWAVE_API_URL']
-        self.koji_base_url = self.flask_app.config['KOJI_BASE_URL']
         self.cache = self.flask_app.cache
 
+        koji_base_url = self.flask_app.config['KOJI_BASE_URL']
+        if koji_base_url:
+            self.koji_proxy = xmlrpc.client.ServerProxy(koji_base_url)
+        else:
+            self.koji_proxy = None
+
         log.info('Greenwave resultsdb handler listening on: %s', self.topic)
 
     @staticmethod
@@ -247,7 +251,7 @@ class ResultsDBHandler(fedmsg.consumers.FedmsgConsumer):
             testcase (munch.Munch): the name of a testcase to consider.
         """
         product_version = _subject_product_version(
-            subject_identifier, subject_type, self.koji_base_url)
+            subject_identifier, subject_type, self.koji_proxy)
         policies = self.flask_app.config['policies']
         contexts_product_versions = applicable_decision_context_product_version_pairs(
             policies,
diff --git a/greenwave/tests/test_resultsdb_consumer.py b/greenwave/tests/test_resultsdb_consumer.py
index 2f28db0..15baeba 100644
--- a/greenwave/tests/test_resultsdb_consumer.py
+++ b/greenwave/tests/test_resultsdb_consumer.py
@@ -330,6 +330,22 @@ def test_guess_product_version():
         assert product_version == 'rhel-8'
 
 
+def test_guess_product_version_with_koji():
+    class DummyKojiProxy():
+        def getBuild(self, subject_identifier):
+            assert 'fake_koji_build' == subject_identifier
+
+    koji_proxy = mock.MagicMock()
+    koji_proxy.getBuild.return_value = {'task_id': 666}
+    koji_proxy.getTaskRequest.return_value = ['git://example.com/project', 'rawhide', {}]
+
+    product_version = greenwave.consumers.resultsdb._subject_product_version(
+        'fake_koji_build', 'container-build', koji_proxy)
+    koji_proxy.getBuild.assert_called_once_with('fake_koji_build')
+    koji_proxy.getTaskRequest.assert_called_once_with(666)
+    assert product_version == 'fedora-rawhide'
+
+
 @pytest.mark.parametrize("config,publish", parameters)
 @mock.patch('greenwave.resources.ResultsRetriever.retrieve')
 @mock.patch('greenwave.resources.retrieve_decision')

From dec3b6608b7ddc9e4de5452db033819de3708b86 Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: Jul 09 2019 09:56:01 +0000
Subject: [PATCH 2/2] Tests: Omit calling real Koji XMLRPC in tests


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 314e95d..fd74041 100644
--- a/functional-tests/consumers/test_resultsdb.py
+++ b/functional-tests/consumers/test_resultsdb.py
@@ -551,6 +551,7 @@ def test_consume_new_result_container_image(
         }
     }
     handler = create_resultdb_handler(greenwave_server)
+    handler.koji_proxy = None
     handler.consume(message)
 
     # get old decision
diff --git a/greenwave/config.py b/greenwave/config.py
index f657746..7b7ebf8 100644
--- a/greenwave/config.py
+++ b/greenwave/config.py
@@ -56,6 +56,7 @@ class TestingConfig(Config):
     RESULTSDB_API_URL = 'http://localhost:5001/api/v2.0'
     WAIVERDB_API_URL = 'http://localhost:5004/api/v1.0'
     GREENWAVE_API_URL = 'http://localhost:5005/api/v1.0'
+    KOJI_BASE_URL = 'http://localhost:5006/kojihub'
     POLICIES_DIR = os.path.join(
         os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'conf',
         'policies'
diff --git a/greenwave/tests/test_resultsdb_consumer.py b/greenwave/tests/test_resultsdb_consumer.py
index 15baeba..9ac48b1 100644
--- a/greenwave/tests/test_resultsdb_consumer.py
+++ b/greenwave/tests/test_resultsdb_consumer.py
@@ -560,6 +560,7 @@ def test_real_fedora_messaging_msg(
             }
             handler = greenwave.consumers.resultsdb.ResultsDBHandler(hub)
 
+            handler.koji_proxy = None
             handler.flask_app.config['policies'] = Policy.safe_load_all(policies)
             with handler.flask_app.app_context():
                 handler.consume(message)

