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.
This commit is contained in:
Liam Young
2020-04-01 10:18:30 +00:00
parent a3f1230aea
commit be8889e581
2 changed files with 45 additions and 47 deletions

View File

@@ -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))

View File

@@ -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.