Add support for tests defining resource_cleanup

Add support for tests having a resource_cleanup method. Whether this
is run or not is dictated by the class variable
'run_resource_cleanup'. By default this will be True but any test
can switch it to False so that resources created by the tests are
preserved. Helpful for debugging failures.

FWIW future work might include automatically disabling resource
cleanup if a test fail is detected.
This commit is contained in:
Liam Young
2019-11-27 09:05:01 +00:00
parent 0022d3bdaf
commit 39cb8ea0cc

View File

@@ -93,6 +93,24 @@ def audit_assertions(action,
class OpenStackBaseTest(unittest.TestCase):
"""Generic helpers for testing OpenStack API charms."""
@classmethod
def resource_cleanup(cls):
"""Cleanup any resources created during the test run.
Override this method with a method which removes any resources
which were created during the test run. If the test sets
"self.run_resource_cleanup = False" then cleanup will be
skipped.
"""
pass
@classmethod
def tearDown(cls):
"""Run teardown for test class."""
if cls.run_resource_cleanup:
logging.info('Running resource cleanup')
cls.resource_cleanup()
@classmethod
def setUpClass(cls, application_name=None):
"""Run setup for test class to create common resourcea."""
@@ -106,6 +124,7 @@ class OpenStackBaseTest(unittest.TestCase):
cls.lead_unit = model.get_lead_unit_name(
cls.application_name,
model_name=cls.model_name)
cls.run_resource_cleanup = True
logging.debug('Leader unit is {}'.format(cls.lead_unit))
@contextlib.contextmanager