Debian Codegraph @

dirsrvtests/tests/suites/schema/schema_csn_replication_test.py

raw
1
2 # --- BEGIN COPYRIGHT BLOCK ---
3 # Copyright (C) 2025 Red Hat, Inc.
4 # All rights reserved.
5 #
6 # License: GPL (version 3 or any later version).
7 # See LICENSE for details.
8 # --- END COPYRIGHT BLOCK ---
9 #
10 import logging
11 import pytest
12 import time, os
13 from lib389._constants import DEFAULT_SUFFIX
14 from test389.topologies import topology_m2
15 from lib389.replica import ReplicationManager
16 from lib389.schema import Schema
17 from lib389.idm.user import UserAccounts
18 from lib389.config import Config
19
20 pytestmark = pytest.mark.tier2
21
22 logging.getLogger(__name__).setLevel(logging.DEBUG)
23 log = logging.getLogger(__name__)
24
25 MAX_TEST_USERS = 4
26
27
28 def create_custom_attributetype(name='testAT', num=1):
29 """Create a custom attribute type definition"""
30 return {
31 'names': (f"{name}{num}",),
32 'oid': str(9000 + num),
33 'desc': f'Test attribute type for schema replication - {name}',
34 'sup': (),
35 'syntax': '1.3.6.1.4.1.1466.115.121.1.15',
36 'syntax_len': None,
37 'x_ordered': None,
38 'collective': None,
39 'obsolete': None,
40 'single_value': None,
41 'no_user_mod': None,
42 'equality': None,
43 'substr': None,
44 'ordering': None,
45 'usage': None,
46 'x_origin': ('Test Schema Replication',)
47 }
48
49 def create_custom_objectclass(name='testOC', num=1):
50 """Create a custom object class definition"""
51 return {
52 'names': (f"{name}{num}",),
53 'oid': str(9100 + num),
54 'desc': f'Test object class for schema replication - {name}',
55 'sup': ('top',),
56 'kind': 1, # STRUCTURAL
57 'must': ('ou',),
58 'may': (),
59 'obsolete': None,
60 'x_origin': ('Test Schema Replication',)
61 }
62
63
64
65 @pytest.fixture(scope="function")
66 def schema_csn_setup(topology_m2, request):
67 """Setup fixture for schema CSN replication tests"""
68 supplier1 = topology_m2.ms["supplier1"]
69 supplier2 = topology_m2.ms["supplier2"]
70
71 log.info("Setting up schema CSN replication test environment")
72
73 config1 = Config(supplier1)
74 config2 = Config(supplier2)
75 config1.set('nsslapd-errorlog-level', '8192')
76 config2.set('nsslapd-errorlog-level', '8192')
77
78 users1 = UserAccounts(supplier1, DEFAULT_SUFFIX)
79
80 test_users = []
81 for i in range(MAX_TEST_USERS):
82 test_user = users1.create_test_user(uid=90000 + i)
83 test_users.append(test_user)
84
85 repl = ReplicationManager(DEFAULT_SUFFIX)
86 repl.wait_for_replication(supplier1, supplier2)
87
88 def cleanup():
89 """Cleanup function"""
90 log.info("Cleaning up schema CSN test environment")
91 try:
92 config1.set('nsslapd-errorlog-level', '16384')
93 config2.set('nsslapd-errorlog-level', '16384')
94 for user in test_users:
95 user.delete()
96 except Exception as e:
97 log.warning(f"Cleanup warning: {e}")
98
99 request.addfinalizer(cleanup)
100
101 return test_users
102
103
104 def wait_for_schema_csn_sync(instance1, instance2, timeout=30):
105 """Wait for schema CSN synchronization"""
106 schema1 = Schema(instance1)
107 schema2 = Schema(instance2)
108
109 for i in range(timeout):
110 csn1 = schema1.get_schema_csn()
111 csn2 = schema2.get_schema_csn()
112
113 if csn1 is not None and csn2 is not None and csn1 == csn2:
114 return True
115
116 if i == 0:
117 log.debug(f"Schema CSN sync in progress: {instance1.serverid} to {instance2.serverid}")
118 time.sleep(1)
119
120 log.error(
121 f"Schema CSN sync failed after {timeout}s: "
122 f"{instance1.serverid} CSN={csn1}, "
123 f"{instance2.serverid} CSN={csn2}"
124 )
125 pytest.fail(f"Schema CSN did not sync in {timeout} seconds")
126
127
128 def trigger_replication(repl_manager, supplier1, supplier2, test_user):
129 """Trigger replication by modifying a test entry"""
130 test_user.replace('description', 'Replication trigger')
131 repl_manager.wait_for_replication(supplier1, supplier2)
132
133
134 def test_schema_csn_replication_basic_sync(topology_m2, schema_csn_setup):
135 """Test basic schema replication and CSN synchronization
136
137 :id: 8a9b5c42-6d7e-4f3c-a5b2-1e9f8d7c6b5a
138 :setup: Two supplier replication topology with test users
139 :steps:
140 1. Add custom and standard schema definitions to supplier2
141 2. Trigger replication from supplier2 to supplier1
142 3. Verify that schema CSNs are synchronized
143 :expectedresults:
144 1. The schema changes are applied successfully on supplier2
145 2. The data modification replicates successfully to supplier1
146 3. The schema CSNs on both suppliers are identical
147 """
148 supplier1 = topology_m2.ms["supplier1"]
149 supplier2 = topology_m2.ms["supplier2"]
150 test_users = schema_csn_setup
151 test_user = test_users[0]
152 repl = ReplicationManager(DEFAULT_SUFFIX)
153
154 log.info('Add custom attribute types and object classes to supplier2')
155 schema2 = Schema(supplier2)
156
157 custom_at = create_custom_attributetype('testAttr', 1)
158 schema2.add_attributetype(custom_at)
159
160 custom_oc = create_custom_objectclass('testClass', 1)
161 schema2.add_objectclass(custom_oc)
162
163 log.info('Modify standard schema definitions on supplier2')
164 new_at = (
165 "( 2.16.840.1.113730.3.1.569 NAME 'cosPriority' "
166 "DESC 'Netscape defined attribute type - modified for test' "
167 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
168 "X-ORIGIN 'Netscape Directory Server - Test Modified' )"
169 )
170 schema2.add('attributetypes', new_at)
171
172 new_oc = (
173 "( 5.3.6.1.1.1.2.0 NAME 'trustAccount' "
174 "DESC 'Sets trust accounts information - modified for test' "
175 "SUP top AUXILIARY MUST trustModel MAY ( accessTo $ ou ) "
176 "X-ORIGIN 'nss_ldap/pam_ldap - Test Modified' )"
177 )
178 schema2.add('objectClasses', new_oc)
179
180 log.info('Trigger replication from supplier2 to supplier1')
181
182 users2 = UserAccounts(supplier2, DEFAULT_SUFFIX)
183 user_on_s2 = users2.get(test_user.get_attr_val_utf8('uid'))
184 trigger_replication(repl, supplier2, supplier1, user_on_s2)
185
186 users1 = UserAccounts(supplier1, DEFAULT_SUFFIX)
187 user1 = users1.get(test_user.get_attr_val_utf8('uid'))
188 assert user1.get_attr_val_utf8('description') == user_on_s2.get_attr_val_utf8('description')
189
190 log.info('Verify schema CSN synchronization')
191 wait_for_schema_csn_sync(supplier1, supplier2)
192
193
194 def test_schema_csn_after_data_replication(topology_m2, schema_csn_setup):
195 """Test that schema CSN synchronizes after schema and data replication
196
197 :id: 7f8e9d6c-5b4a-3c2d-1e0f-9e8d7c6b5a49
198 :setup: Two supplier replication topology
199 :steps:
200 1. Add new schema definitions to supplier1
201 2. Modify a user on supplier1 to trigger replication
202 3. Verify that schema CSNs are synchronized
203 :expectedresults:
204 1. The new schema is added successfully on supplier1
205 2. The user modification replicates successfully to supplier2
206 3. The schema CSNs on both suppliers are identical
207 """
208 supplier1 = topology_m2.ms["supplier1"]
209 supplier2 = topology_m2.ms["supplier2"]
210 test_users = schema_csn_setup
211 test_user = test_users[1]
212 repl = ReplicationManager(DEFAULT_SUFFIX)
213
214 log.info('Add schema changes to supplier1')
215 schema1 = Schema(supplier1)
216 custom_at = create_custom_attributetype('dataTestAttr', 5)
217 schema1.add_attributetype(custom_at)
218
219 custom_oc = create_custom_objectclass('dataTestClass', 5)
220 schema1.add_objectclass(custom_oc)
221
222 log.info('Perform a data modification on supplier1')
223 trigger_replication(repl, supplier1, supplier2, test_user)
224
225 users2 = UserAccounts(supplier2, DEFAULT_SUFFIX)
226 user2 = users2.get(test_user.get_attr_val_utf8('uid'))
227 assert user2.get_attr_val_utf8('description') == test_user.get_attr_val_utf8('description')
228
229 log.info('Check schema CSN synchronization')
230 wait_for_schema_csn_sync(supplier1, supplier2)
231
232
233 def test_schema_learning_with_paused_agreement(topology_m2, schema_csn_setup):
234 """Test schema learning when replication agreement is paused
235
236 :id: 9c8d7b6a-5e4f-3a2b-1c0d-8e7d6c5b4a38
237 :setup: Two supplier replication topology
238 :steps:
239 1. Get baseline schema CSNs
240 2. Pause replication from supplier2 to supplier1
241 3. Add new schema elements to supplier2
242 4. Verify schema CSNs immediately to avoid keep-alive interference
243 5. Resume replication and trigger data replication
244 :expectedresults:
245 1. Baseline CSNs are captured
246 2. Agreements are paused successfully
247 3. Schema is added to supplier2
248 4. Schema CSNs show supplier2 has newer schema than supplier1
249 5. Data replication works after resume
250 """
251 supplier1 = topology_m2.ms["supplier1"]
252 supplier2 = topology_m2.ms["supplier2"]
253 test_users = schema_csn_setup
254 test_user = test_users[2]
255 repl = ReplicationManager(DEFAULT_SUFFIX)
256
257 log.info('Get baseline schema CSNs')
258 schema1 = Schema(supplier1)
259 schema2 = Schema(supplier2)
260 initial_csn1 = schema1.get_schema_csn()
261 initial_csn2 = schema2.get_schema_csn()
262
263 log.info('Pause replication from supplier2 to supplier1')
264 repl.disable_to_supplier(supplier1, [supplier2])
265
266 log.info('Add new schema elements to supplier2')
267 custom_at3 = create_custom_attributetype('testAttr', 3)
268 schema2.add_attributetype(custom_at3)
269 log.info(f"Added custom attribute type to supplier2: {custom_at3['names'][0]}")
270
271 custom_oc3 = create_custom_objectclass('testClass', 3)
272 schema2.add_objectclass(custom_oc3)
273 log.info(f"Added custom object class to supplier2: {custom_oc3['names'][0]}")
274
275 log.info('Verify schema CSNs immediately after schema changes')
276 new_csn2 = schema2.get_schema_csn()
277 current_csn1 = schema1.get_schema_csn()
278
279 assert new_csn2 != initial_csn2, "Supplier2 schema CSN should have changed after schema addition"
280 assert current_csn1 == initial_csn1, "Supplier1 schema CSN should be unchanged (paused agreement)"
281 assert current_csn1 != new_csn2, "Schema CSNs should be different between suppliers"
282
283 log.info('Resume replication and trigger data replication')
284 repl.enable_to_supplier(supplier1, [supplier2])
285
286 users2 = UserAccounts(supplier2, DEFAULT_SUFFIX)
287 user_on_s2 = users2.get(test_user.get_attr_val_utf8('uid'))
288 trigger_replication(repl, supplier2, supplier1, user_on_s2)
289
290 users1 = UserAccounts(supplier1, DEFAULT_SUFFIX)
291 user_on_s1 = users1.get(test_user.get_attr_val_utf8('uid'))
292 assert user_on_s1.get_attr_val_utf8('description') == user_on_s2.get_attr_val_utf8('description')
293
294
295 def test_schema_supplier1_to_supplier2_replication(topology_m2, schema_csn_setup):
296 """Test schema replication from supplier1 to supplier2 and CSN synchronization
297
298 :id: 4a3b2c1d-0e9f-8d7c-6b5a-4a3b2c1d0e9f
299 :setup: Two supplier replication topology
300 :steps:
301 1. Add schema changes to supplier1
302 2. Trigger replication from supplier1 to supplier2
303 3. Verify schema CSNs synchronize
304 :expectedresults:
305 1. Schema changes are applied to supplier1
306 2. Replication pushes schema changes to supplier2
307 3. Schema CSNs synchronize between suppliers
308 """
309 supplier1 = topology_m2.ms["supplier1"]
310 supplier2 = topology_m2.ms["supplier2"]
311 test_users = schema_csn_setup
312 test_user = test_users[3]
313 repl = ReplicationManager(DEFAULT_SUFFIX)
314
315 log.info('Add schema changes to supplier1')
316 schema1 = Schema(supplier1)
317
318 custom_at4 = create_custom_attributetype('testAttr', 4)
319 schema1.add_attributetype(custom_at4)
320
321 custom_oc4 = create_custom_objectclass('testClass', 4)
322 schema1.add_objectclass(custom_oc4)
323
324 log.info('Trigger replication from supplier1 to supplier2')
325 trigger_replication(repl, supplier1, supplier2, test_user)
326
327 users2 = UserAccounts(supplier2, DEFAULT_SUFFIX)
328 user2 = users2.get(test_user.get_attr_val_utf8('uid'))
329 assert user2.get_attr_val_utf8('description') == test_user.get_attr_val_utf8('description')
330
331 log.info('Verify schema CSNs synchronize after schema push')
332 wait_for_schema_csn_sync(supplier1, supplier2)
333
334
335 if __name__ == '__main__':
336 # Run isolated
337 # -s for DEBUG mode
338 CURRENT_FILE = os.path.realpath(__file__)
339 pytest.main(["-s", CURRENT_FILE])