| 1 |
# Copyright (c) 2025 Aleksandr Gabidullin <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import sys |
| 8 |
import unittest |
| 9 |
from unittest.mock import Mock, patch |
| 10 |
|
| 11 |
|
| 12 |
class TestSssdInfo(unittest.TestCase): |
| 13 |
"""Unit tests for the sssd_info module.""" |
| 14 |
|
| 15 |
@classmethod |
| 16 |
def setUpClass(cls): |
| 17 |
"""Mock dbus module before importing the module.""" |
| 18 |
# Mock the entire dbus module |
| 19 |
cls.mock_dbus = Mock() |
| 20 |
cls.mock_dbus.SystemBus = Mock() |
| 21 |
cls.mock_dbus.Interface = Mock() |
| 22 |
|
| 23 |
# Create mock exceptions |
| 24 |
class MockDBusException(Exception): |
| 25 |
def __init__(self, *args, **kwargs): |
| 26 |
super().__init__(*args) |
| 27 |
self._dbus_error_name = kwargs.get("dbus_error_name", "org.freedesktop.DBus.Error.UnknownObject") |
| 28 |
|
| 29 |
def get_dbus_name(self): |
| 30 |
return self._dbus_error_name |
| 31 |
|
| 32 |
cls.mock_dbus.exceptions = Mock() |
| 33 |
cls.mock_dbus.exceptions.DBusException = MockDBusException |
| 34 |
|
| 35 |
# Mock the dbus module in sys.modules |
| 36 |
sys.modules["dbus"] = cls.mock_dbus |
| 37 |
|
| 38 |
@classmethod |
| 39 |
def tearDownClass(cls): |
| 40 |
"""Clean up after all tests.""" |
| 41 |
# Remove the mocked dbus module |
| 42 |
if "dbus" in sys.modules and sys.modules["dbus"] == cls.mock_dbus: |
| 43 |
del sys.modules["dbus"] |
| 44 |
|
| 45 |
def setUp(self): |
| 46 |
"""Set up test fixtures.""" |
| 47 |
# Ensure the mocked dbus is in sys.modules |
| 48 |
sys.modules["dbus"] = self.mock_dbus |
| 49 |
|
| 50 |
# Reset all mocks |
| 51 |
self.mock_dbus.reset_mock() |
| 52 |
self.mock_dbus.SystemBus.reset_mock() |
| 53 |
self.mock_dbus.Interface.reset_mock() |
| 54 |
|
| 55 |
# Create fresh mocks for each test |
| 56 |
self.mock_bus = Mock() |
| 57 |
self.mock_sssd_obj = Mock() |
| 58 |
self.mock_infopipe_iface = Mock() |
| 59 |
self.mock_domain_obj = Mock() |
| 60 |
self.mock_domain_iface = Mock() |
| 61 |
|
| 62 |
# Configure the mock chain |
| 63 |
self.mock_dbus.SystemBus.return_value = self.mock_bus |
| 64 |
self.mock_bus.get_object.return_value = self.mock_sssd_obj |
| 65 |
|
| 66 |
# Fix the Interface mock to accept dbus_interface parameter |
| 67 |
def interface_side_effect(obj, dbus_interface=None): |
| 68 |
if dbus_interface == "org.freedesktop.sssd.infopipe": |
| 69 |
return self.mock_infopipe_iface |
| 70 |
elif dbus_interface == "org.freedesktop.sssd.infopipe.Domains.Domain": |
| 71 |
return self.mock_domain_iface |
| 72 |
return Mock() |
| 73 |
|
| 74 |
self.mock_dbus.Interface.side_effect = interface_side_effect |
| 75 |
|
| 76 |
def tearDown(self): |
| 77 |
"""Clean up after test.""" |
| 78 |
# Restore the mocked dbus module in sys.modules |
| 79 |
sys.modules["dbus"] = self.mock_dbus |
| 80 |
|
| 81 |
def test_domain_list_success(self): |
| 82 |
"""Test successful retrieval of domain list.""" |
| 83 |
# Mock the ListDomains response |
| 84 |
self.mock_infopipe_iface.ListDomains.return_value = [ |
| 85 |
"/org/freedesktop/sssd/infopipe/Domains/ipa_2eexample_2ecom", |
| 86 |
"/org/freedesktop/sssd/infopipe/Domains/ad_2eexample_2ecom", |
| 87 |
] |
| 88 |
|
| 89 |
# Import the module (dbus is already mocked in sys.modules) |
| 90 |
from ansible_collections.community.general.plugins.modules import sssd_info |
| 91 |
|
| 92 |
# Mock AnsibleModule |
| 93 |
with patch.object(sssd_info, "AnsibleModule") as mock_module_class: |
| 94 |
mock_module = Mock() |
| 95 |
mock_module.params = {"action": "domain_list"} |
| 96 |
mock_module.fail_json = Mock(side_effect=Exception("fail_json called")) |
| 97 |
mock_module.exit_json = Mock() |
| 98 |
mock_module_class.return_value = mock_module |
| 99 |
|
| 100 |
# Run the module |
| 101 |
sssd_info.main() |
| 102 |
|
| 103 |
# Verify exit_json was called with correct results |
| 104 |
mock_module.exit_json.assert_called_once() |
| 105 |
result = ( |
| 106 |
mock_module.exit_json.call_args[0][0] |
| 107 |
if mock_module.exit_json.call_args[0] |
| 108 |
else mock_module.exit_json.call_args[1] |
| 109 |
) |
| 110 |
|
| 111 |
self.assertIn("domain_list", result) |
| 112 |
self.assertEqual(result["domain_list"], ["ipa.example.com", "ad.example.com"]) |
| 113 |
|
| 114 |
def test_domain_status_online(self): |
| 115 |
"""Test checking online domain status.""" |
| 116 |
# Mock domain status as online |
| 117 |
self.mock_domain_iface.IsOnline.return_value = True |
| 118 |
|
| 119 |
# Setup mock chain for domain object |
| 120 |
self.mock_bus.get_object.return_value = self.mock_domain_obj |
| 121 |
|
| 122 |
# Import the module |
| 123 |
from ansible_collections.community.general.plugins.modules import sssd_info |
| 124 |
|
| 125 |
# Mock AnsibleModule |
| 126 |
with patch.object(sssd_info, "AnsibleModule") as mock_module_class: |
| 127 |
mock_module = Mock() |
| 128 |
mock_module.params = {"action": "domain_status", "domain": "example.com"} |
| 129 |
mock_module.fail_json = Mock(side_effect=Exception("fail_json called")) |
| 130 |
mock_module.exit_json = Mock() |
| 131 |
mock_module_class.return_value = mock_module |
| 132 |
|
| 133 |
# Run the module |
| 134 |
sssd_info.main() |
| 135 |
|
| 136 |
# Verify exit_json was called with correct results |
| 137 |
mock_module.exit_json.assert_called_once() |
| 138 |
result = ( |
| 139 |
mock_module.exit_json.call_args[0][0] |
| 140 |
if mock_module.exit_json.call_args[0] |
| 141 |
else mock_module.exit_json.call_args[1] |
| 142 |
) |
| 143 |
|
| 144 |
self.assertIn("online", result) |
| 145 |
self.assertEqual(result["online"], "online") |
| 146 |
|
| 147 |
def test_domain_status_offline(self): |
| 148 |
"""Test checking offline domain status.""" |
| 149 |
# Mock domain status as offline |
| 150 |
self.mock_domain_iface.IsOnline.return_value = False |
| 151 |
|
| 152 |
# Setup mock chain for domain object |
| 153 |
self.mock_bus.get_object.return_value = self.mock_domain_obj |
| 154 |
|
| 155 |
# Import the module |
| 156 |
from ansible_collections.community.general.plugins.modules import sssd_info |
| 157 |
|
| 158 |
# Mock AnsibleModule |
| 159 |
with patch.object(sssd_info, "AnsibleModule") as mock_module_class: |
| 160 |
mock_module = Mock() |
| 161 |
mock_module.params = {"action": "domain_status", "domain": "example.com"} |
| 162 |
mock_module.fail_json = Mock(side_effect=Exception("fail_json called")) |
| 163 |
mock_module.exit_json = Mock() |
| 164 |
mock_module_class.return_value = mock_module |
| 165 |
|
| 166 |
# Run the module |
| 167 |
sssd_info.main() |
| 168 |
|
| 169 |
# Verify exit_json was called with correct results |
| 170 |
mock_module.exit_json.assert_called_once() |
| 171 |
result = ( |
| 172 |
mock_module.exit_json.call_args[0][0] |
| 173 |
if mock_module.exit_json.call_args[0] |
| 174 |
else mock_module.exit_json.call_args[1] |
| 175 |
) |
| 176 |
|
| 177 |
self.assertIn("online", result) |
| 178 |
self.assertEqual(result["online"], "offline") |
| 179 |
|
| 180 |
def test_domain_not_found(self): |
| 181 |
"""Test error when domain is not found.""" |
| 182 |
# Mock DBusException for domain not found |
| 183 |
from ansible_collections.community.general.plugins.modules import sssd_info |
| 184 |
|
| 185 |
# Mock AnsibleModule |
| 186 |
with patch.object(sssd_info, "AnsibleModule") as mock_module_class: |
| 187 |
mock_module = Mock() |
| 188 |
mock_module.params = {"action": "domain_status", "domain": "nonexistent.com"} |
| 189 |
mock_module.fail_json = Mock() |
| 190 |
mock_module.exit_json = Mock() |
| 191 |
mock_module_class.return_value = mock_module |
| 192 |
|
| 193 |
# Mock the exception in domain_object |
| 194 |
with patch.object(sssd_info.SSSDHandler, "domain_object") as mock_get_domain: |
| 195 |
mock_get_domain.side_effect = Exception("Domain not found: nonexistent.com. Error: Domain not found") |
| 196 |
|
| 197 |
# Run the module |
| 198 |
sssd_info.main() |
| 199 |
|
| 200 |
# Verify fail_json was called with error message |
| 201 |
mock_module.fail_json.assert_called_once() |
| 202 |
error_msg = mock_module.fail_json.call_args[1].get("msg", "") |
| 203 |
self.assertIn("Domain not found: nonexistent.com", error_msg) |