Debian Codegraph @

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

raw
1 # Author: Dexter Le ([email protected])
2 # Largely adapted from test_redhat_subscription by
3 # Jiri Hnidek ([email protected])
4 #
5 # Copyright (c) Dexter Le ([email protected])
6 # Copyright (c) Jiri Hnidek ([email protected])
7 #
8 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
9 # SPDX-License-Identifier: GPL-3.0-or-later
10
11 from __future__ import annotations
12
13 import json
14
15 import pytest
16
17 from ansible_collections.community.general.plugins.modules import pacemaker_stonith
18
19 from .uthelper import RunCommandMock, UTHelper
20
21 UTHelper.from_module(pacemaker_stonith, __name__, mocks=[RunCommandMock])
22
23
24 # ---------------------------------------------------------------------------
25 # Race condition tests: resource starts after one or more Stopped polls
26 # ---------------------------------------------------------------------------
27
28
29 @pytest.fixture
30 def patch_bin(mocker):
31 def mockie(self_, path, *args, **kwargs):
32 return f"/testbin/{path}"
33
34 mocker.patch("ansible.module_utils.basic.AnsibleModule.get_bin_path", mockie)
35
36
37 @pytest.mark.usefixtures("patch_bin")
38 def test_present_race_condition_stopped_then_started(mocker, capfd):
39 """Resource reports Stopped on the first poll then Started on the second — must succeed."""
40 mocker.patch("ansible_collections.community.general.plugins.module_utils._pacemaker.time.sleep")
41
42 # Sequence of run_command calls:
43 # 1. initial _get(): stonith status → not found (rc=1)
44 # 2. state_present create → rc=0
45 # 3. wait_for_resource poll 1: status → Stopped (not yet running)
46 # 4. wait_for_resource poll 2: status → Started
47 # 5. __quit_module__ _get(): status → Started
48 run_command_calls = [
49 (1, "", ""),
50 (0, "", ""),
51 (0, " * virtual-stonith\t(stonith:fence_virt):\t Stopped", ""),
52 (0, " * virtual-stonith\t(stonith:fence_virt):\t Started", ""),
53 (0, " * virtual-stonith\t(stonith:fence_virt):\t Started", ""),
54 ]
55
56 def side_effect(self_, **kwargs):
57 return run_command_calls.pop(0)
58
59 mocker.patch("ansible.module_utils.basic.AnsibleModule.run_command", side_effect=side_effect)
60
61 with pytest.raises(SystemExit):
62 with pytest.MonkeyPatch().context() as mp:
63 mp.setattr(
64 "ansible.module_utils.basic._ANSIBLE_ARGS",
65 json.dumps(
66 {
67 "ANSIBLE_MODULE_ARGS": {
68 "state": "present",
69 "name": "virtual-stonith",
70 "stonith_type": "fence_virt",
71 "stonith_options": ["pcmk_host_list=f1"],
72 "wait": 30,
73 }
74 }
75 ).encode(),
76 )
77 mp.setattr("ansible.module_utils.basic._ANSIBLE_PROFILE", "legacy", raising=False)
78 pacemaker_stonith.main()
79
80 out, _err = capfd.readouterr()
81 result = json.loads(out)
82 assert result["changed"] is True
83 assert result.get("failed") is not True
84 assert "Started" in result["value"]
85
86
87 @pytest.mark.usefixtures("patch_bin")
88 def test_present_wait_timeout_raises(mocker, capfd):
89 """Resource never starts within the wait window — must fail with a timeout message."""
90 mocker.patch("ansible_collections.community.general.plugins.module_utils._pacemaker.time.sleep")
91
92 # Simulate time advancing past the deadline immediately on the first poll
93 monotonic_values = iter([0.0, 999.0])
94 mocker.patch(
95 "ansible_collections.community.general.plugins.module_utils._pacemaker.time.monotonic",
96 side_effect=lambda: next(monotonic_values),
97 )
98
99 # Sequence: initial _get() → not found, create → rc=0, poll → Stopped (deadline exceeded)
100 run_command_calls = [
101 (1, "", ""),
102 (0, "", ""),
103 (0, " * virtual-stonith\t(stonith:fence_virt):\t Stopped", ""),
104 ]
105
106 def side_effect(self_, **kwargs):
107 return run_command_calls.pop(0)
108
109 mocker.patch("ansible.module_utils.basic.AnsibleModule.run_command", side_effect=side_effect)
110
111 with pytest.raises(SystemExit):
112 with pytest.MonkeyPatch().context() as mp:
113 mp.setattr(
114 "ansible.module_utils.basic._ANSIBLE_ARGS",
115 json.dumps(
116 {
117 "ANSIBLE_MODULE_ARGS": {
118 "state": "present",
119 "name": "virtual-stonith",
120 "stonith_type": "fence_virt",
121 "stonith_options": ["pcmk_host_list=f1"],
122 "wait": 10,
123 }
124 }
125 ).encode(),
126 )
127 mp.setattr("ansible.module_utils.basic._ANSIBLE_PROFILE", "legacy", raising=False)
128 pacemaker_stonith.main()
129
130 out, _err = capfd.readouterr()
131 result = json.loads(out)
132 assert result.get("failed") is True
133 assert "Timed out" in result["msg"]
134 assert "virtual-stonith" in result["msg"]