Skip to content

Commit 8e549c3

Browse files
committed
proxmox_zone: Add unit tests
1 parent e69c8d9 commit 8e549c3

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) 2025, Jana Hoch <[email protected]>
4+
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
# SPDX-License-Identifier: GPL-3.0-or-later
6+
7+
from __future__ import absolute_import, division, print_function
8+
9+
__metaclass__ = type
10+
11+
from unittest.mock import patch, Mock
12+
13+
import pytest
14+
15+
proxmoxer = pytest.importorskip("proxmoxer")
16+
17+
from ansible_collections.community.proxmox.plugins.modules import proxmox_zone
18+
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
19+
ModuleTestCase,
20+
set_module_args,
21+
)
22+
import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils
23+
24+
RAW_ZONES = [
25+
{
26+
"zone": "ans1",
27+
"digest": "e3105246736ab2420104e34bca1dea68d152acc7",
28+
"ipam": "pve",
29+
"dhcp": "dnsmasq",
30+
"type": "simple"
31+
},
32+
{
33+
"type": "vlan",
34+
"zone": "lab",
35+
"digest": "e3105246736ab2420104e34bca1dea68d152acc7",
36+
"ipam": "pve",
37+
"bridge": "vmbr100"
38+
},
39+
{
40+
"digest": "e3105246736ab2420104e34bca1dea68d152acc7",
41+
"ipam": "pve",
42+
"zone": "test1",
43+
"type": "simple",
44+
"dhcp": "dnsmasq"
45+
}
46+
]
47+
48+
49+
def exit_json(*args, **kwargs):
50+
"""function to patch over exit_json; package return data into an exception"""
51+
if 'changed' not in kwargs:
52+
kwargs['changed'] = False
53+
raise SystemExit(kwargs)
54+
55+
56+
def fail_json(*args, **kwargs):
57+
"""function to patch over fail_json; package return data into an exception"""
58+
kwargs['failed'] = True
59+
raise SystemExit(kwargs)
60+
61+
62+
def get_module_args_state_none():
63+
return {
64+
'api_host': 'host',
65+
'api_user': 'user',
66+
'api_password': 'password',
67+
}
68+
69+
70+
def get_module_args_zone(type, zone, state='present', force=False, bridge=None):
71+
return {
72+
'api_host': 'host',
73+
'api_user': 'user',
74+
'api_password': 'password',
75+
'type': type,
76+
'zone': zone,
77+
'state': state,
78+
'force': force,
79+
'bridge': bridge
80+
}
81+
82+
83+
class TestProxmoxZoneModule(ModuleTestCase):
84+
def setUp(self):
85+
super(TestProxmoxZoneModule, self).setUp()
86+
proxmox_utils.HAS_PROXMOXER = True
87+
self.module = proxmox_zone
88+
self.fail_json_patcher = patch('ansible.module_utils.basic.AnsibleModule.fail_json',
89+
new=Mock(side_effect=fail_json))
90+
self.exit_json_patcher = patch('ansible.module_utils.basic.AnsibleModule.exit_json', new=exit_json)
91+
92+
self.fail_json_mock = self.fail_json_patcher.start()
93+
self.exit_json_patcher.start()
94+
self.connect_mock = patch(
95+
"ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
96+
).start()
97+
self.connect_mock.return_value.cluster.return_value.sdn.return_value.zones.return_value.get.return_value = RAW_ZONES
98+
99+
def tearDown(self):
100+
self.connect_mock.stop()
101+
# self.mock_module_helper.stop()
102+
self.exit_json_patcher.stop()
103+
self.fail_json_patcher.stop()
104+
super(TestProxmoxZoneModule, self).tearDown()
105+
106+
def test_get_zones(self):
107+
with pytest.raises(SystemExit) as exc_info:
108+
with set_module_args(get_module_args_state_none()):
109+
self.module.main()
110+
result = exc_info.value.args[0]
111+
assert result["changed"] is False
112+
assert result["msg"] == "Successfully retrieved zone info."
113+
assert result["zones"] == RAW_ZONES
114+
115+
def test_zone_present(self):
116+
# Create new Zone
117+
with pytest.raises(SystemExit) as exc_info:
118+
with set_module_args(get_module_args_zone(type='simple', zone='test')):
119+
self.module.main()
120+
result = exc_info.value.args[0]
121+
assert result["changed"] is True
122+
assert result["msg"] == "Created new Zone - test"
123+
assert result['zone'] == 'test'
124+
125+
# Zone Already exists without force
126+
with pytest.raises(SystemExit) as exc_info:
127+
with set_module_args(get_module_args_zone(type='simple', zone='test1')):
128+
self.module.main()
129+
result = exc_info.value.args[0]
130+
assert result["changed"] is False
131+
assert result["msg"] == 'Zone test1 already exists and force is false!'
132+
assert result['zone'] == 'test1'
133+
134+
# Zone Already exists with force and different type
135+
with pytest.raises(SystemExit) as exc_info:
136+
with set_module_args(get_module_args_zone(type='vlan', zone='test1', force=True, bridge='test')):
137+
self.module.main()
138+
result = exc_info.value.args[0]
139+
assert self.fail_json_mock.called
140+
assert result['failed'] is True
141+
assert result['msg'] == 'zone test1 exists with different type and we cannot change type post fact.'
142+
143+
def test_zone_update(self):
144+
with pytest.raises(SystemExit) as exc_info:
145+
with set_module_args(get_module_args_zone(type='simple', zone='test1', state='update')):
146+
self.module.main()
147+
result = exc_info.value.args[0]
148+
assert result["changed"] is True
149+
assert result["msg"] == "Updated zone test1"
150+
assert result['zone'] == 'test1'
151+
152+
def test_zone_absent(self):
153+
with pytest.raises(SystemExit) as exc_info:
154+
with set_module_args(get_module_args_zone(type='simple', zone='test1', state='absent')):
155+
self.module.main()
156+
result = exc_info.value.args[0]
157+
assert result["changed"] is True
158+
assert result["msg"] == "Successfully deleted zone test1"
159+
assert result['zone'] == 'test1'

0 commit comments

Comments
 (0)