diff --git a/bodhi/server/__init__.py b/bodhi/server/__init__.py index 0f661d423a..ee3a8fb741 100644 --- a/bodhi/server/__init__.py +++ b/bodhi/server/__init__.py @@ -29,7 +29,7 @@ from sqlalchemy.orm import scoped_session, sessionmaker from zope.sqlalchemy import ZopeTransactionExtension -from bodhi.server import buildsys, ffmarkdown +from bodhi.server import bugs, buildsys, ffmarkdown log = logging.getLogger(__name__) @@ -123,7 +123,8 @@ def exception_filter(response, request): def main(global_config, testing=None, session=None, **settings): """ This function returns a WSGI application """ - # Setup our buildsystem + # Setup our bugtracker and buildsystem + bugs.set_bugtracker() buildsys.setup_buildsystem(settings) # Sessions & Caching diff --git a/bodhi/server/bugs.py b/bodhi/server/bugs.py index cfc1d03f1d..57278b1575 100644 --- a/bodhi/server/bugs.py +++ b/bodhi/server/bugs.py @@ -22,6 +22,7 @@ from bodhi.server.config import config +bugtracker = None log = logging.getLogger('bodhi') @@ -176,9 +177,14 @@ def modified(self, bug_id): log.exception("Unable to alter bug #%d" % bug_id) -if config.get('bugtracker') == 'bugzilla': - log.info('Using python-bugzilla') - bugtracker = Bugzilla() -else: - log.info('Using the FakeBugTracker') - bugtracker = FakeBugTracker() +def set_bugtracker(): + """ + Set the module-level bugtracker attribute to the correct bugtracker, based on the config. + """ + global bugtracker + if config.get('bugtracker') == 'bugzilla': + log.info('Using python-bugzilla') + bugtracker = Bugzilla() + else: + log.info('Using the FakeBugTracker') + bugtracker = FakeBugTracker() diff --git a/bodhi/server/consumers/masher.py b/bodhi/server/consumers/masher.py index 38e1eec390..a2384c33d8 100644 --- a/bodhi/server/consumers/masher.py +++ b/bodhi/server/consumers/masher.py @@ -36,7 +36,7 @@ from sqlalchemy import engine_from_config import fedmsg.consumers -from bodhi.server import log, buildsys, notifications, mail, util +from bodhi.server import bugs, log, buildsys, notifications, mail, util from bodhi.server.config import config from bodhi.server.exceptions import BodhiException from bodhi.server.metadata import ExtendedMetadata @@ -126,6 +126,7 @@ def __init__(self, hub, db_factory=None, mash_dir=config.get('mash_dir'), self.db_factory = db_factory buildsys.setup_buildsystem(config) + bugs.set_bugtracker() self.mash_dir = mash_dir prefix = hub.config.get('topic_prefix') env = hub.config.get('environment') diff --git a/bodhi/server/consumers/updates.py b/bodhi/server/consumers/updates.py index b98825f014..a092df7146 100644 --- a/bodhi/server/consumers/updates.py +++ b/bodhi/server/consumers/updates.py @@ -36,7 +36,7 @@ import fedmsg.consumers -from bodhi.server.bugs import bugtracker +from bodhi.server import bugs as bug_module from bodhi.server.config import config from bodhi.server.exceptions import BodhiException from bodhi.server.models import Bug, Update, UpdateType @@ -67,6 +67,8 @@ def __init__(self, hub, *args, **kwargs): self.handle_bugs = bool(config.get('bodhi_email')) if not self.handle_bugs: log.warn("No bodhi_email defined; not fetching bug details") + else: + bug_module.set_bugtracker() super(UpdatesHandler, self).__init__(hub, *args, **kwargs) log.info('Bodhi updates handler listening on:\n' @@ -123,7 +125,7 @@ def work_on_bugs(self, session, update, bugs): for bug in bugs: log.info("Getting RHBZ bug %r" % bug.bug_id) try: - rhbz_bug = bugtracker.getbug(bug.bug_id) + rhbz_bug = bug_module.bugtracker.getbug(bug.bug_id) log.info("Updating our details for %r" % bug.bug_id) bug.update_details(rhbz_bug) diff --git a/bodhi/server/models.py b/bodhi/server/models.py index 671c2c0c93..bacba3b584 100644 --- a/bodhi/server/models.py +++ b/bodhi/server/models.py @@ -46,7 +46,7 @@ from sqlalchemy.types import SchemaType, TypeDecorator, Enum from pyramid.settings import asbool -from bodhi.server import buildsys, mail, notifications, log +from bodhi.server import bugs, buildsys, mail, notifications, log from bodhi.server.util import ( header, build_evr, get_nvr, flash_log, get_age, get_critpath_pkgs, get_rpm_header, get_age_in_days, avatar as get_avatar, tokenize, @@ -54,7 +54,6 @@ import bodhi.server.util from bodhi.server.exceptions import BodhiException, LockedUpdateException from bodhi.server.config import config -from bodhi.server.bugs import bugtracker from bodhi.server.util import transactional_session_maker @@ -2184,7 +2183,7 @@ def update_details(self, bug=None): This is typically called "offline" in the UpdatesHandler consumer. """ - bugtracker.update_details(bug, self) + bugs.bugtracker.update_details(bug, self) def default_message(self, update): message = config['stable_bug_msg'] % ( @@ -2210,7 +2209,7 @@ def add_comment(self, update, comment=None): if not comment: comment = self.default_message(update) log.debug("Adding comment to Bug #%d: %s" % (self.bug_id, comment)) - bugtracker.comment(self.bug_id, comment) + bugs.bugtracker.comment(self.bug_id, comment) def testing(self, update): """ @@ -2222,7 +2221,7 @@ def testing(self, update): log.debug('Not modifying on parent security bug %s', self.bug_id) else: comment = self.default_message(update) - bugtracker.on_qa(self.bug_id, comment) + bugs.bugtracker.on_qa(self.bug_id, comment) def close_bug(self, update): # Build a mapping of package names to build versions @@ -2230,14 +2229,14 @@ def close_bug(self, update): versions = dict([ (get_nvr(b.nvr)[0], b.nvr) for b in update.builds ]) - bugtracker.close(self.bug_id, versions=versions, comment=self.default_message(update)) + bugs.bugtracker.close(self.bug_id, versions=versions, comment=self.default_message(update)) def modified(self, update): """ Change the status of this bug to MODIFIED """ if update.type is UpdateType.security and self.parent: log.debug('Not modifying on parent security bug %s', self.bug_id) else: - bugtracker.modified(self.bug_id) + bugs.bugtracker.modified(self.bug_id) user_group_table = Table('user_group_table', Base.metadata, diff --git a/bodhi/tests/server/base.py b/bodhi/tests/server/base.py index 2824426ea2..429253ea9d 100644 --- a/bodhi/tests/server/base.py +++ b/bodhi/tests/server/base.py @@ -26,7 +26,7 @@ import requests import transaction -from bodhi.server import log, models +from bodhi.server import bugs, log, models from bodhi.tests.server import create_update, populate @@ -51,6 +51,7 @@ class BaseTestCase(unittest.TestCase): def setUp(self): + bugs.set_bugtracker() engine = create_engine(DB_PATH) # We want a special session that lasts longer than a transaction self.Session = scoped_session( diff --git a/bodhi/tests/server/consumers/test_masher.py b/bodhi/tests/server/consumers/test_masher.py index 4c443d02d5..ef20ba2dfa 100644 --- a/bodhi/tests/server/consumers/test_masher.py +++ b/bodhi/tests/server/consumers/test_masher.py @@ -144,6 +144,15 @@ def set_stable_request(self, title): update.request = UpdateRequest.stable session.flush() + @mock.patch('bodhi.server.consumers.masher.bugs.set_bugtracker') + def test___init___sets_bugtracker(self, set_bugtracker): + """ + Assert that Masher.__init__() calls bodhi.server.bugs.set_bugtracker(). + """ + Masher(FakeHub(), db_factory=self.db_factory, mash_dir=self.tempdir) + + set_bugtracker.assert_called_once_with() + @mock.patch('bodhi.server.notifications.publish') def test_invalid_signature(self, publish): """Make sure the masher ignores messages that aren't signed with the diff --git a/bodhi/tests/server/consumers/test_updates.py b/bodhi/tests/server/consumers/test_updates.py index ece80cf15f..4ce06e4192 100644 --- a/bodhi/tests/server/consumers/test_updates.py +++ b/bodhi/tests/server/consumers/test_updates.py @@ -231,7 +231,8 @@ def test_super___init___called(self, __init__): __init__.assert_called_once_with(hub) - def test_typical_config(self): + @mock.patch('bodhi.server.consumers.updates.bug_module.set_bugtracker') + def test_typical_config(self, set_bugtracker): """ Test the method with a typical config. """ @@ -248,3 +249,4 @@ def test_typical_config(self): h.topic, ['topic_prefix.environment.bodhi.update.request.testing', 'topic_prefix.environment.bodhi.update.edit']) + set_bugtracker.assert_called_once_with() diff --git a/bodhi/tests/server/test___init__.py b/bodhi/tests/server/test___init__.py new file mode 100644 index 0000000000..497ef65bce --- /dev/null +++ b/bodhi/tests/server/test___init__.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- + +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +"""This test suite contains tests for bodhi.server.__init__.""" +import mock + +from bodhi import server +from bodhi.tests.server.functional import base + + +class TestMain(base.BaseWSGICase): + """ + Assert correct behavior from the main() function. + """ + @mock.patch('bodhi.server.bugs.set_bugtracker') + def test_calls_set_bugtracker(self, set_bugtracker): + """ + Ensure that main() calls set_bugtracker(). + """ + server.main({}, testing='guest', session=self.db, **self.app_settings) + + set_bugtracker.assert_called_once_with() diff --git a/bodhi/tests/server/test_bugs.py b/bodhi/tests/server/test_bugs.py index c6fbafd479..2a6ab0fa23 100644 --- a/bodhi/tests/server/test_bugs.py +++ b/bodhi/tests/server/test_bugs.py @@ -219,3 +219,28 @@ def test___noop__(self, debug): bt.__noop__(1, 2) debug.assert_called_once_with('__noop__((1, 2))') + + +class TestSetBugtracker(unittest.TestCase): + """ + Test the set_bugtracker() function. + """ + @mock.patch('bodhi.server.bugs.bugtracker', None) + @mock.patch.dict('bodhi.server.bugs.config', {'bugtracker': 'bugzilla'}) + def test_config_bugzilla(self): + """ + Test when the config is set for bugzilla to be the bugtracker. + """ + bugs.set_bugtracker() + + self.assertTrue(isinstance(bugs.bugtracker, bugs.Bugzilla)) + + @mock.patch('bodhi.server.bugs.bugtracker', None) + @mock.patch.dict('bodhi.server.bugs.config', {'bugtracker': 'fake'}) + def test_config_not_bugzilla(self): + """ + Test when the config is no set for bugzilla to be the bugtracker. + """ + bugs.set_bugtracker() + + self.assertTrue(isinstance(bugs.bugtracker, bugs.FakeBugTracker)) diff --git a/bodhi/tests/server/test_models.py b/bodhi/tests/server/test_models.py index bf64940810..3da504f5fa 100644 --- a/bodhi/tests/server/test_models.py +++ b/bodhi/tests/server/test_models.py @@ -28,7 +28,7 @@ import cornice import mock -from bodhi.server import models as model, buildsys, mail, util +from bodhi.server import models as model, bugs, buildsys, mail, util from bodhi.server.config import config from bodhi.server.exceptions import BodhiException from bodhi.server.models import ( @@ -47,6 +47,7 @@ class ModelTest(object): attrs = {} def setUp(self): + bugs.set_bugtracker() buildsys.setup_buildsystem({'buildsystem': 'dev'}) engine = create_engine('sqlite://') Session = scoped_session( @@ -314,8 +315,8 @@ def test_days_to_stable_not_meets_testing_requirements_with_date_testing(self): eq_(update.days_to_stable, 3) - @mock.patch('bodhi.server.models.bugtracker.close') - @mock.patch('bodhi.server.models.bugtracker.comment') + @mock.patch('bodhi.server.models.bugs.bugtracker.close') + @mock.patch('bodhi.server.models.bugs.bugtracker.comment') def test_modify_bugs_stable_close(self, comment, close): """Test the modify_bugs() method with a stable status and with close_bugs set to True.""" update = self.get_update() @@ -341,8 +342,8 @@ def test_modify_bugs_stable_close(self, comment, close): for c in close.mock_calls]), True) - @mock.patch('bodhi.server.models.bugtracker.close') - @mock.patch('bodhi.server.models.bugtracker.comment') + @mock.patch('bodhi.server.models.bugs.bugtracker.close') + @mock.patch('bodhi.server.models.bugs.bugtracker.comment') def test_modify_bugs_stable_no_close(self, comment, close): """Test the modify_bugs() method with a stable status and with close_bugs set to False.""" update = self.get_update()