Debian Codegraph @

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

raw
1 #
2 # Copyright (c) 2025 Marzieh Raoufnezhad <raoufnezhad at gmail.com>
3 # Copyright (c) 2025 Maryam Mayabi <mayabi.ahm at gmail.com>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7
8 import sys
9
10 import pytest
11
12 proxmoxer = pytest.importorskip("proxmoxer")
13 mandatory_py_version = pytest.mark.skipif(
14 sys.version_info < (2, 7),
15 reason="The proxmoxer dependency requires python2.7 or higher",
16 )
17
18 from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch
19 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
20 AnsibleExitJson,
21 AnsibleFailJson,
22 ModuleTestCase,
23 set_module_args,
24 )
25
26 import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils
27 from ansible_collections.community.proxmox.plugins.modules import proxmox_backup_schedule
28
29 RESOURCE_LIST = [
30 {
31 "uptime": 0,
32 "diskwrite": 0,
33 "name": "test01",
34 "maxcpu": 0,
35 "node": "NODE1",
36 "mem": 0,
37 "netout": 0,
38 "netin": 0,
39 "maxmem": 0,
40 "diskread": 0,
41 "disk": 0,
42 "maxdisk": 0,
43 "status": "running",
44 "cpu": 0,
45 "id": "qemu/100",
46 "template": 0,
47 "vmid": 100,
48 "type": "qemu",
49 },
50 {
51 "uptime": 0,
52 "diskwrite": 0,
53 "name": "test02",
54 "maxcpu": 0,
55 "node": "NODE1",
56 "mem": 0,
57 "netout": 0,
58 "netin": 0,
59 "maxmem": 0,
60 "diskread": 0,
61 "disk": 0,
62 "maxdisk": 0,
63 "status": "running",
64 "cpu": 0,
65 "id": "qemu/101",
66 "template": 0,
67 "vmid": 101,
68 "type": "qemu",
69 },
70 {
71 "uptime": 0,
72 "diskwrite": 0,
73 "name": "test03",
74 "maxcpu": 0,
75 "node": "NODE2",
76 "mem": 0,
77 "netout": 0,
78 "netin": 0,
79 "maxmem": 0,
80 "diskread": 0,
81 "disk": 0,
82 "maxdisk": 0,
83 "status": "running",
84 "cpu": 0,
85 "id": "qemu/102",
86 "template": 0,
87 "vmid": 102,
88 "type": "qemu",
89 },
90 {
91 "uptime": 0,
92 "diskwrite": 0,
93 "name": "test04",
94 "maxcpu": 0,
95 "node": "NODE3",
96 "mem": 0,
97 "netout": 0,
98 "netin": 0,
99 "maxmem": 0,
100 "diskread": 0,
101 "disk": 0,
102 "maxdisk": 0,
103 "status": "running",
104 "cpu": 0,
105 "id": "qemu/103",
106 "template": 0,
107 "vmid": 103,
108 "type": "qemu",
109 },
110 {
111 "uptime": 0,
112 "diskwrite": 0,
113 "name": "test05",
114 "maxcpu": 0,
115 "node": "NODE3",
116 "mem": 0,
117 "netout": 0,
118 "netin": 0,
119 "maxmem": 0,
120 "diskread": 0,
121 "disk": 0,
122 "maxdisk": 0,
123 "status": "running",
124 "cpu": 0,
125 "id": "qemu/105",
126 "template": 0,
127 "vmid": 105,
128 "type": "qemu",
129 },
130 ]
131
132 BACKUP_JOBS = [
133 {
134 "type": "vzdump",
135 "id": "backup-001",
136 "storage": "local",
137 "vmid": "100,101,102",
138 "enabled": 1,
139 "next-run": 1735138800,
140 "mailnotification": "always",
141 "schedule": "06,18:30",
142 "mode": "snapshot",
143 "notes-template": "{{guestname}}",
144 },
145 {
146 "schedule": "sat 15:00",
147 "notes-template": "{{guestname}}",
148 "mode": "snapshot",
149 "mailnotification": "always",
150 "next-run": 1735385400,
151 "type": "vzdump",
152 "enabled": 1,
153 "vmid": "101,102,103",
154 "storage": "local",
155 "id": "backup-002",
156 },
157 {
158 "schedule": "sun 16:00",
159 "notes-template": "{{guestname}}",
160 "mode": "snapshot",
161 "mailnotification": "always",
162 "next-run": 1735385400,
163 "type": "vzdump",
164 "enabled": 1,
165 "vmid": "101",
166 "storage": "local",
167 "id": "backup-003",
168 },
169 ]
170
171
172 class TestProxmoxBackupScheduleModule(ModuleTestCase):
173 def setUp(self):
174 super().setUp()
175 proxmox_utils.HAS_PROXMOXER = True
176 self.module = proxmox_backup_schedule
177 self.connect_mock = patch(
178 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
179 ).start()
180 self.connect_mock.return_value.cluster.resources.get.return_value = RESOURCE_LIST
181 self.connect_mock.return_value.cluster.backup.get.side_effect = lambda backup_id=None: (
182 BACKUP_JOBS if backup_id is None else [job for job in BACKUP_JOBS if job["id"] == backup_id]
183 )
184
185 def tearDown(self):
186 self.connect_mock.stop()
187 super().tearDown()
188
189 def test_module_fail_when_required_args_missing(self):
190 with pytest.raises(AnsibleFailJson) as exc_info, set_module_args({}):
191 self.module.main()
192
193 result = exc_info.value.args[0]
194 assert result["msg"] == "missing required arguments: api_host, api_user, state"
195
196 def test_update_vmid_in_backup(self):
197 with pytest.raises(AnsibleExitJson) as exc_info, set_module_args(
198 {
199 "api_host": "proxmoxhost",
200 "api_user": "root@pam",
201 "api_password": "supersecret",
202 "vm_name": "test05",
203 "backup_id": "backup-001",
204 "state": "present",
205 }
206 ):
207 self.module.main()
208
209 result = exc_info.value.args[0]
210 assert result["changed"] is True
211
212 def test_delete_vmid_from_backup(self):
213 with pytest.raises(AnsibleExitJson) as exc_info, set_module_args(
214 {
215 "api_host": "proxmoxhost",
216 "api_user": "root@pam",
217 "api_password": "supersecret",
218 "vm_id": 102,
219 "state": "absent",
220 }
221 ):
222 self.module.main()
223
224 result = exc_info.value.args[0]
225 assert result["changed"] is True
226
227 def test_ensure_vmid_is_absent_from_backup(self):
228 with pytest.raises(AnsibleExitJson) as exc_info, set_module_args(
229 {
230 "api_host": "proxmoxhost",
231 "api_user": "root@pam",
232 "api_password": "supersecret",
233 "vm_id": 105,
234 "state": "absent",
235 }
236 ):
237 self.module.main()
238
239 result = exc_info.value.args[0]
240 assert result["changed"] is False
241
242 def test_ensure_vmid_is_absent_from_specfic_backup(self):
243 with pytest.raises(AnsibleExitJson) as exc_info, set_module_args(
244 {
245 "api_host": "proxmoxhost",
246 "api_user": "root@pam",
247 "api_password": "supersecret",
248 "vm_id": 102,
249 "backup_id": "backup-003",
250 "state": "absent",
251 }
252 ):
253 self.module.main()
254
255 result = exc_info.value.args[0]
256 assert result["changed"] is False
257
258 def test_delete_vmid_from_specfic_backup_id(self):
259 with pytest.raises(AnsibleExitJson) as exc_info, set_module_args(
260 {
261 "api_host": "proxmoxhost",
262 "api_user": "root@pam",
263 "api_password": "supersecret",
264 "vm_id": 101,
265 "backup_id": "backup-002",
266 "state": "absent",
267 }
268 ):
269 self.module.main()
270
271 result = exc_info.value.args[0]
272 assert result["changed"] is True
273
274 def test_fail_when_there_is_one_vmid_for_delete_in_backup_job(self):
275 with pytest.raises(AnsibleFailJson) as exc_info, set_module_args(
276 {
277 "api_host": "proxmoxhost",
278 "api_user": "root@pam",
279 "api_password": "supersecret",
280 "vm_id": 101,
281 "backup_id": "backup-003",
282 "state": "absent",
283 }
284 ):
285 self.module.main()
286
287 result = exc_info.value.args[0]
288 assert result["msg"] == "No more than one vmid is assigned to backup-003. You just can remove job."
289
290 def test_fail_when_there_is_one_vm_name_for_delete_in_backup_job(self):
291 with pytest.raises(AnsibleFailJson) as exc_info, set_module_args(
292 {
293 "api_host": "proxmoxhost",
294 "api_user": "root@pam",
295 "api_password": "supersecret",
296 "vm_name": "test02",
297 "backup_id": "backup-003",
298 "state": "absent",
299 }
300 ):
301 self.module.main()
302
303 result = exc_info.value.args[0]
304 assert result["msg"] == "No more than one vmid is assigned to backup-003. You just can remove job."