]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Fix error reporting in @stack_delete_after unit tests
authorZane Bitter <zbitter@redhat.com>
Fri, 3 May 2013 13:57:57 +0000 (15:57 +0200)
committerZane Bitter <zbitter@redhat.com>
Fri, 3 May 2013 15:34:38 +0000 (17:34 +0200)
If a unit test decorated with @stack_delete_after (to ensure that a stack
being tested is cleaned up) fails in such a way that the stack cannot be
cleaned up, the original error was suppressed, making debugging very
difficult. In this case, we should re-raise the original exception.

Change-Id: I2b0c326f819cd8a6bc772fe52deb14fb1c41bf93

heat/tests/utils.py

index 3b5d5ffdb3101949ee679836f2feaaca3fc57f78..583d8939d84b1ccaeef5e38e38aaee78343e14c9 100644 (file)
@@ -12,6 +12,8 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+import sys
+import functools
 
 from testtools import skipIf
 
@@ -40,15 +42,24 @@ def stack_delete_after(test_fn):
     Decorator which calls test class self.stack.delete()
     to ensure tests clean up their stacks regardless of test success/failure
     """
-    def wrapped_test(test_cls):
+    @functools.wraps(test_fn)
+    def wrapped_test(test_case, *args, **kwargs):
+        def delete_stack():
+            stack = getattr(test_case, 'stack', None)
+            if stack is not None and stack.id is not None:
+                stack.delete()
+
         try:
-            test_fn(test_cls)
-        finally:
+            test_fn(test_case, *args, **kwargs)
+        except:
+            exc_class, exc_val, exc_tb = sys.exc_info()
             try:
-                if test_cls.stack.id is not None:
-                    test_cls.stack.delete()
-            except AttributeError:
-                pass
+                delete_stack()
+            finally:
+                raise exc_class, exc_val, exc_tb
+        else:
+            delete_stack()
+
     return wrapped_test