Debian Codegraph @

dirsrvtests/tests/suites/replication/regression_m2_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 os
10 import re
11 import time
12 import logging
13 import ldif
14 import ldap
15 import pprint
16 import pytest
17 import subprocess
18 import time
19 import random
20 import string
21 from shutil import rmtree
22 from lib389.dbgen import dbgen_users
23 from lib389.idm.user import TEST_USER_PROPERTIES, UserAccount, UserAccounts
24 from lib389.pwpolicy import PwPolicyManager
25 from lib389.utils import *
26 from lib389._constants import *
27 from lib389.idm.domain import Domain
28 from lib389.idm.organizationalunit import OrganizationalUnits
29 from lib389.idm.group import Groups, Group
30 from lib389.idm.domain import Domain
31 from lib389.idm.directorymanager import DirectoryManager
32 from lib389.idm.services import ServiceAccounts, ServiceAccount
33 from lib389.replica import (
34 Replicas, ReplicationManager, ReplicationMonitor, ReplicaRole,
35 BootstrapReplicationManager, NormalizedRidDict
36 )
37 from lib389.agreement import Agreements
38 from lib389 import pid_from_file
39 from lib389.dseldif import *
40 from lib389.tasks import Tasks
41 from test389.topologies import topology_m2 as topo_m2, TopologyMain, create_topology, _remove_ssca_db
42
43
44 pytestmark = pytest.mark.tier1
45
46 NEW_SUFFIX_NAME = 'test_repl'
47 NEW_SUFFIX = 'o={}'.format(NEW_SUFFIX_NAME)
48 NEW_BACKEND = 'repl_base'
49 CHANGELOG = 'cn=changelog,{}'.format(DN_USERROOT_LDBM)
50 MAXAGE_ATTR = 'nsslapd-changelogmaxage'
51 MAXAGE_STR = '30'
52 TRIMINTERVAL_STR = '5'
53 TRIMINTERVAL = 'nsslapd-changelogtrim-interval'
54
55 DEBUGGING = os.getenv("DEBUGGING", default=False)
56 if DEBUGGING:
57 logging.getLogger(__name__).setLevel(logging.DEBUG)
58 else:
59 logging.getLogger(__name__).setLevel(logging.INFO)
60 log = logging.getLogger(__name__)
61
62
63 class _AgmtHelper:
64 """test_change_repl_passwd helper (Easy access to bind and agmt entries)"""
65
66 def __init__(self, from_inst, to_inst, cn = None):
67 self.from_inst = from_inst
68 self.to_inst = to_inst
69 if cn:
70 self.usedn = True
71 self.cn = cn
72 self.binddn = f'cn={cn},cn=config'
73 else:
74 self.usedn = False
75 self.cn = ldap.dn.escape_dn_chars(f'{DEFAULT_SUFFIX}:{self.from_inst.host}:{self.from_inst.sslport}')
76 self.binddn = f'cn={self.cn}, ou=Services, {DEFAULT_SUFFIX}'
77 self.original_state = []
78 self._pass = False
79
80 def _save_vals(self, entry, attrslist, name):
81 """Get current property value for cn and requested attributes"""
82 repl_prop = []
83 del_prop = []
84 for attr in attrslist:
85 try:
86 val = entry.get_attr_val_utf8(attr)
87 if val is None:
88 del_prop.append((attr,))
89 else:
90 repl_prop.append((attr,val))
91 except ldap.NO_SUCH_OBJECT:
92 del_prop.append((attr,))
93 self.original_state.append((entry, repl_prop, del_prop))
94
95 def init(self, request):
96 """Initialize the _AgmtHelper"""
97 agmt = self.get_agmt()
98 replica = Replicas(self.to_inst).get(DEFAULT_SUFFIX)
99 bind_entry = self.get_bind_entry()
100 # Preserve current configuartion
101 self._save_vals(agmt, ('nsds5ReplicaCredentials', 'nsds5ReplicaBindDN'), 'agmt')
102 self._save_vals(replica, ('nsds5ReplicaBindDN', 'nsDS5ReplicaBindDNGroup'), 'replica')
103 if not self.usedn:
104 self._save_vals(bind_entry, ('userPassword',), 'bind_entry')
105
106 if self.usedn:
107 # if using bind group, the topology is already initted (by topo_m2)
108 # if using bind dn, should create the bind entry and configure the agmt and replica
109 passwd='replrepl'
110 # Creates the bind entry
111 bind_entry.ensure_state(properties={
112 'cn' : self.cn,
113 'userPassword': passwd
114 })
115 # Modify the replica
116 replica.replace('nsds5ReplicaBindDN', self.binddn)
117 replica.remove_all('nsds5ReplicaBindDNGroup')
118 # Modify the agmt
119 agmt.replace_many( ('nsds5ReplicaCredentials', passwd),
120 ('nsds5ReplicaBindDN', self.binddn))
121 # Add a finalizer to restore the original configuration
122 def fin():
123 if not self._pass and "-x" in sys.argv:
124 # Keep config as is if debugging a failed test
125 return
126 # remove the added bind entry
127 if self.usedn:
128 bind_entry.delete()
129 # Restore the original entries
130 for entry, repl_prop, del_prop, in self.original_state:
131 log.debug(f"dn: {entry.dn} repl_prop={repl_prop} del_prop={del_prop}")
132 if repl_prop:
133 entry.replace_many(*repl_prop)
134 if del_prop:
135 for attr, in del_prop:
136 entry.remove_all(attr)
137 request.addfinalizer(fin)
138
139
140 def get_bind_entry(self):
141 """Get bind entry (on consumer)"""
142 return ServiceAccount(self.to_inst, dn=self.binddn)
143
144 def get_agmt(self):
145 """Get agmt entry (on supplier)"""
146 agmts = Agreements(self.from_inst)
147 for agmt in agmts.list():
148 port = agmt.get_attr_val_utf8('nsDS5ReplicaPort')
149 if port == str(self.to_inst.port) or port == str(self.to_inst.sslport):
150 return agmt
151 raise AssertionError(f'no agmt toward {self.to_inst.serverid} found on {self.from_inst.serverid}')
152
153 def change_pw(self, passwd):
154 """Change bind entry and agmt entry password"""
155 self.get_bind_entry().replace('userPassword', passwd)
156 self.get_agmt().replace('nsds5ReplicaCredentials', passwd)
157
158 def testok(self):
159 self._pass = True
160
161
162 def find_start_location(file, no):
163 log_pattern = re.compile("slapd_daemon - slapd started.")
164 count = 0
165 while True:
166 line = file.readline()
167 log.debug("_pattern_errorlog: [%d] %s" % (file.tell(), line))
168 found = log_pattern.search(line)
169 if (found):
170 count = count + 1
171 if (count == no):
172 return file.tell()
173 if (line == ''):
174 break
175 return -1
176
177
178 def pattern_errorlog(file, log_pattern, start_location=0):
179
180 count = 0
181 log.debug("_pattern_errorlog: start from the beginning")
182 file.seek(start_location)
183
184 # Use a while true iteration because 'for line in file: hit a
185 # python bug that break file.tell()
186 while True:
187 line = file.readline()
188 log.debug("_pattern_errorlog: [%d] %s" % (file.tell(), line))
189 found = log_pattern.search(line)
190 if (found):
191 count = count + 1
192 if (line == ''):
193 break
194
195 log.debug("_pattern_errorlog: complete (count=%d)" % count)
196 return count
197
198
199 def _move_ruv(ldif_file):
200 """ Move RUV entry in an ldif file to the top"""
201
202 with open(ldif_file) as f:
203 parser = ldif.LDIFRecordList(f)
204 parser.parse()
205
206 ldif_list = parser.all_records
207 for dn in ldif_list:
208 if dn[0].startswith('nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff'):
209 ruv_index = ldif_list.index(dn)
210 ldif_list.insert(0, ldif_list.pop(ruv_index))
211 break
212
213 with open(ldif_file, 'w') as f:
214 ldif_writer = ldif.LDIFWriter(f)
215 for dn, entry in ldif_list:
216 ldif_writer.unparse(dn, entry)
217
218
219 def _remove_replication_data(ldif_file):
220 """ Remove the replication data from ldif file:
221 db2lif without -r includes some of the replica data like
222 - nsUniqueId
223 - keepalive entries
224 This function filters the ldif fil to remove these data
225 """
226
227 with open(ldif_file) as f:
228 parser = ldif.LDIFRecordList(f)
229 parser.parse()
230
231 ldif_list = parser.all_records
232 # Iterate on a copy of the ldif entry list
233 for dn, entry in ldif_list[:]:
234 if dn.startswith('cn=repl keep alive'):
235 ldif_list.remove((dn, entry))
236 else:
237 entry.pop('nsUniqueId')
238 with open(ldif_file, 'w') as f:
239 ldif_writer = ldif.LDIFWriter(f)
240 for dn, entry in ldif_list:
241 ldif_writer.unparse(dn, entry)
242
243
244 @pytest.fixture(scope="function")
245 def topo_with_sigkill(request):
246 """Create Replication Deployment with two suppliers"""
247
248 topology = create_topology({ReplicaRole.SUPPLIER: 2})
249
250 def _kill_ns_slapd(inst):
251 pid = str(pid_from_file(inst.ds_paths.pid_file))
252 cmd = ['kill', '-9', pid]
253 subprocess.Popen(cmd, stdout=subprocess.PIPE)
254
255 def fin():
256 # Kill the hanging process at the end of test to prevent failures in the following tests
257 if DEBUGGING:
258 [_kill_ns_slapd(inst) for inst in topology]
259 else:
260 [_kill_ns_slapd(inst) for inst in topology]
261 assert _remove_ssca_db(topology)
262 [inst.stop() for inst in topology if inst.exists()]
263 [inst.delete() for inst in topology if inst.exists()]
264 request.addfinalizer(fin)
265
266 return topology
267
268
269 @pytest.fixture(scope="function")
270 def preserve_topo_m2(topo_m2, request):
271 """Backup the topology and restore it at the end."""
272
273 saves = []
274
275 def fin():
276 for inst,backup_dir in saves:
277 inst.tasks.bak2db(backup_dir=backup_dir, args={TASK_WAIT: True})
278 rmtree(backup_dir)
279
280 if not DEBUGGING:
281 request.addfinalizer(fin)
282
283 bindcn = "replication manager"
284 binddn = f"cn={bindcn},cn=config"
285 bindpw = ''.join(random.choices(string.ascii_letters + string.digits, k=15))
286 for inst in topo_m2:
287 backup_dir = f'{inst.ds_paths.backup_dir}/topo_bak'
288 try:
289 rmtree(backup_dir)
290 except FileNotFoundError:
291 pass
292 inst.tasks.db2bak(backup_dir=backup_dir, args={TASK_WAIT: True})
293 # Ensure that we are not using group bind dn
294 # because the test may delete credentials
295 replmgr = BootstrapReplicationManager(inst, dn=binddn)
296 if replmgr.exists():
297 replmgr.replace('userPassword', bindpw)
298 else:
299 replmgr.create(properties={'cn':bindcn, 'userPassword':bindpw})
300 replica = Replicas(inst).get(DEFAULT_SUFFIX)
301 replica.replace(REPL_BINDDN, binddn);
302 replica.remove_all(REPL_BIND_GROUP);
303 for agmt in replica.get_agreements().list():
304 agmt.replace_many((AGMT_CRED, bindpw), (REPL_BINDDN, binddn))
305 saves.append((inst, backup_dir))
306 return topo_m2
307
308
309 @pytest.fixture()
310 def create_entry(topo_m2, request):
311 """Add test entry using UserAccounts"""
312
313 log.info('Adding a test entry user')
314 users = UserAccounts(topo_m2.ms["supplier1"], DEFAULT_SUFFIX)
315 tuser = users.ensure_state(properties=TEST_USER_PROPERTIES)
316 return tuser
317
318
319 def add_ou_entry(server, idx, parent):
320 ous = OrganizationalUnits(server, parent)
321 name = 'OU%d' % idx
322 ous.create(properties={'ou': '%s' % name})
323
324
325 def add_user_entry(server, idx, parent):
326 users = UserAccounts(server, DEFAULT_SUFFIX, rdn=parent)
327 user_properties = {
328 'uid': 'tuser%d' % idx,
329 'givenname': 'test',
330 'cn': 'Test User%d' % idx,
331 'sn': 'user%d' % idx,
332 'userpassword': PW_DM,
333 'uidNumber': '1000%d' % idx,
334 'gidNumber': '2000%d' % idx,
335 'homeDirectory': '/home/{}'.format('tuser%d' % idx)
336 }
337 users.create(properties=user_properties)
338
339
340 def del_user_entry(server, idx, parent):
341 users = UserAccounts(server, DEFAULT_SUFFIX, rdn=parent)
342 test_user = users.get('tuser%d' % idx)
343 test_user.delete()
344
345
346 def rename_entry(server, idx, ou_name, new_parent):
347 users = UserAccounts(server, DEFAULT_SUFFIX, rdn=ou_name)
348 name = 'tuser%d' % idx
349 rdn = 'uid=%s' % name
350 test_user = users.get(name)
351 test_user.rename(new_rdn=rdn, newsuperior=new_parent)
352
353
354 def add_ldapsubentry(server, parent):
355 pwp = PwPolicyManager(server)
356 policy_props = {'passwordStorageScheme': 'ssha',
357 'passwordCheckSyntax': 'on',
358 'passwordInHistory': '6',
359 'passwordChange': 'on',
360 'passwordMinAge': '0',
361 'passwordExp': 'off',
362 'passwordMustChange': 'off',}
363 log.info('Create password policy for subtree {}'.format(parent))
364 pwp.create_subtree_policy(parent, policy_props)
365
366
367 def test_double_delete(topo_m2, create_entry):
368 """Check that double delete of the entry doesn't crash server
369
370 :id: 3496c82d-636a-48c9-973c-2455b12164cc
371 :setup: Two suppliers replication setup, a test entry
372 :steps:
373 1. Delete the entry on the first supplier
374 2. Delete the entry on the second supplier
375 3. Check that server is alive
376 :expectedresults:
377 1. Entry should be successfully deleted from first supplier
378 2. Entry should be successfully deleted from second aster
379 3. Server should me alive
380 """
381
382 m1 = topo_m2.ms["supplier1"]
383 m2 = topo_m2.ms["supplier2"]
384
385 repl = ReplicationManager(DEFAULT_SUFFIX)
386 repl.disable_to_supplier(m1, [m2])
387 repl.disable_to_supplier(m2, [m1])
388
389 log.info('Deleting entry {} from supplier1'.format(create_entry.dn))
390 topo_m2.ms["supplier1"].delete_s(create_entry.dn)
391
392 try:
393 log.info('Deleting entry {} from supplier2'.format(create_entry.dn))
394 topo_m2.ms["supplier2"].delete_s(create_entry.dn)
395 except ldap.NO_SUCH_OBJECT:
396 # replication was too fast (DEBUGGING is probably set)
397 pass
398
399 repl.enable_to_supplier(m2, [m1])
400 repl.enable_to_supplier(m1, [m2])
401
402 repl.test_replication(m1, m2)
403 repl.test_replication(m2, m1)
404
405
406 def test_repl_modrdn(topo_m2):
407 """Test that replicated MODRDN does not break replication
408
409 :id: a3e17698-9eb4-41e0-b537-8724b9915fa6
410 :setup: Two suppliers replication setup
411 :steps:
412 1. Add 3 test OrganizationalUnits A, B and C
413 2. Add 1 test user under OU=A
414 3. Add same test user under OU=B
415 4. Stop Replication
416 5. Apply modrdn to M1 - move test user from OU A -> C
417 6. Apply modrdn on M2 - move test user from OU B -> C
418 7. Start Replication
419 8. Check that there should be only one test entry under ou=C on both suppliers
420 9. Check that the replication is working fine both ways M1 <-> M2
421 :expectedresults:
422 1. This should pass
423 2. This should pass
424 3. This should pass
425 4. This should pass
426 5. This should pass
427 6. This should pass
428 7. This should pass
429 8. This should pass
430 9. This should pass
431 """
432
433 supplier1 = topo_m2.ms["supplier1"]
434 supplier2 = topo_m2.ms["supplier2"]
435
436 repl = ReplicationManager(DEFAULT_SUFFIX)
437
438 log.info("Add test entries - Add 3 OUs and 2 same users under 2 different OUs")
439 OUs = OrganizationalUnits(supplier1, DEFAULT_SUFFIX)
440 OU_A = OUs.create(properties={
441 'ou': 'A',
442 'description': 'A',
443 })
444 OU_B = OUs.create(properties={
445 'ou': 'B',
446 'description': 'B',
447 })
448 OU_C = OUs.create(properties={
449 'ou': 'C',
450 'description': 'C',
451 })
452
453 users = UserAccounts(supplier1, DEFAULT_SUFFIX, rdn='ou={}'.format(OU_A.rdn))
454 tuser_A = users.create(properties=TEST_USER_PROPERTIES)
455
456 users = UserAccounts(supplier1, DEFAULT_SUFFIX, rdn='ou={}'.format(OU_B.rdn))
457 tuser_B = users.create(properties=TEST_USER_PROPERTIES)
458
459 repl.test_replication(supplier1, supplier2)
460 repl.test_replication(supplier2, supplier1)
461
462 log.info("Stop Replication")
463 topo_m2.pause_all_replicas()
464
465 log.info("Apply modrdn to M1 - move test user from OU A -> C")
466 supplier1.rename_s(tuser_A.dn, 'uid=testuser1', newsuperior=OU_C.dn, delold=1)
467
468 log.info("Apply modrdn on M2 - move test user from OU B -> C")
469 supplier2.rename_s(tuser_B.dn, 'uid=testuser1', newsuperior=OU_C.dn, delold=1)
470
471 log.info("Start Replication")
472 topo_m2.resume_all_replicas()
473
474 log.info("Wait for sometime for repl to resume")
475 repl.test_replication(supplier1, supplier2)
476 repl.test_replication(supplier2, supplier1)
477
478 log.info("Check that there should be only one test entry under ou=C on both suppliers")
479 users = UserAccounts(supplier1, DEFAULT_SUFFIX, rdn='ou={}'.format(OU_C.rdn))
480 assert len(users.list()) == 1
481
482 users = UserAccounts(supplier2, DEFAULT_SUFFIX, rdn='ou={}'.format(OU_C.rdn))
483 assert len(users.list()) == 1
484
485 log.info("Check that the replication is working fine both ways, M1 <-> M2")
486 repl.test_replication(supplier1, supplier2)
487 repl.test_replication(supplier2, supplier1)
488
489
490 def test_password_repl_error(topo_m2, create_entry):
491 """Check that error about userpassword replication is properly logged
492
493 :id: 714130ff-e4f0-4633-9def-c1f4b24abfef
494 :setup: Four suppliers replication setup, a test entry
495 :steps:
496 1. Change userpassword on the first supplier
497 2. Restart the servers to flush the logs
498 3. Check the error log for an replication error
499 :expectedresults:
500 1. Password should be successfully changed
501 2. Server should be successfully restarted
502 3. There should be no replication errors in the error log
503 """
504
505 m1 = topo_m2.ms["supplier1"]
506 m2 = topo_m2.ms["supplier2"]
507 TEST_ENTRY_NEW_PASS = 'new_pass'
508
509 log.info('Clean the error log')
510 m2.deleteErrorLogs()
511
512 log.info('Set replication loglevel')
513 m2.config.loglevel((ErrorLog.REPLICA,))
514
515 log.info('Modifying entry {} - change userpassword on supplier 1'.format(create_entry.dn))
516
517 create_entry.set('userpassword', TEST_ENTRY_NEW_PASS)
518
519 repl = ReplicationManager(DEFAULT_SUFFIX)
520 repl.wait_for_replication(m1, m2)
521
522 log.info('Restart the servers to flush the logs')
523 for num in range(1, 3):
524 topo_m2.ms["supplier{}".format(num)].restart()
525
526 try:
527 log.info('Check that password works on supplier 2')
528 create_entry_m2 = UserAccount(m2, create_entry.dn)
529 create_entry_m2.bind(TEST_ENTRY_NEW_PASS)
530
531 log.info('Check the error log for the error with {}'.format(create_entry.dn))
532 assert not m2.ds_error_log.match('.*can.t add a change for {}.*'.format(create_entry.dn))
533 finally:
534 log.info('Set the default loglevel')
535 m2.config.loglevel((ErrorLog.DEFAULT,))
536
537
538 def test_invalid_agmt(topo_m2):
539 """Test adding that an invalid agreement is properly rejected and does not crash the server
540
541 :id: 6c3b2a7e-edcd-4327-a003-6bd878ff722b
542 :setup: Four suppliers replication setup
543 :steps:
544 1. Add invalid agreement (nsds5ReplicaEnabled set to invalid value)
545 2. Verify the server is still running
546 :expectedresults:
547 1. Invalid repl agreement should be rejected
548 2. Server should be still running
549 """
550
551 m1 = topo_m2.ms["supplier1"]
552 m2 = topo_m2.ms["supplier2"]
553
554 repl = ReplicationManager(DEFAULT_SUFFIX)
555
556 replicas = Replicas(m1)
557 replica = replicas.get(DEFAULT_SUFFIX)
558 agmts = replica.get_agreements()
559
560 # Add invalid agreement (nsds5ReplicaEnabled set to invalid value)
561 with pytest.raises(ldap.UNWILLING_TO_PERFORM):
562 agmts.create(properties={
563 'cn': 'whatever',
564 'nsDS5ReplicaRoot': DEFAULT_SUFFIX,
565 'nsDS5ReplicaBindDN': 'cn=replication manager,cn=config',
566 'nsDS5ReplicaBindMethod': 'simple',
567 'nsDS5ReplicaTransportInfo': 'LDAP',
568 'nsds5replicaTimeout': '5',
569 'description': "test agreement",
570 'nsDS5ReplicaHost': m2.host,
571 'nsDS5ReplicaPort': str(m2.port),
572 'nsDS5ReplicaCredentials': 'whatever',
573 'nsds5ReplicaEnabled': 'YEAH MATE, LETS REPLICATE'
574 })
575
576 # Verify the server is still running
577 repl = ReplicationManager(DEFAULT_SUFFIX)
578 repl.test_replication(m1, m2)
579 repl.test_replication(m2, m1)
580
581
582 def test_fetch_bindDnGroup(topo_m2):
583 """Check the bindDNGroup is fetched on first replication session
584
585 :id: 5f1b1f59-6744-4260-b091-c82d22130025
586 :setup: 2 Supplier Instances
587 :steps:
588 1. Create a replication bound user and group, but the user *not* member of the group
589 2. Check that replication is working
590 3. Some preparation is required because of lib389 magic that already define a replication via group
591 - define the group as groupDN for replication and 60sec as fetch interval
592 - pause RA in both direction
593 - Define the user as bindDn of the RAs
594 4. restart servers.
595 It sets the fetch time to 0, so next session will refetch the group
596 5. Before resuming RA, add user to groupDN (on both side as replication is not working at that time)
597 6. trigger an update and check replication is working and
598 there is no failure logged on supplier side 'does not have permission to supply replication updates to the replica'
599 :expectedresults:
600 1. Success
601 2. Success
602 3. Success
603 4. Success
604 5. Success
605 6. Success
606 """
607
608 # If you need any test suite initialization,
609 # please, write additional fixture for that (including finalizer).
610 # Topology for suites are predefined in lib389/topologies.py.
611
612 # If you need host, port or any other data about instance,
613 # Please, use the instance object attributes for that (for example, topo.ms["supplier1"].serverid)
614 M1 = topo_m2.ms['supplier1']
615 M2 = topo_m2.ms['supplier2']
616
617 # Enable replication log level. Not really necessary
618 M1.modify_s('cn=config', [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', b'8192')])
619 M2.modify_s('cn=config', [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', b'8192')])
620
621 # Create a group and a user
622 PEOPLE = "ou=People,%s" % SUFFIX
623 PASSWD = 'password'
624 REPL_MGR_BOUND_DN = 'repl_mgr_bound_dn'
625
626 uid = REPL_MGR_BOUND_DN.encode()
627 users = UserAccounts(M1, PEOPLE, rdn=None)
628 user_props = TEST_USER_PROPERTIES.copy()
629 user_props.update({'uid': uid, 'cn': uid, 'sn': '_%s' % uid, 'userpassword': PASSWD.encode(), 'description': b'value creation'})
630 create_user = users.create(properties=user_props)
631
632 groups_M1 = Groups(M1, DEFAULT_SUFFIX)
633 group_properties = {
634 'cn': 'group1',
635 'description': 'testgroup'}
636 group_M1 = groups_M1.create(properties=group_properties)
637 group_M2 = Group(M2, group_M1.dn)
638 assert(not group_M1.is_member(create_user.dn))
639
640 # Check that M1 and M2 are in sync
641 repl = ReplicationManager(DEFAULT_SUFFIX)
642 repl.wait_for_replication(M1, M2, timeout=20)
643
644 save_values = [] # Save original value to restore them at the end of the test
645 # Define the group as the replication manager and fetch interval as 60sec
646 replicas = Replicas(M1)
647 replica = replicas.list()[0]
648 save_values.append((replica, 'nsDS5ReplicaBindDnGroupCheckInterval', replica.get_attr_val_utf8('nsDS5ReplicaBindDnGroupCheckInterval')))
649 save_values.append((replica, 'nsDS5ReplicaBindDnGroup', replica.get_attr_val_utf8('nsDS5ReplicaBindDnGroup')))
650 replica.apply_mods([(ldap.MOD_REPLACE, 'nsDS5ReplicaBindDnGroupCheckInterval', '60'),
651 (ldap.MOD_REPLACE, 'nsDS5ReplicaBindDnGroup', group_M1.dn)])
652
653 replicas = Replicas(M2)
654 replica = replicas.list()[0]
655 save_values.append((replica, 'nsDS5ReplicaBindDnGroupCheckInterval', replica.get_attr_val_utf8('nsDS5ReplicaBindDnGroupCheckInterval')))
656 save_values.append((replica, 'nsDS5ReplicaBindDnGroup', replica.get_attr_val_utf8('nsDS5ReplicaBindDnGroup')))
657 replica.apply_mods([(ldap.MOD_REPLACE, 'nsDS5ReplicaBindDnGroupCheckInterval', '60'),
658 (ldap.MOD_REPLACE, 'nsDS5ReplicaBindDnGroup', group_M1.dn)])
659
660 # Then pause the replication agreement to prevent them trying to acquire
661 # while the user is not member of the group
662 topo_m2.pause_all_replicas()
663
664 # Define the user as the bindDN of the RAs
665 for inst in (M1, M2):
666 agmts = Agreements(inst)
667 agmt = agmts.list()[0]
668 save_values.append((agmt, 'nsDS5ReplicaBindDN', agmt.get_attr_val_utf8('nsDS5ReplicaBindDN')))
669 save_values.append((agmt, 'nsds5ReplicaCredentials', agmt.get_attr_val_utf8('nsds5ReplicaCredentials')))
670 agmt.replace('nsDS5ReplicaBindDN', create_user.dn.encode())
671 agmt.replace('nsds5ReplicaCredentials', PASSWD.encode())
672
673 # Key step
674 # The restart will fetch the group/members define in the replica
675 #
676 # The user NOT member of the group replication will not work until bindDNcheckInterval
677 #
678 # With the fix, the first fetch is not taken into account (fetch time=0)
679 # so on the first session, the group will be fetched
680 M1.restart()
681 M2.restart()
682
683 # Replication being broken here we need to directly do the same update.
684 # Sorry not found another solution except total update
685 group_M1.add_member(create_user.dn)
686 group_M2.add_member(create_user.dn)
687
688 topo_m2.resume_all_replicas()
689
690 # trigger updates to be sure to have a replication session, giving some time
691 M1.modify_s(create_user.dn, [(ldap.MOD_ADD, 'description', b'value_1_1')])
692 M2.modify_s(create_user.dn, [(ldap.MOD_ADD, 'description', b'value_2_2')])
693 time.sleep(10)
694
695 # Check replication is working
696 ents = M1.search_s(create_user.dn, ldap.SCOPE_BASE, '(objectclass=*)')
697 for ent in ents:
698 assert (ent.hasAttr('description'))
699 found = 0
700 for val in ent.getValues('description'):
701 if (val == b'value_1_1'):
702 found = found + 1
703 elif (val == b'value_2_2'):
704 found = found + 1
705 assert (found == 2)
706
707 ents = M2.search_s(create_user.dn, ldap.SCOPE_BASE, '(objectclass=*)')
708 for ent in ents:
709 assert (ent.hasAttr('description'))
710 found = 0
711 for val in ent.getValues('description'):
712 if (val == b'value_1_1'):
713 found = found + 1
714 elif (val == b'value_2_2'):
715 found = found + 1
716 assert (found == 2)
717
718 # Check in the logs that the member was detected in the group although
719 # at startup it was not member of the group
720 regex = re.compile("does not have permission to supply replication updates to the replica.")
721 errorlog_M1 = open(M1.errlog, "r")
722 errorlog_M2 = open(M1.errlog, "r")
723
724 # Find the last restart position
725 restart_location_M1 = find_start_location(errorlog_M1, 2)
726 assert (restart_location_M1 != -1)
727 restart_location_M2 = find_start_location(errorlog_M2, 2)
728 assert (restart_location_M2 != -1)
729
730 # Then check there is no failure to authenticate
731 count = pattern_errorlog(errorlog_M1, regex, start_location=restart_location_M1)
732 assert(count <= 1)
733 count = pattern_errorlog(errorlog_M2, regex, start_location=restart_location_M2)
734 assert(count <= 1)
735
736 # Restore the agmt values to avoid impacting the other tests.
737 for entry, attr, val in save_values:
738 entry.replace(attr, val)
739
740
741 def test_plugin_bind_dn_tracking_and_replication(topo_m2):
742 """Testing nsslapd-plugin-binddn-tracking does not cause issues around
743 access control and reconfiguring replication/repl agmt.
744
745 :id: dd689d03-69b8-4bf9-a06e-2acd19d5e2c9
746 :setup: 2 supplier topology
747 :steps:
748 1. Turn on plugin binddn tracking
749 2. Add some users
750 3. Make an update as a user
751 4. Make an update to the replica config
752 5. Make an update to the repliocation agreement
753 :expectedresults:
754 1. Success
755 2. Success
756 3. Success
757 4. Success
758 5. Success
759 """
760
761 m1 = topo_m2.ms["supplier1"]
762
763 # Turn on bind dn tracking
764 m1.config.set('nsslapd-plugin-binddn-tracking', 'on')
765
766 # Add two users
767 users = UserAccounts(m1, DEFAULT_SUFFIX)
768 user1 = users.create_test_user(uid=1011)
769 user1.set('userpassword', PASSWORD)
770 user2 = users.create_test_user(uid=1012)
771
772 # Add an aci
773 acival = '(targetattr ="cn")(version 3.0;acl "Test bind dn tracking"' + \
774 ';allow (all) (userdn = "ldap:///{}");)'.format(user1.dn)
775 Domain(m1, DEFAULT_SUFFIX).add('aci', acival)
776
777 # Bind as user and make an update
778 user1.rebind(PASSWORD)
779 user2.set('cn', 'new value')
780 dm = DirectoryManager(m1)
781 dm.rebind()
782
783 # modify replica
784 replica = Replicas(m1).get(DEFAULT_SUFFIX)
785 replica.set(REPL_PROTOCOL_TIMEOUT, "30")
786
787 # modify repl agmt
788 agmt = replica.get_agreements().list()[0]
789 agmt.set(REPL_PROTOCOL_TIMEOUT, "20")
790
791
792 def test_moving_entry_make_online_init_fail(topo_m2):
793 """
794 Moving an entry could make the online init fail
795
796 :id: e3895be7-884a-4e9f-80e3-24e9a5167c9e
797 :setup: Two suppliers replication setup
798 :steps:
799 1. Generate DIT_0
800 2. Generate password policy for DIT_0
801 3. Create users for DIT_0
802 4. Turn idx % 2 == 0 users into tombstones
803 5. Generate DIT_1
804 6. Move 'ou=OU0,ou=OU0,dc=example,dc=com' to DIT_1
805 7. Move 'ou=OU0,dc=example,dc=com' to DIT_1
806 8. Move idx % 2 == 1 users to 'ou=OU0,ou=OU0,ou=OU1,dc=example,dc=com'
807 9. Init replicas
808 10. Number of entries should match on both suppliers
809
810 :expectedresults:
811 1. Success
812 2. Success
813 3. Success
814 4. Success
815 5. Success
816 6. Success
817 7. Success
818 8. Success
819 9. Success
820 10. Success
821 """
822
823 M1 = topo_m2.ms["supplier1"]
824 M2 = topo_m2.ms["supplier2"]
825
826 log.info("Generating DIT_0")
827 idx = 0
828 add_ou_entry(M1, idx, DEFAULT_SUFFIX)
829 log.info("Created entry: ou=OU0, dc=example, dc=com")
830
831 ou0 = 'ou=OU%d' % idx
832 first_parent = '%s,%s' % (ou0, DEFAULT_SUFFIX)
833 add_ou_entry(M1, idx, first_parent)
834 log.info("Created entry: ou=OU0, ou=OU0, dc=example, dc=com")
835
836 add_ldapsubentry(M1, first_parent)
837
838 ou_name = 'ou=OU%d,ou=OU%d' % (idx, idx)
839 second_parent = 'ou=OU%d,%s' % (idx, first_parent)
840 for idx in range(0, 9):
841 add_user_entry(M1, idx, ou_name)
842 if idx % 2 == 0:
843 log.info("Turning tuser%d into a tombstone entry" % idx)
844 del_user_entry(M1, idx, ou_name)
845
846 log.info('%s => %s => %s => 10 USERS' % (DEFAULT_SUFFIX, first_parent, second_parent))
847
848 log.info("Generating DIT_1")
849 idx = 1
850 add_ou_entry(M1, idx, DEFAULT_SUFFIX)
851 log.info("Created entry: ou=OU1,dc=example,dc=com")
852
853 third_parent = 'ou=OU%d,%s' % (idx, DEFAULT_SUFFIX)
854 add_ou_entry(M1, idx, third_parent)
855 log.info("Created entry: ou=OU1, ou=OU1, dc=example, dc=com")
856
857 add_ldapsubentry(M1, third_parent)
858
859 log.info("Moving %s to DIT_1" % second_parent)
860 OrganizationalUnits(M1, second_parent).get('OU0').rename(ou0, newsuperior=third_parent)
861
862 log.info("Moving %s to DIT_1" % first_parent)
863 fourth_parent = '%s,%s' % (ou0, third_parent)
864 OrganizationalUnits(M1, first_parent).get('OU0').rename(ou0, newsuperior=fourth_parent)
865
866 fifth_parent = '%s,%s' % (ou0, fourth_parent)
867
868 ou_name = 'ou=OU0,ou=OU1'
869 log.info("Moving USERS to %s" % fifth_parent)
870 for idx in range(0, 9):
871 if idx % 2 == 1:
872 rename_entry(M1, idx, ou_name, fifth_parent)
873
874 log.info('%s => %s => %s => %s => 10 USERS' % (DEFAULT_SUFFIX, third_parent, fourth_parent, fifth_parent))
875
876 log.info("Run Initialization.")
877 repl = ReplicationManager(DEFAULT_SUFFIX)
878 repl.wait_for_replication(M1, M2, timeout=5)
879
880 m1entries = M1.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE,
881 '(|(objectclass=ldapsubentry)(objectclass=nstombstone)(nsuniqueid=*))')
882 m2entries = M2.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE,
883 '(|(objectclass=ldapsubentry)(objectclass=nstombstone)(nsuniqueid=*))')
884
885 log.info("m1entry count - %d", len(m1entries))
886 log.info("m2entry count - %d", len(m2entries))
887
888 assert len(m1entries) == len(m2entries)
889
890
891 def get_keepalive_entries(instance, replica):
892 # Returns the keep alive entries that exists with the suffix of the server instance
893 try:
894 entries = instance.search_s(replica.get_suffix(), ldap.SCOPE_ONELEVEL,
895 "(&(objectclass=ldapsubentry)(cn=repl keep alive*))",
896 ['cn', 'keepalivetimestamp', 'nsUniqueId', 'modifierTimestamp'])
897 except ldap.LDAPError as e:
898 log.fatal('Failed to retrieve keepalive entry (%s) on instance %s: error %s' % (dn, instance, str(e)))
899 assert False
900 # No error, so lets log the keepalive entries
901 if log.isEnabledFor(logging.DEBUG):
902 for ret in entries:
903 log.debug("Found keepalive entry:\n"+str(ret));
904 return entries
905
906
907 def verify_keepalive_entries(topo, expected):
908 # Check that keep alive entries exists (or not exists) for every suppliers on every suppliers
909 # Note: The testing method is quite basic: counting that there is one keepalive entry per supplier.
910 # that is ok for simple test cases like test_online_init_should_create_keepalive_entries but
911 # not for the general case as keep alive associated with no more existing supplier may exists
912 # (for example after: db2ldif / demote a supplier / ldif2db / init other suppliers)
913 # ==> if the function is somehow pushed in lib389, a check better than simply counting the entries
914 # should be done.
915 entries = []
916 for supplierId in topo.ms:
917 supplier = topo.ms[supplierId]
918 for replica in Replicas(supplier).list():
919 if (replica.get_role() != ReplicaRole.SUPPLIER):
920 continue
921 replica_info = f'supplier: {supplierId} RID: {replica.get_rid()} suffix: {replica.get_suffix()}'
922 log.debug(f'Checking keepAliveEntries on {replica_info}')
923 keepaliveEntries = get_keepalive_entries(supplier, replica);
924 expectedCount = len(topo.ms) if expected else 0
925 foundCount = len(keepaliveEntries)
926 entries += keepaliveEntries
927 if (foundCount == expectedCount):
928 log.debug(f'Found {foundCount} keepalive entries as expected on {replica_info}.')
929 else:
930 log.error(f'{foundCount} Keepalive entries are found '
931 f'while {expectedCount} were expected on {replica_info}.')
932 assert False
933
934 return entries
935
936
937 def test_keepalive_entries(topo_m2):
938 """Check that keep alive entries are created
939
940 :id: d5940e71-d18a-4b71-aaf7-b9185361fffe
941 :setup: Two suppliers replication setup
942 :steps:
943 1. Keep alives entries are present
944 2. Keep alive entries are updated every 60 seconds
945 :expectedresults:
946 1. Success
947 2. Success
948
949 """
950
951 # default interval is 1 hour, too long for test, set it to the minimum of
952 # 60 seconds
953 for supplierId in topo_m2.ms:
954 supplier = topo_m2.ms[supplierId]
955 replica = Replicas(supplier).get(DEFAULT_SUFFIX)
956 replica.replace('nsds5ReplicaKeepAliveUpdateInterval', '60')
957 supplier.restart()
958
959 # verify entries exist
960 entries = verify_keepalive_entries(topo_m2, True);
961
962 # Get current time from keep alive entry
963 keep_alive_s1 = str(entries[0].data['keepalivetimestamp'])
964 keep_alive_s2 = str(entries[1].data['keepalivetimestamp'])
965
966 # Wait for event interval (60 secs) to pass, but first update doesn't
967 # start until 30 seconds after startup
968 time.sleep(91)
969
970 # Check keep alives entries have been updated
971 entries = verify_keepalive_entries(topo_m2, True);
972 assert keep_alive_s1 != str(entries[0].data['keepalivetimestamp'])
973 assert keep_alive_s2 != str(entries[1].data['keepalivetimestamp'])
974
975 # Test replication
976 supplier = topo_m2.ms['supplier1']
977 replica = Replicas(supplier).get(DEFAULT_SUFFIX)
978 assert replica.test_replication([topo_m2.ms['supplier2']])
979
980
981 # Parameters for test_change_repl_passwd
982 @pytest.mark.parametrize(
983 "bind_cn",
984 [
985 pytest.param( None, id="using-bind-group"),
986 pytest.param( "replMgr", id="using-bind-dn"),
987 ],
988 )
989 def test_change_repl_passwd(topo_m2, request, bind_cn):
990 """Replication may break after changing password.
991 Testing when agmt bind group are used.
992
993 :id: a305913a-cc76-11ec-b324-482ae39447e5
994 :parametrized: yes
995 :setup: 2 Supplier Instances
996 :steps:
997 1. Insure agmt from supplier1 to supplier2 is properly set to use bind group
998 2. Insure agmt from supplier2 to supplier1 is properly set to use bind group
999 3. Check that replication is working
1000 4. Modify supplier1 agreement password and the associated bind entry
1001 5. Modify supplier2 agreement password and the associated bind entry
1002 6. Check that replication is working
1003 :expectedresults:
1004 1. Step should run sucessfully without errors
1005 2. Step should run sucessfully without errors
1006 3. Replication should work
1007 4. Step should run sucessfully without errors
1008 5. Step should run sucessfully without errors
1009 6. Replication should work
1010 """
1011
1012 m1 = topo_m2.ms["supplier1"]
1013 m2 = topo_m2.ms["supplier2"]
1014 # Step 1
1015 a1 = _AgmtHelper(m1, m2, cn=bind_cn)
1016 a1.init(request)
1017 # Step 2
1018 a2 = _AgmtHelper(m2, m1, cn=bind_cn)
1019 a2.init(request)
1020 # Step 3
1021 repl = ReplicationManager(DEFAULT_SUFFIX)
1022 repl.wait_for_replication(m1, m2)
1023 # Step 4
1024 TEST_ENTRY_NEW_PASS = 'new_pass2'
1025 a1.change_pw(TEST_ENTRY_NEW_PASS)
1026 # Step 5
1027 a2.change_pw(TEST_ENTRY_NEW_PASS)
1028 # Step 6
1029 repl.wait_for_replication(m1, m2)
1030 # Mark test as successul before exiting
1031 a1.testok()
1032 a2.testok()
1033
1034
1035 def test_repl_after_reindex(topo_m2):
1036 """Check that replication does not break after reindex.
1037
1038 :id: 0b48992e-bac6-11ee-9cbe-482ae39447e5
1039 :setup: 2 Supplier Instances
1040 :steps:
1041 1. Check that replication is working
1042 2. Perform a few changes on supplier1
1043 3. Reindex 'cn' on supplier1
1044 4. Check that replication is still working
1045 :expectedresults:
1046 1. Replication should work
1047 2. Step should run sucessfully without errors
1048 3. Step should run sucessfully without errors
1049 4. Replication should still work
1050 """
1051
1052 m1 = topo_m2.ms["supplier1"]
1053 m2 = topo_m2.ms["supplier2"]
1054 repl = ReplicationManager(DEFAULT_SUFFIX)
1055 tasks = Tasks(m1)
1056 # Check that replication is working
1057 repl.wait_for_replication(m1, m2)
1058 repl.wait_for_replication(m2, m1)
1059 # Perform a few changes on supplier1
1060 for idx in range(2):
1061 Domain(m1, DEFAULT_SUFFIX).replace('description', f'test_repl_after_reindex {idx}')
1062 # Reindex 'cn' on supplier1
1063 tasks.reindex(
1064 suffix=DEFAULT_SUFFIX,
1065 attrname='cn',
1066 args={TASK_WAIT: True}
1067 )
1068 # Check that replication is still working
1069 repl.wait_for_replication(m1, m2)
1070 repl.wait_for_replication(m2, m1)
1071
1072
1073 def test_suffix_entryid(preserve_topo_m2):
1074 """Test that entryid attribute is present in suffix entry
1075
1076 :id: 9201f2c8-3eb8-11ef-80cc-482ae39447e5
1077 :setup: Two suppliers replicated instances
1078 :steps:
1079 1. Generate LDIF file
1080 2. Import the ldif file
1081 3. Check that identry is still present in suffix entry
1082 4. Check that parentid>=1 search returns all entries but one
1083 :expectedresults:
1084 1. Operation successful
1085 2. Operation successful
1086 3. Suffix id2entry should be 1
1087 4. parentid>=1 search returns all entries but one
1088 """
1089 s1 = preserve_topo_m2.ms["supplier1"]
1090 ldif_file = f'{s1.get_ldif_dir()}/db10.ldif'
1091 dbgen_users(s1, 10, ldif_file, DEFAULT_SUFFIX)
1092 s1.tasks.importLDIF(benamebase=DEFAULT_BENAME,
1093 input_file=ldif_file,
1094 args={TASK_WAIT: True})
1095 dc = Domain(s1, dn=DEFAULT_SUFFIX)
1096 assert dc.get_attr_val_utf8('entryid') == '1'
1097 all_entries = s1.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "(objectclass=*)", attrlist=('dn',), escapehatch='i am sure')
1098 childrens = s1.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "(parentid>=1)", attrlist=('dn',), escapehatch='i am sure')
1099 assert len(all_entries) == (len(childrens) + 1)
1100
1101
1102 def test_bulk_import(preserve_topo_m2):
1103 """Test that bulk import is working properly
1104
1105 :id: 4e73254a-3ed6-11ef-aa44-482ae39447e5
1106 :setup: Two suppliers replicated instances
1107 :steps:
1108 1. Generate LDIF file
1109 2. Import the ldif file
1110 3. Add replication_managers group
1111 4. Perform bulk import
1112 5. Check that replication is still working
1113 6. Check that the replicas have the same number of users
1114 :expectedresults:
1115 1. Operation successful
1116 2. Operation successful
1117 3. Operation successful
1118 4. Operation successful
1119 5. Replication should be in sync
1120 6. Replicas should have same number of user entries
1121 """
1122 s1 = preserve_topo_m2.ms["supplier1"]
1123 s2 = preserve_topo_m2.ms["supplier2"]
1124 ldif_file = f'{s1.get_ldif_dir()}/db2K.ldif'
1125 dbgen_users(s1, 2000, ldif_file, DEFAULT_SUFFIX)
1126 s1.tasks.importLDIF(benamebase=DEFAULT_BENAME,
1127 input_file=ldif_file,
1128 args={TASK_WAIT: True})
1129
1130 # Create replication_managers group so that we can use
1131 # repl.test_replication_topology
1132 repl = ReplicationManager(DEFAULT_SUFFIX)
1133 repl._create_service_group(s1)
1134 repl._create_service_account(s1, s2)
1135
1136 agmt = Agreements(s1).list()[0]
1137 agmt.begin_reinit()
1138 (done, error) = agmt.wait_reinit()
1139 assert done is True
1140 assert error is False
1141
1142 repl.test_replication_topology(preserve_topo_m2)
1143
1144 # Cannot use UserAccounts().list() because 'account' objectclass is missing
1145 users_s1 = s1.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "(uid=*)", escapehatch='i am sure')
1146 log.info(f"{len(users_s1)} user entries found on supplier1")
1147 users_s2 = s2.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, "(uid=*)", escapehatch='i am sure')
1148 log.info(f"{len(users_s2)} user entries found on supplier2")
1149 assert len(users_s1) == len(users_s2)
1150
1151
1152 def check_monitoring_status(inst):
1153 creds = { 'binddn': DN_DM, 'bindpw': PW_DM }
1154 repl_monitor = ReplicationMonitor(inst)
1155 report_dict = repl_monitor.generate_report(lambda h,p: creds, use_json=True)
1156 log.debug(f'(Monitoring status: {pprint.pformat(report_dict)}')
1157
1158 agmts_status = {}
1159 for inst_status in report_dict.values():
1160 for replica_status in inst_status:
1161 suffix = replica_status['replica_root']
1162 rid = replica_status['replica_id']
1163 for agmt_status in replica_status['agmts_status']:
1164 rag_status = agmt_status['replication-status'][0]
1165 if 'Unavailable' in rag_status:
1166 aname = agmt_status['agmt-name'][0]
1167 url = f'{agmt_status["replica"][0]}/{suffix}'
1168 assert False, f"'Unavailable' found in agreement {aname} of replica {url} : {rag_status}"
1169
1170 assert 'Unavailable' not in str(report_dict)
1171
1172
1173 def reinit_replica(S1, S2):
1174 # Reinit replication
1175 agmt = Agreements(S1).list()[0]
1176 agmt.begin_reinit()
1177 (done, error) = agmt.wait_reinit()
1178 assert done is True
1179 assert error is False
1180
1181 repl = ReplicationManager(DEFAULT_SUFFIX)
1182 repl.wait_for_replication(S1, S2)
1183 repl.wait_for_replication(S2, S1)
1184
1185
1186 def test_rid_starting_with_0(topo_m2, request):
1187 """Check that replication monitoring works if replica
1188 id starts with 0
1189
1190 :id: ed0176e6-0bf7-11f0-9846-482ae39447e5
1191 :setup: 2 Supplier Instances
1192 :steps:
1193 1. Initialize replication to ensure that init status is set
1194 2. Check that monitoring status does not contains 'Unavailable'
1195 3. Change replica ids to 001 and 002
1196 4. Initialize replication to ensure that init status is set
1197 5. Check that monitoring status does not contains 'Unavailable'
1198 6. Restore the replica ids to 1 and 2
1199 :expectedresults:
1200 1. Success
1201 2. Success
1202 3. Success
1203 4. Success
1204 5. Success
1205 6. Success
1206 """
1207 S1 = topo_m2.ms["supplier1"]
1208 S2 = topo_m2.ms["supplier2"]
1209 replicas = [ Replicas(inst).get(DEFAULT_SUFFIX) for inst in topo_m2 ]
1210
1211 # Reinit replication (to ensure that init status is set)
1212 reinit_replica(S1, S2)
1213
1214 # Get replication monitoring results
1215 check_monitoring_status(S1)
1216
1217 # Change replica id
1218 for replica,rid in zip(replicas, ['010', '020']):
1219 replica.replace('nsDS5ReplicaId', rid)
1220
1221 # Restart required - replica IDs are loaded at startup and cached in memory
1222 S1.restart()
1223 S2.restart()
1224
1225 # Restore replica id in finalizer
1226 def fin():
1227 for replica,rid in zip(replicas, ['1', '2']):
1228 replica.replace('nsDS5ReplicaId', rid)
1229 reinit_replica(S1, S2)
1230
1231 request.addfinalizer(fin)
1232 # Reinit replication
1233 reinit_replica(S1, S2)
1234
1235 # Get replication monitoring results
1236 check_monitoring_status(S1)
1237
1238
1239 def test_normalized_rid_dict():
1240 """Check that lib389.replica NormalizedRidDict class behaves as expected
1241
1242 :id: 0f88a29c-0fcd-11f0-b5df-482ae39447e5
1243 :setup: None
1244 :steps:
1245 1. Initialize a NormalizedRidDict
1246 2. Check that normalization do something
1247 3. Check that key stored in NormalizedRidDict are normalized
1248 4. Check that normalized and non normalized keys have the same value
1249 :expectedresults:
1250 1. Success
1251 2. Success
1252 3. Success
1253 4. Success
1254 """
1255
1256 sd = { '1': 'v1', '020': 'v2' }
1257 nsd = { NormalizedRidDict.normalize_rid(key): val for key,val in sd.items() }
1258 nkeys = list(nsd.keys())
1259
1260 # Initialize a NormalizedRidDict
1261 nrd = NormalizedRidDict()
1262 for key,val in sd.items():
1263 nrd[key] = val
1264
1265 # Check that normalization do something
1266 assert nkeys != list(sd.keys())
1267
1268 # Check that key stored in NormalizedRidDict are normalized
1269 for key in nrd.keys():
1270 assert key in nkeys
1271
1272 # Check that normalized and non normalized keys have the same value
1273 for key,val in sd.items():
1274 nkey = NormalizedRidDict.normalize_rid(key)
1275 assert nrd[key] == val
1276 assert nrd[nkey] == val
1277
1278
1279 def test_get_with_normalized_rid_dict():
1280 """Check lib389.replica NormalizedRidDict.get() function
1281
1282 :id: 4422e1be-1619-11f0-a37a-482ae39447e5
1283 :setup: None
1284 :steps:
1285 1. Initialize a NormalizedRidDict
1286 2. Check that normalization do something
1287 3. Check that get() returns the expected value
1288 4. Check get() with wrong key
1289 :expectedresults:
1290 1. Success
1291 2. Success
1292 3. Success
1293 4. Should return the default value if it is provided
1294 otherwise it should return None
1295 """
1296
1297 sd = { '1': 'v1', '020': 'v2' }
1298 nsd = { NormalizedRidDict.normalize_rid(key): val for key,val in sd.items() }
1299 nkeys = list(nsd.keys())
1300
1301 # Initialize a NormalizedRidDict
1302 nrd = NormalizedRidDict()
1303 for key,val in sd.items():
1304 nrd[key] = val
1305
1306 # Check that get() returns the expected value
1307 for key,val in sd.items():
1308 nkey = NormalizedRidDict.normalize_rid(key)
1309 assert nrd.get(key) == val
1310 assert nrd.get(nkey) == val
1311 assert nrd.get(key, 'foo') == val
1312 assert nrd.get(nkey, 'foo') == val
1313
1314 # Check get() with wrong key
1315 assert nrd.get('99', 'foo2') == 'foo2'
1316 assert nrd.get('099', 'foo2') == 'foo2'
1317 assert nrd.get('99') is None
1318 assert nrd.get('099') is None
1319
1320
1321 def test_online_reinit_may_hang(topo_with_sigkill):
1322 """Online reinitialization may hang when the first
1323 entry of the DB is RUV entry instead of the suffix
1324
1325 :id: cded6afa-66c0-4c65-9651-993ba3f7a49c
1326 :setup: 2 Supplier Instances
1327 :steps:
1328 1. Export the database
1329 2. Move RUV entry to the top in the ldif file
1330 3. Import the ldif file
1331 4. Check that replication is still working
1332 5. Online replica initializaton
1333 :expectedresults:
1334 1. Ldif file should be created successfully
1335 2. RUV entry should be on top in the ldif file
1336 3. Import should be successful
1337 4. Replication should work
1338 5. Server should not hang and consume 100% CPU
1339 """
1340 M1 = topo_with_sigkill.ms["supplier1"]
1341 M2 = topo_with_sigkill.ms["supplier2"]
1342
1343 # The RFE 5367 (when enabled) retrieves the DN
1344 # from the dncache. This hides an issue
1345 # with primary fix for 6417.
1346 # We need to disable the RFE to verify that the primary
1347 # fix is properly fixed.
1348 if ds_is_newer('2.3.1'):
1349 M1.config.replace('nsslapd-return-original-entrydn', 'off')
1350
1351 M1.stop()
1352 ldif_file = '%s/supplier1.ldif' % M1.get_ldif_dir()
1353 M1.db2ldif(bename=DEFAULT_BENAME, suffixes=[DEFAULT_SUFFIX],
1354 excludeSuffixes=None, repl_data=True,
1355 outputfile=ldif_file, encrypt=False)
1356 _move_ruv(ldif_file)
1357 M1.ldif2db(DEFAULT_BENAME, None, None, None, ldif_file)
1358 M1.start()
1359 # After this server may hang
1360 # Exporting idle server with replication data and reimporting
1361 # should not break replication (Unless we hit issue 5098)
1362 # So let check that replication is still working.
1363 repl = ReplicationManager(DEFAULT_SUFFIX)
1364 repl.test_replication_topology(topo_with_sigkill)
1365 agmt = Agreements(M1).list()[0]
1366 agmt.begin_reinit()
1367 (done, error) = agmt.wait_reinit()
1368 assert done is True
1369 assert error is False
1370 repl = ReplicationManager(DEFAULT_SUFFIX)
1371 repl.test_replication_topology(topo_with_sigkill)
1372
1373 if DEBUGGING:
1374 # Add debugging steps(if any)...
1375 pass
1376
1377
1378 ##############################################################################
1379 #### WARNING ! New tests must be added before test_online_reinit_may_hang ####
1380 #### because topo_with_sigkill and topo_m2 fixtures are not compatible as ####
1381 #### topo_with_sigkill stops and destroy topo_m2 instances. ####
1382 ##############################################################################
1383
1384
1385 if __name__ == '__main__':
1386 # Run isolated
1387 # -s for DEBUG mode
1388 CURRENT_FILE = os.path.realpath(__file__)
1389 pytest.main("-s %s" % CURRENT_FILE)