From 38eefdf1a2f7808525140c02455ff3792867481d Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Sat, 22 Mar 2014 08:17:20 +0900 Subject: [PATCH] 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 --- neutron/tests/unit/db/vpn/test_db_vpnaas.py | 101 ++++++++++---------- 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/neutron/tests/unit/db/vpn/test_db_vpnaas.py b/neutron/tests/unit/db/vpn/test_db_vpnaas.py index a32830683..826416d72 100644 --- a/neutron/tests/unit/db/vpn/test_db_vpnaas.py +++ b/neutron/tests/unit/db/vpn/test_db_vpnaas.py @@ -125,12 +125,10 @@ class VPNTestMixin(object): **kwargs) if res.status_int >= 400: raise webob.exc.HTTPClientError(code=res.status_int) - try: - ikepolicy = self.deserialize(fmt or self.fmt, res) - yield ikepolicy - finally: - if not no_delete: - self._delete('ikepolicies', ikepolicy['ikepolicy']['id']) + ikepolicy = self.deserialize(fmt or self.fmt, res) + yield ikepolicy + if not no_delete: + self._delete('ikepolicies', ikepolicy['ikepolicy']['id']) def _create_ipsecpolicy(self, fmt, name='ipsecpolicy1', @@ -188,12 +186,10 @@ class VPNTestMixin(object): **kwargs) if res.status_int >= 400: raise webob.exc.HTTPClientError(code=res.status_int) - try: - ipsecpolicy = self.deserialize(fmt or self.fmt, res) - yield ipsecpolicy - finally: - if not no_delete: - self._delete('ipsecpolicies', ipsecpolicy['ipsecpolicy']['id']) + ipsecpolicy = self.deserialize(fmt or self.fmt, res) + yield ipsecpolicy + if not no_delete: + self._delete('ipsecpolicies', ipsecpolicy['ipsecpolicy']['id']) def _create_vpnservice(self, fmt, name, admin_state_up, @@ -250,36 +246,37 @@ class VPNTestMixin(object): 'add', tmp_router['router']['id'], tmp_subnet['subnet']['id'], None) - try: - res = self._create_vpnservice(fmt, - name, - admin_state_up, - router_id=(tmp_router['router'] - ['id']), - subnet_id=(tmp_subnet['subnet'] - ['id']), - **kwargs) - vpnservice = self.deserialize(fmt or self.fmt, res) - if res.status_int >= 400: - raise webob.exc.HTTPClientError( - code=res.status_int, detail=vpnservice) + + res = self._create_vpnservice(fmt, + name, + admin_state_up, + router_id=(tmp_router['router'] + ['id']), + subnet_id=(tmp_subnet['subnet'] + ['id']), + **kwargs) + vpnservice = self.deserialize(fmt or self.fmt, res) + if res.status_int < 400: yield vpnservice - finally: - if not no_delete and vpnservice.get('vpnservice'): - self._delete('vpnservices', - vpnservice['vpnservice']['id']) - if plug_subnet: - self._router_interface_action( - 'remove', - tmp_router['router']['id'], - tmp_subnet['subnet']['id'], None) - if external_router: - external_gateway = tmp_router['router'].get( - 'external_gateway_info') - if external_gateway: - network_id = external_gateway['network_id'] - self._remove_external_gateway_from_router( - tmp_router['router']['id'], network_id) + + if not no_delete and vpnservice.get('vpnservice'): + self._delete('vpnservices', + vpnservice['vpnservice']['id']) + if plug_subnet: + self._router_interface_action( + 'remove', + tmp_router['router']['id'], + tmp_subnet['subnet']['id'], None) + if external_router: + external_gateway = tmp_router['router'].get( + 'external_gateway_info') + if external_gateway: + network_id = external_gateway['network_id'] + self._remove_external_gateway_from_router( + 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', peer_address='192.168.1.10', @@ -379,18 +376,18 @@ class VPNTestMixin(object): **kwargs) if res.status_int >= 400: raise webob.exc.HTTPClientError(code=res.status_int) - try: - ipsec_site_connection = self.deserialize( - fmt or self.fmt, res + + ipsec_site_connection = self.deserialize( + 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): self.assertEqual(