UT: do not hide an original error in test resource ctxtmgr

In unit tests, resource contextmanagers such as network(), subnet()
try to delete themselves after returning from yield even if an
exception occurs. However when an exception occurs, there is a case
where deletion fails. In this case original exception will be hidden
and it makes difficult to debug test failures.

Before each test starts, resources like database entries will be
recreated, so there is no need to try to delete resources even
when an exception occurs. This commit removes try-finally clause
from resource contextmanagers to make original errors visible.

Closes-Bug: #1295887
Change-Id: Ia844d2aa2c9fc036e643068c5284f64798963ee3
This commit is contained in:
Akihiro Motoki 2014-03-22 08:17:20 +09:00 committed by Gerrit Code Review
parent e6e5e0daac
commit 38eefdf1a2

View File

@ -125,12 +125,10 @@ class VPNTestMixin(object):
**kwargs) **kwargs)
if res.status_int >= 400: if res.status_int >= 400:
raise webob.exc.HTTPClientError(code=res.status_int) raise webob.exc.HTTPClientError(code=res.status_int)
try: ikepolicy = self.deserialize(fmt or self.fmt, res)
ikepolicy = self.deserialize(fmt or self.fmt, res) yield ikepolicy
yield ikepolicy if not no_delete:
finally: self._delete('ikepolicies', ikepolicy['ikepolicy']['id'])
if not no_delete:
self._delete('ikepolicies', ikepolicy['ikepolicy']['id'])
def _create_ipsecpolicy(self, fmt, def _create_ipsecpolicy(self, fmt,
name='ipsecpolicy1', name='ipsecpolicy1',
@ -188,12 +186,10 @@ class VPNTestMixin(object):
**kwargs) **kwargs)
if res.status_int >= 400: if res.status_int >= 400:
raise webob.exc.HTTPClientError(code=res.status_int) raise webob.exc.HTTPClientError(code=res.status_int)
try: ipsecpolicy = self.deserialize(fmt or self.fmt, res)
ipsecpolicy = self.deserialize(fmt or self.fmt, res) yield ipsecpolicy
yield ipsecpolicy if not no_delete:
finally: self._delete('ipsecpolicies', ipsecpolicy['ipsecpolicy']['id'])
if not no_delete:
self._delete('ipsecpolicies', ipsecpolicy['ipsecpolicy']['id'])
def _create_vpnservice(self, fmt, name, def _create_vpnservice(self, fmt, name,
admin_state_up, admin_state_up,
@ -250,36 +246,37 @@ class VPNTestMixin(object):
'add', 'add',
tmp_router['router']['id'], tmp_router['router']['id'],
tmp_subnet['subnet']['id'], None) tmp_subnet['subnet']['id'], None)
try:
res = self._create_vpnservice(fmt, res = self._create_vpnservice(fmt,
name, name,
admin_state_up, admin_state_up,
router_id=(tmp_router['router'] router_id=(tmp_router['router']
['id']), ['id']),
subnet_id=(tmp_subnet['subnet'] subnet_id=(tmp_subnet['subnet']
['id']), ['id']),
**kwargs) **kwargs)
vpnservice = self.deserialize(fmt or self.fmt, res) vpnservice = self.deserialize(fmt or self.fmt, res)
if res.status_int >= 400: if res.status_int < 400:
raise webob.exc.HTTPClientError(
code=res.status_int, detail=vpnservice)
yield vpnservice yield vpnservice
finally:
if not no_delete and vpnservice.get('vpnservice'): if not no_delete and vpnservice.get('vpnservice'):
self._delete('vpnservices', self._delete('vpnservices',
vpnservice['vpnservice']['id']) vpnservice['vpnservice']['id'])
if plug_subnet: if plug_subnet:
self._router_interface_action( self._router_interface_action(
'remove', 'remove',
tmp_router['router']['id'], tmp_router['router']['id'],
tmp_subnet['subnet']['id'], None) tmp_subnet['subnet']['id'], None)
if external_router: if external_router:
external_gateway = tmp_router['router'].get( external_gateway = tmp_router['router'].get(
'external_gateway_info') 'external_gateway_info')
if external_gateway: if external_gateway:
network_id = external_gateway['network_id'] network_id = external_gateway['network_id']
self._remove_external_gateway_from_router( self._remove_external_gateway_from_router(
tmp_router['router']['id'], network_id) tmp_router['router']['id'], network_id)
if res.status_int >= 400:
raise webob.exc.HTTPClientError(
code=res.status_int, detail=vpnservice)
def _create_ipsec_site_connection(self, fmt, name='test', def _create_ipsec_site_connection(self, fmt, name='test',
peer_address='192.168.1.10', peer_address='192.168.1.10',
@ -379,18 +376,18 @@ class VPNTestMixin(object):
**kwargs) **kwargs)
if res.status_int >= 400: if res.status_int >= 400:
raise webob.exc.HTTPClientError(code=res.status_int) raise webob.exc.HTTPClientError(code=res.status_int)
try:
ipsec_site_connection = self.deserialize( ipsec_site_connection = self.deserialize(
fmt or self.fmt, res fmt or self.fmt, res
)
yield ipsec_site_connection
if not no_delete:
self._delete(
'ipsec-site-connections',
ipsec_site_connection[
'ipsec_site_connection']['id']
) )
yield ipsec_site_connection
finally:
if not no_delete:
self._delete(
'ipsec-site-connections',
ipsec_site_connection[
'ipsec_site_connection']['id']
)
def _check_ipsec_site_connection(self, ipsec_site_connection, keys, dpd): def _check_ipsec_site_connection(self, ipsec_site_connection, keys, dpd):
self.assertEqual( self.assertEqual(