Merge "Use different context for each API request in unit tests"

This commit is contained in:
Jenkins 2014-02-21 18:36:16 +00:00 committed by Gerrit Code Review
commit 4b5e3b80f2
2 changed files with 38 additions and 50 deletions

View File

@ -63,19 +63,6 @@ class ServiceRouterTestExtensionManager(object):
return []
class NvpRouterTestCase(test_nicira_plugin.TestNiciraL3NatTestCase):
def setUp(self, plugin=None, ext_mgr=None, service_plugins=None):
plugin = plugin or SERVICE_PLUGIN_NAME
# Disable the proxying in the tests but functionality will
# be covered separately
mock_proxy = mock.patch(
"%s.%s" % (SERVICE_PLUGIN_NAME, '_set_create_lswitch_proxy'))
mock_proxy.start()
super(NvpRouterTestCase, self).setUp(plugin=plugin, ext_mgr=ext_mgr,
service_plugins=service_plugins)
class ServiceRouterTest(test_nicira_plugin.NiciraL3NatTest,
test_l3_plugin.L3NatTestCaseMixin):
@ -170,7 +157,8 @@ class ServiceRouterTest(test_nicira_plugin.NiciraL3NatTest,
return router_req.get_response(self.ext_api)
class ServiceRouterTestCase(ServiceRouterTest, NvpRouterTestCase):
class ServiceRouterTestCase(ServiceRouterTest,
test_nicira_plugin.TestNiciraL3NatTestCase):
def test_router_create(self):
name = 'router1'

View File

@ -379,7 +379,8 @@ class L3NatTestCaseMixin(object):
def _router_interface_action(self, action, router_id, subnet_id, port_id,
expected_code=exc.HTTPOk.code,
expected_body=None):
expected_body=None,
tenant_id=None):
interface_data = {}
if subnet_id:
interface_data.update({'subnet_id': subnet_id})
@ -388,6 +389,10 @@ class L3NatTestCaseMixin(object):
req = self.new_action_request('routers', interface_data, router_id,
"%s_router_interface" % action)
# if tenant_id was specified, create a tenant context for this request
if tenant_id:
req.environ['neutron.context'] = context.Context(
'', tenant_id)
res = req.get_response(self.ext_api)
self.assertEqual(res.status_int, expected_code)
response = self.deserialize(self.fmt, res)
@ -734,10 +739,6 @@ class L3NatTestCaseBase(L3NatTestCaseMixin):
def test_router_add_interface_subnet_with_port_from_other_tenant(self):
tenant_id = _uuid()
other_tenant_id = _uuid()
tenant_context = context.Context(user_id=None, tenant_id=tenant_id)
admin_context = context.get_admin_context()
with mock.patch('neutron.context.Context') as ctx:
ctx.return_value = admin_context
with contextlib.nested(
self.router(tenant_id=tenant_id),
self.network(tenant_id=tenant_id),
@ -745,26 +746,25 @@ class L3NatTestCaseBase(L3NatTestCaseMixin):
with contextlib.nested(
self.subnet(network=n1, cidr='10.0.0.0/24'),
self.subnet(network=n2, cidr='10.1.0.0/24')) as (s1, s2):
ctx.return_value = admin_context
body = self._router_interface_action(
'add',
r['router']['id'],
s2['subnet']['id'],
None)
self.assertIn('port_id', body)
ctx.return_value = tenant_context
self._router_interface_action(
'add',
r['router']['id'],
s1['subnet']['id'],
None)
None,
tenant_id=tenant_id)
self.assertIn('port_id', body)
self._router_interface_action(
'remove',
r['router']['id'],
s1['subnet']['id'],
None)
ctx.return_value = admin_context
None,
tenant_id=tenant_id)
body = self._router_interface_action(
'remove',
r['router']['id'],