| 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 |
import json |
| 7 |
from unittest.mock import MagicMock, patch |
| 8 |
|
| 9 |
import pytest |
| 10 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args |
| 11 |
|
| 12 |
import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils |
| 13 |
from ansible_collections.community.proxmox.plugins.modules import proxmox_snap_info |
| 14 |
|
| 15 |
proxmoxer = pytest.importorskip("proxmoxer") |
| 16 |
|
| 17 |
|
| 18 |
def get_resources(**_): |
| 19 |
return [{"vmid": 100, "node": "localhost", "type": "lxc", "name": "test-lxc"}] |
| 20 |
|
| 21 |
|
| 22 |
def get_snapshots(): |
| 23 |
return [ |
| 24 |
{"name": "current", "description": "You are here"}, |
| 25 |
{"name": "before-upgrade", "description": "Pre-upgrade backup", "snaptime": 1600000000}, |
| 26 |
{"name": "after-upgrade", "description": "Post-upgrade state", "snaptime": 1600000001}, |
| 27 |
] |
| 28 |
|
| 29 |
|
| 30 |
def fake_api(mocker): |
| 31 |
r = mocker.MagicMock() |
| 32 |
r.cluster.resources.get = MagicMock(side_effect=get_resources) |
| 33 |
r.nodes.return_value.lxc.return_value.snapshot.get = MagicMock(return_value=get_snapshots()) |
| 34 |
return r |
| 35 |
|
| 36 |
|
| 37 |
@patch("ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect") |
| 38 |
def test_list_all_snapshots(connect_mock, capfd, mocker): |
| 39 |
with set_module_args({"api_user": "root@pam", "api_password": "secret", "api_host": "127.0.0.1", "vmid": "100"}): |
| 40 |
proxmox_utils.HAS_PROXMOXER = True |
| 41 |
connect_mock.side_effect = lambda: fake_api(mocker) |
| 42 |
|
| 43 |
with pytest.raises(SystemExit): |
| 44 |
proxmox_snap_info.main() |
| 45 |
|
| 46 |
out, err = capfd.readouterr() |
| 47 |
result = json.loads(out) |
| 48 |
|
| 49 |
assert not err |
| 50 |
assert not result.get("failed") |
| 51 |
assert len(result["snapshots"]) == len(get_snapshots()) |
| 52 |
assert result["snapshots"][1]["name"] == "before-upgrade" |
| 53 |
|
| 54 |
|
| 55 |
@patch("ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect") |
| 56 |
def test_get_specific_snapshot(connect_mock, capfd, mocker): |
| 57 |
with set_module_args( |
| 58 |
{ |
| 59 |
"api_user": "root@pam", |
| 60 |
"api_password": "secret", |
| 61 |
"api_host": "127.0.0.1", |
| 62 |
"vmid": "100", |
| 63 |
"snapname": "before-upgrade", |
| 64 |
} |
| 65 |
): |
| 66 |
proxmox_utils.HAS_PROXMOXER = True |
| 67 |
connect_mock.side_effect = lambda: fake_api(mocker) |
| 68 |
|
| 69 |
with pytest.raises(SystemExit): |
| 70 |
proxmox_snap_info.main() |
| 71 |
|
| 72 |
out, err = capfd.readouterr() |
| 73 |
result = json.loads(out) |
| 74 |
|
| 75 |
assert not err |
| 76 |
assert result["snapshot"]["name"] == "before-upgrade" |
| 77 |
assert "snapshots" not in result |
| 78 |
|
| 79 |
|
| 80 |
@patch("ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect") |
| 81 |
def test_snapshot_not_found(connect_mock, capfd, mocker): |
| 82 |
with set_module_args( |
| 83 |
{ |
| 84 |
"api_user": "root@pam", |
| 85 |
"api_password": "secret", |
| 86 |
"api_host": "127.0.0.1", |
| 87 |
"vmid": "100", |
| 88 |
"snapname": "non-existent", |
| 89 |
} |
| 90 |
): |
| 91 |
proxmox_utils.HAS_PROXMOXER = True |
| 92 |
connect_mock.side_effect = lambda: fake_api(mocker) |
| 93 |
|
| 94 |
with pytest.raises(SystemExit): |
| 95 |
proxmox_snap_info.main() |
| 96 |
|
| 97 |
out, err = capfd.readouterr() |
| 98 |
result = json.loads(out) |
| 99 |
|
| 100 |
assert not err |
| 101 |
assert result["msg"] == "Snapshot 'non-existent' not found" |
| 102 |
assert result["snapshots"] == [] |