Skip to content

Commit f9e0ccb

Browse files
committed
Update unit tests for proxmox_zone & proxmox_zone_info
1 parent 17d1505 commit f9e0ccb

File tree

2 files changed

+131
-29
lines changed

2 files changed

+131
-29
lines changed

tests/unit/plugins/modules/test_proxmox_zone.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ def get_module_args_state_none():
6767
}
6868

6969

70-
def get_module_args_zone(type, zone, state='present', force=False, bridge=None):
70+
def get_module_args_zone(zone_type, zone, state='present', update=True, bridge=None):
7171
return {
7272
'api_host': 'host',
7373
'api_user': 'user',
7474
'api_password': 'password',
75-
'type': type,
75+
'type': zone_type,
7676
'zone': zone,
7777
'state': state,
78-
'force': force,
78+
'update': update,
7979
'bridge': bridge
8080
}
8181

@@ -98,60 +98,50 @@ def setUp(self):
9898

9999
def tearDown(self):
100100
self.connect_mock.stop()
101-
# self.mock_module_helper.stop()
102101
self.exit_json_patcher.stop()
103102
self.fail_json_patcher.stop()
104103
super(TestProxmoxZoneModule, self).tearDown()
105104

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-
115105
def test_zone_present(self):
116106
# Create new Zone
117107
with pytest.raises(SystemExit) as exc_info:
118-
with set_module_args(get_module_args_zone(type='simple', zone='test')):
108+
with set_module_args(get_module_args_zone(zone_type='simple', zone='test')):
119109
self.module.main()
120110
result = exc_info.value.args[0]
121111
assert result["changed"] is True
122112
assert result["msg"] == "Created new Zone - test"
123113
assert result['zone'] == 'test'
124114

125-
# Zone Already exists without force
115+
# Update the zone
126116
with pytest.raises(SystemExit) as exc_info:
127-
with set_module_args(get_module_args_zone(type='simple', zone='test1')):
117+
with set_module_args(get_module_args_zone(zone_type='simple', zone='test1', state='present')):
128118
self.module.main()
129119
result = exc_info.value.args[0]
130-
assert result["changed"] is False
131-
assert result["msg"] == 'Zone test1 already exists and force is false!'
120+
assert result["changed"] is True
121+
assert result["msg"] == "Updated zone - test1"
132122
assert result['zone'] == 'test1'
133123

134-
# Zone Already exists with force and different type
124+
# Zone Already exists update=False
135125
with pytest.raises(SystemExit) as exc_info:
136-
with set_module_args(get_module_args_zone(type='vlan', zone='test1', force=True, bridge='test')):
126+
with set_module_args(get_module_args_zone(zone_type='simple', zone='test1', update=False)):
137127
self.module.main()
138128
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.'
129+
assert result["changed"] is False
130+
assert result["msg"] == 'Zone test1 already exists and update is false!'
131+
assert result['zone'] == 'test1'
142132

143-
def test_zone_update(self):
133+
# Zone Already exists with update=True
144134
with pytest.raises(SystemExit) as exc_info:
145-
with set_module_args(get_module_args_zone(type='simple', zone='test1', state='update')):
135+
with set_module_args(get_module_args_zone(zone_type='vlan', zone='test1', update=True, bridge='test')):
146136
self.module.main()
147137
result = exc_info.value.args[0]
148-
assert result["changed"] is True
149-
assert result["msg"] == "Updated zone test1"
150-
assert result['zone'] == 'test1'
138+
assert self.fail_json_mock.called
139+
assert result['failed'] is True
140+
assert result['msg'] == 'zone test1 exists with different type and we cannot change type post fact.'
151141

152142
def test_zone_absent(self):
153143
with pytest.raises(SystemExit) as exc_info:
154-
with set_module_args(get_module_args_zone(type='simple', zone='test1', state='absent')):
144+
with set_module_args(get_module_args_zone(zone_type='simple', zone='test1', state='absent')):
155145
self.module.main()
156146
result = exc_info.value.args[0]
157147
assert result["changed"] is True
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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_info
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(zone_type, zone, state='present', update=True, bridge=None):
71+
return {
72+
'api_host': 'host',
73+
'api_user': 'user',
74+
'api_password': 'password',
75+
'type': zone_type,
76+
'zone': zone,
77+
'state': state,
78+
'update': update,
79+
'bridge': bridge
80+
}
81+
82+
83+
class TestProxmoxZoneInfoModule(ModuleTestCase):
84+
def setUp(self):
85+
super(TestProxmoxZoneInfoModule, self).setUp()
86+
proxmox_utils.HAS_PROXMOXER = True
87+
self.module = proxmox_zone_info
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.exit_json_patcher.stop()
102+
self.fail_json_patcher.stop()
103+
super(TestProxmoxZoneInfoModule, self).tearDown()
104+
105+
def test_get_zones(self):
106+
with pytest.raises(SystemExit) as exc_info:
107+
with set_module_args(get_module_args_state_none()):
108+
self.module.main()
109+
result = exc_info.value.args[0]
110+
assert result["changed"] is False
111+
assert result["msg"] == "Successfully retrieved zone info."
112+
assert result["zones"] == RAW_ZONES

0 commit comments

Comments
 (0)