| 1 |
# |
| 2 |
# Copyright (c) 2025, teslamania <[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 |
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.modules import ( |
| 17 |
proxmox_domain_sync, |
| 18 |
) |
| 19 |
|
| 20 |
proxmoxer = pytest.importorskip("proxmoxer") |
| 21 |
|
| 22 |
RAW_DOMAINS = [ |
| 23 |
{"type": "pve", "comment": "Proxmox VE authentication server", "realm": "pve"}, |
| 24 |
{"realm": "ad", "comment": "comment test", "type": "ad"}, |
| 25 |
{"realm": "openid", "type": "openid"}, |
| 26 |
{"type": "pam", "realm": "pam", "comment": "Linux PAM standard authentication"}, |
| 27 |
{"realm": "example.test", "default": 1, "type": "ldap"}, |
| 28 |
] |
| 29 |
|
| 30 |
|
| 31 |
def exit_json(*args, **kwargs): |
| 32 |
"""function to patch over exit_json; |
| 33 |
package return data into an exception""" |
| 34 |
if "changed" not in kwargs: |
| 35 |
kwargs["changed"] = False |
| 36 |
raise SystemExit(kwargs) |
| 37 |
|
| 38 |
|
| 39 |
def fail_json(*args, **kwargs): |
| 40 |
"""function to patch over fail_json; |
| 41 |
package return data into an exception""" |
| 42 |
kwargs["failed"] = True |
| 43 |
raise SystemExit(kwargs) |
| 44 |
|
| 45 |
|
| 46 |
def build_sync_arg(realm, check=False): |
| 47 |
args = { |
| 48 |
"api_user": "root@pam", |
| 49 |
"api_password": "secret", |
| 50 |
"api_host": "192.168.1.21", |
| 51 |
"realm": realm, |
| 52 |
"scope": "both", |
| 53 |
"enable_new": True, |
| 54 |
"remove_vanished": "acl;properties;entry", |
| 55 |
} |
| 56 |
if check: |
| 57 |
args["_ansible_check_mode"] = True |
| 58 |
return args |
| 59 |
|
| 60 |
|
| 61 |
class TestProxmoxDomainSync(ModuleTestCase): |
| 62 |
def setUp(self): |
| 63 |
super().setUp() |
| 64 |
proxmox_utils.HAS_PROXMOXER = True |
| 65 |
self.module = proxmox_domain_sync |
| 66 |
|
| 67 |
self.fail_json_patcher = patch( |
| 68 |
"ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json) |
| 69 |
) |
| 70 |
self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json) |
| 71 |
|
| 72 |
self.fail_json_mock = self.fail_json_patcher.start() |
| 73 |
self.exit_json_patcher.start() |
| 74 |
|
| 75 |
self.connect_mock = patch( |
| 76 |
"ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect", |
| 77 |
).start() |
| 78 |
|
| 79 |
mock_obj = self.connect_mock.return_value |
| 80 |
mock_obj.access.domains.get.return_value = RAW_DOMAINS |
| 81 |
|
| 82 |
def tearDown(self): |
| 83 |
self.connect_mock.stop() |
| 84 |
self.exit_json_patcher.stop() |
| 85 |
self.fail_json_patcher.stop() |
| 86 |
super().tearDown() |
| 87 |
|
| 88 |
def test_domain_sync_check_mode(self): |
| 89 |
args = build_sync_arg("example.test", True) |
| 90 |
with set_module_args(args), pytest.raises(SystemExit) as exc_info: |
| 91 |
proxmox_domain_sync.main() |
| 92 |
|
| 93 |
result = exc_info.value.args[0] |
| 94 |
|
| 95 |
assert result["changed"] is True |
| 96 |
assert result["msg"] == "Domain example.test would be synced." |
| 97 |
|
| 98 |
def test_domain_sync(self): |
| 99 |
args = build_sync_arg("example.test", False) |
| 100 |
with set_module_args(args), pytest.raises(SystemExit) as exc_info: |
| 101 |
proxmox_domain_sync.main() |
| 102 |
|
| 103 |
result = exc_info.value.args[0] |
| 104 |
|
| 105 |
assert result["changed"] is True |
| 106 |
assert result["msg"] == "Domain example.test synced." |
| 107 |
|
| 108 |
def test_domain_sync_not_present(self): |
| 109 |
args = build_sync_arg("not_exist", True) |
| 110 |
with set_module_args(args), pytest.raises(SystemExit) as exc_info: |
| 111 |
proxmox_domain_sync.main() |
| 112 |
|
| 113 |
result = exc_info.value.args[0] |
| 114 |
|
| 115 |
assert result["failed"] is True |
| 116 |
assert result["msg"] == "Domain not_exist not present." |