Debian Codegraph @

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

raw
1 #
2 # Copyright (c) 2024 Marzieh Raoufnezhad <raoufnezhad at gmail.com>
3 # Copyright (c) 2024 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 from unittest.mock import patch
9
10 import pytest
11
12 proxmoxer = pytest.importorskip("proxmoxer")
13
14 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
15 AnsibleExitJson,
16 AnsibleFailJson,
17 ModuleTestCase,
18 set_module_args,
19 )
20
21 import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils
22 from ansible_collections.community.proxmox.plugins.modules import proxmox_backup_info
23
24 RESOURCE_LIST = [
25 {
26 "uptime": 0,
27 "diskwrite": 0,
28 "name": "test01",
29 "maxcpu": 0,
30 "node": "NODE1",
31 "mem": 0,
32 "netout": 0,
33 "netin": 0,
34 "maxmem": 0,
35 "diskread": 0,
36 "disk": 0,
37 "maxdisk": 0,
38 "status": "running",
39 "cpu": 0,
40 "id": "qemu/100",
41 "template": 0,
42 "vmid": 100,
43 "type": "qemu",
44 },
45 {
46 "uptime": 0,
47 "diskwrite": 0,
48 "name": "test02",
49 "maxcpu": 0,
50 "node": "NODE1",
51 "mem": 0,
52 "netout": 0,
53 "netin": 0,
54 "maxmem": 0,
55 "diskread": 0,
56 "disk": 0,
57 "maxdisk": 0,
58 "status": "running",
59 "cpu": 0,
60 "id": "qemu/101",
61 "template": 0,
62 "vmid": 101,
63 "type": "qemu",
64 },
65 {
66 "uptime": 0,
67 "diskwrite": 0,
68 "name": "test03",
69 "maxcpu": 0,
70 "node": "NODE2",
71 "mem": 0,
72 "netout": 0,
73 "netin": 0,
74 "maxmem": 0,
75 "diskread": 0,
76 "disk": 0,
77 "maxdisk": 0,
78 "status": "running",
79 "cpu": 0,
80 "id": "qemu/102",
81 "template": 0,
82 "vmid": 102,
83 "type": "qemu",
84 },
85 ]
86 BACKUP_JOBS = [
87 {
88 "type": "vzdump",
89 "id": "backup-83831498-c631",
90 "storage": "local",
91 "vmid": "100",
92 "enabled": 1,
93 "next-run": 1735138800,
94 "mailnotification": "always",
95 "schedule": "06,18:30",
96 "mode": "snapshot",
97 "notes-template": "guestname",
98 },
99 {
100 "schedule": "sat 15:00",
101 "notes-template": "guestname",
102 "mode": "snapshot",
103 "mailnotification": "always",
104 "next-run": 1735385400,
105 "type": "vzdump",
106 "enabled": 1,
107 "vmid": "100,101,102",
108 "storage": "local",
109 "id": "backup-70025700-2302",
110 },
111 ]
112
113 EXPECTED_BACKUP_OUTPUT = [
114 {
115 "bktype": "vzdump",
116 "enabled": 1,
117 "id": "backup-83831498-c631",
118 "mode": "snapshot",
119 "next-run": "2024-12-25 15:00:00",
120 "schedule": "06,18:30",
121 "storage": "local",
122 "vm_name": "test01",
123 "vmid": "100",
124 },
125 {
126 "bktype": "vzdump",
127 "enabled": 1,
128 "id": "backup-70025700-2302",
129 "mode": "snapshot",
130 "next-run": "2024-12-28 11:30:00",
131 "schedule": "sat 15:00",
132 "storage": "local",
133 "vm_name": "test01",
134 "vmid": "100",
135 },
136 {
137 "bktype": "vzdump",
138 "enabled": 1,
139 "id": "backup-70025700-2302",
140 "mode": "snapshot",
141 "next-run": "2024-12-28 11:30:00",
142 "schedule": "sat 15:00",
143 "storage": "local",
144 "vm_name": "test02",
145 "vmid": "101",
146 },
147 {
148 "bktype": "vzdump",
149 "enabled": 1,
150 "id": "backup-70025700-2302",
151 "mode": "snapshot",
152 "next-run": "2024-12-28 11:30:00",
153 "schedule": "sat 15:00",
154 "storage": "local",
155 "vm_name": "test03",
156 "vmid": "102",
157 },
158 ]
159 EXPECTED_BACKUP_JOBS_OUTPUT = [
160 {
161 "enabled": 1,
162 "id": "backup-83831498-c631",
163 "mailnotification": "always",
164 "mode": "snapshot",
165 "next-run": 1735138800,
166 "notes-template": "guestname",
167 "schedule": "06,18:30",
168 "storage": "local",
169 "type": "vzdump",
170 "vmid": "100",
171 },
172 {
173 "enabled": 1,
174 "id": "backup-70025700-2302",
175 "mailnotification": "always",
176 "mode": "snapshot",
177 "next-run": 1735385400,
178 "notes-template": "guestname",
179 "schedule": "sat 15:00",
180 "storage": "local",
181 "type": "vzdump",
182 "vmid": "100,101,102",
183 },
184 ]
185
186
187 class TestProxmoxBackupInfoModule(ModuleTestCase):
188 def setUp(self):
189 super().setUp()
190 proxmox_utils.HAS_PROXMOXER = True
191 self.module = proxmox_backup_info
192 self.connect_mock = patch(
193 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
194 ).start()
195 self.connect_mock.return_value.cluster.resources.get.return_value = RESOURCE_LIST
196 self.connect_mock.return_value.cluster.backup.get.return_value = BACKUP_JOBS
197
198 def tearDown(self):
199 self.connect_mock.stop()
200 super().tearDown()
201
202 def test_module_fail_when_required_args_missing(self):
203 with pytest.raises(AnsibleFailJson) as exc_info, set_module_args({}):
204 self.module.main()
205
206 result = exc_info.value.args[0]
207 assert result["msg"] == "missing required arguments: api_host, api_user"
208
209 def test_get_all_backups_information(self):
210 with pytest.raises(AnsibleExitJson) as exc_info, set_module_args(
211 {"api_host": "proxmoxhost", "api_user": "root@pam", "api_password": "supersecret"}
212 ):
213 self.module.main()
214
215 result = exc_info.value.args[0]
216 assert result["backup_info"] == EXPECTED_BACKUP_OUTPUT
217
218 def test_get_specific_backup_information_by_vmname(self):
219 with pytest.raises(AnsibleExitJson) as exc_info:
220 vmname = "test01"
221 expected_output = [backup for backup in EXPECTED_BACKUP_OUTPUT if backup["vm_name"] == vmname]
222 with set_module_args(
223 {"api_host": "proxmoxhost", "api_user": "root@pam", "api_password": "supersecret", "vm_name": vmname}
224 ):
225 self.module.main()
226
227 result = exc_info.value.args[0]
228 assert result["backup_info"] == expected_output
229 assert len(result["backup_info"]) == len(expected_output)
230
231 def test_get_specific_backup_information_by_vmid(self):
232 with pytest.raises(AnsibleExitJson) as exc_info:
233 vmid = "101"
234 expected_output = [backup for backup in EXPECTED_BACKUP_OUTPUT if backup["vmid"] == vmid]
235 with set_module_args(
236 {"api_host": "proxmoxhost", "api_user": "root@pam", "api_password": "supersecret", "vm_id": vmid}
237 ):
238 self.module.main()
239 result = exc_info.value.args[0]
240 assert result["backup_info"] == expected_output
241 assert len(result["backup_info"]) == 1
242
243 def test_get_specific_backup_information_by_backupjobs(self):
244 with pytest.raises(AnsibleExitJson) as exc_info:
245 backupjobs = True
246 with set_module_args(
247 {
248 "api_host": "proxmoxhost",
249 "api_user": "root@pam",
250 "api_password": "supersecret",
251 "backup_jobs": backupjobs,
252 }
253 ):
254 self.module.main()
255
256 result = exc_info.value.args[0]
257 assert result["backup_info"] == EXPECTED_BACKUP_JOBS_OUTPUT