Merge "Clean/leave OpenStack after a stress test"
diff --git a/etc/tempest.conf.sample b/etc/tempest.conf.sample
index fb11d96..990cb37 100644
--- a/etc/tempest.conf.sample
+++ b/etc/tempest.conf.sample
@@ -753,6 +753,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 cb186ca..068193b 100644
--- a/tempest/config.py
+++ b/tempest/config.py
@@ -587,7 +587,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()