use exception classes from utils

This commit is contained in:
Simon Kelly 2020-10-15 09:24:56 +02:00
commit f2c220a925

View file

@ -6,22 +6,14 @@ __metaclass__ = type
import mock import mock
from ansible_collections.community.general.tests.unit.compat import unittest from ansible_collections.community.general.tests.unit.compat import unittest
from ansible_collections.community.general.plugins.modules.monitoring import monit from ansible_collections.community.general.plugins.modules.monitoring import monit
from ansible_collections.community.general.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson
class AnsibleExitJson(Exception):
"""Exception class to be raised by module.exit_json and caught by the test case"""
pass
class AnsibleFailJson(Exception):
"""Exception class to be raised by module.fail_json and caught by the test case"""
pass
class MonitTest(unittest.TestCase): class MonitTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.module = mock.MagicMock() self.module = mock.MagicMock()
self.module.exit_json.side_effect = AnsibleExitJson(Exception) self.module.exit_json.side_effect = AnsibleExitJson
self.module.fail_json.side_effect = AnsibleFailJson
self.monit = monit.Monit(self.module, 'monit', 'processX', 1) self.monit = monit.Monit(self.module, 'monit', 'processX', 1)
def patch_status(self, side_effect): def patch_status(self, side_effect):
@ -29,8 +21,8 @@ class MonitTest(unittest.TestCase):
def test_min_version(self): def test_min_version(self):
with mock.patch.object(self.monit, 'monit_version', return_value=(5, 20)): with mock.patch.object(self.monit, 'monit_version', return_value=(5, 20)):
self.monit.check_version() with self.assertRaises(AnsibleFailJson):
self.module.fail_json.assert_called_once() self.monit.check_version()
def test_change_state_success(self): def test_change_state_success(self):
with self.patch_status(['not monitored']): with self.patch_status(['not monitored']):
@ -41,8 +33,8 @@ class MonitTest(unittest.TestCase):
def test_change_state_fail(self): def test_change_state_fail(self):
with self.patch_status(['monitored']): with self.patch_status(['monitored']):
self.monit.stop() with self.assertRaises(AnsibleFailJson):
self.module.fail_json.assert_called_once() self.monit.stop()
def test_reload_fail(self): def test_reload_fail(self):
self.module.run_command.return_value = (1, 'stdout', 'stderr') self.module.run_command.return_value = (1, 'stdout', 'stderr')
@ -64,8 +56,8 @@ class MonitTest(unittest.TestCase):
def test_monitor_fail(self): def test_monitor_fail(self):
with self.patch_status(['not monitored']): with self.patch_status(['not monitored']):
self.monit.monitor() with self.assertRaises(AnsibleFailJson):
self.module.fail_json.assert_called_once() self.monit.monitor()
def test_timeout(self): def test_timeout(self):
self.monit.timeout = 0 self.monit.timeout = 0