Clean/leave OpenStack after a stress test

New parameter "leave_dirty_stack" available in tempest.conf
to prevent (or not) the execution of tearDownClass()
after an exception during a stress test

Closes-Bug: #1273245
Change-Id: I89ecac1fb428cf8809a3634869f2dfdf564aea64
diff --git a/etc/tempest.conf.sample b/etc/tempest.conf.sample
index 48ca905..5ff6d23 100644
--- a/etc/tempest.conf.sample
+++ b/etc/tempest.conf.sample
@@ -748,6 +748,11 @@
 # value)
 #default_thread_number_per_action=4
 
+# Prevent the cleaning (tearDownClass()) between each stress
+# test run if an exception occurs during this run. (boolean
+# value)
+#leave_dirty_stack=false
+
 
 [telemetry]
 
diff --git a/tempest/config.py b/tempest/config.py
index ccb3125..01ba095 100644
--- a/tempest/config.py
+++ b/tempest/config.py
@@ -582,7 +582,12 @@
                help='time (in seconds) between log file error checks.'),
     cfg.IntOpt('default_thread_number_per_action',
                default=4,
-               help='The number of threads created while stress test.')
+               help='The number of threads created while stress test.'),
+    cfg.BoolOpt('leave_dirty_stack',
+                default=False,
+                help='Prevent the cleaning (tearDownClass()) between'
+                     ' each stress test run if an exception occurs'
+                     ' during this run.')
 ]
 
 
diff --git a/tempest/stress/actions/unit_test.py b/tempest/stress/actions/unit_test.py
index 8bd2f22..2f1d28f 100644
--- a/tempest/stress/actions/unit_test.py
+++ b/tempest/stress/actions/unit_test.py
@@ -10,10 +10,13 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from tempest import config
 from tempest.openstack.common import importutils
 from tempest.openstack.common import log as logging
 import tempest.stress.stressaction as stressaction
 
+CONF = config.CONF
+
 
 class SetUpClassRunTime(object):
 
@@ -73,10 +76,14 @@
                 self.klass.setUpClass()
                 self.setupclass_called = True
 
-            self.run_core()
-
-            if (self.class_setup_per == SetUpClassRunTime.action):
-                self.klass.tearDownClass()
+            try:
+                self.run_core()
+            except Exception as e:
+                raise e
+            finally:
+                if (CONF.stress.leave_dirty_stack is False
+                    and self.class_setup_per == SetUpClassRunTime.action):
+                    self.klass.tearDownClass()
         else:
             self.run_core()