| 1 |
# |
| 2 |
# Copyright (c) 2026, Clément Cruau (@PendaGTP) <[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 patch |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
proxmoxer = pytest.importorskip("proxmoxer") |
| 12 |
|
| 13 |
from ansible.module_utils import basic |
| 14 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 15 |
ModuleTestCase, |
| 16 |
set_module_args, |
| 17 |
) |
| 18 |
|
| 19 |
from ansible_collections.community.proxmox.plugins.modules import proxmox_cluster_firewall |
| 20 |
from ansible_collections.community.proxmox.plugins.modules.proxmox_cluster_firewall import ( |
| 21 |
_build_log_ratelimit_string, |
| 22 |
_parse_log_ratelimit_string, |
| 23 |
_validate_log_ratelimit_rate, |
| 24 |
) |
| 25 |
|
| 26 |
# -- Fixtures |
| 27 |
|
| 28 |
PROXMOX_OPTIONS_ENABLED = { |
| 29 |
"enable": 1, |
| 30 |
"ebtables": 1, |
| 31 |
"policy_in": "DROP", |
| 32 |
"policy_out": "ACCEPT", |
| 33 |
"policy_forward": "ACCEPT", |
| 34 |
"log_ratelimit": "enable=1,burst=5,rate=1/second", |
| 35 |
} |
| 36 |
|
| 37 |
PROXMOX_OPTIONS_DISABLED = { |
| 38 |
**PROXMOX_OPTIONS_ENABLED, |
| 39 |
"enable": 0, |
| 40 |
} |
| 41 |
|
| 42 |
# Expected Ansible-side representation of PROXMOX_OPTIONS_ENABLED |
| 43 |
ANSIBLE_OPTIONS_ENABLED = { |
| 44 |
"enabled": True, |
| 45 |
"ebtables": True, |
| 46 |
"input_policy": "DROP", |
| 47 |
"output_policy": "ACCEPT", |
| 48 |
"forward_policy": "ACCEPT", |
| 49 |
"log_ratelimit": {"enabled": True, "burst": 5, "rate": "1/second"}, |
| 50 |
} |
| 51 |
|
| 52 |
# Expected Ansible-side representation of PROXMOX_OPTIONS_DISABLED |
| 53 |
ANSIBLE_OPTIONS_DISABLED = { |
| 54 |
**ANSIBLE_OPTIONS_ENABLED, |
| 55 |
"enabled": False, |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
# -- Helpers |
| 60 |
|
| 61 |
|
| 62 |
def exit_json(*args, **kwargs): |
| 63 |
kwargs.setdefault("changed", False) |
| 64 |
raise SystemExit(kwargs) |
| 65 |
|
| 66 |
|
| 67 |
def fail_json(*args, **kwargs): |
| 68 |
kwargs["failed"] = True |
| 69 |
raise SystemExit(kwargs) |
| 70 |
|
| 71 |
|
| 72 |
def build_module_args(state="enabled", **overrides): |
| 73 |
return { |
| 74 |
"api_host": "host", |
| 75 |
"api_user": "user", |
| 76 |
"api_password": "password", |
| 77 |
"state": state, |
| 78 |
**overrides, |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
# -- Unit tests for module helper function |
| 83 |
|
| 84 |
|
| 85 |
class TestValidateLogRatelimitRate: |
| 86 |
@pytest.mark.parametrize("rate", [ |
| 87 |
"1/second", "10/minute", "100/hour", "5/day", "99/second", |
| 88 |
]) # fmt: skip |
| 89 |
def test_valid_rates(self, rate): |
| 90 |
assert _validate_log_ratelimit_rate(rate) is True |
| 91 |
|
| 92 |
@pytest.mark.parametrize("rate", [ |
| 93 |
"0/second", "1/week", "1/Second", "1 /second", "second", "1/", "", "abc", |
| 94 |
]) # fmt: skip |
| 95 |
def test_invalid_rates(self, rate): |
| 96 |
assert _validate_log_ratelimit_rate(rate) is False |
| 97 |
|
| 98 |
def test_none_rate_returns_true(self): |
| 99 |
assert _validate_log_ratelimit_rate(None) is True |
| 100 |
|
| 101 |
|
| 102 |
class TestParseLogRatelimitString: |
| 103 |
def test_full_string(self): |
| 104 |
result = _parse_log_ratelimit_string("enable=1,burst=5,rate=1/second") |
| 105 |
assert result == {"enabled": True, "burst": 5, "rate": "1/second"} |
| 106 |
|
| 107 |
def test_partial_string_rate_only(self): |
| 108 |
assert _parse_log_ratelimit_string("rate=2/hour") == {"rate": "2/hour"} |
| 109 |
|
| 110 |
def test_empty_or_none_returns_none(self): |
| 111 |
assert _parse_log_ratelimit_string("") is None |
| 112 |
assert _parse_log_ratelimit_string(None) is None |
| 113 |
|
| 114 |
def test_invalid_burst_value_is_skipped(self): |
| 115 |
result = _parse_log_ratelimit_string("enable=1,burst=abc,rate=1/second") |
| 116 |
assert "burst" not in result |
| 117 |
assert result == {"enabled": True, "rate": "1/second"} |
| 118 |
|
| 119 |
|
| 120 |
class TestBuildLogRatelimitString: |
| 121 |
def test_full_params(self): |
| 122 |
assert _build_log_ratelimit_string(enabled=True, burst=5, rate="1/second") == "enable=1,burst=5,rate=1/second" |
| 123 |
|
| 124 |
def test_none_param_excluded_from_output(self): |
| 125 |
assert _build_log_ratelimit_string(enabled=None, burst=5, rate="1/second") == "burst=5,rate=1/second" |
| 126 |
|
| 127 |
def test_all_none_returns_none(self): |
| 128 |
assert _build_log_ratelimit_string(None, None, None) is None |
| 129 |
|
| 130 |
def test_build_then_parse_returns_original_values(self): |
| 131 |
built = _build_log_ratelimit_string(enabled=True, burst=7, rate="3/hour") |
| 132 |
assert _parse_log_ratelimit_string(built) == {"enabled": True, "burst": 7, "rate": "3/hour"} |
| 133 |
|
| 134 |
|
| 135 |
# -- Module tests |
| 136 |
|
| 137 |
|
| 138 |
class TestProxmoxClusterFirewallModule(ModuleTestCase): |
| 139 |
def setUp(self): |
| 140 |
super().setUp() |
| 141 |
self.module = proxmox_cluster_firewall |
| 142 |
|
| 143 |
self.mock_module_helper = patch.multiple( |
| 144 |
basic.AnsibleModule, |
| 145 |
exit_json=exit_json, |
| 146 |
fail_json=fail_json, |
| 147 |
) |
| 148 |
self.mock_module_helper.start() |
| 149 |
|
| 150 |
self.connect_mock = patch( |
| 151 |
"ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect", |
| 152 |
).start() |
| 153 |
|
| 154 |
mock_api = self.connect_mock.return_value |
| 155 |
self.mock_api_fw_options = mock_api.cluster.return_value.firewall.return_value.options |
| 156 |
|
| 157 |
def tearDown(self): |
| 158 |
self.connect_mock.stop() |
| 159 |
self.mock_module_helper.stop() |
| 160 |
super().tearDown() |
| 161 |
|
| 162 |
def _run_module(self, args): |
| 163 |
with pytest.raises(SystemExit) as exc_info, set_module_args(args): |
| 164 |
self.module.main() |
| 165 |
return exc_info.value.args[0] |
| 166 |
|
| 167 |
# -- Helpers |
| 168 |
|
| 169 |
def _run_module(self, args): |
| 170 |
with pytest.raises(SystemExit) as exc_info, set_module_args(args): |
| 171 |
self.module.main() |
| 172 |
return exc_info.value.args[0] |
| 173 |
|
| 174 |
def _check_mode(self, **kwargs): |
| 175 |
return {**build_module_args(**kwargs), "_ansible_check_mode": True} |
| 176 |
|
| 177 |
def _assert_ansible_options(self, result, expected): |
| 178 |
for key, value in expected.items(): |
| 179 |
assert result[key] == value, f"Mismatch on '{key}': {result[key]!r} != {value!r}" |
| 180 |
|
| 181 |
def _run_disabled_update(self, initial_state): |
| 182 |
final_state = { |
| 183 |
"enable": 0, |
| 184 |
"ebtables": 1, |
| 185 |
"policy_in": "DROP", |
| 186 |
"policy_out": "ACCEPT", |
| 187 |
"policy_forward": "ACCEPT", |
| 188 |
"log_ratelimit": "enable=0,burst=10,rate=5/minute", |
| 189 |
} |
| 190 |
self.mock_api_fw_options.get.side_effect = [initial_state, final_state] |
| 191 |
|
| 192 |
result = self._run_module( |
| 193 |
build_module_args( |
| 194 |
state="disabled", |
| 195 |
log_ratelimit={"enabled": False, "burst": 10, "rate": "5/minute"}, |
| 196 |
) |
| 197 |
) |
| 198 |
|
| 199 |
assert result["changed"] is True |
| 200 |
assert result["msg"] == "Cluster firewall options updated" |
| 201 |
assert result["enabled"] is False |
| 202 |
assert result["log_ratelimit"] == {"enabled": False, "burst": 10, "rate": "5/minute"} |
| 203 |
assert self.mock_api_fw_options.put.called |
| 204 |
|
| 205 |
# -- API error |
| 206 |
|
| 207 |
def test_get_options_api_failure(self): |
| 208 |
self.mock_api_fw_options.get.side_effect = Exception() |
| 209 |
|
| 210 |
result = self._run_module(build_module_args(state="enabled")) |
| 211 |
|
| 212 |
assert result["failed"] is True |
| 213 |
assert "Failed to retrieve cluster firewall options" in result["msg"] |
| 214 |
|
| 215 |
def test_put_options_api_failure(self): |
| 216 |
self.mock_api_fw_options.get.return_value = PROXMOX_OPTIONS_DISABLED |
| 217 |
self.mock_api_fw_options.put.side_effect = Exception() |
| 218 |
result = self._run_module(build_module_args(state="enabled")) |
| 219 |
assert result["failed"] is True |
| 220 |
assert "Failed to set cluster firewall options" in result["msg"] |
| 221 |
|
| 222 |
self.mock_api_fw_options.get.return_value = PROXMOX_OPTIONS_ENABLED |
| 223 |
self.mock_api_fw_options.put.side_effect = Exception() |
| 224 |
result = self._run_module(build_module_args(state="disabled")) |
| 225 |
assert result["failed"] is True |
| 226 |
assert "Failed to set cluster firewall options" in result["msg"] |
| 227 |
|
| 228 |
# -- state=enabled |
| 229 |
|
| 230 |
def test_enabled_idempotent(self): |
| 231 |
self.mock_api_fw_options.get.return_value = PROXMOX_OPTIONS_ENABLED |
| 232 |
|
| 233 |
result = self._run_module( |
| 234 |
build_module_args( |
| 235 |
state="enabled", |
| 236 |
ebtables=True, |
| 237 |
input_policy="DROP", |
| 238 |
output_policy="ACCEPT", |
| 239 |
forward_policy="ACCEPT", |
| 240 |
log_ratelimit={"enabled": True, "burst": 5, "rate": "1/second"}, |
| 241 |
) |
| 242 |
) |
| 243 |
|
| 244 |
assert result["changed"] is False |
| 245 |
assert result["msg"] == "Cluster firewall options already match desired state" |
| 246 |
assert ANSIBLE_OPTIONS_ENABLED.items() <= result.items() |
| 247 |
assert not self.mock_api_fw_options.put.called |
| 248 |
|
| 249 |
def test_enabled_update(self): |
| 250 |
self.mock_api_fw_options.get.side_effect = [ |
| 251 |
# First call, differs from desired |
| 252 |
{ |
| 253 |
"enable": 0, |
| 254 |
"ebtables": 0, |
| 255 |
"policy_in": "ACCEPT", |
| 256 |
"policy_out": "ACCEPT", |
| 257 |
"policy_forward": "DROP", |
| 258 |
"log_ratelimit": "enable=0,burst=5,rate=1/second", |
| 259 |
}, |
| 260 |
# Re-call after PUT |
| 261 |
PROXMOX_OPTIONS_ENABLED, |
| 262 |
] |
| 263 |
|
| 264 |
result = self._run_module( |
| 265 |
build_module_args( |
| 266 |
state="enabled", |
| 267 |
ebtables=True, |
| 268 |
input_policy="DROP", |
| 269 |
output_policy="ACCEPT", |
| 270 |
forward_policy="ACCEPT", |
| 271 |
log_ratelimit={"enabled": True, "burst": 5, "rate": "1/second"}, |
| 272 |
) |
| 273 |
) |
| 274 |
|
| 275 |
assert result["changed"] is True |
| 276 |
assert result["msg"] == "Cluster firewall options updated" |
| 277 |
assert ANSIBLE_OPTIONS_ENABLED.items() <= result.items() |
| 278 |
assert self.mock_api_fw_options.put.called |
| 279 |
|
| 280 |
def test_enabled_update_check_mode(self): |
| 281 |
self.mock_api_fw_options.get.return_value = { |
| 282 |
"enable": 0, |
| 283 |
"ebtables": 1, |
| 284 |
"policy_in": "ACCEPT", |
| 285 |
"policy_out": "ACCEPT", |
| 286 |
"policy_forward": "DROP", |
| 287 |
"log_ratelimit": "enable=0,burst=5,rate=1/second", |
| 288 |
} |
| 289 |
|
| 290 |
result = self._run_module( |
| 291 |
{ |
| 292 |
**build_module_args( |
| 293 |
state="enabled", |
| 294 |
ebtables=True, |
| 295 |
input_policy="DROP", |
| 296 |
output_policy="ACCEPT", |
| 297 |
forward_policy="ACCEPT", |
| 298 |
log_ratelimit={"enabled": True, "burst": 5, "rate": "1/second"}, |
| 299 |
), |
| 300 |
"_ansible_check_mode": True, |
| 301 |
} |
| 302 |
) |
| 303 |
|
| 304 |
assert result["changed"] is True |
| 305 |
assert result["msg"] == "Cluster firewall options would be updated" |
| 306 |
assert ANSIBLE_OPTIONS_ENABLED.items() <= result.items() |
| 307 |
assert not self.mock_api_fw_options.put.called |
| 308 |
|
| 309 |
def test_enabled_idempotent_without_log_ratelimit(self): |
| 310 |
self.mock_api_fw_options.get.return_value = { |
| 311 |
"enable": 1, |
| 312 |
"ebtables": 1, |
| 313 |
"policy_in": "DROP", |
| 314 |
"policy_out": "ACCEPT", |
| 315 |
"policy_forward": "ACCEPT", |
| 316 |
# no log_ratelimit key |
| 317 |
} |
| 318 |
|
| 319 |
result = self._run_module( |
| 320 |
build_module_args( |
| 321 |
state="enabled", |
| 322 |
ebtables=True, |
| 323 |
input_policy="DROP", |
| 324 |
output_policy="ACCEPT", |
| 325 |
forward_policy="ACCEPT", |
| 326 |
) |
| 327 |
) |
| 328 |
|
| 329 |
assert result["changed"] is False |
| 330 |
assert "log_ratelimit" not in result |
| 331 |
assert not self.mock_api_fw_options.put.called |
| 332 |
|
| 333 |
# -- state=disabled |
| 334 |
|
| 335 |
def test_disabled_idempotent(self): |
| 336 |
self.mock_api_fw_options.get.return_value = { |
| 337 |
"enable": 0, |
| 338 |
"ebtables": 0, |
| 339 |
"policy_in": "ACCEPT", |
| 340 |
"policy_out": "REJECT", |
| 341 |
"policy_forward": "DROP", |
| 342 |
"log_ratelimit": "enable=0,burst=10,rate=5/minute", |
| 343 |
} |
| 344 |
|
| 345 |
result = self._run_module( |
| 346 |
build_module_args( |
| 347 |
state="disabled", |
| 348 |
ebtables=False, |
| 349 |
input_policy="ACCEPT", |
| 350 |
output_policy="REJECT", |
| 351 |
forward_policy="DROP", |
| 352 |
log_ratelimit={"enabled": False, "burst": 10, "rate": "5/minute"}, |
| 353 |
) |
| 354 |
) |
| 355 |
|
| 356 |
assert result["changed"] is False |
| 357 |
assert result["msg"] == "Cluster firewall options already match desired state" |
| 358 |
assert result["enabled"] is False |
| 359 |
assert result["log_ratelimit"] == {"enabled": False, "burst": 10, "rate": "5/minute"} |
| 360 |
assert not self.mock_api_fw_options.put.called |
| 361 |
|
| 362 |
def test_disabled_update_when_previously_enabled(self): |
| 363 |
self._run_disabled_update(PROXMOX_OPTIONS_ENABLED) |
| 364 |
|
| 365 |
def test_disabled_update_when_already_disabled_with_different_ratelimit(self): |
| 366 |
self._run_disabled_update(PROXMOX_OPTIONS_DISABLED) |
| 367 |
|
| 368 |
def test_disabled_update_check_mode(self): |
| 369 |
self.mock_api_fw_options.get.return_value = PROXMOX_OPTIONS_ENABLED |
| 370 |
|
| 371 |
result = self._run_module(self._check_mode(state="disabled")) |
| 372 |
|
| 373 |
assert result["changed"] is True |
| 374 |
assert result["msg"] == "Cluster firewall options would be updated" |
| 375 |
assert not self.mock_api_fw_options.put.called |