Fix test_volumes_get volumes cleanup.

Currently for test_volumes_get if the volume creation fails and we won't define
the volume variable which will result in an undefined error in the finally
block that always gets run. This will mask the actual cause of the test
failure. This commit fixes this by reworking the cleanup logic in the tests
to use the addCleanup so if the api call fails the actual error will be printed.

Change-Id: I2619575fe80d4f3ce38cc951e7dbd44311d61432
diff --git a/tempest/api/compute/volumes/test_volumes_get.py b/tempest/api/compute/volumes/test_volumes_get.py
index a6302e6..1acc57d 100644
--- a/tempest/api/compute/volumes/test_volumes_get.py
+++ b/tempest/api/compute/volumes/test_volumes_get.py
@@ -33,72 +33,68 @@
     def test_volume_create_get_delete(self):
         # CREATE, GET, DELETE Volume
         volume = None
-        try:
-            v_name = rand_name('Volume-%s-') % self._interface
-            metadata = {'Type': 'work'}
-            #Create volume
-            resp, volume = self.client.create_volume(size=1,
-                                                     display_name=v_name,
-                                                     metadata=metadata)
-            self.assertEqual(200, resp.status)
-            self.assertTrue('id' in volume)
-            self.assertTrue('displayName' in volume)
-            self.assertEqual(volume['displayName'], v_name,
-                             "The created volume name is not equal "
-                             "to the requested name")
-            self.assertTrue(volume['id'] is not None,
-                            "Field volume id is empty or not found.")
-            #Wait for Volume status to become ACTIVE
-            self.client.wait_for_volume_status(volume['id'], 'available')
-            #GET Volume
-            resp, fetched_volume = self.client.get_volume(volume['id'])
-            self.assertEqual(200, resp.status)
-            #Verfication of details of fetched Volume
-            self.assertEqual(v_name,
-                             fetched_volume['displayName'],
-                             'The fetched Volume is different '
-                             'from the created Volume')
-            self.assertEqual(volume['id'],
-                             fetched_volume['id'],
-                             'The fetched Volume is different '
-                             'from the created Volume')
-            self.assertEqual(metadata,
-                             fetched_volume['metadata'],
-                             'The fetched Volume is different '
-                             'from the created Volume')
-
-        finally:
-            if volume:
-                #Delete the Volume created in this method
-                resp, _ = self.client.delete_volume(volume['id'])
-                self.assertEqual(202, resp.status)
-                #Checking if the deleted Volume still exists
-                self.client.wait_for_resource_deletion(volume['id'])
+        v_name = rand_name('Volume-%s-') % self._interface
+        metadata = {'Type': 'work'}
+        #Create volume
+        resp, volume = self.client.create_volume(size=1,
+                                                 display_name=v_name,
+                                                 metadata=metadata)
+        self.addCleanup(self._delete_volume, volume)
+        self.assertEqual(200, resp.status)
+        self.assertTrue('id' in volume)
+        self.assertTrue('displayName' in volume)
+        self.assertEqual(volume['displayName'], v_name,
+                         "The created volume name is not equal "
+                         "to the requested name")
+        self.assertTrue(volume['id'] is not None,
+                        "Field volume id is empty or not found.")
+        #Wait for Volume status to become ACTIVE
+        self.client.wait_for_volume_status(volume['id'], 'available')
+        #GET Volume
+        resp, fetched_volume = self.client.get_volume(volume['id'])
+        self.assertEqual(200, resp.status)
+        #Verfication of details of fetched Volume
+        self.assertEqual(v_name,
+                         fetched_volume['displayName'],
+                         'The fetched Volume is different '
+                         'from the created Volume')
+        self.assertEqual(volume['id'],
+                         fetched_volume['id'],
+                         'The fetched Volume is different '
+                         'from the created Volume')
+        self.assertEqual(metadata,
+                         fetched_volume['metadata'],
+                         'The fetched Volume is different '
+                         'from the created Volume')
 
     @attr(type='gate')
     def test_volume_get_metadata_none(self):
         # CREATE, GET empty metadata dict
+        v_name = rand_name('Volume-')
+        #Create volume
+        resp, volume = self.client.create_volume(size=1,
+                                                 display_name=v_name,
+                                                 metadata={})
+        self.addCleanup(self._delete_volume, volume)
+        self.assertEqual(200, resp.status)
+        self.assertTrue('id' in volume)
+        self.assertTrue('displayName' in volume)
+        #Wait for Volume status to become ACTIVE
+        self.client.wait_for_volume_status(volume['id'], 'available')
+        #GET Volume
+        resp, fetched_volume = self.client.get_volume(volume['id'])
+        self.assertEqual(200, resp.status)
+        self.assertEqual(fetched_volume['metadata'], {})
+
+    def _delete_volume(self, volume):
+        #Delete the Volume created in this method
         try:
-            v_name = rand_name('Volume-')
-            #Create volume
-            resp, volume = self.client.create_volume(size=1,
-                                                     display_name=v_name,
-                                                     metadata={})
-            self.assertEqual(200, resp.status)
-            self.assertTrue('id' in volume)
-            self.assertTrue('displayName' in volume)
-            #Wait for Volume status to become ACTIVE
-            self.client.wait_for_volume_status(volume['id'], 'available')
-            #GET Volume
-            resp, fetched_volume = self.client.get_volume(volume['id'])
-            self.assertEqual(200, resp.status)
-            self.assertEqual(fetched_volume['metadata'], {})
-        finally:
-            #Delete the Volume created in this method
             resp, _ = self.client.delete_volume(volume['id'])
             self.assertEqual(202, resp.status)
             #Checking if the deleted Volume still exists
             self.client.wait_for_resource_deletion(volume['id'])
+        except KeyError:
+            return
 
 
 class VolumesGetTestXML(VolumesGetTestJSON):