Debian Codegraph @

dirsrvtests/tests/suites/replication/repl_conflict_delete_modify_test.py

raw
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 pytest
10 import logging
11 import os
12
13 from test389.topologies import topology_m2
14 from lib389.idm.user import UserAccounts, UserAccount
15 from lib389.idm.organizationalunit import OrganizationalUnits
16 from lib389.replica import ReplicationManager
17 from lib389.tombstone import Tombstones
18 from lib389.config import Config
19 from lib389._constants import DEFAULT_SUFFIX, PASSWORD
20
21 pytestmark = pytest.mark.tier2
22
23 log = logging.getLogger(__name__)
24
25 STAGING_OU = "staging"
26 PRODUCTION_OU = "production"
27 BIND_USER_UID = "bind_user"
28 TEST_USER_PREFIX = "test_account"
29 MAX_TEST_USERS = 5
30
31 @pytest.fixture(scope="function")
32 def repl_conflict_setup(topology_m2, request):
33 """Setup fixture for replication conflict resolution tests"""
34 supplier1 = topology_m2.ms["supplier1"]
35 supplier2 = topology_m2.ms["supplier2"]
36
37 log.info("Setting up replication conflict test environment")
38
39 config1 = Config(supplier1)
40 config2 = Config(supplier2)
41 config1.set('nsslapd-errorlog-level', '8192')
42 config2.set('nsslapd-errorlog-level', '8192')
43
44 ous_s1 = OrganizationalUnits(supplier1, DEFAULT_SUFFIX)
45
46 if not ous_s1.exists(STAGING_OU):
47 staging_ou = ous_s1.create(properties={
48 'ou': STAGING_OU,
49 'description': 'Staging environment for testing replication conflicts'
50 })
51 else:
52 staging_ou = ous_s1.get(STAGING_OU)
53
54 if not ous_s1.exists(PRODUCTION_OU):
55 production_ou = ous_s1.create(properties={
56 'ou': PRODUCTION_OU,
57 'description': 'Production environment for testing replication conflicts'
58 })
59 else:
60 production_ou = ous_s1.get(PRODUCTION_OU)
61
62 users_s1 = UserAccounts(supplier1, DEFAULT_SUFFIX)
63 if not users_s1.exists(BIND_USER_UID):
64 bind_user = users_s1.create(properties={
65 'uid': BIND_USER_UID,
66 'cn': BIND_USER_UID,
67 'sn': 'User',
68 'givenname': 'Bind',
69 'userpassword': PASSWORD,
70 'uidNumber': '10001',
71 'gidNumber': '10001',
72 'homeDirectory': f'/home/{BIND_USER_UID}'
73 })
74 else:
75 bind_user = users_s1.get(BIND_USER_UID)
76
77 test_users = []
78 for i in range(MAX_TEST_USERS):
79 uid_num = 10100 + i
80 user_name = f'{TEST_USER_PREFIX}_{i}'
81 user_dn = f'cn={user_name},{staging_ou.dn}'
82
83 if not UserAccount(supplier1, user_dn).exists():
84 user = UserAccount(supplier1, user_dn)
85 user.create(properties={
86 'cn': user_name,
87 'sn': f'User{i}',
88 'uid': user_name,
89 'uidNumber': str(uid_num),
90 'gidNumber': str(uid_num),
91 'homeDirectory': f'/home/{user_name}'
92 })
93 else:
94 user = UserAccount(supplier1, user_dn)
95 test_users.append(user)
96
97 repl = ReplicationManager(DEFAULT_SUFFIX)
98 repl.wait_for_replication(supplier1, supplier2)
99
100 def cleanup():
101 """Cleanup function"""
102 log.info("Cleaning up replication conflict test environment")
103 try:
104 config1.set('nsslapd-errorlog-level', '16384')
105 config2.set('nsslapd-errorlog-level', '16384')
106
107 for user in test_users:
108 try:
109 if user.exists():
110 user.delete()
111 except Exception:
112 pass
113
114 try:
115 if bind_user.exists():
116 bind_user.delete()
117 except Exception:
118 pass
119
120 try:
121 if staging_ou.exists():
122 staging_ou.delete()
123 except Exception:
124 pass
125
126 try:
127 if production_ou.exists():
128 production_ou.delete()
129 except Exception:
130 pass
131
132 except Exception as e:
133 log.warning(f"Cleanup warning: {e}")
134
135 request.addfinalizer(cleanup)
136
137 return {
138 'staging_ou': staging_ou,
139 'production_ou': production_ou,
140 'bind_user': bind_user,
141 'test_users': test_users
142 }
143
144
145 def verify_tombstone_replication(supplier1, supplier2, expected_uid):
146 """Verify tombstone exists on both suppliers"""
147 repl = ReplicationManager(DEFAULT_SUFFIX)
148
149 repl.wait_for_replication(supplier1, supplier2)
150 repl.wait_for_replication(supplier2, supplier1)
151
152 tombstones_s1 = Tombstones(supplier1, DEFAULT_SUFFIX)
153 tombstones_s2 = Tombstones(supplier2, DEFAULT_SUFFIX)
154
155 ts1_list = tombstones_s1.filter(f"(&(objectClass=nstombstone)(uid={expected_uid}*))")
156 ts2_list = tombstones_s2.filter(f"(&(objectClass=nstombstone)(uid={expected_uid}*))")
157
158 assert len(ts1_list) == 1, f"Expected exactly one tombstone on supplier1 for {expected_uid}"
159 assert len(ts2_list) == 1, f"Expected exactly one tombstone on supplier2 for {expected_uid}"
160
161 return ts1_list[0], ts2_list[0]
162
163
164 def verify_entry_modification_replicated(supplier1, supplier2, user_uid, expected_description):
165 """Verify that entry modification has been replicated"""
166 repl = ReplicationManager(DEFAULT_SUFFIX)
167
168 repl.wait_for_replication(supplier1, supplier2)
169 repl.wait_for_replication(supplier2, supplier1)
170
171 staging_ou_s1 = OrganizationalUnits(supplier1, DEFAULT_SUFFIX).get('staging')
172 staging_ou_s2 = OrganizationalUnits(supplier2, DEFAULT_SUFFIX).get('staging')
173
174 user1_dn = f'cn={user_uid},{staging_ou_s1.dn}'
175 user2_dn = f'cn={user_uid},{staging_ou_s2.dn}'
176
177 user1 = UserAccount(supplier1, user1_dn)
178 user2 = UserAccount(supplier2, user2_dn)
179
180 if user1.exists() and user2.exists():
181 desc1 = user1.get_attr_val_utf8('description')
182 desc2 = user2.get_attr_val_utf8('description')
183
184 return desc1 == expected_description and desc2 == expected_description
185
186 return True
187
188
189 def test_conflict_delete_modify_with_paused_agreements(topology_m2, repl_conflict_setup):
190 """Test replication conflict resolution when DELETE on M1 and MODIFY on M2 occur during paused replication
191
192 :id: b7f8e9d6-c5b4-3c2d-1e0f-9e8d7c6b5a47
193 :setup: Two supplier replication topology with test organizational structure and users
194 :steps:
195 1. Verify initial replication is working and entries exist on both suppliers
196 2. Pause all replication agreements
197 3. Delete a test entry on supplier1
198 4. Modify the same entry on supplier2 (and another for verification)
199 5. Resume replication agreements
200 6. Verify tombstone is created and replicated correctly
201 7. Verify modification on the alternate entry is replicated
202 8. Verify tombstone contains the modification from supplier2
203 :expectedresults:
204 1. Initial replication works correctly
205 2. Agreements are paused successfully
206 3. Entry is deleted on supplier1 (tombstone created)
207 4. Entry is modified on supplier2
208 5. Replication agreements resume successfully
209 6. Tombstone is replicated to supplier2
210 7. Other modifications replicate correctly
211 8. Tombstone on supplier1 contains the modification from supplier2
212 """
213 supplier1 = topology_m2.ms["supplier1"]
214 supplier2 = topology_m2.ms["supplier2"]
215 test_users = repl_conflict_setup['test_users']
216 repl = ReplicationManager(DEFAULT_SUFFIX)
217
218 delete_user = test_users[0]
219 modify_user = test_users[1]
220 verify_user = test_users[2]
221
222 delete_uid = delete_user.get_attr_val_utf8('uid')
223 modify_uid = modify_user.get_attr_val_utf8('uid')
224 verify_uid = verify_user.get_attr_val_utf8('uid')
225
226 log.info("Verify initial replication is working and entries exist on both suppliers")
227 user2_delete = UserAccount(supplier2, delete_user.dn)
228 user2_modify = UserAccount(supplier2, modify_user.dn)
229 user2_verify = UserAccount(supplier2, verify_user.dn)
230
231 assert user2_delete.exists(), f"User {delete_uid} should exist on supplier2"
232 assert user2_modify.exists(), f"User {modify_uid} should exist on supplier2"
233 assert user2_verify.exists(), f"User {verify_uid} should exist on supplier2"
234
235 assert user2_delete.exists()
236 assert user2_modify.exists()
237 assert user2_verify.exists()
238
239 log.info("Pause all replication agreements")
240 topology_m2.pause_all_replicas()
241
242 test_description_s1 = 'Modified on supplier1 for conflict test'
243 test_description_s2 = 'Modified on supplier2 for conflict test'
244 test_description_verify = 'Verification modification for conflict test'
245
246 log.info("Delete test entry on supplier1")
247 verify_user.replace('description', 'dummy_before_delete')
248 delete_user.delete()
249 modify_user.replace('description', test_description_s1)
250
251 log.info("Modify the same entry on supplier2 and another for verification")
252 user2_verify.replace('description', 'dummy_before_modify')
253 user2_delete.replace('description', test_description_s2)
254 user2_modify.replace('description', test_description_s2)
255 user2_verify.replace('description', test_description_verify)
256
257 log.info("Resume replication agreements")
258 topology_m2.resume_all_replicas()
259
260 repl.wait_for_replication(supplier1, supplier2)
261 repl.wait_for_replication(supplier2, supplier1)
262
263 log.info("Verify tombstone is created and replicated correctly")
264 ts1, ts2 = verify_tombstone_replication(supplier1, supplier2, delete_uid)
265
266 log.info("Verify modification on the alternate entry is replicated")
267 assert verify_entry_modification_replicated(supplier1, supplier2, verify_uid, test_description_verify)
268
269 log.info("Verify tombstone contains the modification from supplier2")
270 ts1_description = ts1.get_attr_val_utf8('description')
271
272 assert ts1_description == test_description_s2, f"Expected tombstone description '{test_description_s2}', got '{ts1_description}'"
273
274 user1_modify = UserAccount(supplier1, modify_user.dn)
275 user2_modify_final = UserAccount(supplier2, modify_user.dn)
276
277 final_description = user1_modify.get_attr_val_utf8('description')
278 assert user2_modify_final.get_attr_val_utf8('description') == final_description
279
280 assert final_description == test_description_s2
281
282
283 def test_basic_organizational_structure_replication(topology_m2, repl_conflict_setup):
284 """Test basic replication of organizational structure and user entries
285
286 :id: c8f9a0e1-d6c5-4d3e-2f1a-0f9e8d7c6b58
287 :setup: Two supplier replication topology with organizational structure
288 :steps:
289 1. Verify organizational units exist on both suppliers
290 2. Verify test users exist in staging area on both suppliers
291 3. Create new user and verify replication
292 4. Modify user and verify replication
293 5. Delete user and verify tombstone creation
294 :expectedresults:
295 1. OUs replicated correctly
296 2. Users replicated correctly
297 3. New user creation replicates
298 4. User modification replicates
299 5. User deletion creates tombstone
300 """
301 supplier1 = topology_m2.ms["supplier1"]
302 supplier2 = topology_m2.ms["supplier2"]
303 staging_ou = repl_conflict_setup['staging_ou']
304 test_users = repl_conflict_setup['test_users']
305 repl = ReplicationManager(DEFAULT_SUFFIX)
306
307 log.info("Verify organizational units exist on both suppliers")
308 ous_s2 = OrganizationalUnits(supplier2, DEFAULT_SUFFIX)
309 staging_ou_s2 = ous_s2.get(STAGING_OU)
310 production_ou_s2 = ous_s2.get(PRODUCTION_OU)
311
312 assert staging_ou_s2.exists()
313 assert production_ou_s2.exists()
314
315 log.info("Verify test users exist in staging area on both suppliers")
316 for test_user in test_users:
317 user_s2 = UserAccount(supplier2, test_user.dn)
318 assert user_s2.exists()
319
320 log.info("Create new user and verify replication")
321 new_user_name = 'new_repl_test_user'
322 new_user_dn = f'cn={new_user_name},{staging_ou.dn}'
323 new_user = UserAccount(supplier1, new_user_dn)
324 new_user.create(properties={
325 'cn': new_user_name,
326 'sn': 'TestUser',
327 'uid': new_user_name,
328 'uidNumber': '10200',
329 'gidNumber': '10200',
330 'homeDirectory': f'/home/{new_user_name}',
331 'description': 'Created for replication test'
332 })
333
334 repl.wait_for_replication(supplier1, supplier2)
335
336 new_user_s2_dn = f'cn={new_user_name},{staging_ou.dn}'
337 new_user_s2 = UserAccount(supplier2, new_user_s2_dn)
338 assert new_user_s2.exists(), f"New user {new_user_name} should exist on supplier2"
339 assert new_user_s2.get_attr_val_utf8('description') == 'Created for replication test'
340
341 log.info("Modify user and verify replication")
342 test_description = 'Modified for replication test'
343 new_user.replace('description', test_description)
344
345 repl.wait_for_replication(supplier1, supplier2)
346
347 assert new_user_s2.get_attr_val_utf8('description') == test_description
348
349 log.info("Delete user and verify tombstone creation")
350 new_user.delete()
351
352 repl.wait_for_replication(supplier1, supplier2)
353
354 tombstones_s1 = Tombstones(supplier1, staging_ou.dn)
355 tombstones_s2 = Tombstones(supplier2, staging_ou.dn)
356
357 ts1_count = len(tombstones_s1.list())
358 ts2_count = len(tombstones_s2.list())
359
360 assert ts1_count >= 1, f"Expected at least one tombstone on supplier1, found {ts1_count}"
361 assert ts2_count >= 1, f"Expected at least one tombstone on supplier2, found {ts2_count}"
362
363
364 def test_replication_agreement_pause_resume_functionality(topology_m2, repl_conflict_setup):
365 """Test replication agreement pause and resume functionality using modern APIs
366
367 :id: d9f0b1e2-e7d6-5e4f-3f2b-1f0e9d8c7b69
368 :setup: Two supplier replication topology
369 :steps:
370 1. Verify replication is working initially
371 2. Pause all replication agreements
372 3. Make changes on supplier1
373 4. Verify changes do not replicate immediately
374 5. Resume replication agreements
375 6. Verify changes replicate after resume
376 :expectedresults:
377 1. Initial replication works
378 2. Agreements pause successfully
379 3. Changes made successfully
380 4. Changes do not replicate while paused
381 5. Agreements resume successfully
382 6. Changes replicate after resume
383 """
384 supplier1 = topology_m2.ms["supplier1"]
385 supplier2 = topology_m2.ms["supplier2"]
386 test_users = repl_conflict_setup['test_users']
387 repl = ReplicationManager(DEFAULT_SUFFIX)
388
389 test_user = test_users[0]
390 test_uid = test_user.get_attr_val_utf8('uid')
391
392 log.info("Verify replication is working initially")
393 initial_description = 'Initial test for pause resume'
394 test_user.replace('description', initial_description)
395 repl.wait_for_replication(supplier1, supplier2)
396
397 user_s2 = UserAccount(supplier2, test_user.dn)
398 assert user_s2.get_attr_val_utf8('description') == initial_description
399
400 log.info("Pause all replication agreements")
401 topology_m2.pause_all_replicas()
402
403 log.info("Make changes on supplier1")
404 paused_description = 'Modified during pause test'
405 test_user.replace('description', paused_description)
406
407 log.info("Verify changes do not replicate immediately")
408 user_s2_desc = user_s2.get_attr_val_utf8('description')
409 assert user_s2_desc == initial_description, f"Expected '{initial_description}', got '{user_s2_desc}'"
410
411 log.info("Resume replication agreements")
412 topology_m2.resume_all_replicas()
413
414 log.info("Verify changes replicate after resume")
415 repl.wait_for_replication(supplier1, supplier2)
416
417 final_description = user_s2.get_attr_val_utf8('description')
418 assert final_description == paused_description, f"Expected '{paused_description}', got '{final_description}'"
419
420
421 if __name__ == '__main__':
422 # Run isolated
423 # -s for DEBUG mode
424 CURRENT_FILE = os.path.realpath(__file__)
425 pytest.main(["-s", CURRENT_FILE])