| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2025 Red Hat, Inc. |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# License: GPL (version 3 or any later version). |
| 6 |
# See LICENSE for details. |
| 7 |
# --- END COPYRIGHT BLOCK --- |
| 8 |
# |
| 9 |
import logging |
| 10 |
import os |
| 11 |
import pytest |
| 12 |
import ldap |
| 13 |
from test389.topologies import topology_st |
| 14 |
from lib389._constants import DEFAULT_SUFFIX |
| 15 |
from lib389.plugins import LinkedAttributesPlugin, LinkedAttributesConfigs, USNPlugin |
| 16 |
from lib389.idm.user import UserAccounts |
| 17 |
|
| 18 |
pytestmark = pytest.mark.tier1 |
| 19 |
|
| 20 |
log = logging.getLogger(__name__) |
| 21 |
|
| 22 |
OU_PEOPLE = f'ou=People,{DEFAULT_SUFFIX}' |
| 23 |
LINKTYPE = 'directReport' |
| 24 |
MANAGEDTYPE = 'manager' |
| 25 |
MANAGER = 'manager1' |
| 26 |
EMPLOYEE = 'employee1' |
| 27 |
INVALID = f'uid=doNotExist,{OU_PEOPLE}' |
| 28 |
|
| 29 |
|
| 30 |
@pytest.fixture(scope='function') |
| 31 |
def manager(topology_st, request): |
| 32 |
"""Fixture to create a manager entry.""" |
| 33 |
log.info('Creating manager entry') |
| 34 |
|
| 35 |
users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX) |
| 36 |
if users.exists(MANAGER): |
| 37 |
users.get(MANAGER).delete() |
| 38 |
|
| 39 |
manager = users.create(properties={ |
| 40 |
'uid': MANAGER, |
| 41 |
'cn': MANAGER, |
| 42 |
'sn': MANAGER, |
| 43 |
'uidNumber': '1', |
| 44 |
'gidNumber': '10', |
| 45 |
'homeDirectory': f'/home/{MANAGER}' |
| 46 |
}) |
| 47 |
manager.add('objectclass', 'extensibleObject') |
| 48 |
|
| 49 |
def fin(): |
| 50 |
log.info('Delete manager entry') |
| 51 |
if manager.exists(): |
| 52 |
manager.delete() |
| 53 |
|
| 54 |
request.addfinalizer(fin) |
| 55 |
|
| 56 |
return manager |
| 57 |
|
| 58 |
|
| 59 |
@pytest.fixture(scope='function') |
| 60 |
def employee(topology_st, request): |
| 61 |
"""Fixture to create an employee entry.""" |
| 62 |
log.info('Creating employee entry') |
| 63 |
|
| 64 |
users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX) |
| 65 |
if users.exists(EMPLOYEE): |
| 66 |
users.get(EMPLOYEE).delete() |
| 67 |
|
| 68 |
employee = users.create(properties={ |
| 69 |
'uid': EMPLOYEE, |
| 70 |
'cn': EMPLOYEE, |
| 71 |
'sn': EMPLOYEE, |
| 72 |
'uidNumber': '2', |
| 73 |
'gidNumber': '20', |
| 74 |
'homeDirectory': f'/home/{EMPLOYEE}' |
| 75 |
}) |
| 76 |
employee.add('objectclass', 'extensibleObject') |
| 77 |
|
| 78 |
def fin(): |
| 79 |
log.info('Delete employee entry') |
| 80 |
if employee.exists(): |
| 81 |
employee.delete() |
| 82 |
|
| 83 |
request.addfinalizer(fin) |
| 84 |
|
| 85 |
return employee |
| 86 |
|
| 87 |
|
| 88 |
@pytest.fixture(scope='function') |
| 89 |
def setup_linked_attributes(topology_st, request): |
| 90 |
"""Fixture to set up the Linked Attributes plugin.""" |
| 91 |
log.info('Setting up Linked Attributes plugin') |
| 92 |
|
| 93 |
log.info('Enable Linked Attributes plugin') |
| 94 |
linkedattr = LinkedAttributesPlugin(topology_st.standalone) |
| 95 |
linkedattr.enable() |
| 96 |
topology_st.standalone.restart() |
| 97 |
|
| 98 |
log.info('Add the plugin config entry') |
| 99 |
la_configs = LinkedAttributesConfigs(topology_st.standalone) |
| 100 |
config = la_configs.create(properties={'cn': 'Manager Link', |
| 101 |
'linkType': LINKTYPE, |
| 102 |
'managedType': MANAGEDTYPE}) |
| 103 |
|
| 104 |
def fin(): |
| 105 |
log.info('Cleaning up Linked Attributes plugin') |
| 106 |
config.delete() |
| 107 |
linkedattr.disable() |
| 108 |
topology_st.standalone.restart() |
| 109 |
|
| 110 |
request.addfinalizer(fin) |
| 111 |
|
| 112 |
|
| 113 |
def test_linked_attribute_after_modrdn(topology_st, setup_linked_attributes, manager, employee): |
| 114 |
""" Test that linked attributes are properly updated after a modrdn operation. |
| 115 |
|
| 116 |
:id: 11577519-6e91-428e-b547-4fe49ba29285 |
| 117 |
:setup: |
| 118 |
Standalone instance with Linked Attributes plugin enabled and manager and employee entries created. |
| 119 |
:steps: |
| 120 |
1. Add linktype to manager |
| 121 |
2. Check managed attribute on employee |
| 122 |
3. Rename employee1 to employee2 |
| 123 |
4. Modify the value of directReport to employee2 |
| 124 |
5. Verify that the link is properly updated |
| 125 |
6. Rename employee2 to employee3 |
| 126 |
7. Modify the value of directReport to employee3 by deleting and readding the link |
| 127 |
8. Verify that the link is properly updated |
| 128 |
9. Rename manager1 to manager2 |
| 129 |
10. Verify that the link is properly updated |
| 130 |
:expectedresults: |
| 131 |
1. Linktype is added to manager |
| 132 |
2. Managed attribute is present on employee |
| 133 |
3. Employee is renamed to employee2 |
| 134 |
4. The value of directReport is modified to employee2 |
| 135 |
5. The link is properly updated to employee2 |
| 136 |
6. Employee2 is renamed to employee3 |
| 137 |
7. The value of directReport is modified to employee3 by deleting and readding the link |
| 138 |
8. The link is properly updated to employee3 |
| 139 |
9. Manager is renamed to manager2 |
| 140 |
10. The link is properly updated to manager2 |
| 141 |
""" |
| 142 |
|
| 143 |
log.info('Add linktype to manager') |
| 144 |
manager.add(LINKTYPE, employee.dn) |
| 145 |
|
| 146 |
log.info('Check managed attribute') |
| 147 |
assert employee.present(MANAGEDTYPE, manager.dn) |
| 148 |
|
| 149 |
log.info('Rename employee1 to employee2') |
| 150 |
employee.rename('uid=employee2') |
| 151 |
|
| 152 |
log.info('Modify the value of directReport to employee2') |
| 153 |
manager.replace(LINKTYPE, f'uid=employee2,{OU_PEOPLE}') |
| 154 |
|
| 155 |
log.info('Verify that the link is properly updated') |
| 156 |
assert manager.present(LINKTYPE, employee.dn) |
| 157 |
assert employee.present(MANAGEDTYPE, manager.dn) |
| 158 |
|
| 159 |
log.info('Rename employee2 to employee3') |
| 160 |
employee.rename('uid=employee3') |
| 161 |
|
| 162 |
log.info('Modify the value of directReport to employee3 by deleting and readding the link') |
| 163 |
manager.remove(LINKTYPE, f'uid=employee2,{OU_PEOPLE}') |
| 164 |
manager.add(LINKTYPE, f'uid=employee3,{OU_PEOPLE}') |
| 165 |
|
| 166 |
log.info('Verify that the link is properly updated') |
| 167 |
assert manager.present(LINKTYPE, employee.dn) |
| 168 |
assert employee.present(MANAGEDTYPE, manager.dn) |
| 169 |
|
| 170 |
log.info('Rename manager1 to manager2') |
| 171 |
manager.rename('uid=manager2') |
| 172 |
|
| 173 |
log.info('Verify that the link is properly updated') |
| 174 |
assert manager.present(LINKTYPE, employee.dn) |
| 175 |
assert employee.present(MANAGEDTYPE, manager.dn) |
| 176 |
|
| 177 |
|
| 178 |
def test_replace_linktype_no_spurious_managedtype_mods(topology_st, setup_linked_attributes, request): |
| 179 |
"""A linktype MOD_REPLACE must not re-modify overlap targets. |
| 180 |
|
| 181 |
:id: 405f7dab-1c36-458d-a2bf-abf3284c7c41 |
| 182 |
:setup: Standalone Instance, USN + Linked Attributes enabled |
| 183 |
:steps: |
| 184 |
1. Set linkType=[t0, t3] on a source entry |
| 185 |
2. Snapshot entryUSN of every target |
| 186 |
3. MOD_REPLACE linkType to [t0, t1, t2, t3] |
| 187 |
:expectedresults: |
| 188 |
1. Success |
| 189 |
2. Success |
| 190 |
3. t0 and t3 entryUSN unchanged (overlap), t1 and t2 advanced |
| 191 |
""" |
| 192 |
inst = topology_st.standalone |
| 193 |
USNPlugin(inst).enable() |
| 194 |
inst.restart() |
| 195 |
|
| 196 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 197 |
targets = [users.create_test_user(uid=500000 + i, gid=500000 + i) |
| 198 |
for i in range(4)] |
| 199 |
source = users.create_test_user(uid=500099, gid=500099) |
| 200 |
source.add('objectclass', 'extensibleObject') |
| 201 |
|
| 202 |
def fin(): |
| 203 |
for u in targets + [source]: |
| 204 |
try: |
| 205 |
u.delete() |
| 206 |
except ldap.LDAPError: |
| 207 |
pass |
| 208 |
request.addfinalizer(fin) |
| 209 |
|
| 210 |
source.replace(LINKTYPE, [targets[0].dn, targets[3].dn]) |
| 211 |
assert targets[0].present(MANAGEDTYPE, source.dn) |
| 212 |
assert targets[3].present(MANAGEDTYPE, source.dn) |
| 213 |
|
| 214 |
usn_before = [t.get_attr_val_int('entryusn') for t in targets] |
| 215 |
source.replace(LINKTYPE, [t.dn for t in targets]) |
| 216 |
usn_after = [t.get_attr_val_int('entryusn') for t in targets] |
| 217 |
|
| 218 |
for i in (0, 3): |
| 219 |
assert usn_after[i] == usn_before[i], ( |
| 220 |
f't{i} entryUSN advanced ({usn_before[i]} -> {usn_after[i]}); ' |
| 221 |
f'it was already linked before and after the replace') |
| 222 |
for i in (1, 2): |
| 223 |
assert usn_after[i] > usn_before[i], f't{i} should have been linked' |
| 224 |
|
| 225 |
|
| 226 |
def test_rollback_on_failed_operation(topology_st, setup_linked_attributes, manager, employee): |
| 227 |
""" Test that successful changes are rolled back if made as a part of unsuccessful |
| 228 |
linked attribute operation. |
| 229 |
|
| 230 |
:id: 407c03a1-a5b1-4668-8716-ccecc27414f3 |
| 231 |
:setup: |
| 232 |
Standalone instance with Linked Attributes plugin enabled and manager and employee entries created. |
| 233 |
:steps: |
| 234 |
1. Add linktype to manager with a valid employee DN and an invalid DN. |
| 235 |
2. Check that the link was not added to employee due to failed operation. |
| 236 |
3. Check that manager does not have any link. |
| 237 |
:expectedresults: |
| 238 |
1. Operation fails with UNWILLING_TO_PERFORM error. |
| 239 |
2. No changes are made to employee's managed attribute. |
| 240 |
3. Manager has no linked attribute. |
| 241 |
""" |
| 242 |
log.info('Add linktype to manager') |
| 243 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 244 |
manager.add_many((LINKTYPE, INVALID), (LINKTYPE, employee.dn)) |
| 245 |
|
| 246 |
log.info('Check that no changes were made since previous operation failed') |
| 247 |
assert not employee.present(MANAGEDTYPE, manager.dn) |
| 248 |
assert not manager.present(LINKTYPE, employee.dn) |
| 249 |
assert not manager.present(LINKTYPE, INVALID) |
| 250 |
|
| 251 |
|
| 252 |
if __name__ == '__main__': |
| 253 |
# Run isolated |
| 254 |
# -s for DEBUG mode |
| 255 |
CURRENT_FILE = os.path.realpath(__file__) |
| 256 |
pytest.main(f"-s {CURRENT_FILE}") |