From 588d38d4d7250033565f9898f582987ef449dafe Mon Sep 17 00:00:00 2001 From: Liam Young Date: Wed, 1 Apr 2020 08:58:36 +0000 Subject: [PATCH 1/3] Add custom assertions Add custom assertions to check that remote actions and commands run ok. --- zaza/openstack/charm_tests/test_utils.py | 56 +++++++++++++++++++++--- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/zaza/openstack/charm_tests/test_utils.py b/zaza/openstack/charm_tests/test_utils.py index ac8eb27..c64ff31 100644 --- a/zaza/openstack/charm_tests/test_utils.py +++ b/zaza/openstack/charm_tests/test_utils.py @@ -89,7 +89,53 @@ def audit_assertions(action, assert value == "PASS", "Unexpected failure: {}".format(key) -class BaseCharmTest(unittest.TestCase): +class CharmTestAssertions: + """Custom assertions to support zaza testings.""" + + def assertActionRanOK(self, action): + """Assert that the remote action ran successfully. + + Example usage:: + + self.assertActionRanOK(model.run_action( + unit, + 'pause', + model_name=self.model_name)) + + self.assertActionRanOK(model.run_action_on_leader( + unit, + 'pause', + model_name=self.model_name)) + + :param action: Action object to check. + :type action: juju.action.Action + :raises: AssertionError if the assertion fails. + """ + print("Checking action") + if action.status != 'completed': + msg = ("Action '{name}' exited with status '{status}': " + "'{message}'").format(**action.data) + raise AssertionError(msg) + + def assertRemoteRunOK(self, run_output): + """Use with zaza.model.run_on_unit. + + Example usage:: + + self.assertRemoteRunOK(zaza.model.run_on_unit( + unit, + 'ls /tmp/')) + + :param action: Dict returned from remote run. + :type action: dict + :raises: AssertionError if the assertion fails. + """ + print("Checking remote run") + if int(run_output['Code']) != 0: + raise AssertionError("Command failed: {}".format(run_output)) + + +class BaseCharmTest(unittest.TestCase, CharmTestAssertions): """Generic helpers for testing charms.""" run_resource_cleanup = False @@ -350,10 +396,10 @@ class BaseCharmTest(unittest.TestCase): self.lead_unit, 'active', model_name=self.model_name) - model.run_action( + self.assertActionRanOK(model.run_action( self.lead_unit, 'pause', - model_name=self.model_name) + model_name=self.model_name)) model.block_until_unit_wl_status( self.lead_unit, 'maintenance', @@ -366,10 +412,10 @@ class BaseCharmTest(unittest.TestCase): model_name=self.model_name, pgrep_full=pgrep_full) yield - model.run_action( + self.assertActionRanOK(model.run_action( self.lead_unit, 'resume', - model_name=self.model_name) + model_name=self.model_name)) model.block_until_unit_wl_status( self.lead_unit, 'active', From a3f1230aea5561505225543c72f1a3d692fcc6f1 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Wed, 1 Apr 2020 09:08:10 +0000 Subject: [PATCH 2/3] Remove prints --- zaza/openstack/charm_tests/test_utils.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/zaza/openstack/charm_tests/test_utils.py b/zaza/openstack/charm_tests/test_utils.py index c64ff31..d94ded7 100644 --- a/zaza/openstack/charm_tests/test_utils.py +++ b/zaza/openstack/charm_tests/test_utils.py @@ -111,7 +111,6 @@ class CharmTestAssertions: :type action: juju.action.Action :raises: AssertionError if the assertion fails. """ - print("Checking action") if action.status != 'completed': msg = ("Action '{name}' exited with status '{status}': " "'{message}'").format(**action.data) @@ -130,7 +129,6 @@ class CharmTestAssertions: :type action: dict :raises: AssertionError if the assertion fails. """ - print("Checking remote run") if int(run_output['Code']) != 0: raise AssertionError("Command failed: {}".format(run_output)) From be8889e581a09d7ebf50c34415aa22c19a0f1623 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Wed, 1 Apr 2020 10:18:30 +0000 Subject: [PATCH 3/3] Move new methods out of test class The new methods assertActionRanOK & assertRemoteRunOK are useful in setup as well as tests so it makes sense to move them out of the test class. --- zaza/openstack/charm_tests/test_utils.py | 50 ++---------------------- zaza/openstack/utilities/generic.py | 42 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/zaza/openstack/charm_tests/test_utils.py b/zaza/openstack/charm_tests/test_utils.py index d94ded7..2a22ab1 100644 --- a/zaza/openstack/charm_tests/test_utils.py +++ b/zaza/openstack/charm_tests/test_utils.py @@ -89,51 +89,7 @@ def audit_assertions(action, assert value == "PASS", "Unexpected failure: {}".format(key) -class CharmTestAssertions: - """Custom assertions to support zaza testings.""" - - def assertActionRanOK(self, action): - """Assert that the remote action ran successfully. - - Example usage:: - - self.assertActionRanOK(model.run_action( - unit, - 'pause', - model_name=self.model_name)) - - self.assertActionRanOK(model.run_action_on_leader( - unit, - 'pause', - model_name=self.model_name)) - - :param action: Action object to check. - :type action: juju.action.Action - :raises: AssertionError if the assertion fails. - """ - if action.status != 'completed': - msg = ("Action '{name}' exited with status '{status}': " - "'{message}'").format(**action.data) - raise AssertionError(msg) - - def assertRemoteRunOK(self, run_output): - """Use with zaza.model.run_on_unit. - - Example usage:: - - self.assertRemoteRunOK(zaza.model.run_on_unit( - unit, - 'ls /tmp/')) - - :param action: Dict returned from remote run. - :type action: dict - :raises: AssertionError if the assertion fails. - """ - if int(run_output['Code']) != 0: - raise AssertionError("Command failed: {}".format(run_output)) - - -class BaseCharmTest(unittest.TestCase, CharmTestAssertions): +class BaseCharmTest(unittest.TestCase): """Generic helpers for testing charms.""" run_resource_cleanup = False @@ -394,7 +350,7 @@ class BaseCharmTest(unittest.TestCase, CharmTestAssertions): self.lead_unit, 'active', model_name=self.model_name) - self.assertActionRanOK(model.run_action( + generic_utils.assertActionRanOK(model.run_action( self.lead_unit, 'pause', model_name=self.model_name)) @@ -410,7 +366,7 @@ class BaseCharmTest(unittest.TestCase, CharmTestAssertions): model_name=self.model_name, pgrep_full=pgrep_full) yield - self.assertActionRanOK(model.run_action( + generic_utils.assertActionRanOK(model.run_action( self.lead_unit, 'resume', model_name=self.model_name)) diff --git a/zaza/openstack/utilities/generic.py b/zaza/openstack/utilities/generic.py index 3a22404..e342369 100644 --- a/zaza/openstack/utilities/generic.py +++ b/zaza/openstack/utilities/generic.py @@ -28,6 +28,48 @@ from zaza.openstack.utilities import exceptions as zaza_exceptions from zaza.openstack.utilities.os_versions import UBUNTU_OPENSTACK_RELEASE +def assertActionRanOK(action): + """Assert that the remote action ran successfully. + + Example usage:: + + self.assertActionRanOK(model.run_action( + unit, + 'pause', + model_name=self.model_name)) + + self.assertActionRanOK(model.run_action_on_leader( + unit, + 'pause', + model_name=self.model_name)) + + :param action: Action object to check. + :type action: juju.action.Action + :raises: AssertionError if the assertion fails. + """ + if action.status != 'completed': + msg = ("Action '{name}' exited with status '{status}': " + "'{message}'").format(**action.data) + raise AssertionError(msg) + + +def assertRemoteRunOK(run_output): + """Use with zaza.model.run_on_unit. + + Example usage:: + + self.assertRemoteRunOK(zaza.model.run_on_unit( + unit, + 'ls /tmp/')) + + :param action: Dict returned from remote run. + :type action: dict + :raises: AssertionError if the assertion fails. + """ + if int(run_output['Code']) != 0: + raise AssertionError("Command failed: {}".format(run_output)) + + def dict_to_yaml(dict_data): """Return YAML from dictionary.