#23 Refactor get_modules
Merged 4 years ago by qwan. Opened 4 years ago by qwan.
qwan/ursa-major refactor-get-modules  into  master

file modified
+41 -32
@@ -22,6 +22,7 @@ 

  # Written by Chenxiong Qi <cqi@redhat.com>

  #            Qixiang Wan <qwan@redhat.com>

  

+ import math

  import os

  import json

  try:
@@ -106,44 +107,52 @@ 

              return json.load(f)

  

  

- def make_mbs_response(module_builds, per_page=None):

-     """Helper function to make fake MBS response JSON

+ class MockMBSBuildsData(object):

+     def __init__(self, module_builds, per_page=2):

+         self.module_builds = module_builds

+         self.total = len(module_builds)

+         self.per_page = per_page

  

-     When calling this method, developer only needs to pass a list of module

-     builds. Finally, a full response JSON data with only one page data set is

-     returned.

-     """

-     total = len(module_builds)

-     per_page = per_page or total

-     pages = int(total / per_page)

-     if total % per_page > 0:

-         pages += 1

- 

-     def page_url(page_number):

+     def _page_url(self, page_number):

          return (

              'https://mbs.example.com/module-build-service/1/module-builds/'

-             '?per_page={}&page={}'.format(per_page, page_number)

+             '?per_page={}&page={}'.format(self.per_page, page_number)

          )

  

-     result = []

-     offset = 0

+     def get(self, url, **kwargs):

+         params = kwargs.get('params', {})

+         q_page = params.get('page', None)

+         q_per_page = params.get('per_page', None)

  

-     for page in range(1, pages + 1):

-         page_result = {

-             'items': module_builds[offset:offset + per_page],

-             'meta': {

-                 'first': page_url(1), 'prev': None, 'next': None, 'last': page_url(pages),

-                 'total': total, 'per_page': per_page, 'pages': pages, 'page': page

-             }

-         }

+         page = q_page or 1

  

-         meta = page_result['meta']

-         if page - 1 > 0:

-             meta['prev'] = page_url(page - 1)

-         if page + 1 <= pages:

-             meta['next'] = page_url(page + 1)

+         if q_per_page is not None:

+             self.per_page = q_per_page

+         pages = int(math.ceil(float(self.total)/float(self.per_page)))

  

-         offset += per_page

-         result.append(page_result)

+         start = self.per_page * (page - 1)

+         stop = self.per_page * page

+         modules = self.module_builds[start:stop]

  

-     return result

+         next_page_url = None

+         if pages > page:

+             next_page_url = self._page_url(page + 1)

+ 

+         prev_page_url = None

+         if page > 1:

+             prev_page_url = self._page_url(page - 1)

+ 

+         json_data = {

+             'items': modules,

+             'meta': {

+                 'first': self._page_url(1),

+                 'last': self._page_url(pages),

+                 'next': next_page_url,

+                 'page': page,

+                 'pages': pages,

+                 'per_page': self.per_page,

+                 'prev': prev_page_url,

+                 'total': self.total

+             }

+         }

+         return MockResponse(json_data, 200)

file modified
+9 -7
@@ -26,8 +26,8 @@ 

  import tempfile

  import pytest

  

- from mock import patch, Mock

- from tests import TEST_DATA_DIR, make_mbs_response, make_mmd, URSA_MAJOR_TEST_CONFIG

+ from mock import patch

+ from tests import TEST_DATA_DIR, MockMBSBuildsData, make_mmd, URSA_MAJOR_TEST_CONFIG

  from ursa_major import ModuleConfig

  from ursa_major.cli import main

  from ursa_major.handlers.add_module import AddModuleHandler
@@ -92,8 +92,7 @@ 

          ]

          self.koji_session.getTag.side_effect = get_tag_side_effect

  

-         self.mock_get.return_value = Mock(status_code=200)

-         self.mock_get.return_value.json.return_value = make_mbs_response(mock_mbs_modules or [

+         mock_builds = mock_mbs_modules or [

              {

                  'koji_tag': 'module-mariadb-10.4-3020190313091759-a5b0195c',

                  'modulemd': make_mmd(name='mariadb', stream='10.4',
@@ -108,7 +107,10 @@ 

                                       requires={'platform': 'f30'},

                                       buildrequires={'platform': 'f30'}).dumps(),

              },

-         ])[0]

+         ]

+         mock_builds_data = MockMBSBuildsData(mock_builds)

+ 

+         self.mock_get.side_effect = mock_builds_data.get

  

          # For this test, we write an empty tag config file.

          self.write_tag_config_file(tag_configs_content or {})
@@ -484,8 +486,8 @@ 

               'name': 'module-mariadb-10.4-3020190304180835-a5b0195c'},

          ]

  

-         get.return_value = Mock(status_code=200)

-         get.return_value.json.side_effect = make_mbs_response(module_builds)

+         mock_mbs_builds_data = MockMBSBuildsData(module_builds)

+         get.side_effect = mock_mbs_builds_data.get

  

          handler = AddModuleHandler(config=URSA_MAJOR_TEST_CONFIG)

          handler.connect_koji()

@@ -30,7 +30,7 @@ 

  from argparse import Namespace

  

  from tests import UrsaMajorTestCase, MockResponse, make_mmd

- from ursa_major import ModuleConfig, MBS_BUILD_READY_STATE

+ from ursa_major import ModuleConfig, MBS_BUILD_STATES

  from ursa_major.handlers.add_tag import ModuleInfo, AddTagHandler

  from ursa_major.mbs import MBS

  
@@ -154,7 +154,7 @@ 

  

          ci_message = json.dumps(dict(

              id=123,

-             state=MBS_BUILD_READY_STATE,

+             state=MBS_BUILD_STATES['ready'],

              state_name='ready',

              koji_tag='module-123456'

          ))
@@ -219,7 +219,7 @@ 

      def test_skip_module(self, log):

          ci_message = json.dumps(dict(

              id=123,

-             state=MBS_BUILD_READY_STATE,

+             state=MBS_BUILD_STATES['ready'],

              state_name='ready',

              koji_tag='module-d243299e85d7e9aa'

          ))

file modified
+7 -5
@@ -26,8 +26,8 @@ 

  import pytest

  import tempfile

  

- from mock import call, patch, Mock

- from tests import TEST_DATA_DIR, make_mbs_response, make_mmd

+ from mock import call, patch

+ from tests import TEST_DATA_DIR, MockMBSBuildsData, make_mmd

  from ursa_major.cli import main

  

  
@@ -142,8 +142,7 @@ 

              }

          })

  

-         self.mock_get.return_value = Mock(status_code=200)

-         self.mock_get.return_value.json.return_value = make_mbs_response([

+         mock_builds = [

              {

                  'id': 3617,

                  'name': 'mariadb',
@@ -162,7 +161,10 @@ 

                                       requires={'platform': 'f30'},

                                       buildrequires={'platform': 'f30'}).dumps(),

              }

-         ])[0]

+         ]

+ 

+         mock_mbs_builds_data = MockMBSBuildsData(mock_builds)

+         self.mock_get.side_effect = mock_mbs_builds_data.get

  

          self.koji_session.getInheritanceData.return_value = [

              {

@@ -1,428 +0,0 @@ 

- {

-   "items": [

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-23.el8+597+f4559f16",

-             "task_id": 15981597,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.30-3.el8+597+f4559f16",

-             "task_id": 15982006,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-20.el8+597+f4559f16",

-             "task_id": 15981785,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-05-07T10:24:45Z",

-       "version": "20180507102412",

-       "time_modified": "2018-05-07T10:25:55Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#d8cf5ee02cbbd31615bba3b47c85cca6a8ae54a8",

-       "state": 5,

-       "time_completed": "2018-05-07T10:25:38Z",

-       "koji_tag": "module-e5c3306aa4e9db10",

-       "context": "c2c572ec",

-       "owner": "qwan",

-       "id": 604,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-23.el8+597+f4559f16",

-             "task_id": 15981597,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.30-3.el8+597+f4559f16",

-             "task_id": 15982006,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-20.el8+597+f4559f16",

-             "task_id": 15981785,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-05-04T14:42:54Z",

-       "version": "20180504144016",

-       "time_modified": "2018-05-04T14:43:59Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#c1c797f752aac7049b2cfe829f61f47615fd327c",

-       "state": 5,

-       "time_completed": "2018-05-04T14:43:44Z",

-       "koji_tag": "module-1420813c8fdf4ed3",

-       "context": "c2c572ec",

-       "owner": "qwan",

-       "id": 601,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-23.el8+597+f4559f16",

-             "task_id": 15981597,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.30-3.el8+597+f4559f16",

-             "task_id": 15982006,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-20.el8+597+f4559f16",

-             "task_id": 15981785,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-05-04T13:38:15Z",

-       "version": "20180504133734",

-       "time_modified": "2018-05-04T13:39:22Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#c1bc8f60a2312e1ae9a16e4bbdbd3886bfdcd307",

-       "state": 5,

-       "time_completed": "2018-05-04T13:39:06Z",

-       "koji_tag": "module-77608f07f91fbeb4",

-       "context": "c2c572ec",

-       "owner": "qwan",

-       "id": 599,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "module-build-macros": {

-             "state": 1,

-             "nvr": "module-build-macros-0.1-1.el8+597+f4559f16",

-             "task_id": 15981248,

-             "state_reason": ""

-           },

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-23.el8+597+f4559f16",

-             "task_id": 15981597,

-             "state_reason": ""

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.30-3.el8+597+f4559f16",

-             "task_id": 15982006,

-             "state_reason": ""

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-20.el8+597+f4559f16",

-             "task_id": 15981785,

-             "state_reason": ""

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-05-04T11:27:02Z",

-       "version": "20180427135953",

-       "time_modified": "2018-05-04T12:52:16Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#f3b7e3c959df0924c7b42400e53a8b1612d95811",

-       "state": 5,

-       "time_completed": "2018-05-04T12:52:05Z",

-       "koji_tag": "module-f4559f16be91b6a5",

-       "context": "c2c572ec",

-       "owner": "qwan",

-       "id": 597,

-       "rebuild_strategy": "all"

-     },

-     {

-       "state_reason": "Some components failed to build.",

-       "tasks": {

-         "rpms": {

-           "module-build-macros": {

-             "state": 1,

-             "nvr": "module-build-macros-0.1-1.el8+596+6aea3164",

-             "task_id": 15980980,

-             "state_reason": ""

-           },

-           "attr": {

-             "state": 3,

-             "nvr": "attr-None-None",

-             "task_id": 15981181,

-             "state_reason": "Failed to build artifact attr in Koji"

-           },

-           "tar": {

-             "state": 3,

-             "nvr": null,

-             "task_id": null,

-             "state_reason": "Some components failed to build."

-           },

-           "acl": {

-             "state": 3,

-             "nvr": null,

-             "task_id": null,

-             "state_reason": "Some components failed to build."

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-05-04T10:56:17Z",

-       "version": "20180404100637",

-       "time_modified": "2018-05-04T11:27:56Z",

-       "state_name": "failed",

-       "scmurl": "git://git.example.com/modules/testmodule?#47977ee4c33f8f3587ae4d8f6a5d8060f81ff1e6",

-       "state": 4,

-       "time_completed": "2018-05-04T11:27:55Z",

-       "koji_tag": "module-6aea3164a55257fe",

-       "context": "9e5fe74b",

-       "owner": "qwan",

-       "id": 596,

-       "rebuild_strategy": "all"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+340+14075d1c",

-             "task_id": 15400861,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+340+14075d1c",

-             "task_id": 15400952,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+340+14075d1c",

-             "task_id": 15400907,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-03-01T15:12:03Z",

-       "version": "20180301151140",

-       "time_modified": "2018-03-01T15:13:57Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#b013556b0b6d4a9571090b7d6ce7efa501bd5775",

-       "state": 5,

-       "time_completed": "2018-03-01T15:13:38Z",

-       "koji_tag": "module-b24a77a1448f0694",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 345,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "module-build-macros": {

-             "state": 1,

-             "nvr": "module-build-macros-0.1-1.el8+340+14075d1c",

-             "task_id": 15400820,

-             "state_reason": ""

-           },

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+340+14075d1c",

-             "task_id": 15400861,

-             "state_reason": ""

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+340+14075d1c",

-             "task_id": 15400952,

-             "state_reason": ""

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+340+14075d1c",

-             "task_id": 15400907,

-             "state_reason": ""

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-02-24T01:11:00Z",

-       "version": "20180224011047",

-       "time_modified": "2018-02-24T02:27:37Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#7d75e36e6987b21405881d7af2d391528b5b7aa0",

-       "state": 5,

-       "time_completed": "2018-02-24T02:27:26Z",

-       "koji_tag": "module-e264bb31ded5336a",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 340,

-       "rebuild_strategy": "all"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "module-build-macros": {

-             "state": 1,

-             "nvr": "module-build-macros-0.1-1.el8+337+e6ed8a58",

-             "task_id": 15395536,

-             "state_reason": ""

-           },

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+337+e6ed8a58",

-             "task_id": 15396151,

-             "state_reason": ""

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+337+e6ed8a58",

-             "task_id": 15396780,

-             "state_reason": ""

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+337+e6ed8a58",

-             "task_id": 15396348,

-             "state_reason": ""

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-02-23T13:43:25Z",

-       "version": "20180223134306",

-       "time_modified": "2018-02-23T15:17:27Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#186ef95f5c4eb29649939ae217ec110fba23e7eb",

-       "state": 5,

-       "time_completed": "2018-02-23T15:17:17Z",

-       "koji_tag": "module-f64de0dce712108d",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 337,

-       "rebuild_strategy": "all"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+4f3ed7d2",

-             "task_id": 14673135,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+4f3ed7d2",

-             "task_id": 14673557,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+4f3ed7d2",

-             "task_id": 14673370,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-02-23T13:41:23Z",

-       "version": "20180223134116",

-       "time_modified": "2018-02-23T13:42:51Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#aab4447c752e2f76359b2eabdcc5e2af8c67c2c2",

-       "state": 5,

-       "time_completed": "2018-02-23T13:42:38Z",

-       "koji_tag": "module-1ee24721dddcd2b9",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 336,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+4f3ed7d2",

-             "task_id": 14673135,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+4f3ed7d2",

-             "task_id": 14673557,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+4f3ed7d2",

-             "task_id": 14673370,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-02-23T13:02:08Z",

-       "version": "20180223130158",

-       "time_modified": "2018-02-23T13:03:58Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#6be2fbebf30576d7de76506e79db54e18251f5b7",

-       "state": 5,

-       "time_completed": "2018-02-23T13:03:36Z",

-       "koji_tag": "module-0b37bf984472be82",

-       "context": "9e5fe74b",

-       "owner": "jkaluza",

-       "id": 335,

-       "rebuild_strategy": "only-changed"

-     }

-   ],

-   "meta": {

-     "prev": null,

-     "last": "https://mbs.example.com/module-build-service/1/module-builds/?per_page=10&page=3&name=testmodule",

-     "next": "https://mbs.example.com/module-build-service/1/module-builds/?per_page=10&page=2&name=testmodule",

-     "total": 27,

-     "per_page": 10,

-     "first": "https://mbs.example.com/module-build-service/1/module-builds/?per_page=10&page=1&name=testmodule",

-     "pages": 3,

-     "page": 1

-   }

- } 

\ No newline at end of file

@@ -1,374 +0,0 @@ 

- {

-   "items": [

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+4f3ed7d2",

-             "task_id": 14673135,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+4f3ed7d2",

-             "task_id": 14673557,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+4f3ed7d2",

-             "task_id": 14673370,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-02-19T13:11:36Z",

-       "version": "20180219131131",

-       "time_modified": "2018-02-19T13:13:05Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#9a1957e17ff2916c3d12ba024f67082247001a0a",

-       "state": 5,

-       "time_completed": "2018-02-19T13:12:45Z",

-       "koji_tag": "module-497cc21805cc93b1",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 329,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+4f3ed7d2",

-             "task_id": 14673135,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+4f3ed7d2",

-             "task_id": 14673557,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+4f3ed7d2",

-             "task_id": 14673370,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-02-16T16:30:34Z",

-       "version": "20180216163024",

-       "time_modified": "2018-02-16T16:31:38Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#a4a0b178edc6bf4d13a3850cefcfeb229838887f",

-       "state": 5,

-       "time_completed": "2018-02-16T16:31:27Z",

-       "koji_tag": "module-aaf06184c11b5b93",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 327,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+4f3ed7d2",

-             "task_id": 14673135,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+4f3ed7d2",

-             "task_id": 14673557,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+4f3ed7d2",

-             "task_id": 14673370,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-02-14T13:53:57Z",

-       "version": "20180214135348",

-       "time_modified": "2018-02-14T13:55:05Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#d9fbadfc9116394bdfcd118d2837d0c54b1e80be",

-       "state": 5,

-       "time_completed": "2018-02-14T13:54:54Z",

-       "koji_tag": "module-39801baf83d07771",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 325,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+4f3ed7d2",

-             "task_id": 14673135,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+4f3ed7d2",

-             "task_id": 14673557,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+4f3ed7d2",

-             "task_id": 14673370,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-02-10T18:53:07Z",

-       "version": "20180210185249",

-       "time_modified": "2018-02-10T18:54:05Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#11aeb6ec40cd401fb1c4ed2bd4c3f6842f0b6a5c",

-       "state": 5,

-       "time_completed": "2018-02-10T18:53:45Z",

-       "koji_tag": "module-05918a085ec2a30f",

-       "context": "9e5fe74b",

-       "owner": "vvasilev",

-       "id": 319,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+4f3ed7d2",

-             "task_id": 14673135,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+4f3ed7d2",

-             "task_id": 14673557,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+4f3ed7d2",

-             "task_id": 14673370,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-02-07T16:04:48Z",

-       "version": "20180207160431",

-       "time_modified": "2018-02-07T16:07:20Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#6fd8738fbb89bde67778713881d4090c71335a24",

-       "state": 5,

-       "time_completed": "2018-02-07T16:06:49Z",

-       "koji_tag": "module-6754a86de532ca62",

-       "context": "9e5fe74b",

-       "owner": "vvasilev",

-       "id": 313,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": "Canceled by rbean.",

-       "tasks": {},

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-02-01T18:50:16Z",

-       "version": "20180201185002",

-       "time_modified": "2018-02-02T13:49:12Z",

-       "state_name": "failed",

-       "scmurl": "git://git.example.com/modules/testmodule?#4d50c220397b814ad716100b03d69cc263ebd630",

-       "state": 4,

-       "time_completed": "2018-02-02T13:49:12Z",

-       "koji_tag": null,

-       "context": "00000000",

-       "owner": "rbean",

-       "id": 304,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": "Canceled by rbean.",

-       "tasks": {},

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2018-02-01T17:52:42Z",

-       "version": "20180201175226",

-       "time_modified": "2018-02-02T13:54:10Z",

-       "state_name": "failed",

-       "scmurl": "git://git.example.com/modules/testmodule?#a5b86e67259b6090696124b2e003b61c4c5d3215",

-       "state": 4,

-       "time_completed": "2018-02-02T13:54:10Z",

-       "koji_tag": null,

-       "context": "00000000",

-       "owner": "rbean",

-       "id": 303,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "module-build-macros": {

-             "state": 1,

-             "nvr": "module-build-macros-0.1-1.el8+4f3ed7d2",

-             "task_id": 14673044,

-             "state_reason": ""

-           },

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+4f3ed7d2",

-             "task_id": 14673135,

-             "state_reason": ""

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+4f3ed7d2",

-             "task_id": 14673557,

-             "state_reason": ""

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+4f3ed7d2",

-             "task_id": 14673370,

-             "state_reason": ""

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2017-12-04T13:36:34Z",

-       "version": "20171204133622",

-       "time_modified": "2017-12-04T14:33:39Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#205d563f65ec2e99031c7983a4dccb025fe14378",

-       "state": 5,

-       "time_completed": "2017-12-04T14:33:27Z",

-       "koji_tag": "module-4f3ed7d219ce7988",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 272,

-       "rebuild_strategy": "all"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+a13a2d1b",

-             "task_id": 14569923,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+a13a2d1b",

-             "task_id": 14570100,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+a13a2d1b",

-             "task_id": 14569987,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2017-12-04T13:34:55Z",

-       "version": "20171129184413",

-       "time_modified": "2017-12-04T13:35:58Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#cbe9e60ed323257b788936b32dba8815ef6b649d",

-       "state": 5,

-       "time_completed": "2017-12-04T13:35:46Z",

-       "koji_tag": "module-e1039f2d79cdd168",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 271,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "module-build-macros": {

-             "state": 1,

-             "nvr": "module-build-macros-0.1-1.el8+a13a2d1b",

-             "task_id": 14569690,

-             "state_reason": ""

-           },

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+a13a2d1b",

-             "task_id": 14569923,

-             "state_reason": ""

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+a13a2d1b",

-             "task_id": 14570100,

-             "state_reason": ""

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+a13a2d1b",

-             "task_id": 14569987,

-             "state_reason": ""

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2017-11-16T14:09:44Z",

-       "version": "20171116140940",

-       "time_modified": "2017-11-16T15:05:22Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#ec3d511a34887e31540400d125aedbfb1076195a",

-       "state": 5,

-       "time_completed": "2017-11-16T15:05:12Z",

-       "koji_tag": "module-a13a2d1bf0f0c0c8",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 260,

-       "rebuild_strategy": "all"

-     }

-   ],

-   "meta": {

-     "prev": "https://mbs.example.com/module-build-service/1/module-builds/?per_page=10&page=1&name=testmodule",

-     "last": "https://mbs.example.com/module-build-service/1/module-builds/?per_page=10&page=3&name=testmodule",

-     "next": "https://mbs.example.com/module-build-service/1/module-builds/?per_page=10&page=3&name=testmodule",

-     "total": 27,

-     "per_page": 10,

-     "first": "https://mbs.example.com/module-build-service/1/module-builds/?per_page=10&page=1&name=testmodule",

-     "pages": 3,

-     "page": 2

-   }

- } 

\ No newline at end of file

@@ -1,299 +0,0 @@ 

- {

-   "items": [

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+d1728e3a",

-             "task_id": 14516025,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+e0edb787",

-             "task_id": 14147157,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+e0edb787",

-             "task_id": 14147087,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2017-11-16T13:46:17Z",

-       "version": "20171116134224",

-       "time_modified": "2017-11-16T13:47:17Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#008852fece29968e369e2b9497b8fe559453b366",

-       "state": 5,

-       "time_completed": "2017-11-16T13:47:04Z",

-       "koji_tag": "module-710588a4a48dcf1e",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 259,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "module-build-macros": {

-             "state": 1,

-             "nvr": "module-build-macros-0.1-1.el8+d1728e3a",

-             "task_id": 14515538,

-             "state_reason": ""

-           },

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+d1728e3a",

-             "task_id": 14516025,

-             "state_reason": ""

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+e0edb787",

-             "task_id": 14147157,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+e0edb787",

-             "task_id": 14147087,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2017-11-09T14:16:13Z",

-       "version": "20171109141604",

-       "time_modified": "2017-11-09T14:57:59Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#4c68d6ea9d0adade86f50109831881eed1b3a28a",

-       "state": 5,

-       "time_completed": "2017-11-09T14:57:47Z",

-       "koji_tag": "module-d1728e3a712793dd",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 255,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+e0edb787",

-             "task_id": 14147005,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+e0edb787",

-             "task_id": 14147157,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+e0edb787",

-             "task_id": 14147087,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2017-11-09T14:13:58Z",

-       "version": "20171109141304",

-       "time_modified": "2017-11-09T14:14:48Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#5f93ee37640cfab94f823f946867c2b7c1558a38",

-       "state": 5,

-       "time_completed": "2017-11-09T14:14:37Z",

-       "koji_tag": "module-03d4509d955ec7a8",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 254,

-       "rebuild_strategy": "only-changed"

-     },

-     {

-       "state_reason": "The module was garbage collected since it has failed over 180 day(s) ago",

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+e0edb787",

-             "task_id": 14147005,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+e0edb787",

-             "task_id": 14147157,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+e0edb787",

-             "task_id": 14147087,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2017-11-07T15:07:07Z",

-       "version": "20171107150639",

-       "time_modified": "2018-05-06T15:13:50Z",

-       "state_name": "garbage",

-       "scmurl": "git://git.example.com/modules/testmodule?#fd25dcceaa39d8bb51b5f787fe7f42eddf24dc0a",

-       "state": 6,

-       "time_completed": "2017-11-07T15:08:03Z",

-       "koji_tag": "module-e69b35bdf9852273",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 247,

-       "rebuild_strategy": "changed-and-after"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+e0edb787",

-             "task_id": 14147005,

-             "state_reason": "Reused component from previous module build"

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+e0edb787",

-             "task_id": 14147157,

-             "state_reason": "Reused component from previous module build"

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+e0edb787",

-             "task_id": 14147087,

-             "state_reason": "Reused component from previous module build"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2017-11-07T15:04:45Z",

-       "version": "20171106160301",

-       "time_modified": "2017-11-07T15:05:29Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#d338a77f3cba04f1be0d399f495cdfc71465f6af",

-       "state": 5,

-       "time_completed": "2017-11-07T15:05:19Z",

-       "koji_tag": "module-536a1b6e7e6add45",

-       "context": "9e5fe74b",

-       "owner": "mprahl",

-       "id": 246,

-       "rebuild_strategy": "changed-and-after"

-     },

-     {

-       "state_reason": null,

-       "tasks": {

-         "rpms": {

-           "module-build-macros": {

-             "state": 1,

-             "nvr": "module-build-macros-0.1-1.el8+e0edb787",

-             "task_id": 14146931,

-             "state_reason": ""

-           },

-           "attr": {

-             "state": 1,

-             "nvr": "attr-2.4.47-21.el8+e0edb787",

-             "task_id": 14147005,

-             "state_reason": ""

-           },

-           "tar": {

-             "state": 1,

-             "nvr": "tar-1.29-7.el8+e0edb787",

-             "task_id": 14147157,

-             "state_reason": ""

-           },

-           "acl": {

-             "state": 1,

-             "nvr": "acl-2.2.52-18.el8+e0edb787",

-             "task_id": 14147087,

-             "state_reason": ""

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2017-09-29T17:26:58Z",

-       "version": "20170929172622",

-       "time_modified": "2017-09-29T18:21:40Z",

-       "state_name": "ready",

-       "scmurl": "git://git.example.com/modules/testmodule?#711dc768515ac61f0937e1a2e61b685fc55c0075",

-       "state": 5,

-       "time_completed": "2017-09-29T18:21:28Z",

-       "koji_tag": "module-e0edb7870a81b3c5",

-       "context": "9e5fe74b",

-       "owner": "mikeb",

-       "id": 2,

-       "rebuild_strategy": "changed-and-after"

-     },

-     {

-       "state_reason": "The module was garbage collected since it has failed over 180 day(s) ago",

-       "tasks": {

-         "rpms": {

-           "ed": {

-             "state": 1,

-             "nvr": "ed-1.14.1-4.el8+358b0c17",

-             "task_id": 14146599,

-             "state_reason": ""

-           },

-           "module-build-macros": {

-             "state": 1,

-             "nvr": "module-build-macros-0.1-1.el8+358b0c17",

-             "task_id": 14146061,

-             "state_reason": "Build already exists."

-           },

-           "mksh": {

-             "state": 3,

-             "nvr": "mksh-None-None",

-             "task_id": 14146689,

-             "state_reason": "Failed to build artifact mksh in Koji"

-           }

-         }

-       },

-       "name": "testmodule",

-       "stream": "rhel-8.0",

-       "time_submitted": "2017-09-29T13:37:20Z",

-       "version": "20170928233954",

-       "time_modified": "2018-03-28T17:09:17Z",

-       "state_name": "garbage",

-       "scmurl": "git://git.example.com/modules/testmodule?#53d7992b6e5c154558ff7592ec455392b2c6ff77",

-       "state": 6,

-       "time_completed": "2017-09-29T17:02:34Z",

-       "koji_tag": "module-358b0c179d25310c",

-       "context": "9e5fe74b",

-       "owner": "mikeb",

-       "id": 1,

-       "rebuild_strategy": "changed-and-after"

-     }

-   ],

-   "meta": {

-     "prev": "https://mbs.example.com/module-build-service/1/module-builds/?per_page=10&page=2&name=testmodule",

-     "last": "https://mbs.example.com/module-build-service/1/module-builds/?per_page=10&page=3&name=testmodule",

-     "next": null,

-     "total": 27,

-     "per_page": 10,

-     "first": "https://mbs.example.com/module-build-service/1/module-builds/?per_page=10&page=1&name=testmodule",

-     "pages": 3,

-     "page": 3

-   }

- } 

\ No newline at end of file

file modified
+32 -47
@@ -24,66 +24,51 @@ 

  

  import mock

  

- from tests import UrsaMajorTestCase, MockResponse, make_mbs_response

+ from tests import UrsaMajorTestCase, MockResponse, MockMBSBuildsData

  from ursa_major.mbs import MBS

  

  

  class TestMBS(UrsaMajorTestCase):

-     def _mock_requests_get_for_modules_list(self, url, **kwargs):

-         params = kwargs.get('params', {})

-         q_name = params.get('name', None)

-         if q_name == 'testmodule':

-             page = params.get('page', None)

-             if page is None or page == '1':

-                 filename = 'mbs_modules_testmodule_page1.json'

-             elif page == '2':

-                 filename = 'mbs_modules_testmodule_page2.json'

-             elif page == '3':

-                 filename = 'mbs_modules_testmodule_page3.json'

-             else:

-                 raise RuntimeError('Unsupported page in mock helper')

-             json_data = self.load_json_from_file(filename)

-             return MockResponse(json_data, 200)

-         return MockResponse({}, 404)

- 

      @mock.patch('ursa_major.mbs.requests.get')

-     def test_get_modules_without_auto_next_page(self, get):

-         get.return_value = mock.Mock(status_code=200)

-         get.return_value.json.side_effect = make_mbs_response([

-             # First page

-             {'id': 1, 'name': 'mariadb', 'stream': '10.4'},

-             {'id': 2, 'name': 'mariadb', 'stream': '10.3'},

-             # Second page

-             {'id': 3, 'name': 'ant', 'stream': '1.10'},

-             {'id': 4, 'name': 'fish', 'stream': '3'},

-             # Third page

-             {'id': 5, 'name': 'ruby', 'stream': 'master'},

-         ], per_page=2)

+     def test_get_modules_with_no_page_releated_params(self, get):

+         mock_builds = [

+             {'id': 1, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '123', 'state': 5},

+             {'id': 2, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '234', 'state': 5},

+             {'id': 3, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '456', 'state': 5},

+             {'id': 4, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '789', 'state': 5},

+             {'id': 5, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '012', 'state': 5},

+         ]

+ 

+         mock_data = MockMBSBuildsData(mock_builds)

+ 

+         get.side_effect = mock_data.get

  

          mbs = MBS('http://mbs.example.com')

-         modules = mbs.get_modules(auto_next_page=False, name='mariadb')

+         modules = mbs.get_modules(name='mariadb')

  

-         expected = [

-             {'id': 1, 'name': 'mariadb', 'stream': '10.4'},

-             {'id': 2, 'name': 'mariadb', 'stream': '10.3'},

+         self.assertEqual(modules, mock_builds)

+ 

+     @mock.patch('ursa_major.mbs.requests.get')

+     def test_get_modules_with_page_related_params(self, get):

+         mock_builds = [

+             {'id': 1, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '123', 'state': 5},

+             {'id': 2, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '234', 'state': 5},

+             {'id': 3, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '456', 'state': 5},

+             {'id': 4, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '789', 'state': 5},

+             {'id': 5, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '012', 'state': 5},

          ]

  

-         self.assertEqual(modules, expected)

+         mock_data = MockMBSBuildsData(mock_builds)

  

-     @mock.patch('ursa_major.mbs.requests.get')

-     def test_get_modules_with_auto_next_page(self, get):

-         get.side_effect = self._mock_requests_get_for_modules_list

+         get.side_effect = mock_data.get

  

          mbs = MBS('http://mbs.example.com')

-         modules = mbs.get_modules(auto_next_page=True, name='testmodule')

- 

-         data1 = self.load_json_from_file('mbs_modules_testmodule_page1.json')

-         data2 = self.load_json_from_file('mbs_modules_testmodule_page2.json')

-         data3 = self.load_json_from_file('mbs_modules_testmodule_page3.json')

-         expected = []

-         expected.extend(data1['items'])

-         expected.extend(data2['items'])

-         expected.extend(data3['items'])

+         modules = mbs.get_modules(name='mariadb', page=1, per_page=2)

+ 

+         expected = [

+             {'id': 1, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '123', 'state': 5},

+             {'id': 2, 'name': 'mariadb', 'stream': '10.4', 'koji_tag': '234', 'state': 5},

+         ]

  

          self.assertEqual(modules, expected)

  

file modified
+5 -4
@@ -27,7 +27,7 @@ 

  

  from mock import patch, Mock

  from ursa_major.cli import main

- from tests import TEST_DATA_DIR, make_mbs_response, make_mmd

+ from tests import TEST_DATA_DIR, MockMBSBuildsData, make_mmd

  

  

  class TestRemoveModule:
@@ -112,8 +112,7 @@ 

              'name': 'module-mariadb-10.4-3020190304180835-a5b0195c',

          }

  

-         get.return_value = Mock(status_code=200)

-         get.return_value.json.return_value = make_mbs_response([

+         mock_builds = [

              {

                  'koji_tag': 'module-mariadb-10.4-3020190313091759-a5b0195c',

                  'modulemd': make_mmd(name='mariadb', stream='10.4',
@@ -129,7 +128,9 @@ 

                                       requires={'platform': 'f30'},

                                       buildrequires={'platform': 'f30'}).dumps(),

              }

-         ])[0]

+         ]

+         mock_mbs_builds_data = MockMBSBuildsData(mock_builds)

+         get.side_effect = mock_mbs_builds_data.get

  

          cmd = [

              '--name', 'mariadb', '--stream', '10.4',

file modified
+10 -1
@@ -21,7 +21,16 @@ 

  #

  # Written by Chenxiong Qi <cqi@redhat.com>

  

- MBS_BUILD_READY_STATE = 5

+ # Copied from MBS code

+ MBS_BUILD_STATES = {

+     "init": 0,

+     "wait": 1,

+     "build": 2,

+     "done": 3,

+     "failed": 4,

+     "ready": 5,

+     "garbage": 6,

+ }

  

  

  class ModuleConfigCmp(object):

@@ -23,7 +23,7 @@ 

  #            Qixiang Wan <qwan@redhat.com>

  

  from ursa_major.handlers.base import BaseHandler

- from ursa_major import ModuleConfig, ModuleConfigCmp, MBS_BUILD_READY_STATE

+ from ursa_major import ModuleConfig, ModuleConfigCmp, MBS_BUILD_STATES

  from ursa_major.logger import log

  

  
@@ -289,7 +289,8 @@ 

              requires=module_config.requires,

              name=module_config.name,

              stream=module_config.stream,

-             state=MBS_BUILD_READY_STATE)

+             state=MBS_BUILD_STATES['ready'],

+             page=1)  # we only need the default first page items

          if modules:

              return modules[0]['koji_tag']

          else:

file modified
+2 -3
@@ -27,7 +27,7 @@ 

  from ursa_major.koji_service import KojiService

  from ursa_major.logger import log

  from ursa_major.mbs import MBS

- from ursa_major import MBS_BUILD_READY_STATE

+ from ursa_major import MBS_BUILD_STATES

  

  

  class BaseHandler(object):
@@ -118,7 +118,6 @@ 

          modules = self.mbs.get_modules(

              name=module_config.name,

              stream=module_config.stream,

-             state=MBS_BUILD_READY_STATE,

-             auto_next_page=True)  # only ready modules

+             state=[MBS_BUILD_STATES['ready'], MBS_BUILD_STATES['garbage']])

          tags_from_mbs = set(module['koji_tag'] for module in modules)

          return list(set(tags_from_koji) & set(tags_from_mbs))

@@ -26,7 +26,7 @@ 

  import sys

  import json

  

- from ursa_major import MBS_BUILD_READY_STATE

+ from ursa_major import MBS_BUILD_STATES

  from ursa_major.handlers.base import BaseHandler

  from ursa_major.logger import log

  
@@ -116,14 +116,24 @@ 

                      buildrequires=module_config.get('buildrequires'),

                      name=module_config['name'],

                      stream=module_config['stream'],

-                     state=MBS_BUILD_READY_STATE,

+                     state=MBS_BUILD_STATES['ready'],

+                     page=1,  # we only need the default first page items

                  )

  

                  if not (modules_of_config or self.allow_non_ready_module):

                      log.error("Can't find ready modules in MBS with config: %s", module_config)

                      continue

  

-                 module_tags = [m['koji_tag'] for m in modules_of_config]

+                 # checking existing NAME+STREAM modules, don't include criteria

+                 # of requires or buildrequires, and not limit to ready modules as

+                 # some old tags may be garbaged

+                 existing_modules = self.mbs.get_modules(

+                     name=module_config['name'],

+                     stream=module_config['stream'],

+                     state=[MBS_BUILD_STATES['ready'], MBS_BUILD_STATES['garbage']]

+                 )

+                 module_tags = [m['koji_tag'] for m in existing_modules]

+ 

                  non_priorities = [t['priority'] for t in tag_inheritance

                                    if t['name'] not in module_tags]

                  if module_config['priority'] in non_priorities:

file modified
+29 -37
@@ -23,10 +23,6 @@ 

  #            Qixiang Wan <qwan@redhat.com>

  

  import requests

- try:

-     from urllib import parse as urlparse

- except ImportError:

-     import urlparse

  

  from ursa_major.utils import load_mmd, mmd_has_requires, mmd_has_buildrequires

  
@@ -39,33 +35,6 @@ 

              self.server_url.rstrip('/') +

              '/module-build-service/{!s}/module-builds/'.format(api_version))

  

-     def _get_modules(self, auto_next_page=True, **params):

-         """

-         Query MBS modules with specified query parameters.

- 

-         :param auto_next_page: read next page items automatically if True

-         :param params: query parameters in keyword arguments

-         :return: a list of modules

-         """

-         resp = requests.get(self.module_builds_api_url, params=params)

-         if resp.status_code != 200:

-             resp.raise_for_status()

- 

-         resp_data = resp.json()

- 

-         modules = resp_data['items']

-         if not auto_next_page:

-             return modules

- 

-         next_page = resp_data['meta'].get('next', None)

-         if next_page is not None:

-             parsed_url = urlparse.urlparse(next_page)

-             params = dict(urlparse.parse_qsl(parsed_url.query))

-             next_modules = self.get_modules(auto_next_page=True, **params)

-             modules.extend(next_modules)

- 

-         return modules

- 

      def get_module_mmd(self, build_id):

          """

          Create and return Modulemd.Module object for a module build.
@@ -113,17 +82,41 @@ 

          # ourselves.

          requires = kwargs.pop('requires', None)

          buildrequires = kwargs.pop('buildrequires', None)

-         auto_next_page = kwargs.pop('auto_next_page', False)

+         page = kwargs.get('page', None)

+         per_page = kwargs.get('per_page', None)

  

          inspect_modulemd = False

+ 

+         # get module builds from all pages by default

+         all_pages = True

+         # if page or per_page in query params, just get modules as requested

+         if not (page is None and per_page is None):

+             all_pages = False

+ 

+         # we need modulemd if requires or buildrequires is in query params

          if requires or buildrequires:

              inspect_modulemd = True

- 

-         # we need modulemd in result, so set verbose to true

-         if inspect_modulemd:

              kwargs.update({'verbose': 'true'})

  

-         modules = self._get_modules(auto_next_page=auto_next_page, **kwargs)

+         resp = requests.get(self.module_builds_api_url, params=kwargs)

+         if resp.status_code != 200:

+             resp.raise_for_status()

+ 

+         resp_data = resp.json()

+         modules = resp_data['items']

+         result_pages = resp_data['meta']['pages']

+         result_total = resp_data['meta']['total']

+ 

+         # the module builds are in multiple pages and we want all

+         # modules, query with per_page = result_total

+         if all_pages and result_pages > 1:

+             kwargs['per_page'] = result_total

+             resp = requests.get(self.module_builds_api_url, params=kwargs)

+             if resp.status_code != 200:

+                 resp.raise_for_status()

+ 

+             resp_data = resp.json()

+             modules = resp_data['items']

  

          if inspect_modulemd:

              matched = []
@@ -137,5 +130,4 @@ 

                  if b:

                      matched.append(module)

              return matched

- 

          return modules