unify mocking of tornado_settings

ensures that everywhere we access settings, it's the same dict
and not a copy
This commit is contained in:
Min RK
2018-02-28 15:09:25 +01:00
parent e025d58f6e
commit 015df7e060
5 changed files with 24 additions and 29 deletions

View File

@@ -130,7 +130,7 @@ def mockservice_url(request, app):
@fixture @fixture
def no_patience(app): def no_patience(app):
"""Set slow-spawning timeouts to zero""" """Set slow-spawning timeouts to zero"""
with mock.patch.dict(app.tornado_application.settings, with mock.patch.dict(app.tornado_settings,
{'slow_spawn_timeout': 0, {'slow_spawn_timeout': 0,
'slow_stop_timeout': 0}): 'slow_stop_timeout': 0}):
yield yield
@@ -139,7 +139,7 @@ def no_patience(app):
@fixture @fixture
def slow_spawn(app): def slow_spawn(app):
"""Fixture enabling SlowSpawner""" """Fixture enabling SlowSpawner"""
with mock.patch.dict(app.users.settings, with mock.patch.dict(app.tornado_settings,
{'spawner_class': mocking.SlowSpawner}): {'spawner_class': mocking.SlowSpawner}):
yield yield
@@ -147,7 +147,7 @@ def slow_spawn(app):
@fixture @fixture
def never_spawn(app): def never_spawn(app):
"""Fixture enabling NeverSpawner""" """Fixture enabling NeverSpawner"""
with mock.patch.dict(app.users.settings, with mock.patch.dict(app.tornado_settings,
{'spawner_class': mocking.NeverSpawner}): {'spawner_class': mocking.NeverSpawner}):
yield yield
@@ -155,7 +155,7 @@ def never_spawn(app):
@fixture @fixture
def bad_spawn(app): def bad_spawn(app):
"""Fixture enabling BadSpawner""" """Fixture enabling BadSpawner"""
with mock.patch.dict(app.users.settings, with mock.patch.dict(app.tornado_settings,
{'spawner_class': mocking.BadSpawner}): {'spawner_class': mocking.BadSpawner}):
yield yield
@@ -163,7 +163,7 @@ def bad_spawn(app):
@fixture @fixture
def slow_bad_spawn(app): def slow_bad_spawn(app):
"""Fixture enabling SlowBadSpawner""" """Fixture enabling SlowBadSpawner"""
with mock.patch.dict(app.users.settings, with mock.patch.dict(app.tornado_settings,
{'spawner_class': mocking.SlowBadSpawner}): {'spawner_class': mocking.SlowBadSpawner}):
yield yield
@@ -171,6 +171,6 @@ def slow_bad_spawn(app):
@fixture @fixture
def admin_access(app): def admin_access(app):
"""Grant admin-access with this fixture""" """Grant admin-access with this fixture"""
with mock.patch.dict(app.users.settings, with mock.patch.dict(app.tornado_settings,
{'admin_access': True}): {'admin_access': True}):
yield yield

View File

@@ -218,7 +218,7 @@ class MockHub(JupyterHub):
"""Instantiate the tornado Application object""" """Instantiate the tornado Application object"""
super().init_tornado_application() super().init_tornado_application()
# reconnect tornado_settings so that mocks can update the real thing # reconnect tornado_settings so that mocks can update the real thing
self.tornado_settings = self.tornado_application.settings self.tornado_settings = self.users.settings = self.tornado_application.settings
@gen.coroutine @gen.coroutine
def initialize(self, argv=None): def initialize(self, argv=None):

View File

@@ -518,7 +518,6 @@ def test_never_spawn(app, no_patience, never_spawn):
@mark.gen_test @mark.gen_test
def test_bad_spawn(app, no_patience, bad_spawn): def test_bad_spawn(app, no_patience, bad_spawn):
settings = app.tornado_application.settings
db = app.db db = app.db
name = 'prim' name = 'prim'
user = add_user(db, app=app, name=name) user = add_user(db, app=app, name=name)
@@ -529,7 +528,6 @@ def test_bad_spawn(app, no_patience, bad_spawn):
@mark.gen_test @mark.gen_test
def test_slow_bad_spawn(app, no_patience, slow_bad_spawn): def test_slow_bad_spawn(app, no_patience, slow_bad_spawn):
settings = app.tornado_application.settings
db = app.db db = app.db
name = 'zaphod' name = 'zaphod'
user = add_user(db, app=app, name=name) user = add_user(db, app=app, name=name)
@@ -545,11 +543,10 @@ def test_slow_bad_spawn(app, no_patience, slow_bad_spawn):
@mark.gen_test @mark.gen_test
def test_spawn_limit(app, no_patience, slow_spawn, request): def test_spawn_limit(app, no_patience, slow_spawn, request):
db = app.db db = app.db
settings = app.tornado_application.settings p = mock.patch.dict(app.tornado_settings,
settings['concurrent_spawn_limit'] = 2 {'concurrent_spawn_limit': 2})
def _restore_limit(): p.start()
settings['concurrent_spawn_limit'] = 100 request.addfinalizer(p.stop)
request.addfinalizer(_restore_limit)
# start two pending spawns # start two pending spawns
names = ['ykka', 'hjarka'] names = ['ykka', 'hjarka']
@@ -597,11 +594,10 @@ def test_spawn_limit(app, no_patience, slow_spawn, request):
@mark.gen_test @mark.gen_test
def test_active_server_limit(app, request): def test_active_server_limit(app, request):
db = app.db db = app.db
settings = app.tornado_application.settings p = mock.patch.dict(app.tornado_settings,
settings['active_server_limit'] = 2 {'active_server_limit': 2})
def _restore_limit(): p.start()
settings['active_server_limit'] = 0 request.addfinalizer(p.stop)
request.addfinalizer(_restore_limit)
# start two pending spawns # start two pending spawns
names = ['ykka', 'hjarka'] names = ['ykka', 'hjarka']

View File

@@ -1,4 +1,6 @@
"""Tests for named servers""" """Tests for named servers"""
from unittest import mock
import pytest import pytest
from ..utils import url_path_join from ..utils import url_path_join
@@ -9,12 +11,9 @@ from .utils import async_requests
@pytest.fixture @pytest.fixture
def named_servers(app): def named_servers(app):
key = 'allow_named_servers' with mock.patch.dict(app.tornado_settings,
app.tornado_application.settings[key] = app.tornado_settings[key] = True {'allow_named_servers': True}):
try: yield
yield True
finally:
app.tornado_application.settings[key] = app.tornado_settings[key] = False
@pytest.mark.gen_test @pytest.mark.gen_test

View File

@@ -209,7 +209,7 @@ def test_spawn_form(app):
@pytest.mark.gen_test @pytest.mark.gen_test
def test_spawn_form_admin_access(app, admin_access): def test_spawn_form_admin_access(app, admin_access):
with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}): with mock.patch.dict(app.tornado_settings, {'spawner_class': FormSpawner}):
base_url = ujoin(public_host(app), app.hub.base_url) base_url = ujoin(public_host(app), app.hub.base_url)
cookies = yield app.login_user('admin') cookies = yield app.login_user('admin')
u = add_user(app.db, app=app, name='martha') u = add_user(app.db, app=app, name='martha')
@@ -232,7 +232,7 @@ def test_spawn_form_admin_access(app, admin_access):
@pytest.mark.gen_test @pytest.mark.gen_test
def test_spawn_form_with_file(app): def test_spawn_form_with_file(app):
with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}): with mock.patch.dict(app.tornado_settings, {'spawner_class': FormSpawner}):
base_url = ujoin(public_host(app), app.hub.base_url) base_url = ujoin(public_host(app), app.hub.base_url)
cookies = yield app.login_user('jones') cookies = yield app.login_user('jones')
orm_u = orm.User.find(app.db, 'jones') orm_u = orm.User.find(app.db, 'jones')
@@ -386,7 +386,7 @@ def test_auto_login(app, request):
authenticator = Authenticator(auto_login=True) authenticator = Authenticator(auto_login=True)
authenticator.login_url = lambda base_url: ujoin(base_url, 'dummy') authenticator.login_url = lambda base_url: ujoin(base_url, 'dummy')
with mock.patch.dict(app.tornado_application.settings, { with mock.patch.dict(app.tornado_settings, {
'authenticator': authenticator, 'authenticator': authenticator,
}): }):
r = yield async_requests.get(base_url) r = yield async_requests.get(base_url)
@@ -397,7 +397,7 @@ def test_auto_login_logout(app):
name = 'burnham' name = 'burnham'
cookies = yield app.login_user(name) cookies = yield app.login_user(name)
with mock.patch.dict(app.tornado_application.settings, { with mock.patch.dict(app.tornado_settings, {
'authenticator': Authenticator(auto_login=True), 'authenticator': Authenticator(auto_login=True),
}): }):
r = yield async_requests.get(public_host(app) + app.tornado_settings['logout_url'], cookies=cookies) r = yield async_requests.get(public_host(app) + app.tornado_settings['logout_url'], cookies=cookies)