| 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 Mock, 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.module_utils.proxmox_acme_certificate import ( |
| 20 |
cert_info_to_ansible_result, |
| 21 |
parse_acme_config, |
| 22 |
) |
| 23 |
from ansible_collections.community.proxmox.plugins.modules import proxmox_acme_certificate |
| 24 |
from ansible_collections.community.proxmox.plugins.modules.proxmox_acme_certificate import ( |
| 25 |
build_acmedomain_property_string, |
| 26 |
find_acme_certificate, |
| 27 |
normalize_domain_list, |
| 28 |
) |
| 29 |
|
| 30 |
NODE_NAME = "pve-001" |
| 31 |
UPID_ORDER = "UPID:pve-001:00180447:09B4EA04:69E1C426:acmenewcert::root@pam:" |
| 32 |
UPID_REVOKE = "UPID:pve-001:0014714A:0979C6AF:69E12CC2:acmerevoke::root@pam:" |
| 33 |
UPID_UNKNOWN = "UPID:unknown:unknown:unknown:unknown:unknown::root@pam:" |
| 34 |
|
| 35 |
NODE_CONFIG_WITH_ACME = { |
| 36 |
"acme": "account=production", |
| 37 |
"acmedomain0": "domain=pve.example.com,plugin=cloudflare", |
| 38 |
} |
| 39 |
|
| 40 |
NODE_CONFIG_EMPTY = {} |
| 41 |
|
| 42 |
SAMPLE_CERT_ACME = { |
| 43 |
"filename": "pveproxy-ssl.pem", |
| 44 |
"fingerprint": "12:34:56", |
| 45 |
"issuer": "...", |
| 46 |
"notafter": 1837175623, |
| 47 |
"notbefore": 1774103623, |
| 48 |
"pem": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----", |
| 49 |
"san": [], |
| 50 |
"subject": "CN = pve.example.com", |
| 51 |
} |
| 52 |
|
| 53 |
SAMPLE_CERT_PROXMOX = { |
| 54 |
"filename": "pve-ssl.pem", |
| 55 |
"fingerprint": "11:22:33", |
| 56 |
"issuer": "...", |
| 57 |
"pem": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----", |
| 58 |
"san": ["127.0.0.1"], |
| 59 |
"subject": "/OU=PVE Cluster Node/O=Proxmox Virtual Environment/CN=pve-001", |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
def exit_json(*args, **kwargs): |
| 64 |
kwargs.setdefault("changed", False) |
| 65 |
raise SystemExit(kwargs) |
| 66 |
|
| 67 |
|
| 68 |
def fail_json(*args, **kwargs): |
| 69 |
kwargs["failed"] = True |
| 70 |
raise SystemExit(kwargs) |
| 71 |
|
| 72 |
|
| 73 |
def build_module_args(state="present", **overrides): |
| 74 |
args = { |
| 75 |
"api_host": "host", |
| 76 |
"api_user": "user", |
| 77 |
"api_password": "password", |
| 78 |
"node_name": NODE_NAME, |
| 79 |
"state": state, |
| 80 |
} |
| 81 |
if state == "present": |
| 82 |
args.setdefault("account", "production") |
| 83 |
args.setdefault("domains", [{"domain": "pve.example.com", "plugin": "cloudflare"}]) |
| 84 |
args.update(overrides) |
| 85 |
return args |
| 86 |
|
| 87 |
|
| 88 |
class TestHelpers: |
| 89 |
def test_build_acmedomain_property_string_full(self): |
| 90 |
result = build_acmedomain_property_string("pve.example.com", plugin="cloudflare", alias="alt.example.com") |
| 91 |
assert result == "domain=pve.example.com,plugin=cloudflare,alias=alt.example.com" |
| 92 |
|
| 93 |
def test_build_acmedomain_property_string_domain_only(self): |
| 94 |
result = build_acmedomain_property_string("pve.example.com") |
| 95 |
assert result == "domain=pve.example.com" |
| 96 |
|
| 97 |
def test_parse_acme_config_with_domains(self): |
| 98 |
result = parse_acme_config(NODE_CONFIG_WITH_ACME) |
| 99 |
assert result["account"] == "production" |
| 100 |
assert len(result["domains"]) == 1 |
| 101 |
assert result["domains"][0]["domain"] == "pve.example.com" |
| 102 |
assert result["domains"][0]["plugin"] == "cloudflare" |
| 103 |
assert result["domains"][0]["alias"] is None |
| 104 |
|
| 105 |
def test_parse_acme_config_empty(self): |
| 106 |
result = parse_acme_config(NODE_CONFIG_EMPTY) |
| 107 |
assert result == {"account": None, "domains": []} |
| 108 |
|
| 109 |
def test_parse_acme_config_multiple_domains(self): |
| 110 |
config = { |
| 111 |
"acme": "account=staging", |
| 112 |
"acmedomain0": "domain=a.example.com,plugin=cf", |
| 113 |
"acmedomain1": "domain=b.example.com,plugin=cf,alias=val.example.com", |
| 114 |
} |
| 115 |
result = parse_acme_config(config) |
| 116 |
assert result["account"] == "staging" |
| 117 |
assert len(result["domains"]) == 2 # noqa: PLR2004 |
| 118 |
assert result["domains"][1]["alias"] == "val.example.com" |
| 119 |
|
| 120 |
def test_find_acme_certificate_skips_proxmox(self): |
| 121 |
cert = find_acme_certificate([SAMPLE_CERT_PROXMOX, SAMPLE_CERT_ACME]) |
| 122 |
assert cert is SAMPLE_CERT_ACME |
| 123 |
|
| 124 |
def test_find_acme_certificate_none_when_empty(self): |
| 125 |
assert find_acme_certificate([]) is None |
| 126 |
assert find_acme_certificate(None) is None |
| 127 |
|
| 128 |
def test_find_acme_certificate_none_when_only_proxmox(self): |
| 129 |
assert find_acme_certificate([SAMPLE_CERT_PROXMOX]) is None |
| 130 |
|
| 131 |
def test_normalize_domain_list_sorting(self): |
| 132 |
domains = [ |
| 133 |
{"domain": "b.example.com", "plugin": "cf", "alias": None}, |
| 134 |
{"domain": "a.example.com", "plugin": None, "alias": None}, |
| 135 |
] |
| 136 |
result = normalize_domain_list(domains) |
| 137 |
assert result[0]["domain"] == "a.example.com" |
| 138 |
assert result[1]["domain"] == "b.example.com" |
| 139 |
|
| 140 |
def test_normalize_domain_list_case_insensitive(self): |
| 141 |
domains = [{"domain": "PVE.Example.COM", "plugin": "CF"}] |
| 142 |
result = normalize_domain_list(domains) |
| 143 |
assert result[0]["domain"] == "pve.example.com" |
| 144 |
assert result[0]["plugin"] == "CF" |
| 145 |
|
| 146 |
def test_build_then_parse_roundtrip(self): |
| 147 |
built = build_acmedomain_property_string("pve.example.com", plugin="cf", alias="alt.example.com") |
| 148 |
config = {"acme": "account=test", "acmedomain0": built} |
| 149 |
parsed = parse_acme_config(config) |
| 150 |
assert parsed["account"] == "test" |
| 151 |
assert parsed["domains"][0] == {"domain": "pve.example.com", "plugin": "cf", "alias": "alt.example.com"} |
| 152 |
|
| 153 |
|
| 154 |
class TestProxmoxAcmeCertificateModule(ModuleTestCase): |
| 155 |
def setUp(self): |
| 156 |
super().setUp() |
| 157 |
self.module = proxmox_acme_certificate |
| 158 |
self.warn_mock = Mock() |
| 159 |
self.mock_module_helper = patch.multiple( |
| 160 |
basic.AnsibleModule, |
| 161 |
exit_json=exit_json, |
| 162 |
fail_json=fail_json, |
| 163 |
warn=self.warn_mock, |
| 164 |
) |
| 165 |
self.mock_module_helper.start() |
| 166 |
self.connect_mock = patch( |
| 167 |
"ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect", |
| 168 |
).start() |
| 169 |
mock_api = self.connect_mock.return_value |
| 170 |
node_mock = mock_api.nodes.return_value |
| 171 |
self.node_config = node_mock.config |
| 172 |
self.cert_info = node_mock.certificates.info |
| 173 |
self.cert_acme = node_mock.certificates.acme.certificate |
| 174 |
self.cert_acme.post.return_value = UPID_ORDER |
| 175 |
self.wait_mock = patch.object( |
| 176 |
proxmox_acme_certificate.ProxmoxAcmeCertificateAnsible, |
| 177 |
"_wait_certificate_task", |
| 178 |
).start() |
| 179 |
self.wait_revoke_mock = patch.object( |
| 180 |
proxmox_acme_certificate.ProxmoxAcmeCertificateAnsible, |
| 181 |
"_wait_revocation_task", |
| 182 |
).start() |
| 183 |
|
| 184 |
def tearDown(self): |
| 185 |
self.wait_revoke_mock.stop() |
| 186 |
self.wait_mock.stop() |
| 187 |
self.connect_mock.stop() |
| 188 |
self.mock_module_helper.stop() |
| 189 |
super().tearDown() |
| 190 |
|
| 191 |
def _run_module(self, args): |
| 192 |
with pytest.raises(SystemExit) as exc_info, set_module_args(args): |
| 193 |
self.module.main() |
| 194 |
return exc_info.value.args[0] |
| 195 |
|
| 196 |
def _check_mode_args(self, **kwargs): |
| 197 |
return {**build_module_args(**kwargs), "_ansible_check_mode": True} |
| 198 |
|
| 199 |
# -- state=present |
| 200 |
|
| 201 |
def test_present_creates_certificate(self): |
| 202 |
self.node_config.get.return_value = NODE_CONFIG_EMPTY |
| 203 |
self.cert_info.get.return_value = [SAMPLE_CERT_ACME] |
| 204 |
|
| 205 |
result = self._run_module(build_module_args()) |
| 206 |
|
| 207 |
assert result["changed"] is True |
| 208 |
assert "successfully ordered" in result["msg"] |
| 209 |
assert result["node_name"] == NODE_NAME |
| 210 |
assert result["account"] == "production" |
| 211 |
assert result["fingerprint"] == SAMPLE_CERT_ACME["fingerprint"] |
| 212 |
assert result["issuer"] == SAMPLE_CERT_ACME["issuer"] |
| 213 |
self.node_config.put.assert_called_once() |
| 214 |
put_kwargs = self.node_config.put.call_args[1] |
| 215 |
assert "acme" in put_kwargs |
| 216 |
assert "account=production" in put_kwargs["acme"] |
| 217 |
self.cert_acme.post.assert_called_once() |
| 218 |
|
| 219 |
def test_present_idempotent(self): |
| 220 |
self.node_config.get.return_value = NODE_CONFIG_WITH_ACME |
| 221 |
self.cert_info.get.return_value = [SAMPLE_CERT_PROXMOX, SAMPLE_CERT_ACME] |
| 222 |
|
| 223 |
result = self._run_module(build_module_args()) |
| 224 |
|
| 225 |
assert result["changed"] is False |
| 226 |
assert "already up to date" in result["msg"] |
| 227 |
assert result["fingerprint"] == SAMPLE_CERT_ACME["fingerprint"] |
| 228 |
assert self.node_config.put.call_count == 0 |
| 229 |
assert self.cert_acme.post.call_count == 0 |
| 230 |
|
| 231 |
def test_present_updates_on_account_change(self): |
| 232 |
self.node_config.get.return_value = { |
| 233 |
"acme": "account=old-account", |
| 234 |
"acmedomain0": "domain=pve.example.com,plugin=cloudflare", |
| 235 |
} |
| 236 |
self.cert_info.get.return_value = [SAMPLE_CERT_ACME] |
| 237 |
|
| 238 |
result = self._run_module(build_module_args()) |
| 239 |
|
| 240 |
assert result["changed"] is True |
| 241 |
assert "successfully ordered" in result["msg"] |
| 242 |
self.node_config.put.assert_called_once() |
| 243 |
self.cert_acme.post.assert_called_once() |
| 244 |
|
| 245 |
def test_present_updates_on_domain_change(self): |
| 246 |
self.node_config.get.return_value = { |
| 247 |
"acme": "account=production", |
| 248 |
"acmedomain0": "domain=old.example.com,plugin=cloudflare", |
| 249 |
} |
| 250 |
self.cert_info.get.return_value = [SAMPLE_CERT_ACME] |
| 251 |
|
| 252 |
result = self._run_module(build_module_args()) |
| 253 |
|
| 254 |
assert result["changed"] is True |
| 255 |
self.node_config.put.assert_called_once() |
| 256 |
self.cert_acme.post.assert_called_once() |
| 257 |
|
| 258 |
def test_present_orders_when_no_cert_exists(self): |
| 259 |
self.node_config.get.return_value = NODE_CONFIG_WITH_ACME |
| 260 |
self.cert_info.get.side_effect = [ |
| 261 |
[SAMPLE_CERT_PROXMOX], # first read: only Proxmox cert |
| 262 |
[SAMPLE_CERT_ACME], # after ordering |
| 263 |
] |
| 264 |
|
| 265 |
result = self._run_module(build_module_args()) |
| 266 |
|
| 267 |
assert result["changed"] is True |
| 268 |
assert "successfully ordered" in result["msg"] |
| 269 |
self.cert_acme.post.assert_called_once() |
| 270 |
|
| 271 |
def test_present_standalone_domain_without_plugin(self): |
| 272 |
self.node_config.get.return_value = NODE_CONFIG_EMPTY |
| 273 |
self.cert_info.get.return_value = [SAMPLE_CERT_ACME] |
| 274 |
|
| 275 |
result = self._run_module( |
| 276 |
build_module_args( |
| 277 |
domains=[{"domain": "pve.example.com"}], |
| 278 |
) |
| 279 |
) |
| 280 |
|
| 281 |
assert result["changed"] is True |
| 282 |
assert "successfully ordered" in result["msg"] |
| 283 |
put_kwargs = self.node_config.put.call_args[1] |
| 284 |
assert "acmedomain0" in put_kwargs |
| 285 |
assert "domain=pve.example.com" in put_kwargs["acmedomain0"] |
| 286 |
self.cert_acme.post.assert_called_once() |
| 287 |
|
| 288 |
def test_present_mixed_standalone_and_dns_domains(self): |
| 289 |
self.node_config.get.return_value = NODE_CONFIG_EMPTY |
| 290 |
self.cert_info.get.return_value = [SAMPLE_CERT_ACME] |
| 291 |
|
| 292 |
result = self._run_module( |
| 293 |
build_module_args( |
| 294 |
domains=[ |
| 295 |
{"domain": "standalone.example.com"}, |
| 296 |
{"domain": "dns.example.com", "plugin": "cloudflare"}, |
| 297 |
], |
| 298 |
) |
| 299 |
) |
| 300 |
|
| 301 |
assert result["changed"] is True |
| 302 |
put_kwargs = self.node_config.put.call_args[1] |
| 303 |
assert "acmedomain0" in put_kwargs |
| 304 |
assert "domain=standalone.example.com" in put_kwargs["acmedomain0"] |
| 305 |
assert "plugin" not in put_kwargs["acmedomain0"] |
| 306 |
assert "acmedomain1" in put_kwargs |
| 307 |
assert "domain=dns.example.com" in put_kwargs["acmedomain1"] |
| 308 |
assert "plugin=cloudflare" in put_kwargs["acmedomain1"] |
| 309 |
|
| 310 |
def test_present_check_mode_create(self): |
| 311 |
self.node_config.get.return_value = NODE_CONFIG_EMPTY |
| 312 |
|
| 313 |
result = self._run_module(self._check_mode_args()) |
| 314 |
|
| 315 |
assert result["changed"] is True |
| 316 |
assert "would be ordered" in result["msg"] |
| 317 |
assert self.node_config.put.call_count == 0 |
| 318 |
assert self.cert_acme.post.call_count == 0 |
| 319 |
|
| 320 |
def test_present_check_mode_idempotent(self): |
| 321 |
self.node_config.get.return_value = NODE_CONFIG_WITH_ACME |
| 322 |
self.cert_info.get.return_value = [SAMPLE_CERT_ACME] |
| 323 |
|
| 324 |
result = self._run_module(self._check_mode_args()) |
| 325 |
|
| 326 |
assert result["changed"] is False |
| 327 |
assert "already up to date" in result["msg"] |
| 328 |
|
| 329 |
def test_present_check_mode_force(self): |
| 330 |
self.node_config.get.return_value = NODE_CONFIG_WITH_ACME |
| 331 |
|
| 332 |
result = self._run_module(self._check_mode_args(force=True)) |
| 333 |
|
| 334 |
assert result["changed"] is True |
| 335 |
assert "would be ordered" in result["msg"] |
| 336 |
assert self.cert_acme.post.call_count == 0 |
| 337 |
|
| 338 |
# -- state=absent |
| 339 |
|
| 340 |
def test_absent_deletes(self): |
| 341 |
self.node_config.get.return_value = NODE_CONFIG_WITH_ACME |
| 342 |
|
| 343 |
result = self._run_module(build_module_args(state="absent")) |
| 344 |
|
| 345 |
assert result["changed"] is True |
| 346 |
assert "successfully removed" in result["msg"] |
| 347 |
self.cert_acme.delete.assert_called_once() |
| 348 |
cleanup_call = self.node_config.put.call_args |
| 349 |
assert "acme" in cleanup_call[1]["delete"] |
| 350 |
assert "acmedomain0" in cleanup_call[1]["delete"] |
| 351 |
|
| 352 |
def test_absent_when_no_config(self): |
| 353 |
self.node_config.get.return_value = NODE_CONFIG_EMPTY |
| 354 |
|
| 355 |
result = self._run_module(build_module_args(state="absent")) |
| 356 |
|
| 357 |
assert result["changed"] is False |
| 358 |
assert "No ACME configuration" in result["msg"] |
| 359 |
assert self.cert_acme.delete.call_count == 0 |
| 360 |
assert self.node_config.put.call_count == 0 |
| 361 |
|
| 362 |
def test_absent_check_mode(self): |
| 363 |
self.node_config.get.return_value = NODE_CONFIG_WITH_ACME |
| 364 |
|
| 365 |
result = self._run_module(self._check_mode_args(state="absent")) |
| 366 |
|
| 367 |
assert result["changed"] is True |
| 368 |
assert "would be removed" in result["msg"] |
| 369 |
assert self.cert_acme.delete.call_count == 0 |
| 370 |
assert self.node_config.put.call_count == 0 |
| 371 |
|
| 372 |
def test_absent_waits_for_revocation_task(self): |
| 373 |
self.node_config.get.return_value = NODE_CONFIG_WITH_ACME |
| 374 |
self.cert_acme.delete.return_value = UPID_REVOKE |
| 375 |
|
| 376 |
result = self._run_module(build_module_args(state="absent")) |
| 377 |
|
| 378 |
assert result["changed"] is True |
| 379 |
self.wait_revoke_mock.assert_called_once_with(NODE_NAME, UPID_REVOKE) |
| 380 |
|
| 381 |
# -- result |
| 382 |
|
| 383 |
def test_cert_info_to_ansible_result(self): |
| 384 |
result = cert_info_to_ansible_result(SAMPLE_CERT_ACME) |
| 385 |
assert result["certificate"] == SAMPLE_CERT_ACME["pem"] |
| 386 |
assert result["fingerprint"] == SAMPLE_CERT_ACME["fingerprint"] |
| 387 |
assert result["issuer"] == SAMPLE_CERT_ACME["issuer"] |
| 388 |
assert result["subject"] == SAMPLE_CERT_ACME["subject"] |
| 389 |
assert result["not_before"] == SAMPLE_CERT_ACME["notbefore"] |
| 390 |
assert result["not_after"] == SAMPLE_CERT_ACME["notafter"] |
| 391 |
assert result["subject_alternative_names"] == SAMPLE_CERT_ACME["san"] |
| 392 |
|
| 393 |
# -- Error handling |
| 394 |
|
| 395 |
def test_config_read_failure(self): |
| 396 |
self.node_config.get.side_effect = Exception() |
| 397 |
|
| 398 |
result = self._run_module(build_module_args()) |
| 399 |
|
| 400 |
assert result.get("failed") is True |
| 401 |
assert "Failed to read node config" in result["msg"] |
| 402 |
|
| 403 |
def test_order_failure(self): |
| 404 |
self.node_config.get.return_value = NODE_CONFIG_EMPTY |
| 405 |
self.cert_acme.post.side_effect = Exception() |
| 406 |
|
| 407 |
result = self._run_module(build_module_args()) |
| 408 |
|
| 409 |
assert result.get("failed") is True |
| 410 |
assert "Failed to order" in result["msg"] |
| 411 |
|
| 412 |
def test_order_failure_custom_cert_exists(self): |
| 413 |
self.node_config.get.return_value = NODE_CONFIG_EMPTY |
| 414 |
self.cert_acme.post.side_effect = Exception( |
| 415 |
"400 Bad Request: Parameter verification failed. " |
| 416 |
"- {'force': \"Custom certificate exists but 'force' is not set.\"}" |
| 417 |
) |
| 418 |
|
| 419 |
result = self._run_module(build_module_args(force=False)) |
| 420 |
|
| 421 |
assert result.get("failed") is True |
| 422 |
assert "custom certificate already exists" in result["msg"].lower() |
| 423 |
assert "force=true" in result["msg"] |
| 424 |
|
| 425 |
def test_too_many_domains(self): |
| 426 |
domains = [{"domain": f"d{i}.example.com", "plugin": "cf"} for i in range(7)] |
| 427 |
|
| 428 |
result = self._run_module(build_module_args(domains=domains)) |
| 429 |
|
| 430 |
assert result.get("failed") is True |
| 431 |
assert "maximum of 6" in result["msg"] |
| 432 |
|
| 433 |
def test_configure_node_failure(self): |
| 434 |
self.node_config.get.return_value = NODE_CONFIG_EMPTY |
| 435 |
self.node_config.put.side_effect = Exception() |
| 436 |
|
| 437 |
result = self._run_module(build_module_args()) |
| 438 |
|
| 439 |
assert result.get("failed") is True |
| 440 |
assert "Failed to configure ACME settings" in result["msg"] |
| 441 |
|
| 442 |
def test_cert_info_read_failure(self): |
| 443 |
self.node_config.get.return_value = NODE_CONFIG_EMPTY |
| 444 |
self.cert_info.get.side_effect = Exception() |
| 445 |
|
| 446 |
result = self._run_module(build_module_args()) |
| 447 |
|
| 448 |
assert result.get("failed") is True |
| 449 |
assert "Failed to read certificates info" in result["msg"] |