| 1 |
# Copyright (c) 2025, Florian Paul Azim Hoberg (@gyptazy) <[email protected]> |
| 2 |
# |
| 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 MagicMock, Mock, patch |
| 7 |
|
| 8 |
import pytest |
| 9 |
from ansible.module_utils.basic import AnsibleModule |
| 10 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 11 |
ModuleTestCase, |
| 12 |
set_module_args, |
| 13 |
) |
| 14 |
|
| 15 |
import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils |
| 16 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ProxmoxAnsible |
| 17 |
from ansible_collections.community.proxmox.plugins.modules import proxmox_cluster |
| 18 |
from ansible_collections.community.proxmox.plugins.modules.proxmox_cluster import validate_cluster_name |
| 19 |
|
| 20 |
proxmoxer = pytest.importorskip("proxmoxer") |
| 21 |
|
| 22 |
SINGLE_NODE = [ |
| 23 |
{ |
| 24 |
"level": "", |
| 25 |
"online": 1, |
| 26 |
"nodeid": 0, |
| 27 |
"ip": "192.168.1.2", |
| 28 |
"name": "pve", |
| 29 |
"id": "node/pve", |
| 30 |
"type": "node", |
| 31 |
"local": 1, |
| 32 |
} |
| 33 |
] |
| 34 |
|
| 35 |
CLUSTER = [ |
| 36 |
{"nodes": 3, "id": "cluster", "version": 3, "quorate": 1, "name": "devcluster", "type": "cluster"}, |
| 37 |
{ |
| 38 |
"id": "node/srv-proxmox-03", |
| 39 |
"online": 1, |
| 40 |
"ip": "192.168.1.23", |
| 41 |
"name": "srv-proxmox-03", |
| 42 |
"level": "", |
| 43 |
"type": "node", |
| 44 |
"local": 1, |
| 45 |
"nodeid": 3, |
| 46 |
}, |
| 47 |
{ |
| 48 |
"nodeid": 1, |
| 49 |
"type": "node", |
| 50 |
"local": 0, |
| 51 |
"level": "", |
| 52 |
"name": "srv-proxmox-01", |
| 53 |
"ip": "192.168.1.21", |
| 54 |
"online": 1, |
| 55 |
"id": "node/srv-proxmox-01", |
| 56 |
}, |
| 57 |
{ |
| 58 |
"nodeid": 2, |
| 59 |
"type": "node", |
| 60 |
"local": 0, |
| 61 |
"name": "srv-proxmox-02", |
| 62 |
"level": "", |
| 63 |
"online": 1, |
| 64 |
"ip": "192.168.1.22", |
| 65 |
"id": "node/srv-proxmox-02", |
| 66 |
}, |
| 67 |
] |
| 68 |
|
| 69 |
|
| 70 |
def exit_json(*args, **kwargs): |
| 71 |
"""function to patch over exit_json; |
| 72 |
package return data into an exception""" |
| 73 |
if "changed" not in kwargs: |
| 74 |
kwargs["changed"] = False |
| 75 |
raise SystemExit(kwargs) |
| 76 |
|
| 77 |
|
| 78 |
def fail_json(*args, **kwargs): |
| 79 |
"""function to patch over fail_json; |
| 80 |
package return data into an exception""" |
| 81 |
kwargs["failed"] = True |
| 82 |
raise SystemExit(kwargs) |
| 83 |
|
| 84 |
|
| 85 |
class TestProxmoxCluster(ModuleTestCase): |
| 86 |
def setUp(self): |
| 87 |
super().setUp() |
| 88 |
proxmox_utils.HAS_PROXMOXER = True |
| 89 |
self.module = proxmox_cluster |
| 90 |
|
| 91 |
self.fail_json_patcher = patch( |
| 92 |
"ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json) |
| 93 |
) |
| 94 |
self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json) |
| 95 |
|
| 96 |
self.fail_json_mock = self.fail_json_patcher.start() |
| 97 |
self.exit_json_patcher.start() |
| 98 |
|
| 99 |
self.connect_mock = patch( |
| 100 |
"ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect", |
| 101 |
).start() |
| 102 |
|
| 103 |
def tearDown(self): |
| 104 |
self.connect_mock.stop() |
| 105 |
self.exit_json_patcher.stop() |
| 106 |
self.fail_json_patcher.stop() |
| 107 |
super().tearDown() |
| 108 |
|
| 109 |
def test_create_check_mode(self): |
| 110 |
mock_obj = self.connect_mock.return_value |
| 111 |
mock_obj.cluster.status.get.return_value = SINGLE_NODE |
| 112 |
with set_module_args( |
| 113 |
{ |
| 114 |
"api_user": "root@pam", |
| 115 |
"api_password": "secret", |
| 116 |
"api_host": "192.168.1.21", |
| 117 |
"link0": "192.192.168.21", |
| 118 |
"link1": "10.10.2.1", |
| 119 |
"cluster_name": "devcluster", |
| 120 |
"state": "present", |
| 121 |
"_ansible_check_mode": True, |
| 122 |
} |
| 123 |
), pytest.raises(SystemExit) as exc_info: |
| 124 |
proxmox_cluster.main() |
| 125 |
|
| 126 |
result = exc_info.value.args[0] |
| 127 |
|
| 128 |
assert result["changed"] is True |
| 129 |
assert result["msg"] == "Cluster 'devcluster' would be created." |
| 130 |
assert result["cluster"] == "devcluster" |
| 131 |
|
| 132 |
def test_create(self): |
| 133 |
mock_obj = self.connect_mock.return_value |
| 134 |
mock_obj.cluster.status.get.return_value = SINGLE_NODE |
| 135 |
with set_module_args( |
| 136 |
{ |
| 137 |
"api_user": "root@pam", |
| 138 |
"api_password": "secret", |
| 139 |
"api_host": "192.168.1.21", |
| 140 |
"link0": "192.192.168.21", |
| 141 |
"link1": "10.10.2.1", |
| 142 |
"cluster_name": "devcluster", |
| 143 |
"state": "present", |
| 144 |
} |
| 145 |
), pytest.raises(SystemExit) as exc_info: |
| 146 |
proxmox_cluster.main() |
| 147 |
|
| 148 |
result = exc_info.value.args[0] |
| 149 |
|
| 150 |
assert result["changed"] is True |
| 151 |
assert result["msg"] == "Cluster 'devcluster' created." |
| 152 |
assert result["cluster"] == "devcluster" |
| 153 |
|
| 154 |
def test_create_idempotent(self): |
| 155 |
mock_obj = self.connect_mock.return_value |
| 156 |
mock_obj.cluster.status.get.return_value = CLUSTER |
| 157 |
with set_module_args( |
| 158 |
{ |
| 159 |
"api_user": "root@pam", |
| 160 |
"api_password": "secret", |
| 161 |
"api_host": "192.168.1.21", |
| 162 |
"link0": "192.192.168.21", |
| 163 |
"link1": "10.10.2.1", |
| 164 |
"cluster_name": "devcluster", |
| 165 |
"state": "present", |
| 166 |
} |
| 167 |
), pytest.raises(SystemExit) as exc_info: |
| 168 |
proxmox_cluster.main() |
| 169 |
|
| 170 |
result = exc_info.value.args[0] |
| 171 |
|
| 172 |
assert result["changed"] is False |
| 173 |
assert result["msg"] == "Cluster 'devcluster' already present." |
| 174 |
assert result["cluster"] == "devcluster" |
| 175 |
|
| 176 |
def test_create_fail(self): |
| 177 |
mock_obj = self.connect_mock.return_value |
| 178 |
mock_obj.cluster.status.get.return_value = CLUSTER |
| 179 |
with set_module_args( |
| 180 |
{ |
| 181 |
"api_user": "root@pam", |
| 182 |
"api_password": "secret", |
| 183 |
"api_host": "192.168.1.21", |
| 184 |
"link0": "192.192.168.21", |
| 185 |
"link1": "10.10.2.1", |
| 186 |
"cluster_name": "devcluster2", |
| 187 |
"state": "present", |
| 188 |
} |
| 189 |
), pytest.raises(SystemExit) as exc_info: |
| 190 |
proxmox_cluster.main() |
| 191 |
|
| 192 |
result = exc_info.value.args[0] |
| 193 |
|
| 194 |
assert result["failed"] is True |
| 195 |
assert result["msg"] == 'Error creating cluster: Node is already part of a different cluster - "devcluster".' |
| 196 |
|
| 197 |
def test_join_check_mode(self): |
| 198 |
mock_obj = self.connect_mock.return_value |
| 199 |
mock_obj.cluster.status.get.return_value = SINGLE_NODE |
| 200 |
with set_module_args( |
| 201 |
{ |
| 202 |
"api_user": "root@pam", |
| 203 |
"api_password": "secret", |
| 204 |
"api_host": "192.168.1.22", |
| 205 |
"master_api_password": "secret", |
| 206 |
"master_ip": "192.168.1.21", |
| 207 |
"fingerprint": "BD:D0:A4:04:E6:05:30:74:30:E6:5A:83:78:A8:8F:F7:4C:25:71:DB:07:92:7C:A1:04:B9:CB:12:BB:3C:BE:4D", |
| 208 |
"state": "present", |
| 209 |
"_ansible_check_mode": True, |
| 210 |
} |
| 211 |
), pytest.raises(SystemExit) as exc_info: |
| 212 |
proxmox_cluster.main() |
| 213 |
|
| 214 |
result = exc_info.value.args[0] |
| 215 |
|
| 216 |
assert result["changed"] is True |
| 217 |
assert result["msg"] == "Node would join the cluster." |
| 218 |
|
| 219 |
def test_join(self): |
| 220 |
mock_obj = self.connect_mock.return_value |
| 221 |
mock_obj.cluster.status.get.return_value = SINGLE_NODE |
| 222 |
with set_module_args( |
| 223 |
{ |
| 224 |
"api_user": "root@pam", |
| 225 |
"api_password": "secret", |
| 226 |
"api_host": "192.168.1.22", |
| 227 |
"master_api_password": "secret", |
| 228 |
"master_ip": "192.168.1.21", |
| 229 |
"fingerprint": "BD:D0:A4:04:E6:05:30:74:30:E6:5A:83:78:A8:8F:F7:4C:25:71:DB:07:92:7C:A1:04:B9:CB:12:BB:3C:BE:4D", |
| 230 |
"state": "present", |
| 231 |
} |
| 232 |
), pytest.raises(SystemExit) as exc_info: |
| 233 |
proxmox_cluster.main() |
| 234 |
|
| 235 |
result = exc_info.value.args[0] |
| 236 |
|
| 237 |
assert result["changed"] is True |
| 238 |
assert result["msg"] == "Node joined the cluster." |
| 239 |
|
| 240 |
def test_join_idempotent(self): |
| 241 |
mock_obj = self.connect_mock.return_value |
| 242 |
mock_obj.cluster.status.get.return_value = CLUSTER |
| 243 |
with set_module_args( |
| 244 |
{ |
| 245 |
"api_user": "root@pam", |
| 246 |
"api_password": "secret", |
| 247 |
"api_host": "192.168.1.22", |
| 248 |
"master_api_password": "secret", |
| 249 |
"master_ip": "192.168.1.21", |
| 250 |
"fingerprint": "BD:D0:A4:04:E6:05:30:74:30:E6:5A:83:78:A8:8F:F7:4C:25:71:DB:07:92:7C:A1:04:B9:CB:12:BB:3C:BE:4D", |
| 251 |
"state": "present", |
| 252 |
} |
| 253 |
), pytest.raises(SystemExit) as exc_info: |
| 254 |
proxmox_cluster.main() |
| 255 |
|
| 256 |
result = exc_info.value.args[0] |
| 257 |
|
| 258 |
assert result["changed"] is False |
| 259 |
assert result["msg"] == "Node already in the cluster." |
| 260 |
|
| 261 |
def test_join_failed(self): |
| 262 |
mock_obj = self.connect_mock.return_value |
| 263 |
mock_obj.cluster.status.get.return_value = CLUSTER |
| 264 |
with set_module_args( |
| 265 |
{ |
| 266 |
"api_user": "root@pam", |
| 267 |
"api_password": "secret", |
| 268 |
"api_host": "192.168.1.22", |
| 269 |
"master_api_password": "secret", |
| 270 |
"master_ip": "192.168.1.10", |
| 271 |
"fingerprint": "BD:D0:A4:04:E6:05:30:74:30:E6:5A:83:78:A8:8F:F7:4C:25:71:DB:07:92:7C:A1:04:B9:CB:12:BB:3C:BE:4D", |
| 272 |
"state": "present", |
| 273 |
} |
| 274 |
), pytest.raises(SystemExit) as exc_info: |
| 275 |
proxmox_cluster.main() |
| 276 |
|
| 277 |
result = exc_info.value.args[0] |
| 278 |
|
| 279 |
assert result["failed"] is True |
| 280 |
assert result["msg"] == "Error while joining cluster: Node is already part of a cluster." |
| 281 |
|
| 282 |
|
| 283 |
@pytest.fixture |
| 284 |
def module_args_join(): |
| 285 |
return { |
| 286 |
"api_host": "10.10.10.76", |
| 287 |
"api_user": "root@pam", |
| 288 |
"api_password": "secret", |
| 289 |
"state": "present", |
| 290 |
"master_ip": "10.10.10.75", |
| 291 |
"fingerprint": "BD:D0:A4:04:E6:05:30:74:30:E6:5A:83:78:A8:8F:F7:4C:25:71:DB:07:92:7C:A1:04:B9:CB:12:BB:3C:BE:4D", |
| 292 |
"cluster_name": "devcluster", |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
@pytest.fixture |
| 297 |
def module_args_create(): |
| 298 |
return { |
| 299 |
"api_host": "10.10.10.76", |
| 300 |
"api_user": "root@pam", |
| 301 |
"api_password": "secret", |
| 302 |
"state": "present", |
| 303 |
"cluster_name": "devcluster", |
| 304 |
"link0": "10.10.1.1", |
| 305 |
"link1": "10.10.2.1", |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
@patch.object(ProxmoxAnsible, "__init__", return_value=None) |
| 310 |
@patch.object(ProxmoxAnsible, "proxmox_api", create=True) |
| 311 |
def test_cluster_join(mock_api, mock_init, module_args_join): |
| 312 |
module = MagicMock(spec=AnsibleModule) |
| 313 |
module.params = module_args_join |
| 314 |
module.check_mode = False |
| 315 |
|
| 316 |
mock_api_instance = MagicMock() |
| 317 |
mock_api.return_value = mock_api_instance |
| 318 |
mock_api_instance.cluster.config.join.post.return_value = {} |
| 319 |
|
| 320 |
module.exit_json = lambda **kwargs: (x for x in ()).throw(SystemExit(kwargs)) |
| 321 |
module.fail_json = lambda **kwargs: (x for x in ()).throw(SystemExit(kwargs)) |
| 322 |
|
| 323 |
proxmox = proxmox_cluster.ProxmoxClusterAnsible(module) |
| 324 |
proxmox.module = module |
| 325 |
proxmox.proxmox_api = mock_api_instance |
| 326 |
|
| 327 |
with pytest.raises(SystemExit) as exc: |
| 328 |
proxmox.cluster_join() |
| 329 |
|
| 330 |
result = exc.value.args[0] |
| 331 |
assert result["changed"] is True |
| 332 |
assert result["msg"] == "Node joined the cluster." |
| 333 |
|
| 334 |
mock_api_instance.cluster.config.join.post.assert_called_once_with( |
| 335 |
hostname="10.10.10.75", fingerprint=module_args_join["fingerprint"], password="secret" |
| 336 |
) |
| 337 |
|
| 338 |
|
| 339 |
@patch.object(ProxmoxAnsible, "__init__", return_value=None) |
| 340 |
@patch.object(ProxmoxAnsible, "proxmox_api", create=True) |
| 341 |
def test_cluster_create(mock_api, mock_init, module_args_create): |
| 342 |
module = MagicMock(spec=AnsibleModule) |
| 343 |
module.params = module_args_create |
| 344 |
module.check_mode = False |
| 345 |
|
| 346 |
mock_api_instance = MagicMock() |
| 347 |
mock_api.return_value = mock_api_instance |
| 348 |
mock_api_instance.cluster.config.nodes.get.return_value = [] |
| 349 |
mock_api_instance.cluster.config.post.return_value = {} |
| 350 |
|
| 351 |
module.exit_json = lambda **kwargs: (x for x in ()).throw(SystemExit(kwargs)) |
| 352 |
module.fail_json = lambda **kwargs: (x for x in ()).throw(SystemExit(kwargs)) |
| 353 |
|
| 354 |
proxmox = proxmox_cluster.ProxmoxClusterAnsible(module) |
| 355 |
proxmox.module = module |
| 356 |
proxmox.proxmox_api = mock_api_instance |
| 357 |
|
| 358 |
with pytest.raises(SystemExit) as exc: |
| 359 |
proxmox.cluster_create() |
| 360 |
|
| 361 |
result = exc.value.args[0] |
| 362 |
assert result["changed"] is True |
| 363 |
assert result["msg"] == "Cluster 'devcluster' created." |
| 364 |
assert result["cluster"] == "devcluster" |
| 365 |
|
| 366 |
expected_payload = { |
| 367 |
"clustername": module_args_create["cluster_name"], |
| 368 |
"link0": module_args_create["link0"], |
| 369 |
"link1": module_args_create["link1"], |
| 370 |
} |
| 371 |
|
| 372 |
mock_api_instance.cluster.config.post.assert_called_once_with(**expected_payload) |
| 373 |
|
| 374 |
|
| 375 |
def test_validate_cluster_name_valid(module_args_create): |
| 376 |
module = MagicMock(spec=AnsibleModule) |
| 377 |
module.params = module_args_create |
| 378 |
|
| 379 |
validate_cluster_name(module) |