Skip to content

Commit e0b917e

Browse files
committed
Initialize the bugtracker in main() instead of on import.
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]>
1 parent 5441753 commit e0b917e

File tree

11 files changed

+106
-25
lines changed

11 files changed

+106
-25
lines changed

bodhi/server/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from sqlalchemy.orm import scoped_session, sessionmaker
3030
from zope.sqlalchemy import ZopeTransactionExtension
3131

32-
from bodhi.server import buildsys, ffmarkdown
32+
from bodhi.server import bugs, buildsys, ffmarkdown
3333

3434

3535
log = logging.getLogger(__name__)
@@ -123,7 +123,8 @@ def exception_filter(response, request):
123123

124124
def main(global_config, testing=None, session=None, **settings):
125125
""" This function returns a WSGI application """
126-
# Setup our buildsystem
126+
# Setup our bugtracker and buildsystem
127+
bugs.set_bugtracker()
127128
buildsys.setup_buildsystem(settings)
128129

129130
# Sessions & Caching

bodhi/server/bugs.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from bodhi.server.config import config
2323

2424

25+
bugtracker = None
2526
log = logging.getLogger('bodhi')
2627

2728

@@ -176,9 +177,14 @@ def modified(self, bug_id):
176177
log.exception("Unable to alter bug #%d" % bug_id)
177178

178179

179-
if config.get('bugtracker') == 'bugzilla':
180-
log.info('Using python-bugzilla')
181-
bugtracker = Bugzilla()
182-
else:
183-
log.info('Using the FakeBugTracker')
184-
bugtracker = FakeBugTracker()
180+
def set_bugtracker():
181+
"""
182+
Set the module-level bugtracker attribute to the correct bugtracker, based on the config.
183+
"""
184+
global bugtracker
185+
if config.get('bugtracker') == 'bugzilla':
186+
log.info('Using python-bugzilla')
187+
bugtracker = Bugzilla()
188+
else:
189+
log.info('Using the FakeBugTracker')
190+
bugtracker = FakeBugTracker()

bodhi/server/consumers/masher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from sqlalchemy import engine_from_config
3737
import fedmsg.consumers
3838

39-
from bodhi.server import log, buildsys, notifications, mail, util
39+
from bodhi.server import bugs, log, buildsys, notifications, mail, util
4040
from bodhi.server.config import config
4141
from bodhi.server.exceptions import BodhiException
4242
from bodhi.server.metadata import ExtendedMetadata
@@ -126,6 +126,7 @@ def __init__(self, hub, db_factory=None, mash_dir=config.get('mash_dir'),
126126
self.db_factory = db_factory
127127

128128
buildsys.setup_buildsystem(config)
129+
bugs.set_bugtracker()
129130
self.mash_dir = mash_dir
130131
prefix = hub.config.get('topic_prefix')
131132
env = hub.config.get('environment')

bodhi/server/consumers/updates.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
import fedmsg.consumers
3838

39-
from bodhi.server.bugs import bugtracker
39+
from bodhi.server import bugs as bug_module
4040
from bodhi.server.config import config
4141
from bodhi.server.exceptions import BodhiException
4242
from bodhi.server.models import Bug, Update, UpdateType
@@ -67,6 +67,8 @@ def __init__(self, hub, *args, **kwargs):
6767
self.handle_bugs = bool(config.get('bodhi_email'))
6868
if not self.handle_bugs:
6969
log.warn("No bodhi_email defined; not fetching bug details")
70+
else:
71+
bug_module.set_bugtracker()
7072

7173
super(UpdatesHandler, self).__init__(hub, *args, **kwargs)
7274
log.info('Bodhi updates handler listening on:\n'
@@ -123,7 +125,7 @@ def work_on_bugs(self, session, update, bugs):
123125
for bug in bugs:
124126
log.info("Getting RHBZ bug %r" % bug.bug_id)
125127
try:
126-
rhbz_bug = bugtracker.getbug(bug.bug_id)
128+
rhbz_bug = bug_module.bugtracker.getbug(bug.bug_id)
127129

128130
log.info("Updating our details for %r" % bug.bug_id)
129131
bug.update_details(rhbz_bug)

bodhi/server/models.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@
4646
from sqlalchemy.types import SchemaType, TypeDecorator, Enum
4747
from pyramid.settings import asbool
4848

49-
from bodhi.server import buildsys, mail, notifications, log
49+
from bodhi.server import bugs, buildsys, mail, notifications, log
5050
from bodhi.server.util import (
5151
header, build_evr, get_nvr, flash_log, get_age, get_critpath_pkgs,
5252
get_rpm_header, get_age_in_days, avatar as get_avatar, tokenize,
5353
)
5454
import bodhi.server.util
5555
from bodhi.server.exceptions import BodhiException, LockedUpdateException
5656
from bodhi.server.config import config
57-
from bodhi.server.bugs import bugtracker
5857
from bodhi.server.util import transactional_session_maker
5958

6059

@@ -2184,7 +2183,7 @@ def update_details(self, bug=None):
21842183
21852184
This is typically called "offline" in the UpdatesHandler consumer.
21862185
"""
2187-
bugtracker.update_details(bug, self)
2186+
bugs.bugtracker.update_details(bug, self)
21882187

21892188
def default_message(self, update):
21902189
message = config['stable_bug_msg'] % (
@@ -2210,7 +2209,7 @@ def add_comment(self, update, comment=None):
22102209
if not comment:
22112210
comment = self.default_message(update)
22122211
log.debug("Adding comment to Bug #%d: %s" % (self.bug_id, comment))
2213-
bugtracker.comment(self.bug_id, comment)
2212+
bugs.bugtracker.comment(self.bug_id, comment)
22142213

22152214
def testing(self, update):
22162215
"""
@@ -2222,22 +2221,22 @@ def testing(self, update):
22222221
log.debug('Not modifying on parent security bug %s', self.bug_id)
22232222
else:
22242223
comment = self.default_message(update)
2225-
bugtracker.on_qa(self.bug_id, comment)
2224+
bugs.bugtracker.on_qa(self.bug_id, comment)
22262225

22272226
def close_bug(self, update):
22282227
# Build a mapping of package names to build versions
22292228
# so that .close() can figure out which build version fixes which bug.
22302229
versions = dict([
22312230
(get_nvr(b.nvr)[0], b.nvr) for b in update.builds
22322231
])
2233-
bugtracker.close(self.bug_id, versions=versions, comment=self.default_message(update))
2232+
bugs.bugtracker.close(self.bug_id, versions=versions, comment=self.default_message(update))
22342233

22352234
def modified(self, update):
22362235
""" Change the status of this bug to MODIFIED """
22372236
if update.type is UpdateType.security and self.parent:
22382237
log.debug('Not modifying on parent security bug %s', self.bug_id)
22392238
else:
2240-
bugtracker.modified(self.bug_id)
2239+
bugs.bugtracker.modified(self.bug_id)
22412240

22422241

22432242
user_group_table = Table('user_group_table', Base.metadata,

bodhi/tests/server/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import requests
2727
import transaction
2828

29-
from bodhi.server import log, models
29+
from bodhi.server import bugs, log, models
3030
from bodhi.tests.server import create_update, populate
3131

3232

@@ -51,6 +51,7 @@
5151

5252
class BaseTestCase(unittest.TestCase):
5353
def setUp(self):
54+
bugs.set_bugtracker()
5455
engine = create_engine(DB_PATH)
5556
# We want a special session that lasts longer than a transaction
5657
self.Session = scoped_session(

bodhi/tests/server/consumers/test_masher.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ def set_stable_request(self, title):
144144
update.request = UpdateRequest.stable
145145
session.flush()
146146

147+
@mock.patch('bodhi.server.consumers.masher.bugs.set_bugtracker')
148+
def test___init___sets_bugtracker(self, set_bugtracker):
149+
"""
150+
Assert that Masher.__init__() calls bodhi.server.bugs.set_bugtracker().
151+
"""
152+
Masher(FakeHub(), db_factory=self.db_factory, mash_dir=self.tempdir)
153+
154+
set_bugtracker.assert_called_once_with()
155+
147156
@mock.patch('bodhi.server.notifications.publish')
148157
def test_invalid_signature(self, publish):
149158
"""Make sure the masher ignores messages that aren't signed with the

bodhi/tests/server/consumers/test_updates.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ def test_super___init___called(self, __init__):
231231

232232
__init__.assert_called_once_with(hub)
233233

234-
def test_typical_config(self):
234+
@mock.patch('bodhi.server.consumers.updates.bug_module.set_bugtracker')
235+
def test_typical_config(self, set_bugtracker):
235236
"""
236237
Test the method with a typical config.
237238
"""
@@ -248,3 +249,4 @@ def test_typical_config(self):
248249
h.topic,
249250
['topic_prefix.environment.bodhi.update.request.testing',
250251
'topic_prefix.environment.bodhi.update.edit'])
252+
set_bugtracker.assert_called_once_with()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# This program is free software; you can redistribute it and/or
4+
# modify it under the terms of the GNU General Public License
5+
# as published by the Free Software Foundation; either version 2
6+
# of the License, or (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16+
"""This test suite contains tests for bodhi.server.__init__."""
17+
import mock
18+
19+
from bodhi import server
20+
from bodhi.tests.server.functional import base
21+
22+
23+
class TestMain(base.BaseWSGICase):
24+
"""
25+
Assert correct behavior from the main() function.
26+
"""
27+
@mock.patch('bodhi.server.bugs.set_bugtracker')
28+
def test_calls_set_bugtracker(self, set_bugtracker):
29+
"""
30+
Ensure that main() calls set_bugtracker().
31+
"""
32+
server.main({}, testing='guest', session=self.db, **self.app_settings)
33+
34+
set_bugtracker.assert_called_once_with()

bodhi/tests/server/test_bugs.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,28 @@ def test___noop__(self, debug):
219219
bt.__noop__(1, 2)
220220

221221
debug.assert_called_once_with('__noop__((1, 2))')
222+
223+
224+
class TestSetBugtracker(unittest.TestCase):
225+
"""
226+
Test the set_bugtracker() function.
227+
"""
228+
@mock.patch('bodhi.server.bugs.bugtracker', None)
229+
@mock.patch.dict('bodhi.server.bugs.config', {'bugtracker': 'bugzilla'})
230+
def test_config_bugzilla(self):
231+
"""
232+
Test when the config is set for bugzilla to be the bugtracker.
233+
"""
234+
bugs.set_bugtracker()
235+
236+
self.assertTrue(isinstance(bugs.bugtracker, bugs.Bugzilla))
237+
238+
@mock.patch('bodhi.server.bugs.bugtracker', None)
239+
@mock.patch.dict('bodhi.server.bugs.config', {'bugtracker': 'fake'})
240+
def test_config_not_bugzilla(self):
241+
"""
242+
Test when the config is no set for bugzilla to be the bugtracker.
243+
"""
244+
bugs.set_bugtracker()
245+
246+
self.assertTrue(isinstance(bugs.bugtracker, bugs.FakeBugTracker))

0 commit comments

Comments
 (0)