mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-02-04 07:51:50 +00:00
monit: use enum (#11245)
* monit: use enum * make mypy happy about the var type * add changelog frag * typo - this is getting frequent
This commit is contained in:
parent
76589bd97a
commit
3d25aac978
3 changed files with 97 additions and 81 deletions
|
|
@ -41,14 +41,14 @@ class MonitTest(unittest.TestCase):
|
|||
return mock.patch.object(self.monit, "get_status", side_effect=side_effect)
|
||||
|
||||
def test_change_state_success(self):
|
||||
with self.patch_status([monit.Status.OK, monit.Status.NOT_MONITORED]):
|
||||
with self.patch_status([monit.Status("OK"), 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([monit.Status.OK] * 3):
|
||||
with self.patch_status([monit.Status("OK")] * 3):
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
self.monit.stop()
|
||||
|
||||
|
|
@ -59,60 +59,51 @@ class MonitTest(unittest.TestCase):
|
|||
|
||||
def test_reload(self):
|
||||
self.module.run_command.return_value = (0, "", "")
|
||||
with self.patch_status(monit.Status.OK):
|
||||
with self.patch_status(monit.Status("OK")):
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
self.monit.reload()
|
||||
|
||||
def test_wait_for_status_to_stop_pending(self):
|
||||
status = [
|
||||
monit.Status.MISSING,
|
||||
monit.Status.DOES_NOT_EXIST,
|
||||
monit.Status.INITIALIZING,
|
||||
monit.Status.OK.pending(),
|
||||
monit.Status.OK,
|
||||
monit.Status("MISSING"),
|
||||
monit.Status("DOES_NOT_EXIST"),
|
||||
monit.Status("INITIALIZING"),
|
||||
monit.Status("OK").pending(),
|
||||
monit.Status("OK"),
|
||||
]
|
||||
with self.patch_status(status) as get_status:
|
||||
self.monit.wait_for_monit_to_stop_pending()
|
||||
self.assertEqual(get_status.call_count, len(status))
|
||||
|
||||
def test_wait_for_status_change(self):
|
||||
with self.patch_status([monit.Status.NOT_MONITORED, monit.Status.OK]) as get_status:
|
||||
self.monit.wait_for_status_change(monit.Status.NOT_MONITORED)
|
||||
with self.patch_status([monit.Status("NOT_MONITORED"), monit.Status("OK")]) as get_status:
|
||||
self.monit.wait_for_status_change(monit.Status("NOT_MONITORED"))
|
||||
self.assertEqual(get_status.call_count, 2)
|
||||
|
||||
def test_wait_for_status_change_fail(self):
|
||||
with self.patch_status([monit.Status.OK] * 3):
|
||||
with self.patch_status([monit.Status("OK")] * 3):
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
self.monit.wait_for_status_change(monit.Status.OK)
|
||||
self.monit.wait_for_status_change(monit.Status("OK"))
|
||||
|
||||
def test_monitor(self):
|
||||
with self.patch_status([monit.Status.NOT_MONITORED, monit.Status.OK.pending(), monit.Status.OK]):
|
||||
with self.patch_status([monit.Status("NOT_MONITORED"), monit.Status("OK").pending(), monit.Status("OK")]):
|
||||
with self.assertRaises(AnsibleExitJson):
|
||||
self.monit.monitor()
|
||||
|
||||
def test_monitor_fail(self):
|
||||
with self.patch_status([monit.Status.NOT_MONITORED] * 3):
|
||||
with self.patch_status([monit.Status("NOT_MONITORED")] * 3):
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
self.monit.monitor()
|
||||
|
||||
def test_timeout(self):
|
||||
self.monit.timeout = 0
|
||||
with self.patch_status(monit.Status.NOT_MONITORED.pending()):
|
||||
with self.patch_status(monit.Status("NOT_MONITORED").pending()):
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
self.monit.wait_for_monit_to_stop_pending()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("status_name", monit.StatusValue.ALL_STATUS)
|
||||
def test_status_value(status_name):
|
||||
value = getattr(monit.StatusValue, status_name.upper())
|
||||
status = monit.StatusValue(value)
|
||||
assert getattr(status, f"is_{status_name}")
|
||||
assert not all(getattr(status, f"is_{name}") for name in monit.StatusValue.ALL_STATUS if name != status_name)
|
||||
|
||||
|
||||
BASIC_OUTPUT_CASES = [
|
||||
(TEST_OUTPUT % ("Process", "processX", name), getattr(monit.Status, name.upper()))
|
||||
for name in monit.StatusValue.ALL_STATUS
|
||||
(TEST_OUTPUT % ("Process", "processX", member.value), monit.Status(member.name)) for member in monit.StatusValue
|
||||
]
|
||||
|
||||
|
||||
|
|
@ -120,17 +111,20 @@ BASIC_OUTPUT_CASES = [
|
|||
"output, expected",
|
||||
BASIC_OUTPUT_CASES
|
||||
+ [
|
||||
("", monit.Status.MISSING),
|
||||
(TEST_OUTPUT % ("Process", "processY", "OK"), monit.Status.MISSING),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Not Monitored - start pending"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Monitored - stop pending"), monit.Status.NOT_MONITORED),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Monitored - restart pending"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Not Monitored - monitor pending"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Does not exist"), monit.Status.DOES_NOT_EXIST),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Not monitored"), monit.Status.NOT_MONITORED),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Running"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Execution failed | Does not exist"), monit.Status.EXECUTION_FAILED),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Some Unknown Status"), monit.Status.EXECUTION_FAILED),
|
||||
("", monit.Status("MISSING")),
|
||||
(TEST_OUTPUT % ("Process", "processY", "OK"), monit.Status("MISSING")),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Not Monitored - start pending"), monit.Status("OK", is_pending=True)),
|
||||
(
|
||||
TEST_OUTPUT % ("Process", "processX", "Monitored - stop pending"),
|
||||
monit.Status("NOT_MONITORED", is_pending=True),
|
||||
),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Monitored - restart pending"), monit.Status("OK", is_pending=True)),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Not Monitored - monitor pending"), monit.Status("OK", is_pending=True)),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Does not exist"), monit.Status("DOES_NOT_EXIST")),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Not monitored"), monit.Status("NOT_MONITORED")),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Running"), monit.Status("OK")),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Execution failed | Does not exist"), monit.Status("EXECUTION_FAILED")),
|
||||
(TEST_OUTPUT % ("Process", "processX", "Some Unknown Status"), monit.Status("EXECUTION_FAILED")),
|
||||
],
|
||||
)
|
||||
def test_parse_status(output, expected):
|
||||
|
|
@ -143,16 +137,16 @@ def test_parse_status(output, expected):
|
|||
"output, expected",
|
||||
BASIC_OUTPUT_CASES
|
||||
+ [
|
||||
(TEST_OUTPUT % ("Process", "processX", "OK"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("File", "processX", "OK"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("Fifo", "processX", "OK"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("Filesystem", "processX", "OK"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("Directory", "processX", "OK"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("Remote host", "processX", "OK"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("System", "processX", "OK"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("Program", "processX", "OK"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("Network", "processX", "OK"), monit.Status.OK),
|
||||
(TEST_OUTPUT % ("Unsupported", "processX", "OK"), monit.Status.MISSING),
|
||||
(TEST_OUTPUT % ("Process", "processX", "OK"), monit.Status("OK")),
|
||||
(TEST_OUTPUT % ("File", "processX", "OK"), monit.Status("OK")),
|
||||
(TEST_OUTPUT % ("Fifo", "processX", "OK"), monit.Status("OK")),
|
||||
(TEST_OUTPUT % ("Filesystem", "processX", "OK"), monit.Status("OK")),
|
||||
(TEST_OUTPUT % ("Directory", "processX", "OK"), monit.Status("OK")),
|
||||
(TEST_OUTPUT % ("Remote host", "processX", "OK"), monit.Status("OK")),
|
||||
(TEST_OUTPUT % ("System", "processX", "OK"), monit.Status("OK")),
|
||||
(TEST_OUTPUT % ("Program", "processX", "OK"), monit.Status("OK")),
|
||||
(TEST_OUTPUT % ("Network", "processX", "OK"), monit.Status("OK")),
|
||||
(TEST_OUTPUT % ("Unsupported", "processX", "OK"), monit.Status("MISSING")),
|
||||
],
|
||||
)
|
||||
def test_parse_status_supports_all_services(output, expected):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue