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 [] 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, class ServiceRouterTest(test_nicira_plugin.NiciraL3NatTest,
test_l3_plugin.L3NatTestCaseMixin): test_l3_plugin.L3NatTestCaseMixin):
@ -170,7 +157,8 @@ class ServiceRouterTest(test_nicira_plugin.NiciraL3NatTest,
return router_req.get_response(self.ext_api) return router_req.get_response(self.ext_api)
class ServiceRouterTestCase(ServiceRouterTest, NvpRouterTestCase): class ServiceRouterTestCase(ServiceRouterTest,
test_nicira_plugin.TestNiciraL3NatTestCase):
def test_router_create(self): def test_router_create(self):
name = 'router1' name = 'router1'

View File

@ -379,7 +379,8 @@ class L3NatTestCaseMixin(object):
def _router_interface_action(self, action, router_id, subnet_id, port_id, def _router_interface_action(self, action, router_id, subnet_id, port_id,
expected_code=exc.HTTPOk.code, expected_code=exc.HTTPOk.code,
expected_body=None): expected_body=None,
tenant_id=None):
interface_data = {} interface_data = {}
if subnet_id: if subnet_id:
interface_data.update({'subnet_id': 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, req = self.new_action_request('routers', interface_data, router_id,
"%s_router_interface" % action) "%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) res = req.get_response(self.ext_api)
self.assertEqual(res.status_int, expected_code) self.assertEqual(res.status_int, expected_code)
response = self.deserialize(self.fmt, res) response = self.deserialize(self.fmt, res)
@ -734,42 +739,37 @@ class L3NatTestCaseBase(L3NatTestCaseMixin):
def test_router_add_interface_subnet_with_port_from_other_tenant(self): def test_router_add_interface_subnet_with_port_from_other_tenant(self):
tenant_id = _uuid() tenant_id = _uuid()
other_tenant_id = _uuid() other_tenant_id = _uuid()
tenant_context = context.Context(user_id=None, tenant_id=tenant_id) with contextlib.nested(
admin_context = context.get_admin_context() self.router(tenant_id=tenant_id),
with mock.patch('neutron.context.Context') as ctx: self.network(tenant_id=tenant_id),
ctx.return_value = admin_context self.network(tenant_id=other_tenant_id)) as (r, n1, n2):
with contextlib.nested( with contextlib.nested(
self.router(tenant_id=tenant_id), self.subnet(network=n1, cidr='10.0.0.0/24'),
self.network(tenant_id=tenant_id), self.subnet(network=n2, cidr='10.1.0.0/24')) as (s1, s2):
self.network(tenant_id=other_tenant_id)) as (r, n1, n2): body = self._router_interface_action(
with contextlib.nested( 'add',
self.subnet(network=n1, cidr='10.0.0.0/24'), r['router']['id'],
self.subnet(network=n2, cidr='10.1.0.0/24')) as (s1, s2): s2['subnet']['id'],
ctx.return_value = admin_context None)
body = self._router_interface_action( self.assertIn('port_id', body)
'add', self._router_interface_action(
r['router']['id'], 'add',
s2['subnet']['id'], r['router']['id'],
None) s1['subnet']['id'],
self.assertIn('port_id', body) None,
ctx.return_value = tenant_context tenant_id=tenant_id)
self._router_interface_action( self.assertIn('port_id', body)
'add', self._router_interface_action(
r['router']['id'], 'remove',
s1['subnet']['id'], r['router']['id'],
None) s1['subnet']['id'],
self.assertIn('port_id', body) None,
self._router_interface_action( tenant_id=tenant_id)
'remove', body = self._router_interface_action(
r['router']['id'], 'remove',
s1['subnet']['id'], r['router']['id'],
None) s2['subnet']['id'],
ctx.return_value = admin_context None)
body = self._router_interface_action(
'remove',
r['router']['id'],
s2['subnet']['id'],
None)
def test_router_add_interface_port(self): def test_router_add_interface_port(self):
with self.router() as r: with self.router() as r: