python version compatability

This commit is contained in:
Simon Kelly 2020-10-15 09:18:37 +02:00
commit 9210b59ded

View file

@ -3,9 +3,7 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
from unittest import mock import mock
from unittest.mock import MagicMock
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
@ -22,7 +20,7 @@ class AnsibleFailJson(Exception):
class MonitTest(unittest.TestCase): class MonitTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.module = MagicMock() self.module = mock.MagicMock()
self.module.exit_json.side_effect = AnsibleExitJson(Exception) self.module.exit_json.side_effect = AnsibleExitJson(Exception)
self.monit = monit.Monit(self.module, 'monit', 'processX', 1) self.monit = monit.Monit(self.module, 'monit', 'processX', 1)
@ -32,10 +30,11 @@ 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() self.monit.check_version()
self.module.fail_json.assert_called() self.module.fail_json.assert_called_once()
def test_change_state_success(self): def test_change_state_success(self):
with self.patch_status(['not monitored']), self.assertRaises(AnsibleExitJson): with self.patch_status(['not monitored']):
with self.assertRaises(AnsibleExitJson):
self.monit.stop() self.monit.stop()
self.module.fail_json.assert_not_called() self.module.fail_json.assert_not_called()
self.module.run_command.assert_called_with('monit stop processX', check_rc=True) self.module.run_command.assert_called_with('monit stop processX', check_rc=True)
@ -43,7 +42,7 @@ 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() self.monit.stop()
self.module.fail_json.assert_called() self.module.fail_json.assert_called_once()
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')
@ -54,20 +53,23 @@ class MonitTest(unittest.TestCase):
def test_reload(self): def test_reload(self):
self.module.run_command.return_value = (0, '', '') self.module.run_command.return_value = (0, '', '')
self.monit._sleep_time = 0 self.monit._sleep_time = 0
with self.patch_status(['', 'pending', 'running']), self.assertRaises(AnsibleExitJson): with self.patch_status(['', 'pending', 'running']):
with self.assertRaises(AnsibleExitJson):
self.monit.reload() self.monit.reload()
def test_monitor(self): def test_monitor(self):
with self.patch_status(['not monitored - start pending']), self.assertRaises(AnsibleExitJson): with self.patch_status(['not monitored - start pending']):
with self.assertRaises(AnsibleExitJson):
self.monit.monitor() self.monit.monitor()
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() self.monit.monitor()
self.module.fail_json.assert_called() self.module.fail_json.assert_called_once()
def test_timeout(self): def test_timeout(self):
self.monit.timeout = 0 self.monit.timeout = 0
self.module.fail_json.side_effect = AnsibleFailJson(Exception) self.module.fail_json.side_effect = AnsibleFailJson(Exception)
with self.patch_status(['stop pending']), self.assertRaises(AnsibleFailJson): with self.patch_status(['stop pending']):
with self.assertRaises(AnsibleFailJson):
self.monit.wait_for_monit_to_stop_pending('stopped') self.monit.wait_for_monit_to_stop_pending('stopped')