Debian Codegraph @

dirsrvtests/tests/suites/retrocl/basic_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2021 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 logging
10 import time
11 import os
12 import ldap
13 import pytest
14 from test389.topologies import topology_st
15 from lib389.plugins import RetroChangelogPlugin
16 from lib389._constants import DEFAULT_SUFFIX, RETROCL_SUFFIX, DN_DM, PW_DM
17 from lib389.cli_base import FakeArgs, connect_instance, disconnect_instance
18 from lib389.cli_base.dsrc import dsrc_arg_concat
19 from lib389.cli_conf.plugins.retrochangelog import retrochangelog_add
20 from lib389.idm.user import UserAccount, UserAccounts
21 from lib389.idm.domain import Domain
22 from lib389._mapped_object import DSLdapObjects
23
24 pytestmark = pytest.mark.tier1
25
26 USER1_DN = 'uid=user1,ou=people,'+ DEFAULT_SUFFIX
27 USER2_DN = 'uid=user2,ou=people,'+ DEFAULT_SUFFIX
28 USER_PW = 'password'
29 ATTR_HOMEPHONE = 'homePhone'
30 ATTR_CARLICENSE = 'carLicense'
31
32 log = logging.getLogger(__name__)
33
34 #unstable or unstatus tests, skipped for now
35 @pytest.mark.flaky(max_runs=2, min_passes=1)
36 def test_retrocl_exclude_attr_add(topology_st):
37 """ Test exclude attribute feature of the retrocl plugin for add operation
38
39 :id: 3481650f-2070-45ef-9600-2500cfc51559
40
41 :setup: Standalone instance
42
43 :steps:
44 1. Enable dynamic plugins
45 2. Confige retro changelog plugin
46 3. Add an entry
47 4. Ensure entry attrs are in the changelog
48 5. Exclude an attr
49 6. Add another entry
50 7. Ensure excluded attr is not in the changelog
51
52 :expectedresults:
53 1. Success
54 2. Success
55 3. Success
56 4. Success
57 5. Success
58 6. Success
59 7. Success
60 """
61
62 st = topology_st.standalone
63
64 log.info('Configure retrocl plugin')
65 rcl = RetroChangelogPlugin(st)
66 rcl.disable()
67 rcl.enable()
68 rcl.replace('nsslapd-attribute', 'nsuniqueid:targetUniqueId')
69
70 log.info('Restarting instance')
71 try:
72 st.restart()
73 except ldap.LDAPError as e:
74 ldap.error('Failed to restart instance ' + e.args[0]['desc'])
75 assert False
76
77 users = UserAccounts(st, DEFAULT_SUFFIX)
78
79 log.info('Adding user1')
80 try:
81 users.create(properties={
82 'sn': '1',
83 'cn': 'user 1',
84 'uid': 'user1',
85 'uidNumber': '11',
86 'gidNumber': '111',
87 'givenname': 'user1',
88 'homePhone': '0861234567',
89 'carLicense': '131D16674',
90 'mail': '[email protected]',
91 'homeDirectory': '/home/user1',
92 'userpassword': USER_PW})
93 except ldap.ALREADY_EXISTS:
94 pass
95 except ldap.LDAPError as e:
96 log.error("Failed to add user1: " + str(e))
97
98 log.info('Verify homePhone and carLicense attrs are in the changelog changestring')
99 try:
100 retro_changelog_suffix = DSLdapObjects(st, basedn=RETROCL_SUFFIX)
101 cllist = retro_changelog_suffix.filter(f'(targetDn={USER1_DN})')
102 except ldap.LDAPError as e:
103 log.fatal("Changelog search failed, error: " + str(e))
104 assert False
105 assert len(cllist) > 0
106 if cllist[0].present('changes'):
107 clstr = str(cllist[0].get_attr_vals_utf8('changes'))
108 assert ATTR_HOMEPHONE in clstr
109 assert ATTR_CARLICENSE in clstr
110
111 log.info('Excluding attribute ' + ATTR_HOMEPHONE)
112 args = FakeArgs()
113 args.connections = [st.host + ':' + str(st.port) + ':' + DN_DM + ':' + PW_DM]
114 args.instance = 'standalone1'
115 args.basedn = None
116 args.binddn = None
117 args.starttls = False
118 args.pwdfile = None
119 args.bindpw = None
120 args.prompt = False
121 args.exclude_attrs = ATTR_HOMEPHONE
122 args.max_age = None
123 args.func = retrochangelog_add
124 dsrc_inst = dsrc_arg_concat(args, None)
125 inst = connect_instance(dsrc_inst, False, args)
126 result = args.func(inst, None, log, args)
127 disconnect_instance(inst)
128 assert result is None
129
130 log.info('Restarting instance')
131 try:
132 st.restart()
133 except ldap.LDAPError as e:
134 ldap.error('Failed to restart instance ' + e.args[0]['desc'])
135 assert False
136
137 log.info('Adding user2')
138 try:
139 users.create(properties={
140 'sn': '2',
141 'cn': 'user 2',
142 'uid': 'user2',
143 'uidNumber': '22',
144 'gidNumber': '222',
145 'givenname': 'user2',
146 'homePhone': '0879088363',
147 'carLicense': '04WX11038',
148 'mail': '[email protected]',
149 'homeDirectory': '/home/user2',
150 'userpassword': USER_PW})
151 except ldap.ALREADY_EXISTS:
152 pass
153 except ldap.LDAPError as e:
154 log.error("Failed to add user2: " + str(e))
155
156 log.info('Verify homePhone attr is not in the changelog changestring')
157 try:
158 cllist = retro_changelog_suffix.filter(f'(targetDn={USER2_DN})')
159 assert len(cllist) > 0
160 if cllist[0].present('changes'):
161 clstr = str(cllist[0].get_attr_vals_utf8('changes'))
162 assert ATTR_HOMEPHONE not in clstr
163 assert ATTR_CARLICENSE in clstr
164 except ldap.LDAPError as e:
165 log.fatal("Changelog search failed, error: " + str(e))
166 assert False
167
168 #unstable or unstatus tests, skipped for now
169 @pytest.mark.flaky(max_runs=2, min_passes=1)
170 def test_retrocl_exclude_attr_mod(topology_st):
171 """ Test exclude attribute feature of the retrocl plugin for mod operation
172
173 :id: f6bef689-685b-4f86-a98d-f7e6b1fcada3
174
175 :setup: Standalone instance
176
177 :steps:
178 1. Enable dynamic plugins
179 2. Confige retro changelog plugin
180 3. Add user1 entry
181 4. Ensure entry attrs are in the changelog
182 5. Exclude an attr
183 6. Modify user1 entry
184 7. Ensure excluded attr is not in the changelog
185
186 :expectedresults:
187 1. Success
188 2. Success
189 3. Success
190 4. Success
191 5. Success
192 6. Success
193 7. Success
194 """
195
196 st = topology_st.standalone
197
198 log.info('Configure retrocl plugin')
199 rcl = RetroChangelogPlugin(st)
200 rcl.disable()
201 rcl.enable()
202 rcl.replace('nsslapd-attribute', 'nsuniqueid:targetUniqueId')
203
204 log.info('Restarting instance')
205 try:
206 st.restart()
207 except ldap.LDAPError as e:
208 ldap.error('Failed to restart instance ' + e.args[0]['desc'])
209 assert False
210
211 users = UserAccounts(st, DEFAULT_SUFFIX)
212
213 log.info('Adding user1')
214 try:
215 user1 = users.create(properties={
216 'sn': '1',
217 'cn': 'user 1',
218 'uid': 'user1',
219 'uidNumber': '11',
220 'gidNumber': '111',
221 'givenname': 'user1',
222 'homePhone': '0861234567',
223 'carLicense': '131D16674',
224 'mail': '[email protected]',
225 'homeDirectory': '/home/user1',
226 'userpassword': USER_PW})
227 except ldap.ALREADY_EXISTS:
228 user1 = UserAccount(st, dn=USER1_DN)
229 except ldap.LDAPError as e:
230 log.error("Failed to add user1: " + str(e))
231
232 log.info('Verify homePhone and carLicense attrs are in the changelog changestring')
233 try:
234 retro_changelog_suffix = DSLdapObjects(st, basedn=RETROCL_SUFFIX)
235 cllist = retro_changelog_suffix.filter(f'(targetDn={USER1_DN})')
236 except ldap.LDAPError as e:
237 log.fatal("Changelog search failed, error: " + str(e))
238 assert False
239 assert len(cllist) > 0
240 if cllist[0].present('changes'):
241 clstr = str(cllist[0].get_attr_vals_utf8('changes'))
242 assert ATTR_HOMEPHONE in clstr
243 assert ATTR_CARLICENSE in clstr
244
245 log.info('Excluding attribute ' + ATTR_CARLICENSE)
246 args = FakeArgs()
247 args.connections = [st.host + ':' + str(st.port) + ':' + DN_DM + ':' + PW_DM]
248 args.instance = 'standalone1'
249 args.basedn = None
250 args.binddn = None
251 args.starttls = False
252 args.pwdfile = None
253 args.bindpw = None
254 args.prompt = False
255 args.exclude_attrs = ATTR_CARLICENSE
256 args.max_age = None
257 args.func = retrochangelog_add
258 dsrc_inst = dsrc_arg_concat(args, None)
259 inst = connect_instance(dsrc_inst, False, args)
260 result = args.func(inst, None, log, args)
261 disconnect_instance(inst)
262 assert result is None
263
264 log.info('Restarting instance')
265 try:
266 st.restart()
267 except ldap.LDAPError as e:
268 ldap.error('Failed to restart instance ' + e.args[0]['desc'])
269 assert False
270
271 log.info('Modify user1 carLicense attribute')
272 try:
273 user1.replace(ATTR_CARLICENSE, "123WX321")
274 except ldap.LDAPError as e:
275 log.fatal('test_retrocl_exclude_attr_mod: Failed to update user1 attribute: error ' + e.message['desc'])
276 assert False
277
278 log.info('Verify carLicense attr is not in the changelog changestring')
279 try:
280 cllist = retro_changelog_suffix.filter(f'(targetDn={USER1_DN})')
281 assert len(cllist) > 0
282 # There will be 2 entries in the changelog for this user, we are only
283 #interested in the second one, the modify operation.
284 if cllist[1].present('changes'):
285 clstr = str(cllist[1].get_attr_vals_utf8('changes'))
286 assert ATTR_CARLICENSE not in clstr
287 except ldap.LDAPError as e:
288 log.fatal("Changelog search failed, error: " + str(e))
289 assert False
290
291
292 def test_retrocl_trimming(topology_st):
293 """Test retrocl trimming works
294
295 :id: 54c6747f-6772-43b7-8b03-09e13fa0c205
296 :setup: Standalone Instance
297 :steps:
298 1. Enable Retro changelog
299 2. Add a bunch of entries
300 3. Configure trimming
301 4. Verify trimming occurred
302 :expectedresults:
303 1. Success
304 2. Success
305 3. Success
306 4. Success
307 """
308
309 inst = topology_st.standalone
310 # Configure plugin
311 log.info('Configure retrocl plugin')
312 rcl = RetroChangelogPlugin(inst)
313 rcl.enable()
314 inst.restart()
315
316 # Do some updates
317 suffix = Domain(inst, DEFAULT_SUFFIX)
318 for idx in range(0, 10):
319 suffix.replace('description', str(idx))
320
321 # Setup trimming
322 rcl.replace('nsslapd-changelog-trim-interval', '2')
323 rcl.replace('nsslapd-changelogmaxage', '5s')
324 inst.config.set('nsslapd-errorlog-level', '65536') # plugin logging
325 inst.restart()
326
327 # Verify trimming occurs
328 time.sleep(5)
329 assert inst.searchErrorsLog("trim_changelog: removed ")
330
331 # Clean up
332 inst.config.set('nsslapd-errorlog-level', '0')
333
334 def test_retrocl_trimming_interval(topology_st, request):
335 """Test retrocl trimming interval works
336
337 :id: 261a951d-8fe3-4b94-b933-c21806f795e7
338 :setup: Standalone Instance
339 :steps:
340 1. Enable Retro changelog
341 2. Configure trimming (trimming records older than 20sec)
342 3. Do a dummy update and wait 5sec
343 4. Do a extra updates
344 5. wait for 10sec and restart (after restart the dummy update will soon be trimmed)
345 6. wait 15sec so that the extra updates are trimmed
346 7. stop the instance
347 8. check that dummy/extra updates have been trimmed
348 :expectedresults:
349 1. Success
350 2. Success
351 3. Success
352 4. Success
353 5. Success
354 6. Success
355 7. Success
356 8. Success
357 """
358
359 inst = topology_st.standalone
360 # Configure plugin
361 log.info('Configure retrocl plugin')
362 rcl = RetroChangelogPlugin(inst)
363 rcl.replace('nsslapd-include-suffix', DEFAULT_SUFFIX)
364 rcl.enable()
365 inst.restart()
366
367 # Do some updates
368 suffix = Domain(inst, DEFAULT_SUFFIX)
369
370 # Configure trimming
371 rcl.replace('nsslapd-changelog-trim-interval', '2')
372 rcl.replace('nsslapd-changelogmaxage', '20s')
373 inst.restart()
374
375 # this update will be the first one to be trimmed
376 # and will initialize in the last_trim time.
377 # This initialization is important to trigger the bug
378 # if not initialized it will trim immediately
379 suffix.replace('description', 'dummy')
380
381 # retrieve the index of the update in retroCL as
382 # starting point for the extra updates
383 try:
384 retro_changelog_suffix = DSLdapObjects(inst, basedn=RETROCL_SUFFIX)
385 cl_index = int(str(retro_changelog_suffix.filter(f'(targetDn={DEFAULT_SUFFIX})')[0].get_attr_vals_utf8('changeNumber')[0]))
386 except ldap.LDAPError as e:
387 log.fatal("Changelog search failed, error: " + str(e))
388 assert False
389 time.sleep(5)
390
391 # Those extra updates should be trimmed 5 sec after the previous one
392 for idx in range(0, 10):
393 suffix.replace('description', str(idx))
394
395 # wait for ~14s (sleep+stop+start), so the trimming
396 # of the first update occurs soon after the restart
397 time.sleep(10)
398 inst.config.set('nsslapd-plugin-logging', 'on')
399 inst.config.set('nsslapd-accesslog-level','260')
400 inst.stop()
401 inst.start()
402
403 # wait for an additional 15s.
404 # at that time we should have trim the extra updates
405 # checking both error logs and DEL operations in access logs
406 inst.config.set('nsslapd-errorlog-level', '65536') # plugin logging
407 time.sleep(15)
408 inst.stop()
409 assert inst.searchErrorsLog("trim_changelog: removed 9 change records")
410 for idx in range(cl_index+1,cl_index+10):
411 pattern="changenumber=%d,cn=changelog" % idx
412 assert inst.searchAccessLog(pattern)
413
414 # Clean up
415 inst.restart()
416
417 def fin():
418 inst.config.set('nsslapd-errorlog-level', '0')
419 inst.config.set('nsslapd-plugin-logging', 'off')
420 inst.config.set('nsslapd-accesslog-level','256')
421
422 request.addfinalizer(fin)
423
424
425 def test_retrocl_trimming_entries(topology_st):
426 """Test retrocl trimming reduces changelog entries after maxage timeout
427
428 :id: d7b7cf72-f47c-4b43-b25d-10f7f0ad86f2
429 :setup: Standalone Instance
430 :steps:
431 1. Enable retro changelog without trimming
432 2. Add multiple entries to create new changelog records
433 3. Verify we added the expected number of entries
434 4. Configure aggressive trimming settings and restart
435 5. Enable plugin logging and wait for trimming to occur
436 6. Verify changelog entries were reduced by checking error log and count
437 :expectedresults:
438 1. Success
439 2. Success
440 3. Success
441 4. Success
442 5. Success
443 6. Success
444 """
445
446 inst = topology_st.standalone
447 max_entries = 10
448
449 log.info('Enable retro changelog plugin without trimming')
450 rcl = RetroChangelogPlugin(inst)
451 rcl.enable()
452 rcl.remove_all('nsslapd-changelogmaxage')
453 rcl.remove_all('nsslapd-changelog-trim-interval')
454 inst.restart()
455
456 log.info('Count existing changelog entries before test')
457 retro_changelog = DSLdapObjects(inst, basedn=RETROCL_SUFFIX)
458 initial_entries = retro_changelog.filter('(changenumber=*)')
459 initial_count = len(initial_entries)
460 log.info(f'Found {initial_count} existing changelog entries')
461
462 log.info(f'Add {max_entries} user entries to generate changelog records')
463 users = UserAccounts(inst, DEFAULT_SUFFIX)
464 for idx in range(max_entries):
465 user_name = f'trimtest{idx}'
466 users.create(properties={
467 'uid': user_name,
468 'cn': user_name,
469 'sn': user_name,
470 'uidNumber': str(3000 + idx),
471 'gidNumber': str(4000 + idx),
472 'homeDirectory': f'/home/{user_name}',
473 'userPassword': 'password'
474 })
475
476 log.info('Verify we added the expected number of changelog entries')
477 entries = retro_changelog.filter('(changenumber=*)')
478 new_count = len(entries)
479 added_entries = new_count - initial_count
480 assert added_entries == max_entries
481 log.info(f'Successfully added {added_entries} changelog entries (total: {new_count})')
482
483 log.info('Configure aggressive trimming: 10s maxage, 5s trim interval')
484 rcl.replace('nsslapd-changelogmaxage', '10s')
485 rcl.replace('nsslapd-changelog-trim-interval', '5s')
486
487 log.info('Enable plugin logging to monitor trimming')
488 inst.config.set('nsslapd-errorlog-level', '65536')
489 inst.restart()
490
491 log.info('Wait for entries to age and trimming to occur')
492 for attempt in range(1, 4):
493 time.sleep(6)
494 if inst.searchErrorsLog("trim_changelog: removed "):
495 log.info(f'Trimming detected after {attempt * 6} seconds')
496 break
497
498 log.info('Verify trimming occurred by checking error log')
499 assert inst.searchErrorsLog("trim_changelog: removed ")
500
501 log.info('Verify changelog entries have been reduced')
502 entries = retro_changelog.filter('(changenumber=*)')
503 final_count = len(entries)
504 assert final_count < new_count
505 log.info(f'Trimming successful: reduced from {new_count} to {final_count} entries')
506
507
508 def test_retrocl_changelogmaxage_validation(topology_st):
509 """Verify retro changelog max age validation rejects invalid values
510
511 :id: 4fd38573-3718-4f03-8f32-fd0c9f52e0ac
512 :setup: Standalone Instance
513 :steps:
514 1. Enable retro changelog plugin
515 2. Try setting invalid nsslapd-changelogmaxage values
516 3. Verify each invalid value is rejected with UNWILLING_TO_PERFORM
517 4. Try setting valid nsslapd-changelogmaxage values
518 5. Verify valid values are accepted
519 :expectedresults:
520 1. Success
521 2. Success
522 3. Success
523 4. Success
524 5. Success
525 """
526 inst = topology_st.standalone
527 rcl = RetroChangelogPlugin(inst)
528 rcl.enable()
529 inst.restart()
530
531 invalid_values = ["-1", "1", "d", "1dd", "-12W"]
532 valid_values = ["0", "1h", "1H", "1d", "2D", "1w", "2W", "1m", "2M"]
533
534 for value in invalid_values:
535 log.info(f"Verify invalid nsslapd-changelogmaxage value is rejected: {value}")
536 with pytest.raises(ldap.UNWILLING_TO_PERFORM):
537 rcl.replace('nsslapd-changelogmaxage', value)
538
539 for value in valid_values:
540 log.info(f"Verify valid nsslapd-changelogmaxage value is accepted: {value}")
541 rcl.replace('nsslapd-changelogmaxage', value)
542
543
544 def test_retrocl_trimming_shutdown_crash(topology_st):
545 """Test that shutting down while retrocl trimming is active does not crash
546
547 :id: a178e71b-2f3a-4b12-9c01-ef7d3a5b8c42
548 :setup: Standalone Instance
549 :steps:
550 1. Enable retro changelog with aggressive trimming settings
551 2. Generate changelog entries
552 3. Wait for entries to age past maxage
553 4. Perform multiple rapid stop/start cycles during trim activity
554 5. Check for disorderly shutdown (crash) after each cycle
555 :expectedresults:
556 1. Success
557 2. Success
558 3. Success
559 4. No crash detected on any cycle
560 5. No crash detected
561 """
562 inst = topology_st.standalone
563 NUM_ENTRIES = 50
564 NUM_RESTART_CYCLES = 10
565
566 log.info('Enable retro changelog with aggressive trimming')
567 rcl = RetroChangelogPlugin(inst)
568 rcl.enable()
569 rcl.replace('nsslapd-changelogmaxage', '5s')
570 rcl.replace('nsslapd-changelog-trim-interval', '1')
571 inst.restart()
572
573 log.info(f'Generate {NUM_ENTRIES} changelog entries')
574 users = UserAccounts(inst, DEFAULT_SUFFIX)
575 for idx in range(NUM_ENTRIES):
576 users.create(properties={
577 'uid': f'crashtest{idx}',
578 'cn': f'crashtest{idx}',
579 'sn': f'crashtest{idx}',
580 'uidNumber': str(5000 + idx),
581 'gidNumber': str(6000 + idx),
582 'homeDirectory': f'/home/crashtest{idx}',
583 'userPassword': 'password'
584 })
585
586 log.info('Wait for changelog entries to age past maxage')
587 time.sleep(6)
588
589 log.info(f'Perform {NUM_RESTART_CYCLES} rapid stop/start cycles')
590 for cycle in range(NUM_RESTART_CYCLES):
591 suffix = Domain(inst, DEFAULT_SUFFIX)
592 for j in range(5):
593 suffix.replace('description', f'cycle{cycle}_update{j}')
594
595 time.sleep(1)
596 inst.stop()
597
598 log.info(f'Cycle {cycle + 1}/{NUM_RESTART_CYCLES}: checking for crash')
599 assert not inst.detectDisorderlyShutdown(), \
600 f'Server crashed during shutdown cycle {cycle + 1}'
601
602 inst.start()
603
604 log.info('Final shutdown and crash check')
605 inst.stop()
606 assert not inst.detectDisorderlyShutdown(), \
607 'Server crashed during final shutdown'
608 inst.start()
609
610
611 if __name__ == '__main__':
612 # Run isolated
613 # -s for DEBUG mode
614 CURRENT_FILE = os.path.realpath(__file__)
615 pytest.main("-s %s" % CURRENT_FILE)