| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2017 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 os |
| 10 |
import logging |
| 11 |
import ldap |
| 12 |
import pytest |
| 13 |
import time |
| 14 |
from ldif import LDIFParser |
| 15 |
from lib389.cli_base import LogCapture |
| 16 |
from lib389.dbgen import dbgen_users |
| 17 |
from lib389.replica import Replicas, ReplicationManager, Agreements |
| 18 |
from lib389.backend import Backends |
| 19 |
from lib389.idm.domain import Domain |
| 20 |
from lib389.idm.user import UserAccounts, nsUserAccounts |
| 21 |
from lib389.idm.group import Groups |
| 22 |
from lib389.tasks import ImportTask |
| 23 |
from test389.topologies import create_topology, topology_m3 |
| 24 |
from lib389._constants import * |
| 25 |
from lib389.plugins import MemberOfPlugin |
| 26 |
|
| 27 |
pytestmark = pytest.mark.tier1 |
| 28 |
|
| 29 |
TEST_ENTRY_NAME = 'rep2lusr' |
| 30 |
NEW_RDN_NAME = 'ruvusr' |
| 31 |
ATTRIBUTES = ['objectClass', 'nsUniqueId', 'nsds50ruv', 'nsruvReplicaLastModified'] |
| 32 |
USER_PROPERTIES = { |
| 33 |
'uid': TEST_ENTRY_NAME, |
| 34 |
'cn': TEST_ENTRY_NAME, |
| 35 |
'sn': TEST_ENTRY_NAME, |
| 36 |
'uidNumber': '1001', |
| 37 |
'gidNumber': '2001', |
| 38 |
'userpassword': PASSWORD, |
| 39 |
'description': 'userdesc', |
| 40 |
'homeDirectory': '/home/testuser' |
| 41 |
} |
| 42 |
|
| 43 |
DEBUGGING = os.getenv('DEBUGGING', default=False) |
| 44 |
if DEBUGGING: |
| 45 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 46 |
else: |
| 47 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 48 |
log = logging.getLogger(__name__) |
| 49 |
|
| 50 |
|
| 51 |
@pytest.fixture(scope="function") |
| 52 |
def topo(request): |
| 53 |
"""Create Replication Deployment with two suppliers""" |
| 54 |
|
| 55 |
topology = create_topology({ReplicaRole.SUPPLIER: 2}, request=request) |
| 56 |
|
| 57 |
topology.logcap = LogCapture() |
| 58 |
return topology |
| 59 |
|
| 60 |
|
| 61 |
class MyLDIF(LDIFParser): |
| 62 |
def __init__(self, input): |
| 63 |
LDIFParser.__init__(self, input) |
| 64 |
|
| 65 |
def handle(self, dn, entry): |
| 66 |
if 'nsuniqueid=' + REPLICA_RUV_UUID in dn: |
| 67 |
for attr in ATTRIBUTES: |
| 68 |
assert entry.get(attr), 'Failed to find attribute: {}'.format(attr) |
| 69 |
log.info('Attribute found in RUV: {}'.format(attr)) |
| 70 |
|
| 71 |
|
| 72 |
def _perform_ldap_operations(topo): |
| 73 |
"""Add a test user, modify description, modrdn user and delete it""" |
| 74 |
|
| 75 |
users = UserAccounts(topo.ms['supplier1'], DEFAULT_SUFFIX) |
| 76 |
log.info('Adding user to supplier1') |
| 77 |
tuser = users.create(properties=USER_PROPERTIES) |
| 78 |
tuser.replace('description', 'newdesc') |
| 79 |
log.info('Modify RDN of user: {}'.format(tuser.dn)) |
| 80 |
try: |
| 81 |
topo.ms['supplier1'].modrdn_s(tuser.dn, 'uid={}'.format(NEW_RDN_NAME), 0) |
| 82 |
except ldap.LDAPError as e: |
| 83 |
log.fatal('Failed to modrdn entry: {}'.format(tuser.dn)) |
| 84 |
raise e |
| 85 |
tuser = users.get(NEW_RDN_NAME) |
| 86 |
log.info('Deleting user: {}'.format(tuser.dn)) |
| 87 |
tuser.delete() |
| 88 |
|
| 89 |
|
| 90 |
def _compare_memoryruv_and_databaseruv(topo, operation_type): |
| 91 |
"""Compare the memoryruv and databaseruv for ldap operations""" |
| 92 |
|
| 93 |
log.info('Checking memory ruv for ldap: {} operation'.format(operation_type)) |
| 94 |
replicas = Replicas(topo.ms['supplier1']) |
| 95 |
replica = replicas.list()[0] |
| 96 |
memory_ruv = replica.get_attr_val_utf8('nsds50ruv') |
| 97 |
|
| 98 |
log.info('Checking database ruv for ldap: {} operation'.format(operation_type)) |
| 99 |
entry = replicas.get_ruv_entry(DEFAULT_SUFFIX) |
| 100 |
database_ruv = entry.getValues('nsds50ruv')[0] |
| 101 |
assert memory_ruv == database_ruv |
| 102 |
|
| 103 |
|
| 104 |
def test_ruv_entry_backup(topo): |
| 105 |
"""Check if db2ldif stores the RUV details in the backup file |
| 106 |
|
| 107 |
:id: cbe2c473-8578-4caf-ac0a-841140e41e66 |
| 108 |
:setup: Replication with two suppliers. |
| 109 |
:steps: 1. Add user to server. |
| 110 |
2. Perform ldap modify, modrdn and delete operations. |
| 111 |
3. Stop the server and backup the database using db2ldif task. |
| 112 |
4. Start the server and check if correct RUV is stored in the backup file. |
| 113 |
:expectedresults: |
| 114 |
1. Add user should PASS. |
| 115 |
2. Ldap operations should PASS. |
| 116 |
3. Database backup using db2ldif task should PASS. |
| 117 |
4. Backup file should contain the correct RUV details. |
| 118 |
""" |
| 119 |
|
| 120 |
log.info('LDAP operations add, modify, modrdn and delete') |
| 121 |
_perform_ldap_operations(topo) |
| 122 |
|
| 123 |
output_file = os.path.join(topo.ms['supplier1'].get_ldif_dir(), 'supplier1.ldif') |
| 124 |
log.info('Stopping the server instance to run db2ldif task to create backup file') |
| 125 |
topo.ms['supplier1'].stop() |
| 126 |
topo.ms['supplier1'].db2ldif(bename=DEFAULT_BENAME, suffixes=[DEFAULT_SUFFIX], excludeSuffixes=[], |
| 127 |
encrypt=False, repl_data=True, outputfile=output_file) |
| 128 |
log.info('Starting the server after backup') |
| 129 |
topo.ms['supplier1'].start() |
| 130 |
|
| 131 |
log.info('Checking if backup file contains RUV and required attributes') |
| 132 |
with open(output_file, 'r') as ldif_file: |
| 133 |
parser = MyLDIF(ldif_file) |
| 134 |
parser.parse() |
| 135 |
|
| 136 |
|
| 137 |
@pytest.mark.xfail(reason="No method to safety access DB ruv currently exists online.") |
| 138 |
def test_memoryruv_sync_with_databaseruv(topo): |
| 139 |
"""Check if memory ruv and database ruv are synced |
| 140 |
|
| 141 |
:id: 5f38ac5f-6353-460d-bf60-49cafffda5b3 |
| 142 |
:setup: Replication with two suppliers. |
| 143 |
:steps: 1. Add user to server and compare memory ruv and database ruv. |
| 144 |
2. Modify description of user and compare memory ruv and database ruv. |
| 145 |
3. Modrdn of user and compare memory ruv and database ruv. |
| 146 |
4. Delete user and compare memory ruv and database ruv. |
| 147 |
:expectedresults: |
| 148 |
1. For add user, the memory ruv and database ruv should be the same. |
| 149 |
2. For modify operation, the memory ruv and database ruv should be the same. |
| 150 |
3. For modrdn operation, the memory ruv and database ruv should be the same. |
| 151 |
4. For delete operation, the memory ruv and database ruv should be the same. |
| 152 |
""" |
| 153 |
|
| 154 |
log.info('Adding user: {} to supplier1'.format(TEST_ENTRY_NAME)) |
| 155 |
users = UserAccounts(topo.ms['supplier1'], DEFAULT_SUFFIX) |
| 156 |
tuser = users.create(properties=USER_PROPERTIES) |
| 157 |
_compare_memoryruv_and_databaseruv(topo, 'add') |
| 158 |
|
| 159 |
log.info('Modify user: {} description'.format(TEST_ENTRY_NAME)) |
| 160 |
tuser.replace('description', 'newdesc') |
| 161 |
_compare_memoryruv_and_databaseruv(topo, 'modify') |
| 162 |
|
| 163 |
log.info('Modify RDN of user: {}'.format(tuser.dn)) |
| 164 |
try: |
| 165 |
topo.ms['supplier1'].modrdn_s(tuser.dn, 'uid={}'.format(NEW_RDN_NAME), 0) |
| 166 |
except ldap.LDAPError as e: |
| 167 |
log.fatal('Failed to modrdn entry: {}'.format(tuser.dn)) |
| 168 |
raise e |
| 169 |
_compare_memoryruv_and_databaseruv(topo, 'modrdn') |
| 170 |
|
| 171 |
tuser = users.get(NEW_RDN_NAME) |
| 172 |
log.info('Delete user: {}'.format(tuser.dn)) |
| 173 |
tuser.delete() |
| 174 |
_compare_memoryruv_and_databaseruv(topo, 'delete') |
| 175 |
|
| 176 |
|
| 177 |
def test_ruv_after_reindex(topo): |
| 178 |
"""Test that the tombstone RUV entry is not corrupted after a reindex task |
| 179 |
|
| 180 |
:id: 988c0fab-1905-4dc5-a45d-fbf195843a33 |
| 181 |
:setup: 2 suppliers |
| 182 |
:steps: |
| 183 |
1. Reindex database |
| 184 |
2. Perform some updates |
| 185 |
3. Check error log does not have "_entryrdn_insert_key" errors |
| 186 |
:expectedresults: |
| 187 |
1. Success |
| 188 |
2. Success |
| 189 |
3. Success |
| 190 |
""" |
| 191 |
|
| 192 |
inst = topo.ms['supplier1'] |
| 193 |
suffix = Domain(inst, "ou=people," + DEFAULT_SUFFIX) |
| 194 |
backends = Backends(inst) |
| 195 |
backend = backends.get(DEFAULT_BENAME) |
| 196 |
|
| 197 |
# Reindex nsuniqueid |
| 198 |
backend.reindex(attrs=['nsuniqueid'], wait=True) |
| 199 |
|
| 200 |
# Do some updates |
| 201 |
for idx in range(0, 5): |
| 202 |
suffix.replace('description', str(idx)) |
| 203 |
|
| 204 |
# Check error log for RUV entryrdn errors. Stopping instance forces RUV |
| 205 |
# to be written and quickly exposes the error |
| 206 |
inst.stop() |
| 207 |
assert not inst.searchErrorsLog("entryrdn_insert_key") |
| 208 |
|
| 209 |
|
| 210 |
@pytest.mark.xfail(reason='https://github.com/389ds/389-ds-base/issues/1317') |
| 211 |
def test_ruv_after_import(topo): |
| 212 |
"""Test the RUV behavior after an LDIF import operation. |
| 213 |
|
| 214 |
:id: 6843ab56-0291-425c-954b-3002b8352025 |
| 215 |
:setup: 2 suppliers |
| 216 |
:steps: |
| 217 |
1. Export LDIF from supplier 1. |
| 218 |
2. Create 1000 test users in supplier 1. |
| 219 |
3. Wait for replication to complete from supplier 1 to supplier 2. |
| 220 |
4. Pause all replicas. |
| 221 |
5. Import LDIF back to supplier 1. |
| 222 |
6. Resume all replicas. |
| 223 |
7. Perform attribute updates. |
| 224 |
:expectedresults: |
| 225 |
1. LDIF export should complete successfully. |
| 226 |
2. Test users should be created successfully. |
| 227 |
3. Replication should complete successfully. |
| 228 |
4. All replicas should be paused. |
| 229 |
5. LDIF import should complete successfully. |
| 230 |
6. All replicas should be resumed. |
| 231 |
7. Attribute updates should complete successfully. |
| 232 |
""" |
| 233 |
|
| 234 |
log.info('Getting supplier instances') |
| 235 |
s1 = topo.ms['supplier1'] |
| 236 |
s2 = topo.ms['supplier2'] |
| 237 |
|
| 238 |
log.info('Performing LDIF export on supplier 1') |
| 239 |
ldif_dir = s1.get_ldif_dir() |
| 240 |
export_ldif = ldif_dir + '/export.ldif' |
| 241 |
export_task = Backends(s1).export_ldif(be_names=DEFAULT_BENAME, ldif=export_ldif, replication=True) |
| 242 |
export_task.wait() |
| 243 |
|
| 244 |
log.info('Creating 1000 test users on supplier 1') |
| 245 |
users = UserAccounts(s1, DEFAULT_SUFFIX) |
| 246 |
for idx in range(0, 1000): |
| 247 |
users.create_test_user(uid=idx) |
| 248 |
|
| 249 |
log.info('Waiting for replication to complete') |
| 250 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 251 |
repl.wait_for_replication(s2, s1) |
| 252 |
|
| 253 |
log.info('Performing LDIF import on supplier 1') |
| 254 |
r = Backends(s1).get(DEFAULT_BENAME).import_ldif([export_ldif]) |
| 255 |
s2.stop() |
| 256 |
r.wait() |
| 257 |
|
| 258 |
s2.start() |
| 259 |
|
| 260 |
log.info('Performing attribute updates') |
| 261 |
suffix = Domain(s1, "ou=people," + DEFAULT_SUFFIX) |
| 262 |
for idx in range(0, 5): |
| 263 |
suffix.replace('description', str(idx)) |
| 264 |
|
| 265 |
repl.wait_for_replication(s1, s2) |
| 266 |
repl.wait_for_replication(s2, s1) |
| 267 |
|
| 268 |
|
| 269 |
def check_membership(user, group_dn, find_result=True): |
| 270 |
"""Check if a user has memberOf attribute for the specified group""" |
| 271 |
memberof_values = user.get_attr_vals_utf8_l('memberof') |
| 272 |
print(user.get_all_attrs_utf8()) |
| 273 |
print('\n') |
| 274 |
found = group_dn.lower() in memberof_values |
| 275 |
|
| 276 |
if find_result: |
| 277 |
assert found, f"User {user.dn} should be a member of {group_dn}" |
| 278 |
else: |
| 279 |
assert not found, f"User {user.dn} should NOT be a member of {group_dn}" |
| 280 |
|
| 281 |
|
| 282 |
def test_ruv_after_aborted_plugin_operation(topology_m3): |
| 283 |
"""Test that RUV does not advance after an aborted plugin operation |
| 284 |
|
| 285 |
:id: c439f41e-8ddf-49e6-b449-ef9437e1c97f |
| 286 |
:setup: Replication topology with three suppliers (A <==> B <==> C). |
| 287 |
:steps: |
| 288 |
1. Configure linear replication topology by pausing direct agreements between A and C. |
| 289 |
2. Enable memberOf plugin on supplier B with incompatible objectclass configuration. |
| 290 |
3. Create test user and group, wait for replication. |
| 291 |
4. Capture baseline RUV value on supplier B. |
| 292 |
5. Attempt to add user to group. |
| 293 |
6. Check RUV value on supplier B after the failed operation. |
| 294 |
:expectedresults: |
| 295 |
1. Linear topology configuration should succeed. |
| 296 |
2. MemberOf plugin should be enabled with incompatible configuration. |
| 297 |
3. Test user and group should be created and replicated successfully. |
| 298 |
4. Baseline RUV should be captured successfully. |
| 299 |
5. Group membership operation should fail/abort on B due to plugin error. |
| 300 |
6. RUV should remain unchanged after the failed operation. |
| 301 |
""" |
| 302 |
# Get supplier instances |
| 303 |
A = topology_m3.ms["supplier1"] |
| 304 |
B = topology_m3.ms["supplier2"] |
| 305 |
C = topology_m3.ms["supplier3"] |
| 306 |
|
| 307 |
# Enable replication logging for debugging |
| 308 |
for supplier in [A, B, C]: |
| 309 |
supplier.config.loglevel([ErrorLog.REPLICA]) |
| 310 |
|
| 311 |
log.info("Configuring linear replication topology A <==> B <==> C") |
| 312 |
agmtsA = Agreements(A) |
| 313 |
agmtsC = Agreements(C) |
| 314 |
|
| 315 |
# Find agreements to pause (A <-> C direct connections) |
| 316 |
for agmt in agmtsA.list(): |
| 317 |
if C.serverid in agmt.get_attr_val_utf8('nsDS5ReplicaHost'): |
| 318 |
log.info(f"Pausing agreement: {agmt.dn}") |
| 319 |
agmt.pause() |
| 320 |
|
| 321 |
for agmt in agmtsC.list(): |
| 322 |
if A.serverid in agmt.get_attr_val_utf8('nsDS5ReplicaHost'): |
| 323 |
log.info(f"Pausing agreement: {agmt.dn}") |
| 324 |
agmt.pause() |
| 325 |
|
| 326 |
log.info("Configuring memberOf plugin on supplier B with incompatible objectclass") |
| 327 |
memberof_plugin = MemberOfPlugin(B) |
| 328 |
memberof_plugin.add('memberOfEntryScope', DEFAULT_SUFFIX) |
| 329 |
memberof_plugin.set_autoaddoc('referral') |
| 330 |
memberof_plugin.enable() |
| 331 |
B.restart() |
| 332 |
|
| 333 |
# Waiting for first keepalive to pass as it changes the RUV |
| 334 |
time.sleep(31) |
| 335 |
|
| 336 |
log.info("Creating test user") |
| 337 |
user_props = { |
| 338 |
'uid': 'testuser_not_memberof', |
| 339 |
'cn': 'testuser_not_memberof', |
| 340 |
'displayName': 'testuser_not_memberof', |
| 341 |
'uidNumber': '2', |
| 342 |
'gidNumber': '1002', |
| 343 |
'homeDirectory': '/home/testuser_not_memberof', |
| 344 |
} |
| 345 |
user = nsUserAccounts(A, DEFAULT_SUFFIX).create(properties=user_props) |
| 346 |
|
| 347 |
# Create a successful group first to establish baseline |
| 348 |
log.info("Creating group with compatible members") |
| 349 |
group_props = { |
| 350 |
'cn': 'group', |
| 351 |
'description': 'Test group', |
| 352 |
} |
| 353 |
group = Groups(A, DEFAULT_SUFFIX).create(properties=group_props) |
| 354 |
|
| 355 |
log.info("Waiting for replication to complete") |
| 356 |
# using sleep since wait_for_replication also changes RUV |
| 357 |
time.sleep(10) |
| 358 |
|
| 359 |
log.info("Getting baseline RUV value on supplier B before failing operation") |
| 360 |
replicas2 = Replicas(B) |
| 361 |
replica2 = replicas2.get(DEFAULT_SUFFIX) |
| 362 |
ruv_elements = replica2.get_attr_vals_utf8('nsds50ruv') |
| 363 |
|
| 364 |
# Find the RUV element for replica 2 (B) |
| 365 |
ruv_before = None |
| 366 |
for ruv in ruv_elements: |
| 367 |
if 'replica 2' in ruv: |
| 368 |
ruv_before = ruv |
| 369 |
break |
| 370 |
|
| 371 |
assert ruv_before is not None, "Could not find replica 2 RUV element" |
| 372 |
log.info(f'RUV before failing operation: {ruv_before}') |
| 373 |
|
| 374 |
group.add_member(user.dn) |
| 375 |
|
| 376 |
log.info("Waiting for replication to complete") |
| 377 |
# using sleep since wait_for_replication also changes RUV |
| 378 |
time.sleep(10) |
| 379 |
|
| 380 |
log.info("Checking RUV on supplier B after potentially failing operation") |
| 381 |
# Get RUV after the failing operation |
| 382 |
log.info(replica2.get_attr_vals_utf8('nsds50ruv')) |
| 383 |
ruv_elements_after = replica2.get_attr_vals_utf8('nsds50ruv') |
| 384 |
ruv_after = None |
| 385 |
for ruv in ruv_elements_after: |
| 386 |
if 'replica 2' in ruv: |
| 387 |
ruv_after = ruv |
| 388 |
break |
| 389 |
assert ruv_after is not None, "Could not find replica 2 RUV element after operation" |
| 390 |
log.info(f'RUV after failing operation: {ruv_after}') |
| 391 |
|
| 392 |
# The key assertion: RUV should not have advanced due to the failed memberOf operation |
| 393 |
assert ruv_before == ruv_after, f"RUV advanced after failed operation: before={ruv_before}, after={ruv_after}" |
| 394 |
|
| 395 |
|
| 396 |
if __name__ == '__main__': |
| 397 |
# Run isolated |
| 398 |
# -s for DEBUG mode |
| 399 |
CURRENT_FILE = os.path.realpath(__file__) |
| 400 |
pytest.main('-s {}'.format(CURRENT_FILE)) |