| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2026 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 pytest, time |
| 10 |
import os |
| 11 |
import glob |
| 12 |
import ldap |
| 13 |
from shutil import copyfile, rmtree |
| 14 |
from itertools import permutations, product |
| 15 |
from contextlib import contextmanager |
| 16 |
from lib389.tasks import * |
| 17 |
from lib389.utils import * |
| 18 |
from lib389.dseutils import get_ldapurl_from_serverid |
| 19 |
from test389.topologies import topology_m3, topology_m2, set_timeout |
| 20 |
from lib389.replica import * |
| 21 |
from lib389._constants import * |
| 22 |
from lib389.properties import TASK_WAIT |
| 23 |
from lib389.index import * |
| 24 |
from lib389.mappingTree import * |
| 25 |
from lib389.backend import * |
| 26 |
from lib389.conflicts import ConflictEntry |
| 27 |
from lib389.idm.user import UserAccounts, UserAccount |
| 28 |
from lib389.idm.organization import Organization |
| 29 |
from lib389.agreement import Agreements |
| 30 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 31 |
|
| 32 |
|
| 33 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 34 |
log = logging.getLogger(__name__) |
| 35 |
DEBUGGING = os.getenv("DEBUGGING", default=False) |
| 36 |
SKIP_THE_TESTS = os.getenv('URP_VERY_LONG_TEST') is None |
| 37 |
|
| 38 |
# Replication synchronization timeout in seconds |
| 39 |
REPL_SYNC_TIMEOUT = 300 |
| 40 |
|
| 41 |
# test_urp_delete is a long test spending days |
| 42 |
set_timeout(24*3600*10) |
| 43 |
|
| 44 |
|
| 45 |
def normalize_values(v): |
| 46 |
# Return lower case sorted values. |
| 47 |
if isinstance(v, bytes): |
| 48 |
return v.decode('utf-8').lower() |
| 49 |
result = [ normalize_values(val) for val in v ] |
| 50 |
result.sort() |
| 51 |
return result |
| 52 |
|
| 53 |
|
| 54 |
def get_entry_info(entry): |
| 55 |
norm_entry = { k.lower():normalize_values(v) for k,v in entry.items() } |
| 56 |
uuid = norm_entry['nsuniqueid'][0] |
| 57 |
if 'nstombstone' in norm_entry['objectclass']: |
| 58 |
return "tombstone:"+uuid |
| 59 |
elif 'nsds5replconflict' in norm_entry: |
| 60 |
return "conflict:"+uuid |
| 61 |
else: |
| 62 |
return "regular:"+uuid |
| 63 |
|
| 64 |
|
| 65 |
def search_entries(inst, dnfilter): |
| 66 |
ldapurl, certdir = get_ldapurl_from_serverid(inst.serverid) |
| 67 |
assert 'ldapi://' in ldapurl |
| 68 |
conn = ldap.initialize(ldapurl) |
| 69 |
conn.sasl_interactive_bind_s("", ldap.sasl.external()) |
| 70 |
filter = "(|(objectclass=*)(objectclass=ldapsubentry)(objectclass=nstombstone))" |
| 71 |
attrlist = ['nsuniqueid','objectclass', 'nsds5replconflict' ] |
| 72 |
result = conn.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, filterstr=filter, attrlist=attrlist) |
| 73 |
entry_infos = [ get_entry_info(entry) for dn,entry in result if dnfilter.lower() in dn.lower() ] |
| 74 |
entry_infos.sort() |
| 75 |
return entry_infos |
| 76 |
|
| 77 |
|
| 78 |
def list_all_order_combinations(m, n): |
| 79 |
# list all way to order pair of m per n elements |
| 80 |
# knowing that element (m, 0) should be the first for m |
| 81 |
# But m=3, n=4 generates 1247400 combinations |
| 82 |
# and testing them would requires 3.5 years ... |
| 83 |
# So we are using list_order_combinations which is a subset |
| 84 |
actions = list(product(range(m), range(n))) |
| 85 |
actions.remove((0,0)) |
| 86 |
for order in permutations(actions): |
| 87 |
order = ((0,0),) + order |
| 88 |
higher_instance_seen = 0 |
| 89 |
ok = True |
| 90 |
for (instance,action) in order: |
| 91 |
if instance > higher_instance_seen+1: |
| 92 |
ok = False |
| 93 |
break |
| 94 |
if instance > higher_instance_seen: |
| 95 |
if action != 0: |
| 96 |
ok = False |
| 97 |
break |
| 98 |
higher_instance_seen = instance |
| 99 |
if ok is True: |
| 100 |
yield order |
| 101 |
|
| 102 |
|
| 103 |
def list_order_combinations(m, n): |
| 104 |
# list all way to order pair of m per n elements |
| 105 |
# while keeping the same action sequence on a given instances |
| 106 |
# ( so action i on instance j is always done after action i-1 on instance j ) |
| 107 |
# For m=3, n=4 There is 5774 orders The test requires 6 days |
| 108 |
class OrderedCombination: |
| 109 |
def __init__(self, m, n): |
| 110 |
self.m = m |
| 111 |
self.n = n |
| 112 |
|
| 113 |
def bypass(self, idx): |
| 114 |
# Keep the first action in order |
| 115 |
# Changing the order of the first action is |
| 116 |
# equivalent to swapping the instances so lets avoid to |
| 117 |
# do it to decrease the number of tests to perform. |
| 118 |
for i in range(self.n): |
| 119 |
for j in range(i+1, self.n): |
| 120 |
if idx[j] > 0 and idx[i] == 0: |
| 121 |
return True |
| 122 |
return False |
| 123 |
|
| 124 |
def run(self, s=None, idx=None): |
| 125 |
# Run self.action on every possible order for the n steps on m steps sets |
| 126 |
result = [] |
| 127 |
if s is None: |
| 128 |
s = [] |
| 129 |
if idx is None: |
| 130 |
idx = [ 0 ] * self.n |
| 131 |
stop = True |
| 132 |
if self.bypass(idx): |
| 133 |
return result |
| 134 |
for i in range(self.n): |
| 135 |
if idx[i] < self.m: |
| 136 |
stop = False |
| 137 |
idx2 = idx.copy() |
| 138 |
s2 = s.copy() |
| 139 |
idx2[i] += 1 |
| 140 |
s2.append((i+1, idx2[i])) |
| 141 |
result.extend(self.run(s=s2, idx=idx2)) |
| 142 |
if stop: |
| 143 |
result.append(s) |
| 144 |
return result |
| 145 |
|
| 146 |
if SKIP_THE_TESTS: |
| 147 |
# Lets avoid generating 5000 tests to skip |
| 148 |
return [ 'skipped', ] |
| 149 |
return OrderedCombination(m,n).run() |
| 150 |
|
| 151 |
|
| 152 |
@pytest.fixture(scope="module") |
| 153 |
def urp_tester(topology_m3): |
| 154 |
class UrpTesterInstances: |
| 155 |
# Contains the data and the methods about a specific instance |
| 156 |
def __init__(self, tester, inst): |
| 157 |
self.tester = tester |
| 158 |
self.inst = inst |
| 159 |
self.users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 160 |
ldapurl, certdir = get_ldapurl_from_serverid(inst.serverid) |
| 161 |
assert 'ldapi://' in ldapurl |
| 162 |
self.conn = ldap.initialize(ldapurl) |
| 163 |
self.conn.sasl_interactive_bind_s("", ldap.sasl.external()) |
| 164 |
self.entriesinfo = [] |
| 165 |
self.replicas = [ inst0 for inst0 in tester.topo if inst0 != inst ] |
| 166 |
|
| 167 |
def add_user(self): |
| 168 |
user_properties = { |
| 169 |
'uid': 'testuser', |
| 170 |
'cn': 'testuser', |
| 171 |
'sn': 'testuser', |
| 172 |
'uidNumber': '1', |
| 173 |
'gidNumber': '1', |
| 174 |
'homeDirectory': '/home/testuser' |
| 175 |
} |
| 176 |
# Wait 1 second to ensure that csn time are differents |
| 177 |
time.sleep(1) |
| 178 |
self.user = self.users.create(properties=user_properties) |
| 179 |
log.info(f"Adding entry {self.user.dn} on {self.inst.serverid}") |
| 180 |
self.uuid = self.user.get_attr_val_utf8_l('nsuniqueid') |
| 181 |
|
| 182 |
def sync1(self): |
| 183 |
self.tester.resync_agmt(self.replicas[0], self.inst) |
| 184 |
|
| 185 |
def sync2(self): |
| 186 |
self.tester.resync_agmt(self.replicas[1], self.inst) |
| 187 |
|
| 188 |
def remove_user(self): |
| 189 |
filter = f'(&(nsuniqueid={self.uuid})(|(objectclass=nsTombstone)(objectclass=ldapsubentry)(objectclass=*)))' |
| 190 |
res = self.conn.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, filter, ['dn',]) |
| 191 |
assert len(res) == 1 |
| 192 |
dn,entry = res[0] |
| 193 |
log.info(f'Removing entry: {dn}') |
| 194 |
self.conn.delete_s(dn) |
| 195 |
|
| 196 |
def get_entries(self): |
| 197 |
self.entry_info = search_entries(self.inst, 'uid=testuser') |
| 198 |
|
| 199 |
def run_action(self, action): |
| 200 |
actions = ( ( None, None ), |
| 201 |
( "ADD ENTRY", self.add_user ), |
| 202 |
( "SYNC ENTRY FROM REPLICA ASSOCIATED WITH FIRST AGMT", self.sync1 ), |
| 203 |
( "SYNC ENTRY FROM REPLICA ASSOCIATED WITH SECOND AGMT", self.sync2 ), |
| 204 |
( "REMOVE ENTRY", self.remove_user ), ) |
| 205 |
log.info (f'***** {actions[action][0]} on {self.inst.serverid}') |
| 206 |
actions[action][1]() |
| 207 |
|
| 208 |
|
| 209 |
class UrpTester: |
| 210 |
# Contains the data and the methods for all instances |
| 211 |
def __init__(self, topo): |
| 212 |
self.topo = topo |
| 213 |
self.repl = ReplicationManager(DEFAULT_SUFFIX) |
| 214 |
self.insts = [ UrpTesterInstances(self, inst) for inst in topo ] |
| 215 |
inst = topo[0] |
| 216 |
self.ldif = f'{inst.get_ldif_dir()}/userroot.ldif' |
| 217 |
tasks = Tasks(inst) |
| 218 |
tasks.exportLDIF(DEFAULT_SUFFIX, output_file=self.ldif, args={EXPORT_REPL_INFO: True, TASK_WAIT: True}) |
| 219 |
|
| 220 |
def resync_agmt(self, instfrom, instto): |
| 221 |
log.info(f"Enabling replication agreement from {instfrom.serverid} to {instto.serverid}") |
| 222 |
self.repl.enable_to_supplier(instto, [instfrom,]) |
| 223 |
self.repl.wait_for_replication(instfrom, instto, timeout=REPL_SYNC_TIMEOUT) |
| 224 |
|
| 225 |
def disable_all_agmts(self): |
| 226 |
log.info(f"Disabling replication all replication agreements") |
| 227 |
for inst in self.topo: |
| 228 |
for agmt in Agreements(inst).list(): |
| 229 |
agmt.pause() |
| 230 |
|
| 231 |
def resync_all_agmts(self): |
| 232 |
ilist = [ inst for inst in self.topo ] |
| 233 |
for inst in ilist: |
| 234 |
for inst2 in ilist: |
| 235 |
if inst != inst2: |
| 236 |
self.resync_agmt(inst, inst2) |
| 237 |
|
| 238 |
def get_entries(self): |
| 239 |
for inst in self.insts: |
| 240 |
inst.get_entries() |
| 241 |
|
| 242 |
def reset(self): |
| 243 |
# Reinitilize all the replicas to original data and disable all agmts |
| 244 |
self.disable_all_agmts() |
| 245 |
# In theory importing on all replicas should work but sometime the replication |
| 246 |
# fails to restart (probably worth to investigate) - so let use total reinit instead. |
| 247 |
# for inst in self.topo: |
| 248 |
# tasks = Tasks(inst) |
| 249 |
# tasks.importLDIF(DEFAULT_SUFFIX, input_file=self.ldif, args={TASK_WAIT: True}) |
| 250 |
# self.resync_all_agmts() |
| 251 |
inst = self.topo[0] |
| 252 |
agmts = Agreements(inst).list() |
| 253 |
for agmt in agmts: |
| 254 |
agmt.resume() |
| 255 |
tasks = Tasks(inst) |
| 256 |
tasks.importLDIF(DEFAULT_SUFFIX, input_file=self.ldif, args={TASK_WAIT: True}) |
| 257 |
for agmt in Agreements(inst).list(): |
| 258 |
agmt.begin_reinit() |
| 259 |
(done, error) = agmt.wait_reinit() |
| 260 |
assert done is True |
| 261 |
assert error is False |
| 262 |
for agmt in agmts: |
| 263 |
agmt.pause() |
| 264 |
|
| 265 |
return UrpTester(topology_m3) |
| 266 |
|
| 267 |
|
| 268 |
# Warning. test_urp_delete is very long and requires a reasonably |
| 269 |
# fast machine and enough memory to run 3 instances without swapping. |
| 270 |
# FYI: the test spend 6 days on my laptop with i7-9850H CPU @ 2.60GHz |
| 271 |
# and 32Gb of memory. |
| 272 |
@pytest.mark.skipif(SKIP_THE_TESTS, reason="This test is meant to execute in specific test environment") |
| 273 |
@pytest.mark.parametrize("actionorder", list_order_combinations(4,3)) |
| 274 |
def test_urp_delete(urp_tester, actionorder): |
| 275 |
"""Test urp conflict handling for add and delete operations |
| 276 |
|
| 277 |
:id: 7b08b6ac-f362-11ee-bf7c-482ae39447e5 |
| 278 |
:setup: Three suppliers |
| 279 |
:parametrized: yes |
| 280 |
:steps: |
| 281 |
On every possible combinations (i.e 5775) for the actions on all instances |
| 282 |
|
| 283 |
1. Reinitialise the instances |
| 284 |
2. Run the action on specified order and specified instances |
| 285 |
The actions are: |
| 286 |
|
| 287 |
- Add the entry on current replica |
| 288 |
- Sync first other replica changes with current replica |
| 289 |
- Sync second other replica changes with current replica |
| 290 |
- Find the entry added on this replica and remove it |
| 291 |
|
| 292 |
3. Wait until instances are in sync |
| 293 |
4. Check that entry type,nsuniqueids pair are the same on all replica |
| 294 |
(entry type is either tombstone, conflct, or regular) |
| 295 |
:expectedresults: |
| 296 |
1. Should Success. |
| 297 |
2. Should Success. |
| 298 |
3. Should Success. |
| 299 |
4. Should Success. |
| 300 |
""" |
| 301 |
log.info(f'actions order is: {actionorder}') |
| 302 |
urp_tester.reset() |
| 303 |
# Run the actions on specified server with specified order |
| 304 |
for inst_idx,action in actionorder: |
| 305 |
urp_tester.insts[inst_idx-1].run_action(action) |
| 306 |
# Wait until replication is in sync |
| 307 |
urp_tester.resync_all_agmts() |
| 308 |
# Check that all replica have the sames entries |
| 309 |
urp_tester.get_entries() |
| 310 |
assert urp_tester.insts[0].entry_info == urp_tester.insts[1].entry_info |
| 311 |
assert urp_tester.insts[0].entry_info == urp_tester.insts[2].entry_info |
| 312 |
|
| 313 |
|
| 314 |
def gen_ldif_file(first, second, result): |
| 315 |
with open(result, 'w') as output: |
| 316 |
for file in [ first, second ]: |
| 317 |
with open(file, 'r') as input: |
| 318 |
for line in input: |
| 319 |
output.write(line) |
| 320 |
# ldif sould be readable by dirsrv |
| 321 |
os.chmod(result, 0o755) |
| 322 |
|
| 323 |
|
| 324 |
@pytest.mark.skipif(SKIP_THE_TESTS, reason="This test is meant to execute in specific test environment") |
| 325 |
@pytest.mark.xfail(reason="URP does not properly handle this case") |
| 326 |
def test_urp_with_crossed_entries(topology_m2): |
| 327 |
"""Test urp behaviour if entry conflict entries are crossed |
| 328 |
|
| 329 |
:id: 6b4adfc2-fd9b-11ee-a6f0-482ae39447e5 |
| 330 |
:setup: Two suppliers |
| 331 |
:steps: |
| 332 |
1. Generate ldif files with crossed conflict entries |
| 333 |
2. Import the ldif files |
| 334 |
3. Remove a conflict entry |
| 335 |
4. Wait until le replication is in sync |
| 336 |
5. Check that entries have the same type (tombstone/conflict/regular) on both replica. |
| 337 |
6. Import the ldif files |
| 338 |
7. Remove a regular entry |
| 339 |
8. Wait until le replication is in sync |
| 340 |
9. Check that entries have the same type (tombstone/conflict/regular) on both replica. |
| 341 |
:expectedresults: |
| 342 |
1. Should Success. |
| 343 |
2. Should Success. |
| 344 |
3. Should Success. |
| 345 |
4. Should Success. |
| 346 |
""" |
| 347 |
# Note in theory crossed entries should not happen |
| 348 |
# But this test shows that we are in trouble if that is the case |
| 349 |
s1 = topology_m2.ms["supplier1"] |
| 350 |
s2 = topology_m2.ms["supplier2"] |
| 351 |
datadir = os.path.join(os.path.dirname(__file__), '../../data/urp') |
| 352 |
# 1. Generate ldif files with crossed conflict entries |
| 353 |
# Export the ldif to be sure that the replication credentials are ok |
| 354 |
export_ldif = f'{s1.get_ldif_dir()}/db.ldif' |
| 355 |
import_ldif1 = f'{s1.get_ldif_dir()}/db1.ldif' |
| 356 |
import_ldif2 = f'{s1.get_ldif_dir()}/db2.ldif' |
| 357 |
assert Tasks(s1).exportLDIF(DEFAULT_SUFFIX, output_file=export_ldif, |
| 358 |
args={EXPORT_REPL_INFO: True, TASK_WAIT: True}) == 0 |
| 359 |
gen_ldif_file(export_ldif, f'{datadir}/db1.ldif', import_ldif1) |
| 360 |
gen_ldif_file(export_ldif, f'{datadir}/db2.ldif', import_ldif2) |
| 361 |
# 2. import the ldif files |
| 362 |
assert Tasks(s1).importLDIF(DEFAULT_SUFFIX, input_file=import_ldif1, args={TASK_WAIT: True}) == 0 |
| 363 |
assert Tasks(s2).importLDIF(DEFAULT_SUFFIX, input_file=import_ldif2, args={TASK_WAIT: True}) == 0 |
| 364 |
# 3. Remove a conflict entry |
| 365 |
ConflictEntry(s1, 'cn=u22449+nsUniqueId=c6654281-f11b11ee-ad93a02d-7ba2db25,dc=example,dc=com').delete() |
| 366 |
# 4. Wait until le replication is in sync |
| 367 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 368 |
repl.wait_for_replication(s1, s2) |
| 369 |
# 5. Check that entries have the same type (tombstone/conflict/regular) on both replica. |
| 370 |
u1 = search_entries(s1, 'cn=u22449') |
| 371 |
u2 = search_entries(s2, 'cn=u22449') |
| 372 |
assert u1 == u2 |
| 373 |
# 6. import the ldif files |
| 374 |
Tasks(s1).importLDIF(DEFAULT_SUFFIX, input_file=import_ldif1, args={TASK_WAIT: True}) |
| 375 |
Tasks(s2).importLDIF(DEFAULT_SUFFIX, input_file=import_ldif2, args={TASK_WAIT: True}) |
| 376 |
# 7. Remove a regular entry |
| 377 |
UserAccount(s2, 'cn=u22449,dc=example,dc=com').delete() |
| 378 |
# 8. Wait until le replication is in sync |
| 379 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 380 |
repl.wait_for_replication(s1, s2) |
| 381 |
# 9. Check that entries have the same type (tombstone/conflict/regular) on both replica. |
| 382 |
u1 = search_entries(s1, 'cn=u22449') |
| 383 |
u2 = search_entries(s2, 'cn=u22449') |
| 384 |
assert u1 == u2 |
| 385 |
|
| 386 |
|
| 387 |
if __name__ == "__main__": |
| 388 |
# Run isolated |
| 389 |
# -s for DEBUG mode |
| 390 |
CURRENT_FILE = os.path.realpath(__file__) |
| 391 |
pytest.main("-s %s" % CURRENT_FILE) |