Skip to content

Commit

Permalink
Handle connection problems when talking to Wiki.
Browse files Browse the repository at this point in the history
fix fedora-infra#3361

Signed-off-by: Sebastian Wojciechowski <s.wojciechowski89@gmail.com>
  • Loading branch information
sebwoj committed Jul 7, 2019
1 parent 78d31f1 commit fdba126
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
5 changes: 4 additions & 1 deletion bodhi/server/consumers/updates.py
Expand Up @@ -134,7 +134,10 @@ def fetch_test_cases(self, session, update):
cases for their associated Packages..
"""
for build in update.builds:
build.package.fetch_test_cases(session)
try:
build.package.fetch_test_cases(session)
except BodhiException:
log.warning('Error occurred during fetching testcases', exc_info=True)

def work_on_bugs(self, session, update, bugs):
"""
Expand Down
8 changes: 7 additions & 1 deletion bodhi/server/models.py
Expand Up @@ -27,6 +27,7 @@
import time
import typing
import uuid
from urllib.error import URLError

from simplemediawiki import MediaWiki
from sqlalchemy import (and_, Boolean, Column, DateTime, event, ForeignKey,
Expand Down Expand Up @@ -1089,6 +1090,8 @@ def fetch_test_cases(self, db):
Args:
db (sqlalchemy.orm.session.Session): A database session.
Raises:
BodhiException: When retrieving testcases from Wiki failed.
"""
if not config.get('query_wiki_test_cases'):
return
Expand All @@ -1103,7 +1106,10 @@ def list_categorymembers(wiki, cat_page, limit=500):
# Build query arguments and call wiki
query = dict(action='query', list='categorymembers',
cmtitle=cat_page, cmlimit=limit)
response = wiki.call(query)
try:
response = wiki.call(query)
except URLError:
raise BodhiException('Failed retrieving testcases from Wiki')
members = [entry['title'] for entry in
response.get('query', {}).get('categorymembers', {})
if 'title' in entry]
Expand Down
22 changes: 22 additions & 0 deletions bodhi/tests/server/consumers/test_updates.py
Expand Up @@ -20,6 +20,7 @@
from unittest import mock
import copy
import unittest
from urllib.error import URLError

from fedora_messaging.api import Message
import sqlalchemy
Expand Down Expand Up @@ -381,3 +382,24 @@ def test_work_on_bugs_exception(self, warning):
h.work_on_bugs(h.db_factory, update, bugs)

warning.assert_called_once_with('Error occurred during updating single bug', exc_info=True)


class TestUpdatesHandlerFetchTestCases(base.BaseTestCase):
"""This test class contains tests for the UpdatesHandler.fetch_test_cases() method."""

@mock.patch.dict(config.config, {'query_wiki_test_cases': True})
@mock.patch('bodhi.server.models.MediaWiki')
@mock.patch('bodhi.server.consumers.updates.log.warning')
def test_fetch_test_cases_exception(self, warning, MediaWiki):
"""
Assert that fetch_test_cases logs a warning when an exception is raised.
"""
h = updates.UpdatesHandler()
h.db_factory = base.TransactionalSessionMaker(self.Session)
MediaWiki.return_value.call.side_effect = URLError("oh no!")):

update = self.db.query(models.Update).filter(
models.Build.nvr == 'bodhi-2.0-1.fc17').one()
h.fetch_test_cases(h.db_factory, update)

warning.assert_called_once_with('Error occurred during fetching testcases', exc_info=True)
13 changes: 13 additions & 0 deletions bodhi/tests/server/test_models.py
Expand Up @@ -24,6 +24,7 @@
import time
import unittest
import uuid
from urllib.error import URLError

from fedora_messaging.testing import mock_sends
from pyramid.testing import DummyRequest
Expand Down Expand Up @@ -1230,6 +1231,18 @@ def test_wiki_test_cases_recursive(self, MediaWiki):
{'Does Bodhi eat +1s', 'Fake', 'Uploading cat pictures'})
self.assertEqual({tc.package.name for tc in model.TestCase.query.all()}, {'gnome-shell'})

@mock.patch.dict(config, {'query_wiki_test_cases': True})
@mock.patch('bodhi.server.models.MediaWiki')
def test_wiki_test_cases_exception(self, MediaWiki):
"""Test querying the wiki for test cases when connection to Wiki failed"""
MediaWiki.return_value.call.side_effect = URLError("oh no!")

with self.assertRaises(BodhiException) as exc_context:
pkg = model.RpmPackage(name='gnome-shell')
pkg.fetch_test_cases(self.db)
self.assertEqual(len(pkg.test_cases), 0)
self.assertEqual(str(exc_context.exception), 'Failed retrieving testcases from Wiki')

def test_adding_modulebuild(self):
"""Assert that validation fails when adding a ModuleBuild."""
build1 = model.RpmBuild(nvr='the-greatest-package-1.0.0-fc17.1')
Expand Down

0 comments on commit fdba126

Please sign in to comment.