Pause resource cleanup

When pause_teardown flag in tempest.conf is set to True a pdb breakpoint
is added to tearDown and tearDownClass methods in test.py.
This allows to pause cleaning resources process, so that, used resources
can be examined. Closer examination of used resources may lead to faster
debugging.

Change-Id: I09b7721e64cda161289f915d30888ec54bbed821
diff --git a/releasenotes/notes/pause_teardown-45c9d60ffa889f7f.yaml b/releasenotes/notes/pause_teardown-45c9d60ffa889f7f.yaml
new file mode 100644
index 0000000..a540c7d
--- /dev/null
+++ b/releasenotes/notes/pause_teardown-45c9d60ffa889f7f.yaml
@@ -0,0 +1,9 @@
+---
+features:
+  - |
+    Pause teardown
+    When pause_teardown flag in tempest.conf is set to True a pdb breakpoint
+    is added to tearDown and tearDownClass methods in test.py.
+    This allows to pause cleaning resources process, so that used resources
+    can be examined. Closer examination of used resources may lead to faster
+    debugging.
diff --git a/tempest/config.py b/tempest/config.py
index fbe0a1b..fdf6f0a 100644
--- a/tempest/config.py
+++ b/tempest/config.py
@@ -1070,6 +1070,15 @@
                                  "prefix to ideintify resources which are "
                                  "created by Tempest and no projects set "
                                  "this option on OpenStack dev community."),
+    cfg.BoolOpt('pause_teardown',
+                default=False,
+                help="""Whether to pause a test in global teardown.
+
+The best use case is investigating used resources of one test.
+A test can be run as follows:
+ $ ostestr --pdb TEST_ID
+or
+ $ python -m testtools.run TEST_ID"""),
 ]
 
 _opts = [
diff --git a/tempest/test.py b/tempest/test.py
index f07c071..a81b5d7 100644
--- a/tempest/test.py
+++ b/tempest/test.py
@@ -247,6 +247,9 @@
 
     @classmethod
     def tearDownClass(cls):
+        # insert pdb breakpoint when pause_teardown is enabled
+        if CONF.pause_teardown:
+            cls.insert_pdb_breakpoint()
         at_exit_set.discard(cls)
         # It should never be overridden by descendants
         if hasattr(super(BaseTestCase, cls), 'tearDownClass'):
@@ -283,6 +286,22 @@
             finally:
                 del trace  # to avoid circular refs
 
+    def tearDown(self):
+        super(BaseTestCase, self).tearDown()
+        # insert pdb breakpoint when pause_teardown is enabled
+        if CONF.pause_teardown:
+            BaseTestCase.insert_pdb_breakpoint()
+
+    @classmethod
+    def insert_pdb_breakpoint(cls):
+        """Add pdb breakpoint.
+
+        This can help in debugging process, cleaning of resources is
+        paused, so they can be examined.
+        """
+        import pdb
+        pdb.set_trace()
+
     @classmethod
     def skip_checks(cls):
         """Class level skip checks.