Debian Codegraph @

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

raw
1 # Copyright (c) 2025, Florian Paul Azim Hoberg (@gyptazy) <[email protected]>
2 #
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 from unittest.mock import patch
7
8 import pytest
9 from ansible.module_utils import basic
10 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
11 ModuleTestCase,
12 set_module_args,
13 )
14
15 from ansible_collections.community.proxmox.plugins.modules import proxmox_storage
16
17 # -- Fixtures
18
19 CEPHFS_ARGS = {
20 "name": "cephfs-storage",
21 "type": "cephfs",
22 "nodes": ["pve01", "pve02"],
23 "content": ["images", "rootdir"],
24 "cephfs_options": {
25 "monhost": ["10.0.0.1", "10.0.0.2"],
26 "username": "admin",
27 "password": "secretpass",
28 "path": "/",
29 "subdir": "mydata",
30 "client_keyring": "AQ==",
31 "fs_name": "mycephfs",
32 },
33 }
34
35 CIFS_ARGS = {
36 "name": "cifs-storage",
37 "type": "cifs",
38 "nodes": ["pve01", "pve02"],
39 "content": ["images"],
40 "cifs_options": {
41 "server": "10.0.0.1",
42 "share": "myshare",
43 "username": "user",
44 "password": "secret",
45 },
46 }
47
48 DIR_ARGS = {
49 "name": "dir-storage",
50 "type": "dir",
51 "nodes": ["pve01", "pve02"],
52 "content": ["images"],
53 "dir_options": {"path": "/dir"},
54 }
55
56 ISCSI_ARGS = {
57 "name": "iscsi-storage",
58 "type": "iscsi",
59 "nodes": ["pve01", "pve02"],
60 "content": ["images"],
61 "iscsi_options": {
62 "portal": "10.0.0.1",
63 "target": "iqn.example:444",
64 },
65 }
66
67 NFS_ARGS = {
68 "name": "nfs-share",
69 "type": "nfs",
70 "nodes": ["pve01", "pve02"],
71 "content": ["images"],
72 "nfs_options": {"server": "10.10.10.10", "export": "/mnt/nfs"},
73 }
74
75 PBS_ARGS = {
76 "name": "pbs-backup",
77 "type": "pbs",
78 "nodes": ["pve01", "pve02"],
79 "content": ["backup"],
80 "pbs_options": {
81 "server": "backup.local",
82 "username": "backup@pbs",
83 "password": "secret",
84 "datastore": "backup01",
85 "fingerprint": "21:67:27:63:3c:e5:73",
86 },
87 }
88
89 ZFSPOOL_ARGS = {
90 "name": "zfspool-storage",
91 "type": "zfspool",
92 "nodes": ["pve01", "pve02"],
93 "content": ["images"],
94 "zfspool_options": {"pool": "mypool"},
95 }
96
97
98 # -- Helpers
99
100
101 def exit_json(*args, **kwargs):
102 kwargs.setdefault("changed", False)
103 raise SystemExit(kwargs)
104
105
106 def fail_json(*args, **kwargs):
107 kwargs["failed"] = True
108 raise SystemExit(kwargs)
109
110
111 def build_module_args(state="present", **overrides):
112 return {
113 "api_host": "host",
114 "api_user": "user",
115 "api_password": "password",
116 "state": state,
117 **overrides,
118 }
119
120
121 # -- Module tests
122
123
124 class TestProxmoxStorageModule(ModuleTestCase):
125 def setUp(self):
126 super().setUp()
127 self.module = proxmox_storage
128
129 self.mock_module_helper = patch.multiple(
130 basic.AnsibleModule,
131 exit_json=exit_json,
132 fail_json=fail_json,
133 )
134 self.mock_module_helper.start()
135
136 self.connect_mock = patch(
137 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
138 ).start()
139
140 self.mock_api_storage = self.connect_mock.return_value.storage
141
142 def tearDown(self):
143 self.connect_mock.stop()
144 self.mock_module_helper.stop()
145 super().tearDown()
146
147 def _run_module(self, args):
148 with pytest.raises(SystemExit) as exc_info, set_module_args(args):
149 self.module.main()
150 return exc_info.value.args[0]
151
152 def _check_mode(self, **kwargs):
153 return {**build_module_args(**kwargs), "_ansible_check_mode": True}
154
155 def _run_add_storage_success(self, args, expected_payload_subset):
156 self.mock_api_storage.post.return_value = {}
157
158 result = self._run_module(args)
159
160 assert result["changed"] is True
161 assert "created successfully" in result["msg"]
162 assert self.mock_api_storage.post.called
163
164 actual_payload = self.mock_api_storage.post.call_args[1]
165 for key, value in expected_payload_subset.items():
166 if value is None:
167 assert key not in actual_payload
168 else:
169 assert actual_payload.get(key) == value
170
171 def _run_add_storage_missing_required(self, args, missing_field):
172 result = self._run_module(args)
173
174 assert result["failed"] is True
175 assert missing_field in result["msg"]
176 assert "required" in result["msg"].lower()
177 assert not self.mock_api_storage.post.called
178
179 # -- API errors
180
181 def test_add_storage_post_api_failure(self):
182 self.mock_api_storage.post.side_effect = Exception()
183
184 result = self._run_module(build_module_args(**NFS_ARGS))
185
186 assert result["failed"] is True
187 assert "Failed to create storage" in result["msg"]
188
189 def test_remove_storage_get_api_failure(self):
190 self.mock_api_storage.get.side_effect = Exception("connection failed")
191
192 result = self._run_module(build_module_args(state="absent", name="nfs-share", type="nfs"))
193
194 assert result["failed"] is True
195 assert "Failed to retrieve storage" in result["msg"]
196
197 def test_remove_storage_delete_api_failure(self):
198 self.mock_api_storage.get.return_value = {"storage": "nfs-share"}
199 self.mock_api_storage.return_value.delete.side_effect = Exception()
200
201 result = self._run_module(build_module_args(state="absent", name="nfs-share", type="nfs"))
202
203 assert result["failed"] is True
204 assert "Failed to delete storage" in result["msg"]
205
206 # -- state=present
207
208 def test_add_cephfs_storage(self):
209 self._run_add_storage_success(
210 build_module_args(**CEPHFS_ARGS),
211 {
212 "storage": "cephfs-storage",
213 "type": "cephfs",
214 "monhost": ["10.0.0.1", "10.0.0.2"],
215 "fs-name": "mycephfs",
216 "keyring": "AQ==",
217 "subdir": "mydata",
218 },
219 )
220
221 def test_add_cifs_storage(self):
222 self._run_add_storage_success(
223 build_module_args(**CIFS_ARGS),
224 {
225 "storage": "cifs-storage",
226 "type": "cifs",
227 "server": "10.0.0.1",
228 "share": "myshare",
229 },
230 )
231
232 def test_add_dir_storage(self):
233 self._run_add_storage_success(
234 build_module_args(**DIR_ARGS),
235 {"storage": "dir-storage", "type": "dir", "path": "/dir"},
236 )
237
238 def test_add_iscsi_storage(self):
239 self._run_add_storage_success(
240 build_module_args(**ISCSI_ARGS),
241 {
242 "storage": "iscsi-storage",
243 "type": "iscsi",
244 "portal": "10.0.0.1",
245 "target": "iqn.example:444",
246 },
247 )
248
249 def test_add_nfs_storage(self):
250 self._run_add_storage_success(
251 build_module_args(**NFS_ARGS),
252 {
253 "storage": "nfs-share",
254 "type": "nfs",
255 "server": "10.10.10.10",
256 "export": "/mnt/nfs",
257 },
258 )
259
260 def test_add_pbs_storage(self):
261 self._run_add_storage_success(
262 build_module_args(**PBS_ARGS),
263 {
264 "storage": "pbs-backup",
265 "type": "pbs",
266 "server": "backup.local",
267 "datastore": "backup01",
268 "fingerprint": "21:67:27:63:3c:e5:73",
269 },
270 )
271
272 def test_add_zfspool_storage(self):
273 self._run_add_storage_success(
274 build_module_args(**ZFSPOOL_ARGS),
275 {"storage": "zfspool-storage", "type": "zfspool", "pool": "mypool"},
276 )
277
278 def test_add_storage_already_exists(self):
279 self.mock_api_storage.post.side_effect = Exception("already defined")
280
281 result = self._run_module(build_module_args(**NFS_ARGS))
282
283 assert result["changed"] is False
284 assert "already present" in result["msg"]
285
286 def test_add_storage_check_mode_new(self):
287 self.mock_api_storage.get.side_effect = Exception("storage does not exist")
288
289 result = self._run_module(self._check_mode(**NFS_ARGS))
290
291 assert result["changed"] is True
292 assert "would be created" in result["msg"]
293 assert not self.mock_api_storage.post.called
294
295 def test_add_storage_check_mode_already_exists(self):
296 self.mock_api_storage.get.return_value = {"storage": "nfs-share"}
297
298 result = self._run_module(self._check_mode(**NFS_ARGS))
299
300 assert result["changed"] is False
301 assert "already present" in result["msg"]
302 assert not self.mock_api_storage.post.called
303
304 def test_add_cifs_storage_missing_share(self):
305 self._run_add_storage_missing_required(
306 build_module_args(**{**CIFS_ARGS, "cifs_options": {"server": "10.0.0.1"}}),
307 missing_field="share",
308 )
309
310 def test_add_dir_storage_missing_path(self):
311 self._run_add_storage_missing_required(
312 build_module_args(**{**DIR_ARGS, "dir_options": {}}),
313 missing_field="path",
314 )
315
316 def test_add_iscsi_storage_missing_target(self):
317 self._run_add_storage_missing_required(
318 build_module_args(**{**ISCSI_ARGS, "iscsi_options": {"portal": "10.0.0.1"}}),
319 missing_field="target",
320 )
321
322 def test_add_nfs_storage_missing_export(self):
323 self._run_add_storage_missing_required(
324 build_module_args(**{**NFS_ARGS, "nfs_options": {"server": "10.0.0.1"}}),
325 missing_field="export",
326 )
327
328 def test_add_pbs_storage_missing_datastore(self):
329 self._run_add_storage_missing_required(
330 build_module_args(**{**PBS_ARGS, "pbs_options": {"server": "s", "username": "u", "password": "p"}}),
331 missing_field="datastore",
332 )
333
334 def test_add_zfspool_storage_missing_pool(self):
335 self._run_add_storage_missing_required(
336 build_module_args(**{**ZFSPOOL_ARGS, "zfspool_options": {}}),
337 missing_field="pool",
338 )
339
340 # -- state=absent
341
342 def test_remove_existing_storage(self):
343 self.mock_api_storage.get.return_value = {"storage": "nfs-share"}
344
345 result = self._run_module(build_module_args(state="absent", name="nfs-share", type="nfs"))
346
347 assert result["changed"] is True
348 assert "removed successfully" in result["msg"]
349 self.mock_api_storage.assert_called_with("nfs-share")
350 self.mock_api_storage.return_value.delete.assert_called_once_with()
351
352 def test_remove_nonexistent_storage(self):
353 self.mock_api_storage.get.side_effect = Exception("storage does not exist")
354
355 result = self._run_module(build_module_args(state="absent", name="nonexistent", type="nfs"))
356
357 assert result["changed"] is False
358 assert "does not exist" in result["msg"]
359 self.mock_api_storage.return_value.delete.assert_not_called()
360
361 def test_remove_storage_check_mode_existing(self):
362 self.mock_api_storage.get.return_value = {"storage": "nfs-share"}
363
364 result = self._run_module(self._check_mode(state="absent", name="nfs-share", type="nfs"))
365
366 assert result["changed"] is True
367 assert "would be deleted" in result["msg"]
368 self.mock_api_storage.return_value.delete.assert_not_called()
369
370 def test_remove_storage_check_mode_nonexistent(self):
371 self.mock_api_storage.get.side_effect = Exception("storage does not exist")
372
373 result = self._run_module(self._check_mode(state="absent", name="nonexistent", type="nfs"))
374
375 assert result["changed"] is False
376 assert "does not exist" in result["msg"]
377 self.mock_api_storage.return_value.delete.assert_not_called()