| 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 os |
| 10 |
import logging |
| 11 |
import pytest |
| 12 |
import re |
| 13 |
from lib389._constants import DEFAULT_SUFFIX |
| 14 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 15 |
from lib389.idm.user import UserAccounts |
| 16 |
from lib389.replica import ReplicationManager |
| 17 |
from lib389.tasks import * |
| 18 |
from lib389.tombstone import Tombstones |
| 19 |
from test389.topologies import topology_i2 as topo_i2, topology_m2 as topo_m2 |
| 20 |
from lib389.utils import get_default_db_lib |
| 21 |
|
| 22 |
pytestmark = pytest.mark.tier1 |
| 23 |
|
| 24 |
DEBUGGING = os.getenv("DEBUGGING", default=False) |
| 25 |
if DEBUGGING: |
| 26 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 27 |
else: |
| 28 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 29 |
log = logging.getLogger(__name__) |
| 30 |
|
| 31 |
|
| 32 |
def get_test_name(): |
| 33 |
full_testname = os.getenv('PYTEST_CURRENT_TEST') |
| 34 |
res = re.match('.*::([^ ]+) .*', full_testname) |
| 35 |
assert res |
| 36 |
return res.group(1) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.fixture(scope="function") |
| 40 |
def with_container(topo_m2, request): |
| 41 |
# Creates a organizational unit container with proper cleanup |
| 42 |
testname = get_test_name() |
| 43 |
ou = f'test_container_{testname}' |
| 44 |
S1 = topo_m2.ms["supplier1"] |
| 45 |
S2 = topo_m2.ms["supplier2"] |
| 46 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 47 |
|
| 48 |
log.info(f"Create container ou={ou},{DEFAULT_SUFFIX} on {S1.serverid}") |
| 49 |
ous1 = OrganizationalUnits(S1, DEFAULT_SUFFIX) |
| 50 |
container = ous1.create(properties={ |
| 51 |
'ou': ou, |
| 52 |
'description': f'Test container for {testname} test' |
| 53 |
}) |
| 54 |
|
| 55 |
def fin(): |
| 56 |
container.delete(recursive=True) |
| 57 |
repl.wait_for_replication(S1, S2) |
| 58 |
|
| 59 |
if not DEBUGGING: |
| 60 |
request.addfinalizer(fin) |
| 61 |
repl.wait_for_replication(S1, S2) |
| 62 |
|
| 63 |
return container |
| 64 |
|
| 65 |
|
| 66 |
def verify_value_against_entries(container, attr, entries, msg): |
| 67 |
# Check that container attr value match the number of entries |
| 68 |
num = container.get_attr_val_int(attr) |
| 69 |
num_entries = len(entries) |
| 70 |
dns = [ e.dn for e in entries ] |
| 71 |
log.debug(f"[{msg}] {attr}: entries: {entries}") |
| 72 |
log.info(f"[{msg}] container is {container}") |
| 73 |
log.info(f"[{msg}] {attr}: {num} (Expecting: {num_entries})") |
| 74 |
assert num == num_entries, ( |
| 75 |
f"{attr} attribute has wrong value: {num} {msg}, was expecting: {num_entries}", |
| 76 |
f"entries are {dns}" ) |
| 77 |
|
| 78 |
|
| 79 |
def verify_subordinates(inst, container, msg): |
| 80 |
log.info(f"Verify numSubordinates and tombstoneNumSubordinates {msg}") |
| 81 |
tombstones = Tombstones(inst, container.dn).list() |
| 82 |
entries = container.search(scope='one') |
| 83 |
verify_value_against_entries(container, 'numSubordinates', entries, msg) |
| 84 |
verify_value_against_entries(container, 'tombstoneNumSubordinates', tombstones, msg) |
| 85 |
|
| 86 |
|
| 87 |
def test_numsubordinates_tombstone_replication_mismatch(topo_i2): |
| 88 |
"""Test that numSubordinates values match between replicas after tombstone creation |
| 89 |
|
| 90 |
:id: c43ecc7a-d706-42e8-9179-1ff7d0e7163a |
| 91 |
:setup: Two standalone instances |
| 92 |
:steps: |
| 93 |
1. Create a container (organizational unit) on the first instance |
| 94 |
2. Create a user object in that container |
| 95 |
3. Delete the user object (this creates a tombstone) |
| 96 |
4. Set up replication between the two instances |
| 97 |
5. Wait for replication to complete |
| 98 |
6. Check numSubordinates on both instances |
| 99 |
7. Check tombstoneNumSubordinates on both instances |
| 100 |
8. Verify that numSubordinates values match on both instances |
| 101 |
:expectedresults: |
| 102 |
1. Container should be created successfully |
| 103 |
2. User object should be created successfully |
| 104 |
3. User object should be deleted successfully |
| 105 |
4. Replication should be set up successfully |
| 106 |
5. Replication should complete successfully |
| 107 |
6. numSubordinates should be accessible on both instances |
| 108 |
7. tombstoneNumSubordinates should be accessible on both instances |
| 109 |
8. numSubordinates values should match on both instances |
| 110 |
""" |
| 111 |
|
| 112 |
instance1 = topo_i2.ins["standalone1"] |
| 113 |
instance2 = topo_i2.ins["standalone2"] |
| 114 |
|
| 115 |
log.info("Create a container (organizational unit) on the first instance") |
| 116 |
ous1 = OrganizationalUnits(instance1, DEFAULT_SUFFIX) |
| 117 |
container = ous1.create(properties={ |
| 118 |
'ou': 'test_container', |
| 119 |
'description': 'Test container for numSubordinates replication test' |
| 120 |
}) |
| 121 |
container_rdn = container.rdn |
| 122 |
log.info(f"Created container: {container_rdn}") |
| 123 |
|
| 124 |
log.info("Create a user object in that container") |
| 125 |
users1 = UserAccounts(instance1, DEFAULT_SUFFIX, rdn=f"ou={container_rdn}") |
| 126 |
test_user = users1.create_test_user(uid=1001) |
| 127 |
log.info(f"Created user: {test_user.dn}") |
| 128 |
|
| 129 |
log.info("Checking initial numSubordinates on container") |
| 130 |
container_obj1 = OrganizationalUnits(instance1, DEFAULT_SUFFIX).get(container_rdn) |
| 131 |
initial_numsubordinates = container_obj1.get_attr_val_int('numSubordinates') |
| 132 |
log.info(f"Initial numSubordinates: {initial_numsubordinates}") |
| 133 |
assert initial_numsubordinates == 1 |
| 134 |
|
| 135 |
log.info("Delete the user object (this creates a tombstone)") |
| 136 |
test_user.delete() |
| 137 |
|
| 138 |
log.info("Checking numSubordinates after deletion") |
| 139 |
after_delete_numsubordinates = container_obj1.get_attr_val_int('numSubordinates') |
| 140 |
log.info(f"numSubordinates after deletion: {after_delete_numsubordinates}") |
| 141 |
|
| 142 |
log.info("Checking tombstoneNumSubordinates after deletion") |
| 143 |
try: |
| 144 |
tombstone_numsubordinates = container_obj1.get_attr_val_int('tombstoneNumSubordinates') |
| 145 |
log.info(f"tombstoneNumSubordinates: {tombstone_numsubordinates}") |
| 146 |
except Exception as e: |
| 147 |
log.info(f"tombstoneNumSubordinates not found or error: {e}") |
| 148 |
tombstone_numsubordinates = 0 |
| 149 |
|
| 150 |
log.info("Set up replication between the two instances") |
| 151 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 152 |
repl.create_first_supplier(instance1) |
| 153 |
repl.join_supplier(instance1, instance2) |
| 154 |
|
| 155 |
log.info("Wait for replication to complete") |
| 156 |
repl.wait_for_replication(instance1, instance2) |
| 157 |
|
| 158 |
log.info("Check numSubordinates on both instances") |
| 159 |
container_obj1 = OrganizationalUnits(instance1, DEFAULT_SUFFIX).get(container_rdn) |
| 160 |
numsubordinates_instance1 = container_obj1.get_attr_val_int('numSubordinates') |
| 161 |
log.info(f"numSubordinates on instance1: {numsubordinates_instance1}") |
| 162 |
|
| 163 |
container_obj2 = OrganizationalUnits(instance2, DEFAULT_SUFFIX).get(container_rdn) |
| 164 |
numsubordinates_instance2 = container_obj2.get_attr_val_int('numSubordinates') |
| 165 |
log.info(f"numSubordinates on instance2: {numsubordinates_instance2}") |
| 166 |
|
| 167 |
log.info("Check tombstoneNumSubordinates on both instances") |
| 168 |
try: |
| 169 |
tombstone_numsubordinates_instance1 = container_obj1.get_attr_val_int('tombstoneNumSubordinates') |
| 170 |
log.info(f"tombstoneNumSubordinates on instance1: {tombstone_numsubordinates_instance1}") |
| 171 |
except Exception as e: |
| 172 |
log.info(f"tombstoneNumSubordinates not found on instance1: {e}") |
| 173 |
tombstone_numsubordinates_instance1 = 0 |
| 174 |
|
| 175 |
try: |
| 176 |
tombstone_numsubordinates_instance2 = container_obj2.get_attr_val_int('tombstoneNumSubordinates') |
| 177 |
log.info(f"tombstoneNumSubordinates on instance2: {tombstone_numsubordinates_instance2}") |
| 178 |
except Exception as e: |
| 179 |
log.info(f"tombstoneNumSubordinates not found on instance2: {e}") |
| 180 |
tombstone_numsubordinates_instance2 = 0 |
| 181 |
|
| 182 |
log.info("Verify that numSubordinates values match on both instances") |
| 183 |
log.info(f"Comparison: instance1 numSubordinates={numsubordinates_instance1}, " |
| 184 |
f"instance2 numSubordinates={numsubordinates_instance2}") |
| 185 |
log.info(f"Comparison: instance1 tombstoneNumSubordinates={tombstone_numsubordinates_instance1}, " |
| 186 |
f"instance2 tombstoneNumSubordinates={tombstone_numsubordinates_instance2}") |
| 187 |
|
| 188 |
assert numsubordinates_instance1 == numsubordinates_instance2, ( |
| 189 |
f"numSubordinates mismatch: instance1 has {numsubordinates_instance1}, " |
| 190 |
f"instance2 has {numsubordinates_instance2}. " |
| 191 |
) |
| 192 |
assert tombstone_numsubordinates_instance1 == tombstone_numsubordinates_instance2, ( |
| 193 |
f"tombstoneNumSubordinates mismatch: instance1 has {tombstone_numsubordinates_instance1}, " |
| 194 |
f"instance2 has {tombstone_numsubordinates_instance2}. " |
| 195 |
) |
| 196 |
|
| 197 |
def test_numsubordinates_tombstone_after_import(topo_m2, with_container): |
| 198 |
"""Test that numSubordinates values are the expected one after an import |
| 199 |
|
| 200 |
:id: 67bec454-6bb3-11f0-b9ae-c85309d5c3e3 |
| 201 |
:setup: Two suppliers instances with an ou container |
| 202 |
:steps: |
| 203 |
1. Create a container (organizational unit) on the first instance |
| 204 |
2. Create a user object in that container |
| 205 |
3. Delete the user object (this creates a tombstone) |
| 206 |
4. Set up replication between the two instances |
| 207 |
5. Wait for replication to complete |
| 208 |
6. Check numSubordinates on both instances |
| 209 |
7. Check tombstoneNumSubordinates on both instances |
| 210 |
8. Verify that numSubordinates values match on both instances |
| 211 |
:expectedresults: |
| 212 |
1. Container should be created successfully |
| 213 |
2. User object should be created successfully |
| 214 |
3. User object should be deleted successfully |
| 215 |
4. Replication should be set up successfully |
| 216 |
5. Replication should complete successfully |
| 217 |
6. numSubordinates should be accessible on both instances |
| 218 |
7. tombstoneNumSubordinates should be accessible on both instances |
| 219 |
8. numSubordinates values should match on both instances |
| 220 |
""" |
| 221 |
|
| 222 |
S1 = topo_m2.ms["supplier1"] |
| 223 |
S2 = topo_m2.ms["supplier2"] |
| 224 |
container = with_container |
| 225 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 226 |
tasks = Tasks(S1) |
| 227 |
|
| 228 |
log.info("Create some user objects in that container") |
| 229 |
users1 = UserAccounts(S1, DEFAULT_SUFFIX, rdn=f"ou={container.rdn}") |
| 230 |
users = {} |
| 231 |
for uid in range(1001,1010): |
| 232 |
users[uid] = users1.create_test_user(uid=uid) |
| 233 |
log.info(f"Created user: {users[uid].dn}") |
| 234 |
|
| 235 |
for uid in range(1002,1007,2): |
| 236 |
users[uid].delete() |
| 237 |
log.info(f"Removing user: {users[uid].dn}") |
| 238 |
repl.wait_for_replication(S1, S2) |
| 239 |
|
| 240 |
ldif_file = f"{S1.get_ldif_dir()}/export.ldif" |
| 241 |
log.info(f"Export into {ldif_file}") |
| 242 |
args = {EXPORT_REPL_INFO: True, |
| 243 |
TASK_WAIT: True} |
| 244 |
tasks.exportLDIF(DEFAULT_SUFFIX, None, ldif_file, args) |
| 245 |
|
| 246 |
verify_subordinates(S1, container, "before importing") |
| 247 |
|
| 248 |
# import the ldif file |
| 249 |
log.info(f"Import from {ldif_file}") |
| 250 |
args = {TASK_WAIT: True} |
| 251 |
tasks.importLDIF(DEFAULT_SUFFIX, None, ldif_file, args) |
| 252 |
|
| 253 |
verify_subordinates(S1, container, "after importing") |
| 254 |
|
| 255 |
|
| 256 |
if __name__ == '__main__': |
| 257 |
# Run isolated |
| 258 |
# -s for DEBUG mode |
| 259 |
CURRENT_FILE = os.path.realpath(__file__) |
| 260 |
pytest.main("-s %s" % CURRENT_FILE) |