Debian Codegraph @

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

raw
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
7 import json
8 from unittest.mock import MagicMock, 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 set_module_args
15
16 import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils
17 from ansible_collections.community.proxmox.plugins.modules import proxmox_snap
18
19
20 def get_resources(**_):
21 return [
22 {
23 "diskwrite": 0,
24 "vmid": 100,
25 "node": "localhost",
26 "id": "lxc/100",
27 "maxdisk": 10000,
28 "template": 0,
29 "disk": 10000,
30 "uptime": 10000,
31 "maxmem": 10000,
32 "maxcpu": 1,
33 "netin": 10000,
34 "type": "lxc",
35 "netout": 10000,
36 "mem": 10000,
37 "diskread": 10000,
38 "cpu": 0.01,
39 "name": "test-lxc",
40 "status": "running",
41 }
42 ]
43
44
45 def fake_api(mocker):
46 r = mocker.MagicMock()
47 r.cluster.resources.get = MagicMock(side_effect=get_resources)
48 return r
49
50
51 def test_proxmox_snap_without_argument(capfd):
52 with set_module_args({}), pytest.raises(SystemExit):
53 proxmox_snap.main()
54
55 out, err = capfd.readouterr()
56 assert not err
57 assert json.loads(out)["failed"]
58
59
60 @patch("ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect")
61 def test_create_snapshot_check_mode(connect_mock, capfd, mocker):
62 with set_module_args(
63 {
64 "hostname": "test-lxc",
65 "api_user": "root@pam",
66 "api_password": "secret",
67 "api_host": "127.0.0.1",
68 "state": "present",
69 "snapname": "test",
70 "timeout": "1",
71 "force": True,
72 "_ansible_check_mode": True,
73 }
74 ):
75 proxmox_utils.HAS_PROXMOXER = True
76 connect_mock.side_effect = lambda: fake_api(mocker)
77 with pytest.raises(SystemExit):
78 proxmox_snap.main()
79
80 out, err = capfd.readouterr()
81 assert not err
82 assert not json.loads(out)["changed"]
83
84
85 @patch("ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect")
86 def test_remove_snapshot_check_mode(connect_mock, capfd, mocker):
87 with set_module_args(
88 {
89 "hostname": "test-lxc",
90 "api_user": "root@pam",
91 "api_password": "secret",
92 "api_host": "127.0.0.1",
93 "state": "absent",
94 "snapname": "test",
95 "timeout": "1",
96 "force": True,
97 "_ansible_check_mode": True,
98 }
99 ):
100 proxmox_utils.HAS_PROXMOXER = True
101 connect_mock.side_effect = lambda: fake_api(mocker)
102 with pytest.raises(SystemExit):
103 proxmox_snap.main()
104
105 out, err = capfd.readouterr()
106 assert not err
107 assert not json.loads(out)["changed"]
108
109
110 @patch("ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect")
111 def test_rollback_snapshot_check_mode(connect_mock, capfd, mocker):
112 with set_module_args(
113 {
114 "hostname": "test-lxc",
115 "api_user": "root@pam",
116 "api_password": "secret",
117 "api_host": "127.0.0.1",
118 "state": "rollback",
119 "snapname": "test",
120 "timeout": "1",
121 "force": True,
122 "_ansible_check_mode": True,
123 }
124 ):
125 proxmox_utils.HAS_PROXMOXER = True
126 connect_mock.side_effect = lambda: fake_api(mocker)
127 with pytest.raises(SystemExit):
128 proxmox_snap.main()
129
130 out, err = capfd.readouterr()
131 assert not err
132 output = json.loads(out)
133 assert not output["changed"]
134 assert output["msg"] == "Snapshot test does not exist"