Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_monit.py

raw
1 # Copyright (c) Ansible project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import unittest
8 from unittest import mock
9
10 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
11 AnsibleExitJson,
12 AnsibleFailJson,
13 )
14
15 from ansible_collections.community.general.plugins.modules import monit
16
17 from .uthelper import RunCommandMock, UTHelper
18
19 UTHelper.from_module(monit, __name__, mocks=[RunCommandMock])
20
21
22 class MonitTest(unittest.TestCase):
23 def setUp(self):
24 self.module = mock.MagicMock()
25 self.module.exit_json.side_effect = AnsibleExitJson
26 self.module.fail_json.side_effect = AnsibleFailJson
27 self.module.run_command.return_value = (0, "This is monit version 5.26.0", "")
28 self.monit = monit.Monit(self.module, "monit", "processX", 1)
29 self.monit._status_change_retry_count = 1
30 mock_sleep = mock.patch("time.sleep")
31 mock_sleep.start()
32 self.addCleanup(mock_sleep.stop)
33
34 def patch_status(self, side_effect):
35 if not isinstance(side_effect, list):
36 side_effect = [side_effect]
37 return mock.patch.object(self.monit, "get_status", side_effect=side_effect)
38
39 def test_change_state_fail(self):
40 with self.patch_status([monit.Status("OK")] * 3):
41 with self.assertRaises(AnsibleFailJson):
42 self.monit.stop()
43
44 def test_wait_for_status_to_stop_pending(self):
45 status = [
46 monit.Status("MISSING"),
47 monit.Status("DOES_NOT_EXIST"),
48 monit.Status("INITIALIZING"),
49 monit.Status("OK").pending(),
50 monit.Status("OK"),
51 ]
52 with self.patch_status(status) as get_status:
53 self.monit.wait_for_monit_to_stop_pending()
54 self.assertEqual(get_status.call_count, len(status))
55
56 def test_wait_for_status_change_fail(self):
57 with self.patch_status([monit.Status("OK")] * 3):
58 with self.assertRaises(AnsibleFailJson):
59 self.monit.wait_for_status_change(monit.Status("OK"))
60
61 def test_monitor(self):
62 with self.patch_status([monit.Status("NOT_MONITORED"), monit.Status("OK").pending(), monit.Status("OK")]):
63 with self.assertRaises(AnsibleExitJson):
64 self.monit.monitor()
65
66 def test_monitor_fail(self):
67 with self.patch_status([monit.Status("NOT_MONITORED")] * 3):
68 with self.assertRaises(AnsibleFailJson):
69 self.monit.monitor()