Debian Codegraph @

ansible_collections/community/proxmox/tests/unit/plugins/modules/test_proxmox_zone_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.module_utils.compat.version import LooseVersion
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_zone_info
21
22 RAW_ZONES = [
23 {
24 "zone": "ans1",
25 "digest": "e3105246736ab2420104e34bca1dea68d152acc7",
26 "ipam": "pve",
27 "dhcp": "dnsmasq",
28 "type": "simple",
29 },
30 {
31 "type": "vlan",
32 "zone": "lab",
33 "digest": "e3105246736ab2420104e34bca1dea68d152acc7",
34 "ipam": "pve",
35 "bridge": "vmbr100",
36 },
37 {
38 "digest": "e3105246736ab2420104e34bca1dea68d152acc7",
39 "ipam": "pve",
40 "zone": "test1",
41 "type": "simple",
42 "dhcp": "dnsmasq",
43 },
44 ]
45
46
47 def exit_json(*args, **kwargs):
48 """function to patch over exit_json; package return data into an exception"""
49 if "changed" not in kwargs:
50 kwargs["changed"] = False
51 raise SystemExit(kwargs)
52
53
54 def fail_json(*args, **kwargs):
55 """function to patch over fail_json; package return data into an exception"""
56 kwargs["failed"] = True
57 raise SystemExit(kwargs)
58
59
60 def get_module_args_state_none():
61 return {
62 "api_host": "host",
63 "api_user": "user",
64 "api_password": "password",
65 }
66
67
68 def get_module_args_zone(zone_type, zone, state="present", update=True, bridge=None):
69 return {
70 "api_host": "host",
71 "api_user": "user",
72 "api_password": "password",
73 "type": zone_type,
74 "zone": zone,
75 "state": state,
76 "update": update,
77 "bridge": bridge,
78 }
79
80
81 class TestProxmoxZoneInfoModule(ModuleTestCase):
82 def setUp(self):
83 super().setUp()
84 proxmox_utils.HAS_PROXMOXER = True
85 self.module = proxmox_zone_info
86 self.fail_json_patcher = patch(
87 "ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json)
88 )
89 self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json)
90
91 self.fail_json_mock = self.fail_json_patcher.start()
92 self.exit_json_patcher.start()
93 self.connect_mock = patch(
94 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
95 ).start()
96 self.version_mock = patch.object(
97 proxmox_zone_info.ProxmoxZoneInfoAnsible, "version", return_value=LooseVersion("9.0")
98 ).start()
99 self.connect_mock.return_value.cluster.return_value.sdn.return_value.zones.return_value.get.return_value = (
100 RAW_ZONES
101 )
102
103 def tearDown(self):
104 self.connect_mock.stop()
105 self.exit_json_patcher.stop()
106 self.fail_json_patcher.stop()
107 self.version_mock.stop()
108 super().tearDown()
109
110 def test_get_zones(self):
111 with pytest.raises(SystemExit) as exc_info, set_module_args(get_module_args_state_none()):
112 self.module.main()
113 result = exc_info.value.args[0]
114 assert result["changed"] is False
115 assert result["msg"] == "Successfully retrieved zone info."
116 assert result["zones"] == RAW_ZONES