Testing: mock add_panel_mocks() more flexible

Tis commit enhances openstack_dashboard.test.helpers.TestCase
.add_panel_mocks() method to more flexible way.
Now the mock definitions are defined in the test settings and
setUp() in unit tests and selenium tests sets up mocks defined in
the test settings TEST_GLOBAL_MOCKS_ON_PANELS.

This is a preparation for drop of deprecated options (enable_firewall
and enable_vpn). They were marked as deprecated long ago, but they are
still kept just because we need a lot of changes in unit tests if dropped.
By this change, we no longer need to use settings just for testing.

enable_firewall/enable_vpn=False are dropped (default is True)
to confirm this change works well.

Related-Bug: #1687185
Change-Id: I0a5ebcf8d75e704420d3a5af46fde268a59aa9ec
This commit is contained in:
Akihiro Motoki 2017-04-29 11:26:45 +00:00
parent f100793164
commit b0a5f30248
2 changed files with 45 additions and 24 deletions

View File

@ -116,6 +116,23 @@ def create_stubs(stubs_to_create=None):
return inner_stub_out
def _apply_panel_mocks(patchers=None):
"""Global mocks on panels that get called on all views."""
if patchers is None:
patchers = {}
mocked_methods = getattr(settings, 'TEST_GLOBAL_MOCKS_ON_PANELS', {})
for name, mock_config in mocked_methods.items():
method = mock_config['method']
mock_params = {}
for param in ['return_value', 'side_effect']:
if param in mock_config:
mock_params[param] = mock_config[param]
patcher = mock.patch(method, **mock_params)
patcher.start()
patchers[name] = patcher
return patchers
class RequestFactoryWithMessages(RequestFactory):
def get(self, *args, **kwargs):
req = super(RequestFactoryWithMessages, self).get(*args, **kwargs)
@ -171,8 +188,7 @@ class TestCase(horizon_helpers.TestCase):
self._real_context_processor = context_processors.openstack
context_processors.openstack = lambda request: self.context
self.patchers = {}
self.add_panel_mocks()
self.patchers = _apply_panel_mocks()
super(TestCase, self).setUp()
@ -205,14 +221,6 @@ class TestCase(horizon_helpers.TestCase):
super(TestCase, self)._setup_request()
self.request.session['token'] = self.token.id
def add_panel_mocks(self):
"""Global mocks on panels that get called on all views."""
self.patchers['aggregates'] = mock.patch(
'openstack_dashboard.dashboards.admin'
'.aggregates.panel.Aggregates.can_access',
mock.Mock(return_value=True))
self.patchers['aggregates'].start()
def tearDown(self):
HTTPConnection.connect = self._real_conn_request
context_processors.openstack = self._real_context_processor
@ -532,12 +540,7 @@ class SeleniumTestCase(horizon_helpers.SeleniumTestCase):
tenant_id=self.tenant.id,
service_catalog=self.service_catalog,
authorized_tenants=self.tenants.list())
self.patchers = {}
self.patchers['aggregates'] = mock.patch(
'openstack_dashboard.dashboards.admin'
'.aggregates.panel.Aggregates.can_access',
mock.Mock(return_value=True))
self.patchers['aggregates'].start()
self.patchers = _apply_panel_mocks()
os.environ["HORIZON_TEST_RUN"] = "True"
def tearDown(self):

View File

@ -164,14 +164,6 @@ OPENSTACK_CINDER_FEATURES = {
OPENSTACK_NEUTRON_NETWORK = {
'enable_router': True,
'enable_quotas': False, # Enabled in specific tests only
# Parameters below (enable_firewall, enable_vpn)
# control if these panels are displayed or not,
# i.e. they only affect the navigation menu.
# These panels are registered even if enable_XXX is False,
# so we don't need to set them to True in most unit tests
# to avoid stubbing neutron extension check calls.
'enable_firewall': False,
'enable_vpn': False,
'enable_distributed_router': False,
}
@ -266,3 +258,29 @@ REST_API_REQUIRED_SETTINGS = ['REST_API_SETTING_1']
REST_API_ADDITIONAL_SETTINGS = ['REST_API_SETTING_2']
ALLOWED_PRIVATE_SUBNET_CIDR = {'ipv4': [], 'ipv6': []}
# --------------------
# Test-only settings
# --------------------
# TEST_GLOBAL_MOCKS_ON_PANELS: defines what and how methods should be
# mocked globally for unit tests and Selenium tests.
# 'method' is required. 'return_value' and 'side_effect'
# are optional and passed to mock.patch().
TEST_GLOBAL_MOCKS_ON_PANELS = {
'aggregates': {
'method': ('openstack_dashboard.dashboards.admin'
'.aggregates.panel.Aggregates.can_access'),
'return_value': True,
},
'firewalls': {
'method': ('openstack_dashboard.dashboards.project'
'.firewalls.panel.Firewall.can_access'),
'return_value': True,
},
'vpn': {
'method': ('openstack_dashboard.dashboards.project'
'.vpn.panel.VPN.can_access'),
'return_value': True,
},
}