Debian Codegraph @

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

raw
1 #
2 # Copyright (c) 2026, 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_pool,
18 )
19
20 proxmoxer = pytest.importorskip("proxmoxer")
21
22 RAW_NODES_GET = [
23 {
24 "status": "online",
25 "ssl_fingerprint": "",
26 "id": "node/srv-proxmox-02",
27 "type": "node",
28 "level": "",
29 "disk": 9213120512,
30 "maxdisk": 15517192192,
31 "node": "srv-proxmox-02",
32 "uptime": 87,
33 "cpu": 0.0182131429977849,
34 "mem": 1748975616,
35 "maxcpu": 4,
36 "maxmem": 2063151104,
37 },
38 {
39 "level": "",
40 "disk": 10775777280,
41 "maxdisk": 15517192192,
42 "status": "online",
43 "ssl_fingerprint": "",
44 "id": "node/srv-proxmox-01",
45 "type": "node",
46 "mem": 1741160448,
47 "maxmem": 2063151104,
48 "maxcpu": 4,
49 "node": "srv-proxmox-01",
50 "uptime": 96,
51 "cpu": 0.0615460364352536,
52 },
53 {
54 "maxcpu": 4,
55 "maxmem": 2063151104,
56 "mem": 1529425920,
57 "node": "srv-proxmox-03",
58 "cpu": 0.0157248157248157,
59 "uptime": 87,
60 "disk": 9196486656,
61 "maxdisk": 15517192192,
62 "level": "",
63 "ssl_fingerprint": "",
64 "type": "node",
65 "id": "node/srv-proxmox-03",
66 "status": "online",
67 },
68 ]
69
70 RAW_POOL = [
71 {
72 "pg_num": 128,
73 "bytes_used": 0,
74 "crush_rule": "0",
75 "pool_name": "test",
76 "min_size": 2,
77 "pool": "20",
78 "target_size_ratio": None,
79 "application_metadata": {"rbd": {}},
80 "pg_autoscale_mode": "warn",
81 "size": 3,
82 "autoscale_status": {
83 "pg_num_target": 128,
84 "raw_used": 0,
85 "bulk": False,
86 "effective_target_ratio": 0,
87 "raw_used_rate": 3,
88 "bias": 1,
89 "pool_name": "test",
90 "target_bytes": 0,
91 "pg_num_final": 32,
92 "pg_num_ideal": 0,
93 "subtree_capacity": 51527024640,
94 "pg_autoscale_mode": "warn",
95 "would_adjust": False,
96 "crush_root_id": -1,
97 "capacity_ratio": 0,
98 "actual_capacity_ratio": 0,
99 "pool_id": 20,
100 "target_ratio": 0,
101 "actual_raw_used": 0,
102 "logical_used": 0,
103 },
104 "type": "replicated",
105 "percent_used": 0,
106 "pg_num_final": 32,
107 "crush_rule_name": "replicated_rule",
108 "target_size": None,
109 "pg_num_min": None,
110 }
111 ]
112
113 RAW_POOL_TEST = {
114 "id": 20,
115 "pgp_num": 128,
116 "min_size": 2,
117 "hashpspool": "1",
118 "name": "test",
119 "nodelete": "0",
120 "fast_read": "0",
121 "pg_num": 128,
122 "crush_rule": "replicated_rule",
123 "use_gmt_hitset": "1",
124 "size": 3,
125 "target_size": None,
126 "pg_num_min": None,
127 "nodeep-scrub": "0",
128 "nopgchange": "0",
129 "nosizechange": "0",
130 "pg_autoscale_mode": "warn",
131 "target_size_ratio": None,
132 "write_fadvise_dontneed": "0",
133 "noscrub": "0",
134 }
135
136 RAW_TASK = {"status": "stopped", "exitstatus": "OK"}
137
138
139 def exit_json(*args, **kwargs):
140 """function to patch over exit_json;
141 package return data into an exception"""
142 if "changed" not in kwargs:
143 kwargs["changed"] = False
144 raise SystemExit(kwargs)
145
146
147 def fail_json(*args, **kwargs):
148 """function to patch over fail_json;
149 package return data into an exception"""
150 kwargs["failed"] = True
151 raise SystemExit(kwargs)
152
153
154 def build_arg(state, name, check=False):
155 args = {
156 "api_user": "root@pam",
157 "api_password": "secret",
158 "api_host": "192.168.1.21",
159 "node": "srv-proxmox-01",
160 "name": name,
161 "state": state,
162 }
163 if check:
164 args["_ansible_check_mode"] = True
165 return args
166
167
168 class TestProxmoxCephPool(ModuleTestCase):
169 def setUp(self):
170 super().setUp()
171 proxmox_utils.HAS_PROXMOXER = True
172 self.module = proxmox_ceph_pool
173
174 self.fail_json_patcher = patch(
175 "ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json)
176 )
177 self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json)
178
179 self.fail_json_mock = self.fail_json_patcher.start()
180 self.exit_json_patcher.start()
181
182 self.connect_mock = patch(
183 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
184 ).start()
185
186 mock_obj = self.connect_mock.return_value
187 mock_obj.nodes.get.return_value = RAW_NODES_GET
188 mock_obj.nodes.return_value.ceph.pool.get.return_value = RAW_POOL
189 mock_obj.nodes.return_value.ceph.pool.return_value.status.get.return_value = RAW_POOL_TEST
190 mock_obj.nodes.return_value.tasks.return_value.status.get.return_value = RAW_TASK
191
192 def tearDown(self):
193 self.connect_mock.stop()
194 self.exit_json_patcher.stop()
195 self.fail_json_patcher.stop()
196 super().tearDown()
197
198 def test_add_pool_check_mode(self):
199 with set_module_args(build_arg("present", "test2", True)), pytest.raises(SystemExit) as exc_info:
200 proxmox_ceph_pool.main()
201 result = exc_info.value.args[0]
202 assert result["changed"] is True
203 assert result["msg"] == "Ceph pool test2 would be added."
204
205 def test_add_pool(self):
206 with set_module_args(build_arg("present", "test2")), pytest.raises(SystemExit) as exc_info:
207 proxmox_ceph_pool.main()
208 result = exc_info.value.args[0]
209 assert result["changed"] is True
210 assert result["msg"] == "Ceph pool test2 added."
211
212 def test_add_pool_idempotent(self):
213 with set_module_args(build_arg("present", "test")), pytest.raises(SystemExit) as exc_info:
214 proxmox_ceph_pool.main()
215 result = exc_info.value.args[0]
216 assert result["changed"] is False
217 assert result["msg"] == "Ceph pool test already exists."
218
219 def test_edit_pool_check_mode(self):
220 args = build_arg("present", "test", True)
221 args["pg_autoscale_mode"] = "off"
222 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
223 proxmox_ceph_pool.main()
224 result = exc_info.value.args[0]
225 assert result["changed"] is True
226 assert result["msg"] == "Ceph pool test would be updated."
227
228 def test_edit_pool(self):
229 args = build_arg("present", "test")
230 args["pg_autoscale_mode"] = "off"
231 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
232 proxmox_ceph_pool.main()
233 result = exc_info.value.args[0]
234 assert result["changed"] is True
235 assert result["msg"] == "Ceph pool test updated."
236
237 def test_del_pool_check_mode(self):
238 args = build_arg("absent", "test", True)
239 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
240 proxmox_ceph_pool.main()
241 result = exc_info.value.args[0]
242 assert result["changed"] is True
243 assert result["msg"] == "Ceph pool test would be deleted."
244
245 def test_del_pool(self):
246 args = build_arg("absent", "test")
247 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
248 proxmox_ceph_pool.main()
249 result = exc_info.value.args[0]
250 assert result["changed"] is True
251 assert result["msg"] == "Ceph pool test deleted."
252
253 def test_del_pool_not_present(self):
254 args = build_arg("absent", "test2")
255 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
256 proxmox_ceph_pool.main()
257 result = exc_info.value.args[0]
258 assert result["changed"] is False
259 assert result["msg"] == "Ceph pool test2 not present."