From e3d2c0562f2b97fc1bd3023553527fa6fd9e5a0c Mon Sep 17 00:00:00 2001
From: Chenxiong Qi <cqi@redhat.com>
Date: Feb 24 2018 10:21:23 +0000
Subject: Use new PDC modules API


Fixes #217

Signed-off-by: Chenxiong Qi <cqi@redhat.com>

---

diff --git a/freshmaker/handlers/git/rpm_spec_change.py b/freshmaker/handlers/git/rpm_spec_change.py
index c1ce5a5..1ce5c5c 100644
--- a/freshmaker/handlers/git/rpm_spec_change.py
+++ b/freshmaker/handlers/git/rpm_spec_change.py
@@ -52,8 +52,8 @@ class GitRPMSpecChangeHandler(BaseHandler):
                                          active='true')
 
         for module in modules:
-            name = module['variant_name']
-            version = module['variant_version']
+            name = module['name']
+            version = module['stream']
             if not self.allow_build(ArtifactType.MODULE, name=name, branch=version):
                 log.info("Skip rebuild of %s:%s as it's not allowed by configured whitelist",
                          name, version)
diff --git a/freshmaker/handlers/mbs/module_state_change.py b/freshmaker/handlers/mbs/module_state_change.py
index b04f4b6..243bb97 100644
--- a/freshmaker/handlers/mbs/module_state_change.py
+++ b/freshmaker/handlers/mbs/module_state_change.py
@@ -89,8 +89,8 @@ class MBSModuleStateChangeHandler(BaseHandler):
                                              active='true')
 
             for mod in modules:
-                name = mod['variant_name']
-                version = mod['variant_version']
+                name = mod['name']
+                version = mod['stream']
                 if not self.allow_build(ArtifactType.MODULE, name=name, branch=version):
                     log.info("Skip rebuild of %s:%s as it's not allowed by configured whitelist",
                              name, version)
diff --git a/freshmaker/pdc.py b/freshmaker/pdc.py
index 7b716e8..306e19b 100644
--- a/freshmaker/pdc.py
+++ b/freshmaker/pdc.py
@@ -64,9 +64,9 @@ class PDC(object):
         modules = self.get_modules(**kwargs)
         active = kwargs.get('active', 'true')
         latest_modules = []
-        for (name, version) in set([(m.get('variant_name'), m.get('variant_version')) for m in modules]):
-            mods = self.get_modules(variant_name=name, variant_version=version, active=active)
-            latest_modules.append(sorted(mods, key=lambda x: x['variant_release']).pop())
+        for (name, stream) in set([(m.get('name'), m.get('stream')) for m in modules]):
+            mods = self.get_modules(name=name, stream=stream, active=active)
+            latest_modules.append(sorted(mods, key=lambda x: x['version']).pop())
         return list(filter(lambda x: x in latest_modules, modules))
 
     @freshmaker.utils.retry(wait_on=(requests.Timeout, requests.ConnectionError), logger=freshmaker.log)
@@ -77,7 +77,7 @@ class PDC(object):
         :param kwargs: query parameters in keyword arguments
         :return: a list of modules
         """
-        modules = self.session['unreleasedvariants'](page_size=-1, **kwargs)
+        modules = self.session['modules'](page_size=-1, **kwargs)
         return modules
 
     @freshmaker.utils.retry(wait_on=(requests.Timeout, requests.ConnectionError), logger=freshmaker.log)
diff --git a/tests/helpers.py b/tests/helpers.py
index 96fee4a..f5639d9 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -395,14 +395,12 @@ class KojiTaskStateChangeMessage(FedMsgFactory):
 
 
 class PDCModuleInfoFactory(object):
-    def __init__(self, name, version, release, active=True):
-        self.variant_name = name
-        self.variant_version = version
-        self.variant_release = release
+    def __init__(self, name, stream, version, active=True):
+        self.name = name
+        self.stream = stream
+        self.version = version
         self.active = active
-        self.variant_uid = "%s-%s-%s" % (name, version, release)
-        self.variant_id = name
-        self.variant_type = 'module'
+        self.uid = "%s-%s-%s" % (name, stream, version)
         self.modulemd = ''
         self.build_deps = []
         self.runtime_deps = []
@@ -412,12 +410,9 @@ class PDCModuleInfoFactory(object):
     def produce(self):
         module = {
             'active': self.active,
-            'variant_type': self.variant_type,
-            'variant_id': self.variant_id,
-            'variant_name': self.variant_name,
-            'variant_version': self.variant_version,
-            'variant_release': self.variant_release,
-            'variant_uid': self.variant_uid,
+            'name': self.name,
+            'stream': self.stream,
+            'version': self.version,
             'modulemd': self.modulemd,
             'koji_tag': self.koji_tag,
             'build_deps': self.build_deps,
diff --git a/tests/test_mbs_module_state_change_handler.py b/tests/test_mbs_module_state_change_handler.py
index 2c53db4..6edd674 100644
--- a/tests/test_mbs_module_state_change_handler.py
+++ b/tests/test_mbs_module_state_change_handler.py
@@ -99,10 +99,10 @@ class MBSModuleStateChangeHandlerTest(helpers.ModelsTestCase):
         self.assertEqual(event_list[0].message_id, event.msg_id)
         builds = models.ArtifactBuild.query.all()
         self.assertEqual(len(builds), 2)
-        self.assertEqual(builds[0].name, mod2_r1['variant_name'])
+        self.assertEqual(builds[0].name, mod2_r1['name'])
         self.assertEqual(builds[0].type, ArtifactType.MODULE.value)
         self.assertEqual(builds[0].build_id, 123)
-        self.assertEqual(builds[1].name, mod3_r1['variant_name'])
+        self.assertEqual(builds[1].name, mod3_r1['name'])
         self.assertEqual(builds[1].build_id, 456)
         self.assertEqual(builds[1].type, ArtifactType.MODULE.value)
 
diff --git a/tests/test_pdc.py b/tests/test_pdc.py
new file mode 100644
index 0000000..a49c520
--- /dev/null
+++ b/tests/test_pdc.py
@@ -0,0 +1,99 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2018  Red Hat, Inc.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+
+import unittest
+
+from mock import call, patch
+
+from freshmaker import conf
+from freshmaker.pdc import PDC
+
+
+class TestGetLatestModules(unittest.TestCase):
+    """Test PDC.get_latest_modules"""
+
+    @patch('freshmaker.pdc.PDC.get_modules')
+    def test_exclude_modules_that_doesnt_depend_on_built_module(self, get_modules):
+        get_modules.side_effect = [
+            # modules returned from first call
+            [{'name': '389-ds', 'stream': '1.2', 'version': '20171009091843'},
+             {'name': '389-ds', 'stream': '1.2', 'version': '20171012150041'},
+             {'name': 'apache-commons', 'stream': 'f27', 'version': '20171010111836'}],
+
+            # modules returned from call for name 386-ds and stream 1.2
+            [{'name': '389-ds', 'stream': '1.2', 'version': '20171009105405'},
+             {'name': '389-ds', 'stream': '1.2', 'version': '20171012150041'},
+
+             # *** This is a new version module that already depends on other module.
+             {'name': '389-ds', 'stream': '1.2', 'version': '20171120124934'}],
+
+            # modules returned from call for name apache-commons and stream f27
+            [{'name': 'apache-commons', 'stream': 'f27', 'version': '20171010111836'}]
+        ]
+
+        pdc = PDC(conf)
+        modules = pdc.get_latest_modules(build_dep_name='rebuilt module',
+                                         build_dep_stream='1.7',
+                                         active=True)
+
+        expected_modules = [
+            {'name': 'apache-commons', 'stream': 'f27', 'version': '20171010111836'},
+        ]
+        self.assertEqual(expected_modules, modules)
+
+    @patch('freshmaker.pdc.PDC.get_modules')
+    def test_found_latest_modules(self, get_modules):
+        get_modules.side_effect = [
+            # modules returned from first call
+            [{'name': '389-ds', 'stream': '1.2', 'version': '20171009091843'},
+             {'name': '389-ds', 'stream': '1.2', 'version': '20171012150041'},
+             {'name': '389-ds', 'stream': '1.2', 'version': '20171120124934'},
+             {'name': 'apache-commons', 'stream': 'f27', 'version': '20171010111836'}],
+
+            # modules returned from call for name 386-ds and stream 1.2
+            [{'name': '389-ds', 'stream': '1.2', 'version': '20171009105405'},
+             {'name': '389-ds', 'stream': '1.2', 'version': '20171012150041'},
+             {'name': '389-ds', 'stream': '1.2', 'version': '20171120124934'}],
+
+            # modules returned from call for name apache-commons and stream f27
+            [{'name': 'apache-commons', 'stream': 'f27', 'version': '20171010111836'}]
+        ]
+
+        pdc = PDC(conf)
+        modules = pdc.get_latest_modules(build_dep_name='rebuilt module',
+                                         build_dep_stream='1.7',
+                                         active=True)
+
+        modules = sorted(modules, key=lambda m: m['name'])
+        expected_modules = [
+            {'name': '389-ds', 'stream': '1.2', 'version': '20171120124934'},
+            {'name': 'apache-commons', 'stream': 'f27', 'version': '20171010111836'},
+        ]
+        self.assertEqual(expected_modules, modules)
+
+        get_modules.assert_has_calls([
+            call(build_dep_name='rebuilt module',
+                 build_dep_stream='1.7',
+                 active=True),
+            call(name='389-ds', stream='1.2', active=True),
+            call(name='apache-commons', stream='f27', active=True),
+        ], any_order=True)

