| 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_subnet |
| 21 |
|
| 22 |
RAW_SUBNETS = [ |
| 23 |
{ |
| 24 |
"type": "subnet", |
| 25 |
"subnet": "ans1-10.10.2.0-24", |
| 26 |
"zone": "ans1", |
| 27 |
"snat": 0, |
| 28 |
"cidr": "10.10.2.0/24", |
| 29 |
"id": "ans1-10.10.2.0-24", |
| 30 |
"mask": "24", |
| 31 |
"dhcp-range": [ |
| 32 |
{"start-address": "10.10.2.5", "end-address": "10.10.2.25"}, |
| 33 |
{"start-address": "10.10.2.50", "end-address": "10.10.2.100"}, |
| 34 |
], |
| 35 |
"digest": "c870dc42a3b5356b6037590e9552cbd5d2334963", |
| 36 |
"vnet": "test", |
| 37 |
"network": "10.10.2.0", |
| 38 |
} |
| 39 |
] |
| 40 |
|
| 41 |
|
| 42 |
def exit_json(*args, **kwargs): |
| 43 |
"""function to patch over exit_json; package return data into an exception""" |
| 44 |
if "changed" not in kwargs: |
| 45 |
kwargs["changed"] = False |
| 46 |
raise SystemExit(kwargs) |
| 47 |
|
| 48 |
|
| 49 |
def fail_json(*args, **kwargs): |
| 50 |
"""function to patch over fail_json; package return data into an exception""" |
| 51 |
kwargs["failed"] = True |
| 52 |
raise SystemExit(kwargs) |
| 53 |
|
| 54 |
|
| 55 |
def get_module_args(vnet, subnet, zone, **kwargs): |
| 56 |
defaults = { |
| 57 |
"state": "present", |
| 58 |
"dhcp_range": None, |
| 59 |
"snat": 0, |
| 60 |
"dhcp_range_update_mode": "append", |
| 61 |
} |
| 62 |
return { |
| 63 |
"api_host": "host", |
| 64 |
"api_user": "user", |
| 65 |
"api_password": "password", |
| 66 |
"vnet": vnet, |
| 67 |
"subnet": subnet, |
| 68 |
"zone": zone, |
| 69 |
**defaults, |
| 70 |
**kwargs, |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
class TestProxmoxSubnetModule(ModuleTestCase): |
| 75 |
def setUp(self): |
| 76 |
super().setUp() |
| 77 |
proxmox_utils.HAS_PROXMOXER = True |
| 78 |
self.module = proxmox_subnet |
| 79 |
self.fail_json_patcher = patch( |
| 80 |
"ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json) |
| 81 |
) |
| 82 |
self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json) |
| 83 |
|
| 84 |
self.fail_json_mock = self.fail_json_patcher.start() |
| 85 |
self.exit_json_patcher.start() |
| 86 |
self.connect_mock = patch( |
| 87 |
"ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect", |
| 88 |
).start() |
| 89 |
self.version_mock = patch.object( |
| 90 |
proxmox_subnet.ProxmoxSubnetAnsible, "version", return_value=LooseVersion("9.0") |
| 91 |
).start() |
| 92 |
self.connect_mock.return_value.cluster.return_value.sdn.return_value.vnets.return_value.subnets.return_value.get.return_value = RAW_SUBNETS |
| 93 |
|
| 94 |
def tearDown(self): |
| 95 |
self.connect_mock.stop() |
| 96 |
self.exit_json_patcher.stop() |
| 97 |
self.fail_json_patcher.stop() |
| 98 |
self.version_mock.stop() |
| 99 |
super().tearDown() |
| 100 |
|
| 101 |
def test_subnet_create(self): |
| 102 |
# Create new Zone |
| 103 |
with pytest.raises(SystemExit) as exc_info, set_module_args( |
| 104 |
get_module_args(vnet="new_vnet", subnet="10.10.10.0/24", zone="test_zone") |
| 105 |
): |
| 106 |
self.module.main() |
| 107 |
result = exc_info.value.args[0] |
| 108 |
assert result["changed"] is True |
| 109 |
assert result["msg"] == "Created new subnet 10.10.10.0/24" |
| 110 |
assert result["subnet"] == "test_zone-10.10.10.0-24" |
| 111 |
|
| 112 |
def test_subnet_update(self): |
| 113 |
# Normal subnet param (snat) differ |
| 114 |
with pytest.raises(SystemExit) as exc_info, set_module_args( |
| 115 |
get_module_args(vnet="test", subnet="10.10.2.0/24", zone="ans1", snat=1) |
| 116 |
): |
| 117 |
self.module.main() |
| 118 |
result = exc_info.value.args[0] |
| 119 |
assert result["changed"] is True |
| 120 |
assert result["msg"] == "Updated subnet ans1-10.10.2.0-24" |
| 121 |
assert result["subnet"] == "ans1-10.10.2.0-24" |
| 122 |
|
| 123 |
# No update needed |
| 124 |
with pytest.raises(SystemExit) as exc_info, set_module_args( |
| 125 |
get_module_args(vnet="test", subnet="10.10.2.0/24", zone="ans1") |
| 126 |
): |
| 127 |
self.module.main() |
| 128 |
result = exc_info.value.args[0] |
| 129 |
assert result["changed"] is False |
| 130 |
assert result["msg"] == "subnet ans1-10.10.2.0-24 is already present with correct parameters." |
| 131 |
assert result["subnet"] == "ans1-10.10.2.0-24" |
| 132 |
|
| 133 |
# New dhcp_range |
| 134 |
with pytest.raises(SystemExit) as exc_info, set_module_args( |
| 135 |
get_module_args( |
| 136 |
vnet="test", |
| 137 |
subnet="10.10.2.0/24", |
| 138 |
zone="ans1", |
| 139 |
dhcp_range=[{"start": "10.10.2.150", "end": "10.10.2.200"}], |
| 140 |
) |
| 141 |
): |
| 142 |
self.module.main() |
| 143 |
result = exc_info.value.args[0] |
| 144 |
assert result["changed"] is True |
| 145 |
assert result["msg"] == "Updated subnet ans1-10.10.2.0-24" |
| 146 |
assert result["subnet"] == "ans1-10.10.2.0-24" |
| 147 |
|
| 148 |
# dhcp_range is partially overlapping and mode is append |
| 149 |
with pytest.raises(SystemExit) as exc_info, set_module_args( |
| 150 |
get_module_args( |
| 151 |
vnet="test", |
| 152 |
subnet="10.10.2.0/24", |
| 153 |
zone="ans1", |
| 154 |
dhcp_range=[{"start": "10.10.2.10", "end": "10.10.2.20"}], |
| 155 |
) |
| 156 |
): |
| 157 |
self.module.main() |
| 158 |
result = exc_info.value.args[0] |
| 159 |
assert self.fail_json_mock.called |
| 160 |
assert result["failed"] is True |
| 161 |
assert result["msg"] == "There are partially overlapping DHCP ranges. this is not allowed." |
| 162 |
|
| 163 |
def test_subnet_absent(self): |
| 164 |
with pytest.raises(SystemExit) as exc_info, set_module_args( |
| 165 |
get_module_args(vnet="test", subnet="10.10.2.0/24", zone="ans1", state="absent") |
| 166 |
): |
| 167 |
self.module.main() |
| 168 |
result = exc_info.value.args[0] |
| 169 |
assert result["changed"] is True |
| 170 |
assert result["msg"] == "Deleted subnet ans1-10.10.2.0-24" |
| 171 |
assert result["subnet"] == "ans1-10.10.2.0-24" |