Browse Source

Merge "test: Ensure to stop mock when create_mocks decorator exits"

changes/66/778366/3
Zuul 5 months ago
committed by Gerrit Code Review
parent
commit
adc0127abb
  1. 5
      openstack_dashboard/dashboards/project/instances/tests.py
  2. 9
      openstack_dashboard/dashboards/project/overview/tests.py
  3. 13
      openstack_dashboard/test/helpers.py

5
openstack_dashboard/dashboards/project/instances/tests.py

@ -194,7 +194,10 @@ class InstanceTableTests(InstanceTestBase, InstanceTableTestMixin):
'servers_update_addresses',
),
api.cinder: ('volume_list',),
})
}, stop_mock=False)
# NOTE: _get_index() and _check_get_index() are used as pair
# and the test logic will be placed between these calls,
# so we cannot stop mocking when exiting this method.
def _get_index(self, use_servers_update_address=True):
servers = self.servers.list()
self.mock_is_feature_available.return_value = True

9
openstack_dashboard/dashboards/project/overview/tests.py

@ -35,8 +35,12 @@ class UsageViewTests(test.TestCase):
@test.create_mocks({
api.nova: ('usage_get',),
api.neutron: ('is_quotas_extension_supported',)
})
api.neutron: ('is_quotas_extension_supported',),
usage.quotas: ('tenant_quota_usages',),
}, stop_mock=False)
# NOTE: _stub_api_calls() and _check_api_calls() are used as pair
# and the test logic will be placed between these calls,
# so we cannot stop mocking when exiting this method.
def _stub_api_calls(self, nova_stu_enabled=True,
stu_exception=False, overview_days_range=1,
quota_usage_overrides=None,
@ -70,7 +74,6 @@ class UsageViewTests(test.TestCase):
usages.add_quota(api.base.Quota(k, quota))
usages.tally(k, quota_usages[k]['used'])
@test.create_mocks({usage.quotas: ('tenant_quota_usages',)})
def _stub_tenant_quota_usages(self, overrides):
usages_data = usage.quotas.QuotaUsage()
self._add_quota_usages(usages_data, self.quota_usages.first(),

13
openstack_dashboard/test/helpers.py

@ -54,7 +54,7 @@ IsA = horizon_helpers.IsA
IsHttpRequest = horizon_helpers.IsHttpRequest
def create_mocks(target_methods):
def create_mocks(target_methods, stop_mock=True):
"""decorator to simplify setting up multiple mocks at once
:param target_methods: a dict to define methods to be patched using mock.
@ -109,10 +109,14 @@ def create_mocks(target_methods):
self.mock_cinder_tenant_absolute_limits.return_value = ...
...
:param stop_mock: If True (default), mocks started in this decorator will
be stopped. Set this to False only if you cannot stop mocks when exiting
this decorator. The default value, True, should work for most cases.
"""
def wrapper(function):
@wraps(function)
def wrapped(inst, *args, **kwargs):
patchers = []
for target, methods in target_methods.items():
for method in methods:
if isinstance(method, str):
@ -122,8 +126,13 @@ def create_mocks(target_methods):
method_mocked = method[0]
attr_name = method[1]
m = mock.patch.object(target, method_mocked)
patchers.append(m)
setattr(inst, 'mock_%s' % attr_name, m.start())
return function(inst, *args, **kwargs)
retval = function(inst, *args, **kwargs)
if stop_mock:
for m in patchers:
m.stop()
return retval
return wrapped
return wrapper

Loading…
Cancel
Save