Kill finally, use addCleanUp
Remove the finally usage for cleanup in the floating_ips tests and
in the test_services tests.
The finally blocks may try to delete resources which are even does not
exists, and or referencing to an undefined variables, which could hide
the original exceptions from the resource creation operations.
Change-Id: I23740553c793dcb2904a561b8f2ce801137e32d8
diff --git a/tempest/api/compute/floating_ips/test_floating_ips_actions.py b/tempest/api/compute/floating_ips/test_floating_ips_actions.py
index f4ad449..32e7b39 100644
--- a/tempest/api/compute/floating_ips/test_floating_ips_actions.py
+++ b/tempest/api/compute/floating_ips/test_floating_ips_actions.py
@@ -51,17 +51,15 @@
# Positive test:Allocation of a new floating IP to a project
# should be successful
resp, body = self.client.create_floating_ip()
- self.assertEqual(200, resp.status)
floating_ip_id_allocated = body['id']
- try:
- resp, floating_ip_details = \
- self.client.get_floating_ip_details(floating_ip_id_allocated)
- # Checking if the details of allocated IP is in list of floating IP
- resp, body = self.client.list_floating_ips()
- self.assertIn(floating_ip_details, body)
- finally:
- # Deleting the floating IP which is created in this method
- self.client.delete_floating_ip(floating_ip_id_allocated)
+ self.addCleanup(self.client.delete_floating_ip,
+ floating_ip_id_allocated)
+ self.assertEqual(200, resp.status)
+ resp, floating_ip_details = \
+ self.client.get_floating_ip_details(floating_ip_id_allocated)
+ # Checking if the details of allocated IP is in list of floating IP
+ resp, body = self.client.list_floating_ips()
+ self.assertIn(floating_ip_details, body)
@attr(type='gate')
def test_delete_floating_ip(self):
diff --git a/tempest/api/compute/floating_ips/test_list_floating_ips.py b/tempest/api/compute/floating_ips/test_list_floating_ips.py
index e4d03ae..9238994 100644
--- a/tempest/api/compute/floating_ips/test_list_floating_ips.py
+++ b/tempest/api/compute/floating_ips/test_list_floating_ips.py
@@ -54,25 +54,23 @@
def test_get_floating_ip_details(self):
# Positive test:Should be able to GET the details of floatingIP
# Creating a floating IP for which details are to be checked
- try:
- resp, body = self.client.create_floating_ip()
- floating_ip_instance_id = body['instance_id']
- floating_ip_ip = body['ip']
- floating_ip_fixed_ip = body['fixed_ip']
- floating_ip_id = body['id']
- resp, body = \
- self.client.get_floating_ip_details(floating_ip_id)
- self.assertEqual(200, resp.status)
- # Comparing the details of floating IP
- self.assertEqual(floating_ip_instance_id,
- body['instance_id'])
- self.assertEqual(floating_ip_ip, body['ip'])
- self.assertEqual(floating_ip_fixed_ip,
- body['fixed_ip'])
- self.assertEqual(floating_ip_id, body['id'])
- # Deleting the floating IP created in this method
- finally:
- self.client.delete_floating_ip(floating_ip_id)
+ resp, body = self.client.create_floating_ip()
+ floating_ip_id = body['id']
+ self.addCleanup(self.client.delete_floating_ip,
+ floating_ip_id)
+ floating_ip_instance_id = body['instance_id']
+ floating_ip_ip = body['ip']
+ floating_ip_fixed_ip = body['fixed_ip']
+ resp, body = \
+ self.client.get_floating_ip_details(floating_ip_id)
+ self.assertEqual(200, resp.status)
+ # Comparing the details of floating IP
+ self.assertEqual(floating_ip_instance_id,
+ body['instance_id'])
+ self.assertEqual(floating_ip_ip, body['ip'])
+ self.assertEqual(floating_ip_fixed_ip,
+ body['fixed_ip'])
+ self.assertEqual(floating_ip_id, body['id'])
@attr(type='gate')
def test_list_floating_ip_pools(self):
diff --git a/tempest/api/identity/admin/test_services.py b/tempest/api/identity/admin/test_services.py
index 7fe5171..872adb8 100644
--- a/tempest/api/identity/admin/test_services.py
+++ b/tempest/api/identity/admin/test_services.py
@@ -25,47 +25,47 @@
class ServicesTestJSON(base.BaseIdentityAdminTest):
_interface = 'json'
+ def _del_service(self, service_id):
+ # Deleting the service created in this method
+ resp, _ = self.client.delete_service(service_id)
+ self.assertEqual(resp['status'], '204')
+ # Checking whether service is deleted successfully
+ self.assertRaises(exceptions.NotFound, self.client.get_service,
+ service_id)
+
@attr(type='smoke')
def test_create_get_delete_service(self):
# GET Service
- try:
- # Creating a Service
- name = data_utils.rand_name('service-')
- type = data_utils.rand_name('type--')
- description = data_utils.rand_name('description-')
- resp, service_data = self.client.create_service(
- name, type, description=description)
- self.assertTrue(resp['status'].startswith('2'))
- # Verifying response body of create service
- self.assertIn('id', service_data)
- self.assertFalse(service_data['id'] is None)
- self.assertIn('name', service_data)
- self.assertEqual(name, service_data['name'])
- self.assertIn('type', service_data)
- self.assertEqual(type, service_data['type'])
- self.assertIn('description', service_data)
- self.assertEqual(description, service_data['description'])
- # Get service
- resp, fetched_service = self.client.get_service(service_data['id'])
- self.assertTrue(resp['status'].startswith('2'))
- # verifying the existence of service created
- self.assertIn('id', fetched_service)
- self.assertEqual(fetched_service['id'], service_data['id'])
- self.assertIn('name', fetched_service)
- self.assertEqual(fetched_service['name'], service_data['name'])
- self.assertIn('type', fetched_service)
- self.assertEqual(fetched_service['type'], service_data['type'])
- self.assertIn('description', fetched_service)
- self.assertEqual(fetched_service['description'],
- service_data['description'])
- finally:
- if 'service_data' in locals():
- # Deleting the service created in this method
- resp, _ = self.client.delete_service(service_data['id'])
- self.assertEqual(resp['status'], '204')
- # Checking whether service is deleted successfully
- self.assertRaises(exceptions.NotFound, self.client.get_service,
- service_data['id'])
+ # Creating a Service
+ name = data_utils.rand_name('service-')
+ type = data_utils.rand_name('type--')
+ description = data_utils.rand_name('description-')
+ resp, service_data = self.client.create_service(
+ name, type, description=description)
+ self.assertFalse(service_data['id'] is None)
+ self.addCleanup(self._del_service, service_data['id'])
+ self.assertTrue(resp['status'].startswith('2'))
+ # Verifying response body of create service
+ self.assertIn('id', service_data)
+ self.assertIn('name', service_data)
+ self.assertEqual(name, service_data['name'])
+ self.assertIn('type', service_data)
+ self.assertEqual(type, service_data['type'])
+ self.assertIn('description', service_data)
+ self.assertEqual(description, service_data['description'])
+ # Get service
+ resp, fetched_service = self.client.get_service(service_data['id'])
+ self.assertTrue(resp['status'].startswith('2'))
+ # verifying the existence of service created
+ self.assertIn('id', fetched_service)
+ self.assertEqual(fetched_service['id'], service_data['id'])
+ self.assertIn('name', fetched_service)
+ self.assertEqual(fetched_service['name'], service_data['name'])
+ self.assertIn('type', fetched_service)
+ self.assertEqual(fetched_service['type'], service_data['type'])
+ self.assertIn('description', fetched_service)
+ self.assertEqual(fetched_service['description'],
+ service_data['description'])
@attr(type='smoke')
def test_list_services(self):