Debian Codegraph @

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

raw
1 #
2 # Copyright (c) 2025, Jana Hoch <[email protected]>
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 from unittest.mock import patch
8
9 import pytest
10
11 proxmoxer = pytest.importorskip("proxmoxer")
12
13 from ansible.module_utils import basic
14 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
15 ModuleTestCase,
16 set_module_args,
17 )
18
19 import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils
20 from ansible_collections.community.proxmox.plugins.modules import proxmox_ipam_info
21
22 RAW_IPAM_STATUS = [
23 {"subnet": "10.10.1.0/24", "vnet": "test2", "zone": "test1", "ip": "10.10.1.0", "gateway": 1},
24 {"ip": "10.10.0.1", "gateway": 1, "vnet": "test2", "subnet": "10.10.0.0/24", "zone": "test1"},
25 {
26 "zone": "test1",
27 "vnet": "test2",
28 "subnet": "10.10.0.0/24",
29 "mac": "BC:24:11:F3:B1:81",
30 "vmid": 102,
31 "hostname": "ns3.proxmox.pc",
32 "ip": "10.10.0.8",
33 },
34 {
35 "subnet": "10.10.0.0/24",
36 "vnet": "test2",
37 "zone": "test1",
38 "ip": "10.10.0.7",
39 "hostname": "ns4.proxmox.pc",
40 "vmid": 103,
41 "mac": "BC:24:11:D5:CD:82",
42 },
43 {
44 "ip": "10.10.0.5",
45 "hostname": "ns2.proxmox.pc.test3",
46 "mac": "BC:24:11:86:77:56",
47 "vmid": 101,
48 "subnet": "10.10.0.0/24",
49 "vnet": "test2",
50 "zone": "test1",
51 },
52 ]
53
54 RAW_IPAM_STATUS_PVE8 = [
55 {"subnet": "10.10.1.0/24", "vnet": "test2", "zone": "test1", "ip": "10.10.1.0", "gateway": 1},
56 {"ip": "10.10.0.1", "gateway": 1, "vnet": "test2", "subnet": "10.10.0.0/24", "zone": "test1"},
57 {
58 "zone": "test1",
59 "vnet": "test2",
60 "subnet": "10.10.0.0/24",
61 "mac": "BC:24:11:F3:B1:81",
62 "vmid": "102",
63 "hostname": "ns3.proxmox.pc",
64 "ip": "10.10.0.8",
65 },
66 {
67 "subnet": "10.10.0.0/24",
68 "vnet": "test2",
69 "zone": "test1",
70 "ip": "10.10.0.7",
71 "hostname": "ns4.proxmox.pc",
72 "vmid": "103",
73 "mac": "BC:24:11:D5:CD:82",
74 },
75 {
76 "ip": "10.10.0.5",
77 "hostname": "ns2.proxmox.pc.test3",
78 "mac": "BC:24:11:86:77:56",
79 "vmid": "101",
80 "subnet": "10.10.0.0/24",
81 "vnet": "test2",
82 "zone": "test1",
83 },
84 ]
85
86 RAW_IPAM = [{"ipam": "pve", "type": "pve", "digest": "da39a3ee5e6b4b0d3255bfef95601890afd80709"}]
87
88
89 def exit_json(*args, **kwargs):
90 """function to patch over exit_json; package return data into an exception"""
91 if "changed" not in kwargs:
92 kwargs["changed"] = False
93 raise SystemExit(kwargs)
94
95
96 def fail_json(*args, **kwargs):
97 """function to patch over fail_json; package return data into an exception"""
98 kwargs["failed"] = True
99 raise SystemExit(kwargs)
100
101
102 def get_module_args(ipam=None, vmid=None):
103 return {"api_host": "host", "api_user": "user", "api_password": "password", "ipam": ipam, "vmid": vmid}
104
105
106 class TestProxmoxIpamInfoModule(ModuleTestCase):
107 def setUp(self):
108 super().setUp()
109 proxmox_utils.HAS_PROXMOXER = True
110 self.module = proxmox_ipam_info
111 self.mock_module_helper = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
112 self.mock_module_helper.start()
113 self.connect_mock = patch(
114 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
115 ).start()
116 self.mock_ipam = self.connect_mock.return_value.cluster.return_value.sdn.return_value.ipams.return_value
117 self.mock_ipam.get.return_value = RAW_IPAM
118 self.mock_ipam.pve.return_value.status.return_value.get.return_value = RAW_IPAM_STATUS
119 self.mock_ipam.status.return_value.get.return_value = RAW_IPAM_STATUS
120
121 def tearDown(self):
122 self.connect_mock.stop()
123 self.mock_module_helper.stop()
124 super().tearDown()
125
126 def test_get_all_ipam_status(self):
127 with pytest.raises(SystemExit) as exc_info, set_module_args(get_module_args(ipam=None)):
128 self.module.main()
129
130 result = exc_info.value.args[0]
131 assert result["changed"] is False
132 assert result["ipams"] == {"pve": RAW_IPAM_STATUS}
133
134 def test_get_all_ipam_pve_status(self):
135 with pytest.raises(SystemExit) as exc_info, set_module_args(get_module_args(ipam="pve")):
136 self.module.main()
137
138 result = exc_info.value.args[0]
139 assert result["changed"] is False
140 assert result["ipams"] == RAW_IPAM_STATUS
141
142 def test_get_ip_by_vmid(self):
143 with pytest.raises(SystemExit) as exc_info, set_module_args(get_module_args(vmid=102)):
144 self.module.main()
145
146 result = exc_info.value.args[0]
147 assert result["changed"] is False
148 assert result["ips"] == [
149 {
150 "zone": "test1",
151 "vnet": "test2",
152 "subnet": "10.10.0.0/24",
153 "mac": "BC:24:11:F3:B1:81",
154 "vmid": 102,
155 "hostname": "ns3.proxmox.pc",
156 "ip": "10.10.0.8",
157 }
158 ]
159
160 def test_get_ip_by_vmid_pve8(self):
161 self.mock_ipam.pve.return_value.status.return_value.get.return_value = RAW_IPAM_STATUS_PVE8
162 self.mock_ipam.status.return_value.get.return_value = RAW_IPAM_STATUS_PVE8
163
164 with pytest.raises(SystemExit) as exc_info, set_module_args(get_module_args(vmid=102)):
165 self.module.main()
166
167 result = exc_info.value.args[0]
168 assert result["changed"] is False
169 assert result["ips"] == [
170 {
171 "zone": "test1",
172 "vnet": "test2",
173 "subnet": "10.10.0.0/24",
174 "mac": "BC:24:11:F3:B1:81",
175 "vmid": "102",
176 "hostname": "ns3.proxmox.pc",
177 "ip": "10.10.0.8",
178 }
179 ]