Skip to content

Commit

Permalink
Initialize the bugtracker in main() instead of on import.
Browse files Browse the repository at this point in the history
Before this commit, it was not possible to build the documentation
without installing Bodhi first, since importing bodhi.server would
cause the config to get loaded which would cause a exception when
it could not find the bodhi-server distribution. With this patch,
it is possible to import Bodhi's server code without installing
Bodhi.

Signed-off-by: Randy Barlow <[email protected]>
  • Loading branch information
bowlofeggs committed Mar 28, 2017
1 parent 5441753 commit e0b917e
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 25 deletions.
5 changes: 3 additions & 2 deletions bodhi/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions bodhi/server/bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from bodhi.server.config import config


bugtracker = None
log = logging.getLogger('bodhi')


Expand Down Expand Up @@ -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()
3 changes: 2 additions & 1 deletion bodhi/server/consumers/masher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 4 additions & 2 deletions bodhi/server/consumers/updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions bodhi/server/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@
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,
)
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


Expand Down Expand Up @@ -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'] % (
Expand All @@ -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):
"""
Expand All @@ -2222,22 +2221,22 @@ 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
# so that .close() can figure out which build version fixes which bug.
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,
Expand Down
3 changes: 2 additions & 1 deletion bodhi/tests/server/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions bodhi/tests/server/consumers/test_masher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion bodhi/tests/server/consumers/test_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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()
34 changes: 34 additions & 0 deletions bodhi/tests/server/test___init__.py
Original file line number Diff line number Diff line change
@@ -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()
25 changes: 25 additions & 0 deletions bodhi/tests/server/test_bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
11 changes: 6 additions & 5 deletions bodhi/tests/server/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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(
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit e0b917e

Please sign in to comment.