charm-neutron-gateway/unit_tests/test_actions.py
Alex Kavanagh 86b3e38a54 Remove load-time mocking from unit tests
This patchset removes the load-time mocking necessary due to load-time
function calls in 'constants'.  The constants are changed to run-time
functions and and the lazy feature of restart_on_change is used, and the
harden disable for unit testing is switched on when needed.

Change-Id: I73e719ed082a7c8f9c477fd82094532bdbb8029b
2019-08-23 17:02:20 +01:00

81 lines
2.5 KiB
Python

import sys
import mock
from mock import MagicMock
from test_utils import CharmTestCase
# python-apt is not installed as part of test-requirements but is imported by
# some charmhelpers modules so create a fake import.
sys.modules['apt'] = MagicMock()
sys.modules['apt_pkg'] = MagicMock()
import actions
class PauseTestCase(CharmTestCase):
def setUp(self):
super(PauseTestCase, self).setUp(
actions, ["pause_unit_helper",
"charmhelpers.core.hookenv.log",
"charmhelpers.core.hookenv.status_set",
"charmhelpers.core.hookenv.config"])
self.patch_object(None, "actions.register_configs",
name="register_configs",
return_value='test-config')
def test_pauses_services(self):
actions.pause([])
self.pause_unit_helper.assert_called_once_with('test-config')
class ResumeTestCase(CharmTestCase):
def setUp(self):
super(ResumeTestCase, self).setUp(
actions, ["resume_unit_helper",
"charmhelpers.core.hookenv.log",
"charmhelpers.core.hookenv.status_set",
"charmhelpers.core.hookenv.config"])
self.patch_object(None, "actions.register_configs",
name="register_configs",
return_value='test-config')
def test_resumes_services(self):
actions.resume([])
self.resume_unit_helper.assert_called_once_with('test-config')
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"])