Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_pacemaker_resource.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_resource
18
19 from .uthelper import RunCommandMock, UTHelper
20
21 UTHelper.from_module(pacemaker_resource, __name__, mocks=[RunCommandMock])
22
23
24 # ---------------------------------------------------------------------------
25 # Race condition tests: resource starts after one or more Stopped polls
26 # ---------------------------------------------------------------------------
27
28 NO_MAINTENANCE_OUT = (
29 "Cluster Properties: cib-bootstrap-options\n"
30 "cluster-infrastructure=corosync\n"
31 "cluster-name=hacluster\n"
32 "dc-version=2.1.9-1.fc41-7188dbf\n"
33 "have-watchdog=false\n"
34 )
35
36
37 @pytest.fixture
38 def patch_bin(mocker):
39 def mockie(self_, path, *args, **kwargs):
40 return f"/testbin/{path}"
41
42 mocker.patch("ansible.module_utils.basic.AnsibleModule.get_bin_path", mockie)
43
44
45 @pytest.mark.usefixtures("patch_bin")
46 def test_present_race_condition_stopped_then_started(mocker, capfd):
47 """Resource reports Stopped on the first poll then Started on the second — must succeed."""
48 mocker.patch("ansible_collections.community.general.plugins.module_utils._pacemaker.time.sleep")
49
50 # Sequence of run_command calls:
51 # 1. initial _get(): resource status → not found (rc=1)
52 # 2. state_present: property config → no maintenance
53 # 3. state_present: resource create → rc=0
54 # 4. post-create maintenance check → no maintenance
55 # 5. wait_for_resource poll 1: status → Stopped (not yet running)
56 # 6. wait_for_resource poll 2: status → Started
57 # 7. __quit_module__ _get(): status → Started
58 run_command_calls = [
59 (1, "", "Error: resource or tag id 'virtual-ip' not found"),
60 (1, NO_MAINTENANCE_OUT, ""),
61 (0, "Assumed agent name 'ocf:heartbeat:IPaddr2'", ""),
62 (1, NO_MAINTENANCE_OUT, ""),
63 (0, " * virtual-ip\t(ocf:heartbeat:IPAddr2):\t Stopped", ""),
64 (0, " * virtual-ip\t(ocf:heartbeat:IPAddr2):\t Started", ""),
65 (0, " * virtual-ip\t(ocf:heartbeat:IPAddr2):\t Started", ""),
66 ]
67
68 def side_effect(self_, **kwargs):
69 return run_command_calls.pop(0)
70
71 mocker.patch("ansible.module_utils.basic.AnsibleModule.run_command", side_effect=side_effect)
72
73 with pytest.raises(SystemExit):
74 with pytest.MonkeyPatch().context() as mp:
75 mp.setattr(
76 "ansible.module_utils.basic._ANSIBLE_ARGS",
77 json.dumps(
78 {
79 "ANSIBLE_MODULE_ARGS": {
80 "state": "present",
81 "name": "virtual-ip",
82 "resource_type": {"resource_name": "IPaddr2"},
83 "resource_option": ["ip=192.168.2.1"],
84 "wait": 30,
85 }
86 }
87 ).encode(),
88 )
89 mp.setattr("ansible.module_utils.basic._ANSIBLE_PROFILE", "legacy", raising=False)
90 pacemaker_resource.main()
91
92 out, _err = capfd.readouterr()
93 result = json.loads(out)
94 assert result["changed"] is True
95 assert result.get("failed") is not True
96 assert "Started" in result["value"]
97
98
99 @pytest.mark.usefixtures("patch_bin")
100 def test_present_wait_timeout_raises(mocker, capfd):
101 """Resource never starts within the wait window — must fail with a timeout message."""
102 mocker.patch("ansible_collections.community.general.plugins.module_utils._pacemaker.time.sleep")
103
104 # Simulate time advancing past the deadline immediately on the first poll
105 monotonic_values = iter([0.0, 999.0])
106 mocker.patch(
107 "ansible_collections.community.general.plugins.module_utils._pacemaker.time.monotonic",
108 side_effect=lambda: next(monotonic_values),
109 )
110
111 run_command_calls = [
112 (1, "", "Error: resource or tag id 'virtual-ip' not found"),
113 (1, NO_MAINTENANCE_OUT, ""),
114 (0, "Assumed agent name 'ocf:heartbeat:IPaddr2'", ""),
115 (1, NO_MAINTENANCE_OUT, ""),
116 (0, " * virtual-ip\t(ocf:heartbeat:IPAddr2):\t Stopped", ""),
117 ]
118
119 def side_effect(self_, **kwargs):
120 return run_command_calls.pop(0)
121
122 mocker.patch("ansible.module_utils.basic.AnsibleModule.run_command", side_effect=side_effect)
123
124 with pytest.raises(SystemExit):
125 with pytest.MonkeyPatch().context() as mp:
126 mp.setattr(
127 "ansible.module_utils.basic._ANSIBLE_ARGS",
128 json.dumps(
129 {
130 "ANSIBLE_MODULE_ARGS": {
131 "state": "present",
132 "name": "virtual-ip",
133 "resource_type": {"resource_name": "IPaddr2"},
134 "resource_option": ["ip=192.168.2.1"],
135 "wait": 10,
136 }
137 }
138 ).encode(),
139 )
140 mp.setattr("ansible.module_utils.basic._ANSIBLE_PROFILE", "legacy", raising=False)
141 pacemaker_resource.main()
142
143 out, _err = capfd.readouterr()
144 result = json.loads(out)
145 assert result.get("failed") is True
146 assert "Timed out" in result["msg"]
147 assert "virtual-ip" in result["msg"]