From 39cb8ea0ccbf2c2fc8894f9bb721509e68d54041 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Wed, 27 Nov 2019 09:05:01 +0000 Subject: [PATCH 1/2] 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. --- zaza/openstack/charm_tests/test_utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/zaza/openstack/charm_tests/test_utils.py b/zaza/openstack/charm_tests/test_utils.py index eff6b41..74d880f 100644 --- a/zaza/openstack/charm_tests/test_utils.py +++ b/zaza/openstack/charm_tests/test_utils.py @@ -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 From e839a19321e4f86aa47e9a51c34971f2ed7737fb Mon Sep 17 00:00:00 2001 From: Liam Young Date: Wed, 27 Nov 2019 10:29:06 +0000 Subject: [PATCH 2/2] Flip default for cleanup to False --- zaza/openstack/charm_tests/test_utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zaza/openstack/charm_tests/test_utils.py b/zaza/openstack/charm_tests/test_utils.py index 74d880f..c800bee 100644 --- a/zaza/openstack/charm_tests/test_utils.py +++ b/zaza/openstack/charm_tests/test_utils.py @@ -99,8 +99,8 @@ class OpenStackBaseTest(unittest.TestCase): 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. + "self.run_resource_cleanup = True" then cleanup will be + performed. """ pass @@ -124,7 +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 + cls.run_resource_cleanup = False logging.debug('Leader unit is {}'.format(cls.lead_unit)) @contextlib.contextmanager