Debian Codegraph @

ansible_collections/community/proxmox/tests/unit/plugins/modules/test_proxmox_ceph_mds.py

raw
1 #
2 # Copyright (c) 2025, teslamania <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6
7 from unittest.mock import Mock, patch
8
9 import pytest
10 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
11 ModuleTestCase,
12 set_module_args,
13 )
14
15 import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils
16 from ansible_collections.community.proxmox.plugins.modules import (
17 proxmox_ceph_mds,
18 )
19
20 proxmoxer = pytest.importorskip("proxmoxer")
21
22 RAW_RESOURCES_NODES = [
23 {
24 "maxdisk": 15517192192,
25 "maxmem": 2063962112,
26 "disk": 3952242688,
27 "mem": 1556987904,
28 "type": "node",
29 "cpu": 0.00688637481554353,
30 "level": "",
31 "status": "online",
32 "id": "node/srv-proxmox-02",
33 "uptime": 697,
34 "cgroup-mode": 2,
35 "maxcpu": 4,
36 "node": "srv-proxmox-02",
37 },
38 {
39 "maxmem": 2063958016,
40 "maxdisk": 15517192192,
41 "level": "",
42 "cpu": 0.0118168389955687,
43 "type": "node",
44 "mem": 1854046208,
45 "disk": 3955453952,
46 "uptime": 697,
47 "id": "node/srv-proxmox-01",
48 "status": "online",
49 "node": "srv-proxmox-01",
50 "maxcpu": 4,
51 "cgroup-mode": 2,
52 },
53 {
54 "level": "",
55 "type": "node",
56 "cpu": 0.00760922925871379,
57 "disk": 3907559424,
58 "mem": 1550610432,
59 "maxdisk": 15517192192,
60 "maxmem": 2063953920,
61 "maxcpu": 4,
62 "node": "srv-proxmox-03",
63 "cgroup-mode": 2,
64 "uptime": 697,
65 "id": "node/srv-proxmox-03",
66 "status": "online",
67 },
68 ]
69
70 RAW_MDS = [
71 {
72 "ceph_version_short": "19.2.3",
73 "ceph_version": "ceph version 19.2.3 \
74 (2f03f1cd83e5d40cdf1393cb64a662a8e8bb07c6) \
75 squid (stable)",
76 "addr": "192.168.1.21:6803/3852241471",
77 "rank": -1,
78 "standby_replay": 0,
79 "service": 1,
80 "direxists": 1,
81 "state": "up:standby",
82 "name": "srv-proxmox-01",
83 "host": "srv-proxmox-01",
84 "fs_name": None,
85 },
86 {
87 "direxists": 1,
88 "state": "up:standby",
89 "host": "srv-proxmox-02",
90 "fs_name": None,
91 "ceph_version_short": "19.2.3",
92 "ceph_version": "ceph version 19.2.3 \
93 (2f03f1cd83e5d40cdf1393cb64a662a8e8bb07c6) \
94 squid (stable)",
95 "service": 1,
96 "name": "srv-proxmox-02",
97 "addr": "192.168.1.22:6801/2822666090",
98 "rank": -1,
99 "standby_replay": 0,
100 },
101 ]
102
103
104 def exit_json(*args, **kwargs):
105 """function to patch over exit_json;
106 package return data into an exception"""
107 if "changed" not in kwargs:
108 kwargs["changed"] = False
109 raise SystemExit(kwargs)
110
111
112 def fail_json(*args, **kwargs):
113 """function to patch over fail_json;
114 package return data into an exception"""
115 kwargs["failed"] = True
116 raise SystemExit(kwargs)
117
118
119 def build_arg(node, state, check=False):
120 args = {"api_user": "root@pam", "api_password": "secret", "api_host": "192.168.1.21", "node": node, "state": state}
121 if check:
122 args["_ansible_check_mode"] = True
123 return args
124
125
126 class TestProxmoxCephMds(ModuleTestCase):
127 def setUp(self):
128 super().setUp()
129 proxmox_utils.HAS_PROXMOXER = True
130 self.module = proxmox_ceph_mds
131
132 self.fail_json_patcher = patch(
133 "ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json)
134 )
135 self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json)
136
137 self.fail_json_mock = self.fail_json_patcher.start()
138 self.exit_json_patcher.start()
139
140 self.connect_mock = patch(
141 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
142 ).start()
143
144 mock_obj = self.connect_mock.return_value
145 mock_obj.cluster.resources.get.return_value = RAW_RESOURCES_NODES
146 mock_obj.nodes.return_value.ceph.mds.get.return_value = RAW_MDS
147
148 def tearDown(self):
149 self.connect_mock.stop()
150 self.exit_json_patcher.stop()
151 self.fail_json_patcher.stop()
152 super().tearDown()
153
154 def test_proxmox_ceph_missing_argument(self):
155 with set_module_args(
156 {"api_user": "root@pam", "api_password": "secret", "api_host": "192.168.1.21"}
157 ), pytest.raises(SystemExit) as exc_info:
158 proxmox_ceph_mds.main()
159
160 result = exc_info.value.args[0]
161 msg = "missing required arguments: node, state"
162
163 assert result["failed"] is True
164 assert result["msg"] == msg
165
166 def test_add_mds_check_mode(self):
167 mds = "srv-proxmox-03"
168 with set_module_args(build_arg(mds, "present", True)), pytest.raises(SystemExit) as exc_info:
169 proxmox_ceph_mds.main()
170
171 result = exc_info.value.args[0]
172
173 assert result["changed"] is True
174 assert result["msg"] == f"Mds {mds} would be added"
175 assert result["mds"] == mds
176
177 def test_add_mds(self):
178 mds = "srv-proxmox-03"
179 with set_module_args(build_arg(mds, "present")), pytest.raises(SystemExit) as exc_info:
180 proxmox_ceph_mds.main()
181
182 result = exc_info.value.args[0]
183
184 assert result["changed"] is True
185 assert result["msg"] == f"Mds {mds} added"
186 assert result["mds"] == mds
187
188 def test_add_mds_not_exist(self):
189 mds = "srv-proxmox-04"
190 with set_module_args(build_arg(mds, "present")), pytest.raises(SystemExit) as exc_info:
191 proxmox_ceph_mds.main()
192
193 result = exc_info.value.args[0]
194
195 assert result["failed"] is True
196 assert result["msg"] == f"Node {mds} does not exist in the cluster"
197
198 def test_add_mds_already_mds(self):
199 mds = "srv-proxmox-02"
200 with set_module_args(build_arg(mds, "present")), pytest.raises(SystemExit) as exc_info:
201 proxmox_ceph_mds.main()
202
203 result = exc_info.value.args[0]
204
205 assert result["changed"] is False
206 assert result["msg"] == "Mds already exists"
207 assert result["mds"] == mds
208
209 def test_del_mds_check_mode(self):
210 mds = "srv-proxmox-02"
211 with set_module_args(build_arg(mds, "absent", True)), pytest.raises(SystemExit) as exc_info:
212 proxmox_ceph_mds.main()
213
214 result = exc_info.value.args[0]
215
216 assert result["changed"] is True
217 assert result["msg"] == f"Mds {mds} would be deleted"
218 assert result["mds"] == mds
219
220 def test_del_mds(self):
221 mds = "srv-proxmox-02"
222 with set_module_args(build_arg(mds, "absent")), pytest.raises(SystemExit) as exc_info:
223 proxmox_ceph_mds.main()
224
225 result = exc_info.value.args[0]
226
227 assert result["changed"] is True
228 assert result["msg"] == f"Mds {mds} deleted"
229 assert result["mds"] == mds
230
231 def test_del_mds_not_exist(self):
232 mds = "srv-proxmox-04"
233 with set_module_args(build_arg(mds, "absent")), pytest.raises(SystemExit) as exc_info:
234 proxmox_ceph_mds.main()
235
236 result = exc_info.value.args[0]
237
238 assert result["failed"] is True
239 assert result["msg"] == f"Node {mds} does not exist in the cluster"
240
241 def test_del_mds_already_not_mds(self):
242 mds = "srv-proxmox-03"
243 with set_module_args(build_arg(mds, "absent")), pytest.raises(SystemExit) as exc_info:
244 proxmox_ceph_mds.main()
245
246 result = exc_info.value.args[0]
247
248 assert result["changed"] is False
249 assert result["msg"] == "Mds not present"
250 assert result["mds"] == mds