Files
charm-percona-cluster/unit_tests/test_actions.py
Hervé Beraud 6ff8acbd6a Use unittest.mock instead of mock
The mock third party library was needed for mock support in py2
runtimes. Since we now only support py36 and later, we can use the
standard lib unittest.mock module instead.

Note that https://github.com/openstack/charms.openstack is used during tests
and he need `mock`, unfortunatelly it doesn't declare `mock` in its
requirements so it retrieve mock from other charm project (cross dependency).
So we depend on charms.openstack first and when
Ib1ed5b598a52375e29e247db9ab4786df5b6d142 will be merged then CI
will pass without errors.

Depends-On: Ib1ed5b598a52375e29e247db9ab4786df5b6d142
Change-Id: Ie96a81d19be4f14efc7067ddb9c47827f5255ccf
2021-12-15 11:11:14 +00:00

141 lines
5.1 KiB
Python

from unittest import mock
from unittest.mock import patch
from test_utils import CharmTestCase
# we have to patch out harden decorator because hooks/percona_hooks.py gets
# imported via actions.py and will freak out if it trys to run in the context
# of a test.
with patch('charmhelpers.contrib.hardening.harden.harden') as mock_dec:
mock_dec.side_effect = (lambda *dargs, **dkwargs: lambda f:
lambda *args, **kwargs: f(*args, **kwargs))
from actions import actions
class PauseTestCase(CharmTestCase):
def setUp(self):
super(PauseTestCase, self).setUp(
actions.percona_utils, ["pause_unit_helper", "register_configs"])
def test_pauses_services(self):
self.register_configs.return_value = "test-config"
actions.pause([])
self.pause_unit_helper.assert_called_once_with('test-config')
class ResumeTestCase(CharmTestCase):
def setUp(self):
super(ResumeTestCase, self).setUp(
actions.percona_utils, ["resume_unit_helper", "register_configs"])
def test_pauses_services(self):
self.register_configs.return_value = "test-config"
with patch('actions.actions.percona_hooks.config_changed'
) as config_changed:
actions.resume([])
self.resume_unit_helper.assert_called_once_with('test-config')
config_changed.assert_called_once_with()
class CompleteClusterSeriesUpgrade(CharmTestCase):
def setUp(self):
to_patch = [
"is_leader",
"leader_set",
"relation_ids",
"relation_set"]
super(CompleteClusterSeriesUpgrade, self).setUp(
actions, to_patch)
def test_leader_complete_series_upgrade(self):
self.is_leader.return_value = True
self.relation_ids.return_value = ['relid:1']
calls = [mock.call(cluster_series_upgrading=""),
mock.call(cluster_series_upgrade_leader="")]
with patch('actions.actions.percona_hooks.config_changed'
) as config_changed:
actions.complete_cluster_series_upgrade([])
self.leader_set.assert_has_calls(calls)
config_changed.assert_called_once_with()
self.relation_set.assert_called_once_with(
relation_id='relid:1',
relation_settings={'cluster-series-upgrading': None})
def test_non_leader_complete_series_upgrade(self):
self.is_leader.return_value = False
with patch('actions.actions.percona_hooks.config_changed'
) as config_changed:
actions.complete_cluster_series_upgrade([])
self.leader_set.assert_not_called()
config_changed.assert_called_once_with()
class MainTestCase(CharmTestCase):
def setUp(self):
super(MainTestCase, self).setUp(actions, ["action_fail"])
def test_invokes_action(self):
dummy_calls = []
def dummy_action(args):
dummy_calls.append(True)
with mock.patch.dict(actions.ACTIONS, {"foo": dummy_action}):
actions.main(["foo"])
self.assertEqual(dummy_calls, [True])
def test_unknown_action(self):
"""Unknown actions aren't a traceback."""
exit_string = actions.main(["foo"])
self.assertEqual("Action foo undefined", exit_string)
def test_failing_action(self):
"""Actions which traceback trigger action_fail() calls."""
dummy_calls = []
self.action_fail.side_effect = dummy_calls.append
def dummy_action(args):
raise ValueError("uh oh")
with mock.patch.dict(actions.ACTIONS, {"foo": dummy_action}):
actions.main(["foo"])
self.assertEqual(dummy_calls, ["Action foo failed: uh oh"])
class NagiosTestCase(CharmTestCase):
def setUp(self):
super(NagiosTestCase, self).setUp(actions,
["action_set",
"action_fail",
"is_leader",
"leader_set",
"pwgen",
])
@patch.object(actions.percona_utils, "set_nagios_user")
def test_generate_nagios_password(self, mock_set_nagios_user):
"""Test regenerate new password for nagios user."""
self.is_leader.return_value = True
self.pwgen.return_value = "1234"
actions.generate_nagios_password([])
self.leader_set.assert_called_once_with(
{"mysql-nagios.passwd": "1234"})
mock_set_nagios_user.assert_called_once_with()
self.action_set.assert_called_once_with(
{"output": "New password for nagios created successfully."}
)
def test_generate_nagios_password_no_leader(self):
"""Test regenerate new password for nagios user at no leader unit."""
self.is_leader.return_value = False
actions.generate_nagios_password([])
self.action_fail.assert_called_once_with(
"This action should only take place on the leader unit."
)