Debian Codegraph @

ansible_collections/community/proxmox/tests/unit/plugins/modules/test_proxmox_node_firewall.py

raw
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 from unittest.mock import patch
7
8 import pytest
9
10 proxmoxer = pytest.importorskip("proxmoxer")
11
12 from ansible.module_utils import basic
13 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
14 ModuleTestCase,
15 set_module_args,
16 )
17
18 from ansible_collections.community.proxmox.plugins.modules import proxmox_node_firewall
19
20
21 def exit_json(*args, **kwargs):
22 kwargs.setdefault("changed", False)
23 raise SystemExit(kwargs)
24
25
26 def fail_json(*args, **kwargs):
27 kwargs["failed"] = True
28 raise SystemExit(kwargs)
29
30
31 def build_module_args(state="enabled", **overrides):
32 return {
33 "api_host": "host",
34 "api_user": "user",
35 "api_password": "password",
36 "state": state,
37 "node_name": "pve-001",
38 **overrides,
39 }
40
41
42 SAMPLE_API_NODE_FIREWALL_OPTIONS = {
43 "enable": 1,
44 "log_level_in": "nolog",
45 "log_level_out": "nolog",
46 "log_level_forward": "nolog",
47 "ndp": 1,
48 "nftables": 0,
49 "nosmurfs": 1,
50 "smurf_log_level": "nolog",
51 "tcp_flags_log_level": "nolog",
52 "tcpflags": 0,
53 "nf_conntrack_allow_invalid": 0,
54 "nf_conntrack_helpers": None,
55 "nf_conntrack_max": 262144,
56 "nf_conntrack_tcp_timeout_established": 432000,
57 "nf_conntrack_tcp_timeout_syn_recv": 60,
58 "protection_synflood": 0,
59 "protection_synflood_burst": 1000,
60 "protection_synflood_rate": 200,
61 }
62
63
64 class TestProxmoxNodeFirewallModule(ModuleTestCase):
65 def setUp(self):
66 super().setUp()
67 self.module = proxmox_node_firewall
68
69 self.mock_module_helper = patch.multiple(
70 basic.AnsibleModule,
71 exit_json=exit_json,
72 fail_json=fail_json,
73 )
74 self.mock_module_helper.start()
75
76 self.connect_mock = patch(
77 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
78 ).start()
79
80 mock_api = self.connect_mock.return_value
81 self.node_mock = mock_api.nodes.return_value
82 mock_api.nodes.side_effect = lambda n: self.node_mock
83
84 self.mock_api_fw_options = self.node_mock.firewall.return_value.options
85
86 def tearDown(self):
87 self.connect_mock.stop()
88 self.mock_module_helper.stop()
89 super().tearDown()
90
91 def _run_module(self, args):
92 with pytest.raises(SystemExit) as exc_info, set_module_args(args):
93 self.module.main()
94 return exc_info.value.args[0]
95
96 def _check_mode(self, **kwargs):
97 return {**build_module_args(**kwargs), "_ansible_check_mode": True}
98
99 def test_validate_params_failure(self):
100 cases = [
101 ({"nf_conntrack_max": 1}, "nf_conntrack_max must be greater than 32768"),
102 ({"nf_conntrack_tcp_timeout_syn_recv": 1}, "nf_conntrack_tcp_timeout_syn_recv must be between 30 and 60"),
103 ({"nf_conntrack_tcp_timeout_syn_recv": 61}, "nf_conntrack_tcp_timeout_syn_recv must be between 30 and 60"),
104 ({"protection_synflood_burst": 999}, "protection_synflood_burst must be greater than 1000"),
105 ({"protection_synflood_rate": 199}, "protection_synflood_rate must be greater than 200"),
106 ]
107 for overrides, expected_substring in cases:
108 result = self._run_module(build_module_args(**overrides))
109 assert result["failed"] is True
110 assert expected_substring in result["msg"]
111
112 def test_idempotent_when_options_match(self):
113 self.mock_api_fw_options.get.return_value = SAMPLE_API_NODE_FIREWALL_OPTIONS
114
115 result = self._run_module(build_module_args(state="enabled"))
116
117 assert result["changed"] is False
118 assert result["msg"] == "Node firewall options already match desired state"
119 assert result["node_name"] == "pve-001"
120 assert result["enabled"] is True
121 assert result["ndp"] is True
122 assert result["nftables"] is False
123 assert not self.mock_api_fw_options.put.called
124
125 def test_update_check_mode(self):
126 self.mock_api_fw_options.get.return_value = {**SAMPLE_API_NODE_FIREWALL_OPTIONS, "enable": 0}
127
128 result = self._run_module(self._check_mode(state="enabled"))
129
130 assert result["changed"] is True
131 assert result["msg"] == "Node firewall options would be updated"
132 assert result["enabled"] is True
133 assert not self.mock_api_fw_options.put.called
134
135 def test_update_success_sends_expected_payload(self):
136 self.mock_api_fw_options.get.side_effect = [
137 {**SAMPLE_API_NODE_FIREWALL_OPTIONS, "enable": 0, "nftables": 0},
138 {**SAMPLE_API_NODE_FIREWALL_OPTIONS, "enable": 1, "nftables": 1},
139 ]
140
141 result = self._run_module(build_module_args(state="enabled", nftables=True))
142
143 assert result["changed"] is True
144 assert result["msg"] == "Node firewall options updated"
145 assert result["enabled"] is True
146 assert result["nftables"] is True
147
148 assert self.mock_api_fw_options.put.called
149 payload = self.mock_api_fw_options.put.call_args[1]
150 assert payload["enable"] == 1
151 assert payload["nftables"] == 1
152
153 def test_disable_firewall_updates_enable_flag(self):
154 self.mock_api_fw_options.get.side_effect = [
155 SAMPLE_API_NODE_FIREWALL_OPTIONS,
156 {**SAMPLE_API_NODE_FIREWALL_OPTIONS, "enable": 0},
157 ]
158
159 result = self._run_module(build_module_args(state="disabled"))
160
161 assert result["changed"] is True
162 assert result["msg"] == "Node firewall options updated"
163 assert result["enabled"] is False
164
165 payload = self.mock_api_fw_options.put.call_args[1]
166 assert payload["enable"] == 0