1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-07 20:47:16 +00:00

modify monit to use 'status' output instead of 'summary' output

The summary output is a fixed width table which truncates the
contents and prevents us from parsing the actual status of the
program.
This commit is contained in:
Simon Kelly 2020-10-15 17:18:42 +02:00
parent f2c220a925
commit 00152dbb63
2 changed files with 120 additions and 36 deletions

View file

@ -4,11 +4,21 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import mock
import pytest
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.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson
TEST_OUTPUT = """
Process '%s'
status %s
monitoring status Not monitored
monitoring mode active
"""
class MonitTest(unittest.TestCase):
def setUp(self):
self.module = mock.MagicMock()
@ -17,6 +27,8 @@ class MonitTest(unittest.TestCase):
self.monit = monit.Monit(self.module, 'monit', 'processX', 1)
def patch_status(self, side_effect):
if not isinstance(side_effect, list):
side_effect = [side_effect]
return mock.patch.object(self.monit, 'get_status', side_effect=side_effect)
def test_min_version(self):
@ -25,43 +37,75 @@ class MonitTest(unittest.TestCase):
self.monit.check_version()
def test_change_state_success(self):
with self.patch_status(['not monitored']):
with self.patch_status(monit.Status.NOT_MONITORED):
with self.assertRaises(AnsibleExitJson):
self.monit.stop()
self.module.fail_json.assert_not_called()
self.module.run_command.assert_called_with('monit stop processX', check_rc=True)
def test_change_state_fail(self):
with self.patch_status(['monitored']):
with self.patch_status(monit.Status.OK):
with self.assertRaises(AnsibleFailJson):
self.monit.stop()
def test_reload_fail(self):
self.module.run_command.return_value = (1, 'stdout', 'stderr')
self.module.fail_json.side_effect = AnsibleFailJson(Exception)
with self.assertRaises(AnsibleFailJson):
self.monit.reload()
def test_reload(self):
self.module.run_command.return_value = (0, '', '')
self.monit._sleep_time = 0
with self.patch_status(['', 'pending', 'running']):
status = [
monit.Status.MISSING,
monit.Status.INITIALIZING,
monit.Status.OK.pending(),
monit.Status.OK
]
with self.patch_status(status) as get_status:
with self.assertRaises(AnsibleExitJson):
self.monit.reload()
self.assertEqual(get_status.call_count, len(status))
def test_monitor(self):
with self.patch_status(['not monitored - start pending']):
with self.patch_status(monit.Status.OK.pending()):
with self.assertRaises(AnsibleExitJson):
self.monit.monitor()
def test_monitor_fail(self):
with self.patch_status(['not monitored']):
with self.patch_status(monit.Status.NOT_MONITORED):
with self.assertRaises(AnsibleFailJson):
self.monit.monitor()
def test_timeout(self):
self.monit.timeout = 0
self.module.fail_json.side_effect = AnsibleFailJson(Exception)
with self.patch_status(['stop pending']):
with self.patch_status(monit.Status.NOT_MONITORED.pending()):
with self.assertRaises(AnsibleFailJson):
self.monit.wait_for_monit_to_stop_pending('stopped')
@pytest.mark.parametrize('status_name', [name for name in monit.ALL_STATUS])
def test_status_value(status_name):
value = getattr(monit.StatusValue, status_name.upper())
status = monit.StatusValue(value)
assert getattr(status, 'is_%s' % status_name)
assert not all(getattr(status, 'is_%s' % name) for name in monit.ALL_STATUS if name != status_name)
BASIC_OUTPUT_CASES = [
(TEST_OUTPUT % ('processX', name), getattr(monit.Status, name.upper()))
for name in monit.ALL_STATUS
]
@pytest.mark.parametrize('output, expected', BASIC_OUTPUT_CASES + [
('', monit.Status.MISSING),
(TEST_OUTPUT % ('processY', 'OK'), monit.Status.MISSING),
(TEST_OUTPUT % ('processX', 'Not Monitored - start pending'), monit.Status.OK),
(TEST_OUTPUT % ('processX', 'Monitored - stop pending'), monit.Status.NOT_MONITORED),
(TEST_OUTPUT % ('processX', 'Monitored - restart pending'), monit.Status.OK),
(TEST_OUTPUT % ('processX', 'Not Monitored - monitor pending'), monit.Status.OK),
])
def test_parse_status(output, expected):
status = monit.Monit(None, '', 'processX', 0)._parse_status(output)
assert status == expected