| 1 |
# |
| 2 |
# Copyright (c) 2019, Ansible Project |
| 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_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 10 |
AnsibleExitJson, |
| 11 |
AnsibleFailJson, |
| 12 |
ModuleTestCase, |
| 13 |
set_module_args, |
| 14 |
) |
| 15 |
|
| 16 |
import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils |
| 17 |
from ansible_collections.community.proxmox.plugins.modules import proxmox_backup |
| 18 |
|
| 19 |
proxmoxer = pytest.importorskip("proxmoxer") |
| 20 |
|
| 21 |
|
| 22 |
MINIMAL_PERMISSIONS = { |
| 23 |
"/sdn/zones": {"Datastore.AllocateSpace": 1, "Datastore.Audit": 1}, |
| 24 |
"/nodes": {"Datastore.AllocateSpace": 1, "Datastore.Audit": 1}, |
| 25 |
"/sdn": {"Datastore.AllocateSpace": 1, "Datastore.Audit": 1}, |
| 26 |
"/vms": { |
| 27 |
"VM.Audit": 1, |
| 28 |
"Sys.Audit": 1, |
| 29 |
"Mapping.Audit": 1, |
| 30 |
"VM.Backup": 1, |
| 31 |
"Datastore.Audit": 1, |
| 32 |
"SDN.Audit": 1, |
| 33 |
"Pool.Audit": 1, |
| 34 |
}, |
| 35 |
"/": {"Datastore.Audit": 1, "Datastore.AllocateSpace": 1}, |
| 36 |
"/storage/local-zfs": {"Datastore.AllocateSpace": 1, "Datastore.Audit": 1}, |
| 37 |
"/storage": {"Datastore.AllocateSpace": 1, "Datastore.Audit": 1}, |
| 38 |
"/access": {"Datastore.AllocateSpace": 1, "Datastore.Audit": 1}, |
| 39 |
"/vms/101": { |
| 40 |
"VM.Backup": 1, |
| 41 |
"Mapping.Audit": 1, |
| 42 |
"Datastore.AllocateSpace": 0, |
| 43 |
"Sys.Audit": 1, |
| 44 |
"VM.Audit": 1, |
| 45 |
"SDN.Audit": 1, |
| 46 |
"Pool.Audit": 1, |
| 47 |
"Datastore.Audit": 1, |
| 48 |
}, |
| 49 |
"/vms/100": { |
| 50 |
"VM.Backup": 1, |
| 51 |
"Mapping.Audit": 1, |
| 52 |
"Datastore.AllocateSpace": 0, |
| 53 |
"Sys.Audit": 1, |
| 54 |
"VM.Audit": 1, |
| 55 |
"SDN.Audit": 1, |
| 56 |
"Pool.Audit": 1, |
| 57 |
"Datastore.Audit": 1, |
| 58 |
}, |
| 59 |
"/pool": {"Datastore.Audit": 1, "Datastore.AllocateSpace": 1}, |
| 60 |
} |
| 61 |
|
| 62 |
STORAGE = [ |
| 63 |
{ |
| 64 |
"type": "pbs", |
| 65 |
"username": "test@pbs", |
| 66 |
"datastore": "Backup-Pool", |
| 67 |
"server": "10.0.0.1", |
| 68 |
"shared": 1, |
| 69 |
"fingerprint": "94:fd:ac:e7:d5:36:0e:11:5b:23:05:40:d2:a4:e1:8a:c1:52:41:01:07:28:c0:4d:c5:ee:df:7f:7c:03:ab:41", |
| 70 |
"prune-backups": "keep-all=1", |
| 71 |
"storage": "backup", |
| 72 |
"content": "backup", |
| 73 |
"digest": "ca46a68d7699de061c139d714892682ea7c9d681", |
| 74 |
}, |
| 75 |
{ |
| 76 |
"nodes": "node1,node2,node3", |
| 77 |
"sparse": 1, |
| 78 |
"type": "zfspool", |
| 79 |
"content": "rootdir,images", |
| 80 |
"digest": "ca46a68d7699de061c139d714892682ea7c9d681", |
| 81 |
"pool": "rpool/data", |
| 82 |
"storage": "local-zfs", |
| 83 |
}, |
| 84 |
] |
| 85 |
|
| 86 |
|
| 87 |
VMS = [ |
| 88 |
{ |
| 89 |
"diskwrite": 0, |
| 90 |
"vmid": 100, |
| 91 |
"node": "node1", |
| 92 |
"id": "lxc/100", |
| 93 |
"maxdisk": 10000, |
| 94 |
"template": 0, |
| 95 |
"disk": 10000, |
| 96 |
"uptime": 10000, |
| 97 |
"maxmem": 10000, |
| 98 |
"maxcpu": 1, |
| 99 |
"netin": 10000, |
| 100 |
"type": "lxc", |
| 101 |
"netout": 10000, |
| 102 |
"mem": 10000, |
| 103 |
"diskread": 10000, |
| 104 |
"cpu": 0.01, |
| 105 |
"name": "test-lxc", |
| 106 |
"status": "running", |
| 107 |
}, |
| 108 |
{ |
| 109 |
"diskwrite": 0, |
| 110 |
"vmid": 101, |
| 111 |
"node": "node2", |
| 112 |
"id": "kvm/101", |
| 113 |
"maxdisk": 10000, |
| 114 |
"template": 0, |
| 115 |
"disk": 10000, |
| 116 |
"uptime": 10000, |
| 117 |
"maxmem": 10000, |
| 118 |
"maxcpu": 1, |
| 119 |
"netin": 10000, |
| 120 |
"type": "lxc", |
| 121 |
"netout": 10000, |
| 122 |
"mem": 10000, |
| 123 |
"diskread": 10000, |
| 124 |
"cpu": 0.01, |
| 125 |
"name": "test-kvm", |
| 126 |
"status": "running", |
| 127 |
}, |
| 128 |
] |
| 129 |
|
| 130 |
NODES = [ |
| 131 |
{"level": "", "type": "node", "node": "node1", "status": "online", "id": "node/node1", "cgroup-mode": 2}, |
| 132 |
{"status": "online", "id": "node/node2", "cgroup-mode": 2, "level": "", "node": "node2", "type": "node"}, |
| 133 |
{"status": "online", "id": "node/node3", "cgroup-mode": 2, "level": "", "node": "node3", "type": "node"}, |
| 134 |
] |
| 135 |
|
| 136 |
TASK_API_RETURN = { |
| 137 |
"node1": { |
| 138 |
"starttime": 1732606253, |
| 139 |
"status": "stopped", |
| 140 |
"type": "vzdump", |
| 141 |
"pstart": 517463911, |
| 142 |
"upid": "UPID:node1:003F8C63:1E7FB79C:67449780:vzdump:100:root@pam:", |
| 143 |
"id": "100", |
| 144 |
"node": "hypervisor", |
| 145 |
"pid": 541669, |
| 146 |
"user": "test@pve", |
| 147 |
"exitstatus": "OK", |
| 148 |
}, |
| 149 |
"node2": { |
| 150 |
"starttime": 1732606253, |
| 151 |
"status": "stopped", |
| 152 |
"type": "vzdump", |
| 153 |
"pstart": 517463911, |
| 154 |
"upid": "UPID:node2:000029DD:1599528B:6108F068:vzdump:101:root@pam:", |
| 155 |
"id": "101", |
| 156 |
"node": "hypervisor", |
| 157 |
"pid": 541669, |
| 158 |
"user": "test@pve", |
| 159 |
"exitstatus": "OK", |
| 160 |
}, |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
VZDUMP_API_RETURN = { |
| 165 |
"node1": "UPID:node1:003F8C63:1E7FB79C:67449780:vzdump:100:root@pam:", |
| 166 |
"node2": "UPID:node2:000029DD:1599528B:6108F068:vzdump:101:root@pam:", |
| 167 |
"node3": "OK", |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
TASKLOG_API_RETURN = { |
| 172 |
"node1": [ |
| 173 |
{ |
| 174 |
"n": 1, |
| 175 |
"t": "INFO: starting new backup job: vzdump 100 --mode snapshot --node node1 " |
| 176 |
"--notes-template '{{guestname}}' --storage backup --notification-mode auto", |
| 177 |
}, |
| 178 |
{"t": "INFO: Starting Backup of VM 100 (lxc)", "n": 2}, |
| 179 |
{"n": 23, "t": "INFO: adding notes to backup"}, |
| 180 |
{"n": 24, "t": "INFO: Finished Backup of VM 100 (00:00:03)"}, |
| 181 |
{"n": 25, "t": "INFO: Backup finished at 2024-11-25 16:28:03"}, |
| 182 |
{"t": "INFO: Backup job finished successfully", "n": 26}, |
| 183 |
{"n": 27, "t": "TASK OK"}, |
| 184 |
], |
| 185 |
"node2": [ |
| 186 |
{ |
| 187 |
"n": 1, |
| 188 |
"t": "INFO: starting new backup job: vzdump 101 --mode snapshot --node node2 " |
| 189 |
"--notes-template '{{guestname}}' --storage backup --notification-mode auto", |
| 190 |
}, |
| 191 |
{"t": "INFO: Starting Backup of VM 101 (kvm)", "n": 2}, |
| 192 |
{"n": 24, "t": "INFO: Finished Backup of VM 100 (00:00:03)"}, |
| 193 |
{"n": 25, "t": "INFO: Backup finished at 2024-11-25 16:28:03"}, |
| 194 |
{"t": "INFO: Backup job finished successfully", "n": 26}, |
| 195 |
{"n": 27, "t": "TASK OK"}, |
| 196 |
], |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
def return_valid_resources(resource_type, *args, **kwargs): |
| 201 |
if resource_type == "vm": |
| 202 |
return VMS |
| 203 |
if resource_type == "node": |
| 204 |
return NODES |
| 205 |
|
| 206 |
|
| 207 |
def return_vzdump_api(node, *args, **kwargs): |
| 208 |
if node in ("node1", "node2", "node3"): |
| 209 |
return VZDUMP_API_RETURN[node] |
| 210 |
|
| 211 |
|
| 212 |
def return_logs_api(node, *args, **kwargs): |
| 213 |
if node in ("node1", "node2"): |
| 214 |
return TASKLOG_API_RETURN[node] |
| 215 |
|
| 216 |
|
| 217 |
def return_task_status_api(node, *args, **kwargs): |
| 218 |
if node in ("node1", "node2"): |
| 219 |
return TASK_API_RETURN[node] |
| 220 |
|
| 221 |
|
| 222 |
class TestProxmoxBackup(ModuleTestCase): |
| 223 |
def setUp(self): |
| 224 |
super().setUp() |
| 225 |
proxmox_utils.HAS_PROXMOXER = True |
| 226 |
self.module = proxmox_backup |
| 227 |
self.connect_mock = patch( |
| 228 |
"ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect", |
| 229 |
).start() |
| 230 |
self.mock_get_permissions = patch.object(proxmox_backup.ProxmoxBackupAnsible, "_get_permissions").start() |
| 231 |
self.mock_get_storages = patch.object(proxmox_utils.ProxmoxAnsible, "get_storages").start() |
| 232 |
self.mock_get_resources = patch.object(proxmox_backup.ProxmoxBackupAnsible, "_get_resources").start() |
| 233 |
self.mock_get_tasklog = patch.object(proxmox_backup.ProxmoxBackupAnsible, "_get_tasklog").start() |
| 234 |
self.mock_post_vzdump = patch.object(proxmox_backup.ProxmoxBackupAnsible, "_post_vzdump").start() |
| 235 |
self.mock_get_taskok = patch.object(proxmox_backup.ProxmoxBackupAnsible, "_get_taskok").start() |
| 236 |
self.mock_get_permissions.return_value = MINIMAL_PERMISSIONS |
| 237 |
self.mock_get_storages.return_value = STORAGE |
| 238 |
self.mock_get_resources.side_effect = return_valid_resources |
| 239 |
self.mock_get_taskok.side_effect = return_task_status_api |
| 240 |
self.mock_get_tasklog.side_effect = return_logs_api |
| 241 |
self.mock_post_vzdump.side_effect = return_vzdump_api |
| 242 |
|
| 243 |
def tearDown(self): |
| 244 |
self.connect_mock.stop() |
| 245 |
self.mock_get_permissions.stop() |
| 246 |
self.mock_get_storages.stop() |
| 247 |
self.mock_get_resources.stop() |
| 248 |
super().tearDown() |
| 249 |
|
| 250 |
def test_proxmox_backup_without_argument(self): |
| 251 |
with set_module_args({}), pytest.raises(AnsibleFailJson): |
| 252 |
proxmox_backup.main() |
| 253 |
|
| 254 |
def test_create_backup_check_mode(self): |
| 255 |
with set_module_args( |
| 256 |
{ |
| 257 |
"api_user": "root@pam", |
| 258 |
"api_password": "secret", |
| 259 |
"api_host": "127.0.0.1", |
| 260 |
"mode": "all", |
| 261 |
"storage": "backup", |
| 262 |
"_ansible_check_mode": True, |
| 263 |
} |
| 264 |
), pytest.raises(AnsibleExitJson) as exc_info: |
| 265 |
proxmox_backup.main() |
| 266 |
|
| 267 |
result = exc_info.value.args[0] |
| 268 |
|
| 269 |
assert result["changed"] is True |
| 270 |
assert result["msg"] == "Backups would be created" |
| 271 |
assert len(result["backups"]) == 0 |
| 272 |
assert self.mock_get_taskok.call_count == 0 |
| 273 |
assert self.mock_get_tasklog.call_count == 0 |
| 274 |
assert self.mock_post_vzdump.call_count == 0 |
| 275 |
|
| 276 |
def test_create_backup_all_mode(self): |
| 277 |
with set_module_args( |
| 278 |
{ |
| 279 |
"api_user": "root@pam", |
| 280 |
"api_password": "secret", |
| 281 |
"api_host": "127.0.0.1", |
| 282 |
"mode": "all", |
| 283 |
"storage": "backup", |
| 284 |
} |
| 285 |
), pytest.raises(AnsibleExitJson) as exc_info: |
| 286 |
proxmox_backup.main() |
| 287 |
|
| 288 |
result = exc_info.value.args[0] |
| 289 |
assert result["changed"] is True |
| 290 |
assert result["msg"] == "Backup tasks created" |
| 291 |
for backup_result in result["backups"]: |
| 292 |
assert backup_result["upid"] in {VZDUMP_API_RETURN[key] for key in VZDUMP_API_RETURN} |
| 293 |
assert self.mock_get_taskok.call_count == 0 |
| 294 |
assert self.mock_post_vzdump.call_count == len(VZDUMP_API_RETURN) |
| 295 |
|
| 296 |
def test_create_backup_include_mode_with_wait(self): |
| 297 |
with set_module_args( |
| 298 |
{ |
| 299 |
"api_user": "root@pam", |
| 300 |
"api_password": "secret", |
| 301 |
"api_host": "127.0.0.1", |
| 302 |
"mode": "include", |
| 303 |
"node": "node1", |
| 304 |
"storage": "backup", |
| 305 |
"vmids": [100], |
| 306 |
"wait": True, |
| 307 |
} |
| 308 |
), pytest.raises(AnsibleExitJson) as exc_info: |
| 309 |
proxmox_backup.main() |
| 310 |
|
| 311 |
result = exc_info.value.args[0] |
| 312 |
assert result["changed"] is True |
| 313 |
assert result["msg"] == "Backups succeeded" |
| 314 |
for backup_result in result["backups"]: |
| 315 |
assert backup_result["upid"] in {VZDUMP_API_RETURN[key] for key in VZDUMP_API_RETURN} |
| 316 |
assert self.mock_get_taskok.call_count == 1 |
| 317 |
assert self.mock_post_vzdump.call_count == 1 |
| 318 |
|
| 319 |
def test_fail_insufficient_permissions(self): |
| 320 |
with set_module_args( |
| 321 |
{ |
| 322 |
"api_user": "root@pam", |
| 323 |
"api_password": "secret", |
| 324 |
"api_host": "127.0.0.1", |
| 325 |
"mode": "include", |
| 326 |
"storage": "backup", |
| 327 |
"performance_tweaks": "max-workers=2", |
| 328 |
"vmids": [100], |
| 329 |
"wait": True, |
| 330 |
} |
| 331 |
), pytest.raises(AnsibleFailJson) as exc_info: |
| 332 |
proxmox_backup.main() |
| 333 |
|
| 334 |
result = exc_info.value.args[0] |
| 335 |
assert ( |
| 336 |
result["msg"] |
| 337 |
== "Insufficient permission: Performance_tweaks and bandwidth require 'Sys.Modify' permission for '/'" |
| 338 |
) |
| 339 |
assert self.mock_get_taskok.call_count == 0 |
| 340 |
assert self.mock_post_vzdump.call_count == 0 |
| 341 |
|
| 342 |
def test_fail_missing_node(self): |
| 343 |
with set_module_args( |
| 344 |
{ |
| 345 |
"api_user": "root@pam", |
| 346 |
"api_password": "secret", |
| 347 |
"api_host": "127.0.0.1", |
| 348 |
"mode": "include", |
| 349 |
"storage": "backup", |
| 350 |
"node": "nonexistingnode", |
| 351 |
"vmids": [100], |
| 352 |
"wait": True, |
| 353 |
} |
| 354 |
), pytest.raises(AnsibleFailJson) as exc_info: |
| 355 |
proxmox_backup.main() |
| 356 |
|
| 357 |
result = exc_info.value.args[0] |
| 358 |
assert result["msg"] == "Node nonexistingnode was specified, but does not exist on the cluster" |
| 359 |
assert self.mock_get_taskok.call_count == 0 |
| 360 |
assert self.mock_post_vzdump.call_count == 0 |
| 361 |
|
| 362 |
def test_fail_missing_storage(self): |
| 363 |
with set_module_args( |
| 364 |
{ |
| 365 |
"api_user": "root@pam", |
| 366 |
"api_password": "secret", |
| 367 |
"api_host": "127.0.0.1", |
| 368 |
"mode": "include", |
| 369 |
"storage": "nonexistingstorage", |
| 370 |
"vmids": [100], |
| 371 |
"wait": True, |
| 372 |
} |
| 373 |
), pytest.raises(AnsibleFailJson) as exc_info: |
| 374 |
proxmox_backup.main() |
| 375 |
|
| 376 |
result = exc_info.value.args[0] |
| 377 |
assert result["msg"] == "Storage nonexistingstorage does not exist in the cluster" |
| 378 |
assert self.mock_get_taskok.call_count == 0 |
| 379 |
assert self.mock_post_vzdump.call_count == 0 |