Debian Codegraph @

ansible_collections/community/proxmox/tests/unit/plugins/modules/test_proxmox_ceph_osd.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_osd,
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_DISK_LIST = [
71 {
72 "type": "unknown",
73 "used": "BIOS boot",
74 "health": "OK",
75 "vendor": "QEMU ",
76 "size": 34359738368,
77 "model": "QEMU_HARDDISK",
78 "wwn": "unknown",
79 "osdid-list": None,
80 "devpath": "/dev/sda",
81 "serial": "drive-scsi0",
82 "gpt": 1,
83 "wearout": "N/A",
84 "osdid": -1,
85 "by_id_link": "/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_drive-scsi0",
86 "rpm": -1,
87 },
88 {
89 "by_id_link": "/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_drive-scsi1",
90 "osdid": -1,
91 "wearout": "N/A",
92 "gpt": 0,
93 "rpm": -1,
94 "vendor": "QEMU ",
95 "health": "OK",
96 "type": "unknown",
97 "devpath": "/dev/sdb",
98 "osdid-list": None,
99 "serial": "drive-scsi1",
100 "size": 17179869184,
101 "model": "QEMU_HARDDISK",
102 "wwn": "unknown",
103 },
104 {
105 "rpm": -1,
106 "gpt": 0,
107 "bluestore": 1,
108 "wearout": "N/A",
109 "osdencrypted": 0,
110 "osdid": "0",
111 "by_id_link": "/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_drive-scsi2",
112 "wwn": "unknown",
113 "model": "QEMU_HARDDISK",
114 "size": 17179869184,
115 "devpath": "/dev/sdc",
116 "osdid-list": ["0"],
117 "serial": "drive-scsi2",
118 "used": "LVM",
119 "type": "unknown",
120 "vendor": "QEMU ",
121 "health": "OK",
122 },
123 ]
124
125 RAW_OSD = {
126 "root": {
127 "leaf": 0,
128 "children": [
129 {
130 "pgs": 0,
131 "name": "default",
132 "leaf": 0,
133 "id": "-1",
134 "type": "root",
135 "children": [
136 {
137 "leaf": 0,
138 "type": "host",
139 "id": "-5",
140 "name": "srv-proxmox-02",
141 "version": "19.2.3",
142 "pgs": 0,
143 "reweight": -1,
144 },
145 {
146 "type": "host",
147 "id": "-7",
148 "leaf": 0,
149 "name": "srv-proxmox-03",
150 "version": "19.2.3",
151 "pgs": 0,
152 "reweight": -1,
153 "children": [
154 {
155 "status": "down",
156 "device_class": "hdd",
157 "pgs": 0,
158 "host": "srv-proxmox-03",
159 "leaf": 1,
160 "blfsdev": "/dev/dm-5",
161 "crush_weight": 0.015594482421875,
162 "commit_latency_ms": 0,
163 "ceph_version_short": "19.2.3",
164 "type": "osd",
165 "apply_latency_ms": 0,
166 "percent_used": 0,
167 "osdtype": "bluestore",
168 "dbdev": None,
169 "id": "1",
170 "ceph_version": "ceph version 19.2.3 \
171 (2f03f1cd83e5d40cdf1393cb64a662a8e8bb07c6) \
172 squid (stable)",
173 "in": 0,
174 "name": "osd.1",
175 "reweight": 0,
176 "total_space": 1024,
177 "bytes_used": 0,
178 "waldev": None,
179 }
180 ],
181 },
182 {
183 "name": "srv-proxmox-01",
184 "id": "-3",
185 "type": "host",
186 "leaf": 0,
187 "pgs": 0,
188 "version": "19.2.3",
189 "children": [
190 {
191 "host": "srv-proxmox-01",
192 "leaf": 1,
193 "blfsdev": "/dev/dm-8",
194 "status": "up",
195 "device_class": "hdd",
196 "pgs": 0,
197 "ceph_version_short": "19.2.3",
198 "type": "osd",
199 "apply_latency_ms": 0,
200 "commit_latency_ms": 0,
201 "crush_weight": 0.015594482421875,
202 "percent_used": 2.60750534188034,
203 "dbdev": None,
204 "osdtype": "bluestore",
205 "in": 1,
206 "ceph_version": "ceph version 19.2.3 \
207 (2f03f1cd83e5d40cdf1393cb64a662a8e8bb07c6) \
208 squid (stable)",
209 "id": "0",
210 "name": "osd.0",
211 "waldev": None,
212 "reweight": 1,
213 "total_space": 17175674880,
214 "bytes_used": 447856640,
215 }
216 ],
217 "reweight": -1,
218 },
219 ],
220 "reweight": -1,
221 }
222 ],
223 },
224 "flags": "sortbitwise,recovery_deletes,purged_snapdirs,pglog_hardlimit",
225 }
226
227
228 def exit_json(*args, **kwargs):
229 """function to patch over exit_json;
230 package return data into an exception"""
231 if "changed" not in kwargs:
232 kwargs["changed"] = False
233 raise SystemExit(kwargs)
234
235
236 def fail_json(*args, **kwargs):
237 """function to patch over fail_json;
238 package return data into an exception"""
239 kwargs["failed"] = True
240 raise SystemExit(kwargs)
241
242
243 def build_common_arg(node, state, check=False):
244 args = {"api_user": "root@pam", "api_password": "secret", "api_host": "192.168.1.21", "node": node, "state": state}
245 if check:
246 args["_ansible_check_mode"] = True
247 return args
248
249
250 class TestProxmoxCephOsd(ModuleTestCase):
251 def setUp(self):
252 super().setUp()
253 proxmox_utils.HAS_PROXMOXER = True
254 self.module = proxmox_ceph_osd
255
256 self.fail_json_patcher = patch(
257 "ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json)
258 )
259 self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json)
260
261 self.fail_json_mock = self.fail_json_patcher.start()
262 self.exit_json_patcher.start()
263
264 self.connect_mock = patch(
265 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
266 ).start()
267
268 mock_obj = self.connect_mock.return_value
269 mock_obj.cluster.resources.get.return_value = RAW_RESOURCES_NODES
270 mock_obj.nodes.return_value.disks.list.get.return_value = RAW_DISK_LIST
271 mock_obj.nodes.return_value.ceph.osd.get.return_value = RAW_OSD
272
273 def tearDown(self):
274 self.connect_mock.stop()
275 self.exit_json_patcher.stop()
276 self.fail_json_patcher.stop()
277 super().tearDown()
278
279 def test_add_osd_node_not_present(self):
280 args = build_common_arg("srv-proxmox-04", "present", False)
281 args["dev"] = "/dev/sdb"
282 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
283 proxmox_ceph_osd.main()
284
285 result = exc_info.value.args[0]
286
287 assert result["failed"] is True
288 assert result["msg"] == "Node srv-proxmox-04 does not exist."
289
290 def test_add_osd_disk_not_exist(self):
291 args = build_common_arg("srv-proxmox-01", "present", False)
292 args["dev"] = "/dev/sdd"
293 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
294 proxmox_ceph_osd.main()
295
296 result = exc_info.value.args[0]
297
298 assert result["failed"] is True
299 assert result["msg"] == "/dev/sdd does not exist on the node srv-proxmox-01."
300
301 def test_add_osd_disk_already_used(self):
302 args = build_common_arg("srv-proxmox-01", "present", False)
303 args["dev"] = "/dev/sda"
304 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
305 proxmox_ceph_osd.main()
306
307 result = exc_info.value.args[0]
308
309 assert result["failed"] is True
310 assert result["msg"] == "/dev/sda is already in use by the node srv-proxmox-01."
311
312 def test_add_osd_disk_already_osd(self):
313 args = build_common_arg("srv-proxmox-01", "present", False)
314 args["dev"] = "/dev/sdc"
315 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
316 proxmox_ceph_osd.main()
317
318 result = exc_info.value.args[0]
319
320 assert result["changed"] is False
321 assert result["msg"] == "/dev/sdc is already an osd."
322
323 def test_add_osd_check_mode(self):
324 args = build_common_arg("srv-proxmox-01", "present", True)
325 args["dev"] = "/dev/sdb"
326 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
327 proxmox_ceph_osd.main()
328
329 result = exc_info.value.args[0]
330
331 assert result["changed"] is True
332 assert result["msg"] == "Osd would be added."
333
334 def test_add_osd(self):
335 args = build_common_arg("srv-proxmox-01", "present", False)
336 args["dev"] = "/dev/sdb"
337 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
338 proxmox_ceph_osd.main()
339
340 result = exc_info.value.args[0]
341
342 assert result["changed"] is True
343 assert result["msg"] == "Osd added."
344
345 def test_in_osd_not_present(self):
346 args = build_common_arg("srv-proxmox-01", "in", False)
347 args["osdid"] = 2
348 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
349 proxmox_ceph_osd.main()
350
351 result = exc_info.value.args[0]
352
353 assert result["failed"] is True
354 assert result["msg"] == "Osd 2 does not exist."
355
356 def test_in_osd_already_in(self):
357 args = build_common_arg("srv-proxmox-01", "in", False)
358 args["osdid"] = 0
359 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
360 proxmox_ceph_osd.main()
361
362 result = exc_info.value.args[0]
363
364 assert result["changed"] is False
365 assert result["msg"] == "Osd 0 already in."
366
367 def test_in_osd_check_mode(self):
368 args = build_common_arg("srv-proxmox-03", "in", True)
369 args["osdid"] = 1
370 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
371 proxmox_ceph_osd.main()
372
373 result = exc_info.value.args[0]
374
375 assert result["changed"] is True
376 assert result["msg"] == "Would in osd 1."
377
378 def test_in_osd(self):
379 args = build_common_arg("srv-proxmox-03", "in", False)
380 args["osdid"] = 1
381 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
382 proxmox_ceph_osd.main()
383
384 result = exc_info.value.args[0]
385
386 assert result["changed"] is True
387 assert result["msg"] == "In osd 1."
388
389 def test_out_osd_already_out(self):
390 args = build_common_arg("srv-proxmox-03", "out", False)
391 args["osdid"] = 1
392 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
393 proxmox_ceph_osd.main()
394
395 result = exc_info.value.args[0]
396
397 assert result["changed"] is False
398 assert result["msg"] == "Osd 1 already out."
399
400 def test_out_osd_check_mode(self):
401 args = build_common_arg("srv-proxmox-01", "out", True)
402 args["osdid"] = 0
403 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
404 proxmox_ceph_osd.main()
405
406 result = exc_info.value.args[0]
407
408 assert result["changed"] is True
409 assert result["msg"] == "Would out osd 0."
410
411 def test_out_osd(self):
412 args = build_common_arg("srv-proxmox-01", "out", False)
413 args["osdid"] = 0
414 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
415 proxmox_ceph_osd.main()
416
417 result = exc_info.value.args[0]
418
419 assert result["changed"] is True
420 assert result["msg"] == "Out osd 0."
421
422 def test_scrub_osd_down(self):
423 args = build_common_arg("srv-proxmox-03", "scrub", False)
424 args["osdid"] = 1
425 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
426 proxmox_ceph_osd.main()
427
428 result = exc_info.value.args[0]
429
430 assert result["failed"] is True
431 assert result["msg"] == "Osd 1 is not up."
432
433 def test_scrub_osd_check_mode(self):
434 args = build_common_arg("srv-proxmox-01", "scrub", True)
435 args["osdid"] = 0
436 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
437 proxmox_ceph_osd.main()
438
439 result = exc_info.value.args[0]
440
441 assert result["changed"] is True
442 assert result["msg"] == "Would scrub Osd 0."
443
444 def test_scrub_osd(self):
445 args = build_common_arg("srv-proxmox-01", "scrub", False)
446 args["osdid"] = 0
447 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
448 proxmox_ceph_osd.main()
449
450 result = exc_info.value.args[0]
451
452 assert result["changed"] is True
453 assert result["msg"] == "Scrub Osd 0."
454
455 def test_start_osd_already_started(self):
456 args = build_common_arg("srv-proxmox-01", "start", False)
457 args["osdid"] = 0
458 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
459 proxmox_ceph_osd.main()
460
461 result = exc_info.value.args[0]
462
463 assert result["changed"] is False
464 assert result["msg"] == "Osd 0 already started."
465
466 def test_start_osd_check_mode(self):
467 args = build_common_arg("srv-proxmox-03", "start", True)
468 args["osdid"] = 1
469 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
470 proxmox_ceph_osd.main()
471
472 result = exc_info.value.args[0]
473
474 assert result["changed"] is True
475 assert result["msg"] == "Would start Osd 1."
476
477 def test_start_osd(self):
478 args = build_common_arg("srv-proxmox-03", "start", False)
479 args["osdid"] = 1
480 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
481 proxmox_ceph_osd.main()
482
483 result = exc_info.value.args[0]
484
485 assert result["changed"] is True
486 assert result["msg"] == "Start Osd 1."
487
488 def test_stop_osd_already_down(self):
489 args = build_common_arg("srv-proxmox-03", "stop", False)
490 args["osdid"] = 1
491 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
492 proxmox_ceph_osd.main()
493
494 result = exc_info.value.args[0]
495
496 assert result["changed"] is False
497 assert result["msg"] == "Osd 1 already stopped."
498
499 def test_stop_osd_check_mode(self):
500 args = build_common_arg("srv-proxmox-01", "stop", True)
501 args["osdid"] = 0
502 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
503 proxmox_ceph_osd.main()
504
505 result = exc_info.value.args[0]
506
507 assert result["changed"] is True
508 assert result["msg"] == "Would stop Osd 0."
509
510 def test_stop_osd(self):
511 args = build_common_arg("srv-proxmox-01", "stop", False)
512 args["osdid"] = 0
513 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
514 proxmox_ceph_osd.main()
515
516 result = exc_info.value.args[0]
517
518 assert result["changed"] is True
519 assert result["msg"] == "Stop Osd 0."
520
521 def test_restart_osd_check_mode(self):
522 args = build_common_arg("srv-proxmox-01", "restart", True)
523 args["osdid"] = 0
524 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
525 proxmox_ceph_osd.main()
526
527 result = exc_info.value.args[0]
528
529 assert result["changed"] is True
530 assert result["msg"] == "Would restart Osd 0."
531
532 def test_restart_osd(self):
533 args = build_common_arg("srv-proxmox-01", "restart", False)
534 args["osdid"] = 0
535 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
536 proxmox_ceph_osd.main()
537
538 result = exc_info.value.args[0]
539
540 assert result["changed"] is True
541 assert result["msg"] == "Restart Osd 0."
542
543 def test_del_osd_not_present(self):
544 args = build_common_arg("srv-proxmox-01", "absent", False)
545 args["osdid"] = 2
546 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
547 proxmox_ceph_osd.main()
548
549 result = exc_info.value.args[0]
550
551 assert result["changed"] is False
552 assert result["msg"] == "Osd 2 not present."
553
554 def test_del_osd_still_in(self):
555 args = build_common_arg("srv-proxmox-01", "absent", False)
556 args["osdid"] = 0
557 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
558 proxmox_ceph_osd.main()
559
560 result = exc_info.value.args[0]
561
562 assert result["failed"] is True
563 assert result["msg"] == "Cannot delete osd 0 is in."
564
565 def test_del_osd_check_mode(self):
566 args = build_common_arg("srv-proxmox-03", "absent", True)
567 args["osdid"] = 1
568 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
569 proxmox_ceph_osd.main()
570
571 result = exc_info.value.args[0]
572
573 assert result["changed"] is True
574 assert result["msg"] == "Osd 1 would be deleted."
575
576 def test_del_osd(self):
577 args = build_common_arg("srv-proxmox-03", "absent", False)
578 args["osdid"] = 1
579 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
580 proxmox_ceph_osd.main()
581
582 result = exc_info.value.args[0]
583
584 assert result["changed"] is True
585 assert result["msg"] == "Osd 1 deleted."