| 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 |
"""Test replication deadlock scenarios |
| 10 |
|
| 11 |
This test covers deadlock scenarios that can occur after importing LDIF |
| 12 |
with replication data when replication agreements are present. |
| 13 |
""" |
| 14 |
|
| 15 |
import logging |
| 16 |
import os |
| 17 |
import pytest |
| 18 |
import ldap |
| 19 |
|
| 20 |
from lib389._mapped_object import DSLdapObjects |
| 21 |
from test389.topologies import topology_m2 as topo |
| 22 |
from test389.topologies import topology_st |
| 23 |
from lib389.replica import Replicas, ReplicationManager |
| 24 |
from lib389.agreement import Agreements |
| 25 |
from lib389.tasks import ImportTask, ExportTask |
| 26 |
from lib389.idm.user import UserAccounts |
| 27 |
from lib389.tombstone import Tombstones |
| 28 |
from lib389.backend import Backends |
| 29 |
from lib389._constants import (DEFAULT_SUFFIX, DEFAULT_BENAME, defaultProperties, |
| 30 |
REPLICATION_BIND_DN, REPLICATION_BIND_PW, |
| 31 |
REPLICATION_BIND_METHOD, REPLICATION_TRANSPORT) |
| 32 |
|
| 33 |
pytestmark = pytest.mark.tier2 |
| 34 |
|
| 35 |
DEBUGGING = os.getenv("DEBUGGING", default=False) |
| 36 |
if DEBUGGING: |
| 37 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 38 |
else: |
| 39 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 40 |
log = logging.getLogger(__name__) |
| 41 |
|
| 42 |
|
| 43 |
def test_replication_deadlock_import_invalid_agreement(topo): |
| 44 |
"""Test deadlock scenario during LDIF import with invalid replication agreement |
| 45 |
|
| 46 |
This test verifies that importing LDIF data while replication agreements |
| 47 |
are paused doesn't cause deadlocks. |
| 48 |
|
| 49 |
:id: b8c47d4a-b189-4e70-9070-f6281309cf92 |
| 50 |
:setup: Two supplier replication setup |
| 51 |
:steps: |
| 52 |
1. Create test entries on supplier1 |
| 53 |
2. Pause replication agreement |
| 54 |
3. Export LDIF with replication data |
| 55 |
4. Import LDIF during paused replication |
| 56 |
5. Resume replication agreement |
| 57 |
6. Verify entries exist |
| 58 |
:expectedresults: |
| 59 |
1. Test entries created successfully |
| 60 |
2. Replication agreement paused successfully |
| 61 |
3. LDIF export completes successfully |
| 62 |
4. LDIF import completes without deadlock |
| 63 |
5. Replication agreement resumed successfully |
| 64 |
6. Entries exist after import |
| 65 |
""" |
| 66 |
|
| 67 |
log.info('Testing replication deadlock scenario during LDIF import') |
| 68 |
|
| 69 |
export_ldif = os.path.join(topo.ms["supplier1"].get_ldif_dir(), 'replication_export.ldif') |
| 70 |
|
| 71 |
log.info('Adding test entries to supplier1') |
| 72 |
users = UserAccounts(topo.ms["supplier1"], DEFAULT_SUFFIX) |
| 73 |
|
| 74 |
user1 = users.create(properties={ |
| 75 |
'uid': 'testuser1', |
| 76 |
'cn': 'Test User 1', |
| 77 |
'sn': 'User', |
| 78 |
'userpassword': 'password', |
| 79 |
'uidNumber': '1001', |
| 80 |
'gidNumber': '2001', |
| 81 |
'homeDirectory': '/home/testuser1' |
| 82 |
}) |
| 83 |
|
| 84 |
user2 = users.create(properties={ |
| 85 |
'uid': 'testuser2', |
| 86 |
'cn': 'Test User 2', |
| 87 |
'sn': 'User', |
| 88 |
'userpassword': 'password', |
| 89 |
'uidNumber': '1002', |
| 90 |
'gidNumber': '2002', |
| 91 |
'homeDirectory': '/home/testuser2' |
| 92 |
}) |
| 93 |
|
| 94 |
log.info('Waiting for replication to sync') |
| 95 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 96 |
repl.wait_for_replication(topo.ms["supplier1"], topo.ms["supplier2"]) |
| 97 |
|
| 98 |
log.info('Pausing replication agreement') |
| 99 |
replicas = Replicas(topo.ms["supplier1"]) |
| 100 |
replica = replicas.get(DEFAULT_SUFFIX) |
| 101 |
agreements = Agreements(topo.ms["supplier1"], basedn=replica.dn) |
| 102 |
agmt = agreements.list()[0] |
| 103 |
agmt.pause() |
| 104 |
|
| 105 |
log.info('Creating LDIF export') |
| 106 |
export_task = ExportTask(topo.ms["supplier1"]) |
| 107 |
export_task.export_suffix_to_ldif(ldiffile=export_ldif, suffix=DEFAULT_SUFFIX) |
| 108 |
export_task.wait() |
| 109 |
|
| 110 |
log.info('Importing LDIF during paused replication') |
| 111 |
import_task = ImportTask(topo.ms["supplier1"]) |
| 112 |
import_task.import_suffix_from_ldif(ldiffile=export_ldif, suffix=DEFAULT_SUFFIX) |
| 113 |
import_task.wait() |
| 114 |
|
| 115 |
log.info('Resuming replication agreement') |
| 116 |
agmt.resume() |
| 117 |
|
| 118 |
log.info('Verifying entries exist after import') |
| 119 |
assert user1.exists() |
| 120 |
assert user2.exists() |
| 121 |
|
| 122 |
log.info('Cleaning up test entries') |
| 123 |
user1.delete() |
| 124 |
user2.delete() |
| 125 |
|
| 126 |
if os.path.exists(export_ldif): |
| 127 |
os.remove(export_ldif) |
| 128 |
|
| 129 |
log.info('Test completed successfully - no deadlock detected') |
| 130 |
|
| 131 |
|
| 132 |
def test_replication_deadlock_tombstone_search(topo): |
| 133 |
"""Test deadlock scenario during tombstone search with replication |
| 134 |
|
| 135 |
This test verifies that searching for tombstones while replication |
| 136 |
agreements are paused doesn't cause deadlocks. |
| 137 |
|
| 138 |
:id: c9d58e5b-c298-4f81-a181-f7392410df03 |
| 139 |
:setup: Two supplier replication setup |
| 140 |
:steps: |
| 141 |
1. Create and delete entries to generate tombstones |
| 142 |
2. Pause replication agreement |
| 143 |
3. Export LDIF with tombstone data |
| 144 |
4. Import LDIF during paused replication |
| 145 |
5. Search for tombstone entries |
| 146 |
6. Resume replication agreement |
| 147 |
:expectedresults: |
| 148 |
1. Tombstone entries created successfully |
| 149 |
2. Replication agreement paused successfully |
| 150 |
3. LDIF export completes successfully |
| 151 |
4. LDIF import completes without deadlock |
| 152 |
5. Tombstone search completes without deadlock |
| 153 |
6. Replication agreement resumed successfully |
| 154 |
""" |
| 155 |
|
| 156 |
|
| 157 |
export_ldif = os.path.join(topo.ms["supplier1"].get_ldif_dir(), 'tombstone_export.ldif') |
| 158 |
|
| 159 |
log.info('Creating and deleting entries to generate tombstones') |
| 160 |
users = UserAccounts(topo.ms["supplier1"], DEFAULT_SUFFIX) |
| 161 |
|
| 162 |
user = users.create(properties={ |
| 163 |
'uid': 'testuser_tombstone', |
| 164 |
'cn': 'Test User Tombstone', |
| 165 |
'sn': 'User', |
| 166 |
'userpassword': 'password', |
| 167 |
'uidNumber': '1003', |
| 168 |
'gidNumber': '2003', |
| 169 |
'homeDirectory': '/home/testuser_tombstone' |
| 170 |
}) |
| 171 |
|
| 172 |
user.delete() |
| 173 |
|
| 174 |
log.info('Pausing replication agreement') |
| 175 |
replicas = Replicas(topo.ms["supplier1"]) |
| 176 |
replica = replicas.get(DEFAULT_SUFFIX) |
| 177 |
agreements = Agreements(topo.ms["supplier1"], basedn=replica.dn) |
| 178 |
agmt = agreements.list()[0] |
| 179 |
agmt.pause() |
| 180 |
|
| 181 |
log.info('Creating LDIF export with tombstones') |
| 182 |
export_task = ExportTask(topo.ms["supplier1"]) |
| 183 |
export_task.export_suffix_to_ldif(ldiffile=export_ldif, suffix=DEFAULT_SUFFIX) |
| 184 |
export_task.wait() |
| 185 |
|
| 186 |
log.info('Importing LDIF during paused replication') |
| 187 |
import_task = ImportTask(topo.ms["supplier1"]) |
| 188 |
import_task.import_suffix_from_ldif(ldiffile=export_ldif, suffix=DEFAULT_SUFFIX) |
| 189 |
import_task.wait() |
| 190 |
|
| 191 |
log.info('Searching for tombstone entries') |
| 192 |
try: |
| 193 |
tombstones = Tombstones(topo.ms["supplier1"], DEFAULT_SUFFIX) |
| 194 |
tombstone_entries = tombstones.list() |
| 195 |
log.info(f'Found {len(tombstone_entries)} tombstone entries') |
| 196 |
assert isinstance(tombstone_entries, list), "Search should return a list" |
| 197 |
|
| 198 |
except Exception as e: |
| 199 |
log.error(f"Search failed with error: {e}") |
| 200 |
pytest.fail(f"Search failed: {e}") |
| 201 |
|
| 202 |
log.info('Resuming replication agreement') |
| 203 |
agmt.resume() |
| 204 |
|
| 205 |
if os.path.exists(export_ldif): |
| 206 |
os.remove(export_ldif) |
| 207 |
|
| 208 |
|
| 209 |
def test_replication_deadlock_tombstone_invalid_agreement(topology_st): |
| 210 |
"""Test deadlock scenario after importing LDIF with replication data and invalid agreement |
| 211 |
|
| 212 |
This test verifies that a deadlock does not occur while searching for tombstones after |
| 213 |
setting up an invalid replication agreement (pointing to a non-existent server) |
| 214 |
|
| 215 |
:id: a1b2c3d4-e5f6-4781-9abc-def012345678 |
| 216 |
:setup: Standalone instance with replication enabled |
| 217 |
:steps: |
| 218 |
1. Create a supplier with an invalid replication agreement (port 5555, non-existent server) |
| 219 |
2. Add two test user entries |
| 220 |
3. Export LDIF with replication data |
| 221 |
4. Restart the server |
| 222 |
5. Import the LDIF back |
| 223 |
6. Search for tombstone entries with timeout (should not hang/deadlock) |
| 224 |
7. Cleanup: restore original timeout settings, delete invalid agreement, test entries, and export file |
| 225 |
:expectedresults: |
| 226 |
1. Invalid replication agreement is created successfully |
| 227 |
2. Test entries are created successfully |
| 228 |
3. LDIF export completes successfully |
| 229 |
4. Server restarts successfully |
| 230 |
5. LDIF import completes successfully |
| 231 |
6. Tombstone search completes without deadlock (returns at least one entry; no hang/timeout) |
| 232 |
7. Cleanup completes successfully |
| 233 |
""" |
| 234 |
|
| 235 |
standalone = topology_st.standalone |
| 236 |
export_ldif = os.path.join(standalone.get_ldif_dir(), 'export.ldif') |
| 237 |
|
| 238 |
# Step 1: Create supplier with invalid replication agreement |
| 239 |
log.info('Creating supplier with invalid replication agreement (port 5555 - non-existent server)') |
| 240 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 241 |
repl.create_first_supplier(standalone) |
| 242 |
|
| 243 |
replicas = Replicas(standalone) |
| 244 |
replica = replicas.get(DEFAULT_SUFFIX) |
| 245 |
agreements = Agreements(standalone, basedn=replica.dn) |
| 246 |
|
| 247 |
# Create agreement with invalid port (non-existent server) |
| 248 |
invalid_agreement = agreements.create(properties={ |
| 249 |
'cn': r'meTo_$host:$port', |
| 250 |
'nsDS5ReplicaRoot': DEFAULT_SUFFIX, |
| 251 |
'nsDS5ReplicaHost': standalone.host, |
| 252 |
'nsDS5ReplicaPort': '5555', # Invalid port - server does not exist |
| 253 |
'nsDS5ReplicaBindDN': defaultProperties[REPLICATION_BIND_DN], |
| 254 |
'nsDS5ReplicaBindMethod': defaultProperties[REPLICATION_BIND_METHOD], |
| 255 |
'nsDS5ReplicaTransportInfo': defaultProperties[REPLICATION_TRANSPORT], |
| 256 |
'nsDS5ReplicaCredentials': defaultProperties[REPLICATION_BIND_PW] |
| 257 |
}) |
| 258 |
|
| 259 |
# Step 2: Add two test entries |
| 260 |
log.info('Adding two test entries') |
| 261 |
users = UserAccounts(standalone, DEFAULT_SUFFIX) |
| 262 |
|
| 263 |
entry1 = users.create(properties={ |
| 264 |
'uid': 'entry1', |
| 265 |
'cn': 'entry1', |
| 266 |
'sn': 'user', |
| 267 |
'userpassword': 'password', |
| 268 |
'uidNumber': '1001', |
| 269 |
'gidNumber': '2001', |
| 270 |
'homeDirectory': '/home/entry1' |
| 271 |
}) |
| 272 |
|
| 273 |
entry2 = users.create(properties={ |
| 274 |
'uid': 'entry2', |
| 275 |
'cn': 'entry2', |
| 276 |
'sn': 'user', |
| 277 |
'userpassword': 'password', |
| 278 |
'uidNumber': '1002', |
| 279 |
'gidNumber': '2002', |
| 280 |
'homeDirectory': '/home/entry2' |
| 281 |
}) |
| 282 |
|
| 283 |
# Step 3: Export LDIF with replication data |
| 284 |
log.info('Exporting LDIF with replication data') |
| 285 |
backends = Backends(standalone) |
| 286 |
export_task = backends.export_ldif(be_names=DEFAULT_BENAME, ldif=export_ldif, replication=True) |
| 287 |
export_task.wait() |
| 288 |
|
| 289 |
# Step 4: Restart the server |
| 290 |
log.info('Restarting server') |
| 291 |
standalone.restart() |
| 292 |
|
| 293 |
# Step 5: Import the LDIF |
| 294 |
log.info('Importing replication LDIF file') |
| 295 |
import_task = ImportTask(standalone) |
| 296 |
import_task.import_suffix_from_ldif(ldiffile=export_ldif, suffix=DEFAULT_SUFFIX) |
| 297 |
import_task.wait() |
| 298 |
|
| 299 |
# Step 6: Search for tombstones with timeout - should not hang/deadlock |
| 300 |
log.info('Searching for tombstone entries (should find entries and not hang)') |
| 301 |
# Save original timeout settings so we can restore them in cleanup |
| 302 |
orig_network_timeout = standalone.get_option(ldap.OPT_NETWORK_TIMEOUT) |
| 303 |
orig_timeout = standalone.get_option(ldap.OPT_TIMEOUT) |
| 304 |
# Set explicit timeouts to detect deadlocks |
| 305 |
standalone.set_option(ldap.OPT_NETWORK_TIMEOUT, 5) |
| 306 |
standalone.set_option(ldap.OPT_TIMEOUT, 5) |
| 307 |
|
| 308 |
# Verify at least one tombstone exists and the search does not hang/timeout |
| 309 |
try: |
| 310 |
results = DSLdapObjects(standalone, DEFAULT_SUFFIX).filter('(objectclass=nsTombstone)') |
| 311 |
log.info(f'Found {len(results)} tombstone entries') |
| 312 |
assert len(results) > 0, "Tombstone search should return at least one entry" |
| 313 |
except Exception as e: |
| 314 |
log.error(f"Tombstone search failed with error: {e}") |
| 315 |
pytest.fail(f"Tombstone search failed: {e}") |
| 316 |
finally: |
| 317 |
# Restore original timeout settings |
| 318 |
standalone.set_option(ldap.OPT_NETWORK_TIMEOUT, orig_network_timeout) |
| 319 |
standalone.set_option(ldap.OPT_TIMEOUT, orig_timeout) |
| 320 |
# Cleanup test entries and export file |
| 321 |
log.info('Cleaning up test entries') |
| 322 |
invalid_agreement.delete() |
| 323 |
entry1.delete() |
| 324 |
entry2.delete() |
| 325 |
if os.path.exists(export_ldif): |
| 326 |
os.remove(export_ldif) |
| 327 |
|
| 328 |
log.info('Test completed successfully - no deadlock detected') |
| 329 |
|
| 330 |
if __name__ == '__main__': |
| 331 |
CURRENT_FILE = os.path.realpath(__file__) |
| 332 |
pytest.main(["-s", CURRENT_FILE]) |