Debian Codegraph @

ansible_collections/community/proxmox/tests/unit/plugins/modules/test_proxmox_vnet_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 Mock, patch
8
9 import pytest
10
11 proxmoxer = pytest.importorskip("proxmoxer")
12
13 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
14 ModuleTestCase,
15 set_module_args,
16 )
17
18 import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils
19 from ansible_collections.community.proxmox.plugins.modules import proxmox_vnet_info
20
21 RAW_VNETS = [
22 {"digest": "1c0fb7bcd93d4c8cc9c444a91b9064ee34e5c786", "type": "vnet", "vnet": "lab", "zone": "lab", "tag": 100},
23 {
24 "zone": "ans1",
25 "type": "vnet",
26 "vnet": "test",
27 "isolate-ports": 1,
28 "digest": "1c0fb7bcd93d4c8cc9c444a91b9064ee34e5c786",
29 "alias": "test1",
30 },
31 {"zone": "test1", "vnet": "test2", "type": "vnet", "digest": "1c0fb7bcd93d4c8cc9c444a91b9064ee34e5c786"},
32 ]
33
34 LAB_SUBNETS = [
35 {
36 "network": "10.10.100.0",
37 "snat": 1,
38 "zone": "lab",
39 "mask": "24",
40 "vnet": "lab",
41 "cidr": "10.10.100.0/24",
42 "dhcp-range": [],
43 "digest": "3ad95a6415851300419c14a62fcc0890b9095b88",
44 "subnet": "lab-10.10.100.0-24",
45 "type": "subnet",
46 "id": "lab-10.10.100.0-24",
47 }
48 ]
49
50 TEST_SUBNETS = [
51 {
52 "network": "10.10.2.0",
53 "cidr": "10.10.2.0/24",
54 "snat": 1,
55 "dhcp-range": [
56 {"end-address": "10.10.2.15", "start-address": "10.10.2.5"},
57 {"start-address": "10.10.2.20", "end-address": "10.10.2.40"},
58 ],
59 "zone": "ans1",
60 "mask": "24",
61 "digest": "3ad95a6415851300419c14a62fcc0890b9095b88",
62 "subnet": "ans1-10.10.2.0-24",
63 "vnet": "test",
64 "type": "subnet",
65 "id": "ans1-10.10.2.0-24",
66 }
67 ]
68
69 TEST2_SUBNETS = [
70 {
71 "network": "10.10.0.0",
72 "cidr": "10.10.0.0/24",
73 "dhcp-range": [{"end-address": "10.10.0.50", "start-address": "10.10.0.5"}],
74 "zone": "test1",
75 "mask": "24",
76 "digest": "3ad95a6415851300419c14a62fcc0890b9095b88",
77 "subnet": "test1-10.10.0.0-24",
78 "type": "subnet",
79 "vnet": "test2",
80 "id": "test1-10.10.0.0-24",
81 "gateway": "10.10.0.1",
82 },
83 {
84 "zone": "test1",
85 "dhcp-range": [{"end-address": "10.10.1.50", "start-address": "10.10.1.5"}],
86 "cidr": "10.10.1.0/24",
87 "network": "10.10.1.0",
88 "digest": "3ad95a6415851300419c14a62fcc0890b9095b88",
89 "mask": "24",
90 "id": "test1-10.10.1.0-24",
91 "type": "subnet",
92 "vnet": "test2",
93 "subnet": "test1-10.10.1.0-24",
94 "gateway": "10.10.1.0",
95 },
96 ]
97
98 LAB_FIREWALL = []
99 TEST2_FIREWALL = []
100
101 TEST_FIREWALL = [
102 {
103 "digest": "36016a02a5387d4c1171d29be966d550216bc500",
104 "enable": 1,
105 "macro": "DNS",
106 "action": "ACCEPT",
107 "type": "forward",
108 "dest": "+sdn/test2-gateway",
109 "log": "nolog",
110 "pos": 0,
111 },
112 {
113 "action": "ACCEPT",
114 "macro": "DHCPfwd",
115 "digest": "36016a02a5387d4c1171d29be966d550216bc500",
116 "enable": 1,
117 "pos": 1,
118 "log": "nolog",
119 "type": "forward",
120 },
121 ]
122
123
124 def exit_json(*args, **kwargs):
125 """function to patch over exit_json; package return data into an exception"""
126 if "changed" not in kwargs:
127 kwargs["changed"] = False
128 raise SystemExit(kwargs)
129
130
131 def fail_json(*args, **kwargs):
132 """function to patch over fail_json; package return data into an exception"""
133 kwargs["failed"] = True
134 raise SystemExit(kwargs)
135
136
137 def get_module_args(vnet=None):
138 return {"api_host": "host", "api_user": "user", "api_password": "password", "vnet": vnet}
139
140
141 class TestProxmoxVnetInfoModule(ModuleTestCase):
142 def setUp(self):
143 super().setUp()
144 proxmox_utils.HAS_PROXMOXER = True
145 self.module = proxmox_vnet_info
146 self.fail_json_patcher = patch(
147 "ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json)
148 )
149 self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json)
150
151 self.fail_json_mock = self.fail_json_patcher.start()
152 self.exit_json_patcher.start()
153 self.connect_mock = patch(
154 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
155 ).start()
156 self.mock_vnets = self.connect_mock.return_value.cluster.return_value.sdn.return_value.vnets.return_value
157 self.mock_vnets.get.return_value = RAW_VNETS
158
159 self.mock_vnets.lab.return_value.subnets.return_value.get.return_value = LAB_SUBNETS
160 self.mock_vnets.test.return_value.subnets.return_value.get.return_value = TEST_SUBNETS
161 self.mock_vnets.test2.return_value.subnets.return_value.get.return_value = TEST2_SUBNETS
162
163 self.mock_vnets.lab.return_value.firewall.return_value.rules.return_value.get.return_value = []
164 self.mock_vnets.test.return_value.firewall.return_value.rules.return_value.get.return_value = TEST_FIREWALL
165 self.mock_vnets.test2.return_value.firewall.return_value.rules.return_value.get.return_value = []
166
167 def tearDown(self):
168 self.connect_mock.stop()
169 self.exit_json_patcher.stop()
170 self.fail_json_patcher.stop()
171 super().tearDown()
172
173 def test_get_vnets(self):
174 with pytest.raises(SystemExit) as exc_info, set_module_args(get_module_args()):
175 self.module.main()
176
177 # Aggregate all vnet information into a single variable
178 full_vnet_information = RAW_VNETS.copy()
179 for idx, vnet in enumerate(RAW_VNETS):
180 subnet_var = f"{vnet['vnet'].upper()}_SUBNETS"
181 firewall_var = f"{vnet['vnet'].upper()}_FIREWALL"
182 full_vnet_information[idx]["subnets"] = globals()[subnet_var]
183 full_vnet_information[idx]["firewall_rules"] = globals()[firewall_var]
184
185 result = exc_info.value.args[0]
186 assert result["changed"] is False
187 assert result["msg"] == "Successfully retrieved vnet info"
188 assert result["vnets"] == RAW_VNETS